Opened 2 years ago

Closed 10 days ago

Last modified 4 days ago

#35007 closed Cleanup/optimization (fixed)

Add a formatter for CSS and JS

Reported by: Tom Carrick Owned by: Tom Carrick
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: JaeHyuckSa Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tom Carrick)

From https://forum.djangoproject.com/t/adding-a-formatter-for-css-js/25754:

The idea is to add formatting for CSS and JS.

For motivation, I find myself fairly often in the admin CSS (especially lately) and it’s reasonably laid out for the most part, but there are things that are a little off. Sometimes the indentation is different from the rest of the file, for example. The JS is in a bit worse shape I think, some of the files have whitespace issues. These could be fixed but it would be better to enforce them in a way that’s easier to review, so I think a formatter makes sense here. What’s annoying (for me) is that when editing files, sometimes my editor “fixes” the issues for me, then I have to revert them and save without formatting. If I don’t spot my editor’s work, it’s more annoying to fix. Of course I could turn this stuff off somewhere, but if it’s annoying for me I’m sure it’ll be annoying for others as well.

But barring differing opinions there, the process would be something like:

Make a PR with three commits. The first sets up the machinery, installs the formatter, adds to pre-commit, etc. The second does the work and the third adds the second commit to .git-blame-ignore-revs. As far as I know, the same as when black was set up.

Change History (21)

comment:1 by Tom Carrick, 2 years ago

Owner: changed from nobody to Tom Carrick
Status: newassigned

comment:2 by Natalia Bidart, 2 years ago

Description: modified (diff)
Triage Stage: UnreviewedAccepted

The forum topic has had very positive reactions, accepting.

comment:3 by Mariusz Felisiak, 2 years ago

We're already using ESLint for JS files.

comment:4 by Tom Carrick, 2 years ago

We can have some discussion about whether ESLint's formatting rules are enough, but I don't think they are (at least the current ones). For example, they don't seem to catch obvious whitespace issues (whitespace only lines in SelectFilter2.js, whitespace at end of line in RelatedObjectLookups.js I found in the first two files I opened, so there are probably others).

It was suggested in the forum post to use ESLint only for linting, and use prettier for formatting, and I think this is a good idea.

Last edited 2 years ago by Tom Carrick (previous) (diff)

comment:5 by Thibaud Colas, 2 years ago

ESLint’s formatting rules definitely aren’t enough. It’s more "auto-fix white-space in a few very specific scenarios" than "format everything". ESLint themselves have also deprecated their formatting rules and recommend using a dedicated formatter.

comment:6 by Tom Carrick, 15 months ago

Description: modified (diff)
Summary: Add prettier to format CSS and JSAdd a formatter for CSS and JS

comment:7 by Tom Carrick, 15 months ago

I made some changes to the description and summary to make this tool-agnostic as there are discussions around using other tools, particularly Biome, which I would like to draft a PR for soon.

comment:8 by Tom Carrick, 15 months ago

I had another look into this today. Before trying Biome I'd like to wait for 2.0, it will make some things easier and it would be nice to not add a formatter and then not long after need to migrate to a new version.

in reply to:  8 comment:9 by JaeHyuckSa, 8 months ago

Cc: JaeHyuckSa added

Replying to Tom Carrick:

I had another look into this today. Before trying Biome I'd like to wait for 2.0, it will make some things easier and it would be nice to not add a formatter and then not long after need to migrate to a new version.

Just a quick reminder, Tom — Biome is up to [2.2.2](https://github.com/biomejs/biome) now; might be a good time to give it a try.

Last edited 8 months ago by JaeHyuckSa (previous) (diff)

comment:10 by Tom Carrick, 11 days ago

Has patch: set

comment:11 by Jacob Walls, 11 days ago

Triage Stage: AcceptedReady for checkin

comment:12 by Jacob Walls <jacobtylerwalls@…>, 11 days ago

In 4a6f797e:

Refs #35007 -- Added biome to lint and format CSS files.

comment:13 by Jacob Walls, 11 days ago

Has patch: unset

comment:14 by Jacob Walls <jacobtylerwalls@…>, 11 days ago

In 1b0d46f:

Refs #35007 -- Ignored CSS formatting changes in git blame.

comment:15 by Tom Carrick, 10 days ago

Has patch: set
Triage Stage: Ready for checkinAccepted

comment:16 by Jacob Walls, 10 days ago

Triage Stage: AcceptedReady for checkin

comment:17 by Jacob Walls, 10 days ago

Dropping eslint allows dropping the messy workaround in d29852ae725f673843c46085bb51cbc740d374d7 to allow pre-commit hooks to work for all without having to install node modules.

Last edited 10 days ago by Jacob Walls (previous) (diff)

comment:18 by Jacob Walls <jacobtylerwalls@…>, 10 days ago

Resolution: fixed
Status: assignedclosed

In 290fefe6:

Fixed #35007 -- Replaced ESLint with Biome for JavaScript linting and formatting.

comment:19 by Jacob Walls <jacobtylerwalls@…>, 10 days ago

In 47789e3a:

Refs #35007 -- Removed eslint disable in a test.

comment:20 by Jacob Walls <jacobtylerwalls@…>, 10 days ago

In d3ccbde0:

Refs #35007 -- Ignored JS formatting changes in git blame.

comment:21 by Jacob Walls <jacobtylerwalls@…>, 4 days ago

In 017d7f6:

Refs #35007 -- Pinned exact biome version.

Avoid drift against the following in biome.json:
"$schema": "https://biomejs.dev/schemas/2.4.12/schema.json"

Note: See TracTickets for help on using tickets.
Back to Top