biome: š The formatter cannot remove trailing commas in JSON files, unlike Prettier
Environment information
CLI:
Version: 1.4.0
Color support: true
Platform:
CPU Architecture: x86_64
OS: windows
Environment:
BIOME_LOG_DIR: unset
NO_COLOR: unset
TERM: unset
JS_RUNTIME_VERSION: "v21.2.0"
JS_RUNTIME_NAME: "node"
NODE_PACKAGE_MANAGER: "npm/10.2.4"
Biome Configuration:
Status: Loaded successfully
Formatter disabled: false
Linter disabled: false
Organize imports disabled: true
VCS disabled: true
Workspace:
Open Documents: 0
What happened?
Consider the following JSON files:
{
"a": "b",
}
[1, 2, 3, ]
Biome CLI/VSCode extension fail to format away the trailing commas.
Expected result
Biomeās formatter should remove the trailing commas in JSON files. Prettier handles this fine, so itād improve parity, and itās very convenient when copy/pasting JSON.
Code of Conduct
- I agree to follow Biomeās Code of Conduct
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 16 (11 by maintainers)
@MarArMar
Weāve added a new option to control the trailing comma. It will be available soon on npm
Iād like to add:
biome ci .orbiome format ., Iād expect Biome to complain about trailing,in JSON files.biome format . --writeorbiome check . --apply, biome should automatically remove the trailing,in JSON files.,in JSON files.I think those are fair expectations.
I think this is reasonable. When running with these flags, we can enable the parser options to allow trailing commas, which the formatter and linter will automatically remove as part of their normal operation.
Hey. Iām new to trying out Biome, and just wanted to add my two cents as a regular user on how this feels.
Iāve converted my entire project now from eslint + Prettier to Biome as a test to see how it feels and works. Overall it feels pretty nice, but this issue is one of my major annoyances, having converted.
Removing a line of JSON and having Prettier auto-fix my file on save is completely effortless and requires no thought on my part. With Biome I will probably remove a line in an object, move on (because the red squiggly under 1 character is already very hard to see), and only later notice the error when my build refuses to run or something breaks. And if I do notice it, I will be annoyed because this feels like something my formatting tool should fix (especially since this is automatically fixed with JS/TS formatting with the
"trailingComma": "none"option).After having used it for so long, I had completely forgotten about the level of comfort Prettier actually provides, but these kind of things now are making me go nuts.
IMO it is a bug in three ways:
Though I donāt mind if you prefer to think of it as a feature.
Enabling the option doesnāt help.
Because trailing commas are not valid in JSON, it would be nice if Biome would remove the trailing comma on format, if it exists, just like prettier does
I like this idea.
I was just looking at this yesterday, and I think this is sort of similar to the case of multiple access modifiers on a typescript method, like:
Thatās technically invalid syntax, and Biome appropriately fails to parse it. But syntactically thereās nothing actually wrong with it. Itās still possible to perfectly deduce an AST and operate on it. Prettier in fact explicitly allows this in their parsing configuration (though the way it gets resolved there is that only the first modifier gets stored and the rest are removed. Biome actually stores all of them and is even more recoverable as a result).
I mention that just as another example of a case where a parsing failure prevents formatting and compatibility with Prettier. During the challenge, Iād mentioned this as well, and one of the possible ideas was to treat these kinds of errors as Syntax Correctness analyzer rules, just like a few other things already are, like disallowing
super()in a class that doesnātextendanything, or no duplicate private members (like#foo).I think both the typescript example and this case of trailing commas in JSON might fit that same kind of bill, where we know that the code isnāt strictly valid, but we can also perfectly deduce the AST and operate on it safely, whether thatās formatting away a comma or sorting access modifiers.
I think it might also be useful to somehow be able to indicate in the
ParseResultitself whether or not the parse was āstrictlyā valid, but Iām not sure of a good way to do that.I agree with this, that we can use the formatter to basically ācorrectā the JSON and make it more valid as a result.
FWIW, though, Prettier doesnāt actually treat JSON as JSON, and instead just parses as a JS expression with Babel and throws errors in specific invalid cases. In fact, Prettier will be completely fine if you parse
[1,,,3,,,1,2,34,]as JSON, even though itās very invalid lol, and importantly it wonāt correct that, but will format it in a way that remains incorrect: Prettier Playground ExampleI donāt think this should be considered a bug, because trailing commas arenāt valid in JSON unless you explicitly tell Biome to do so: https://biomejs.dev/reference/configuration#jsonparserallowtrailingcommas
Maybe if the option is enabled? Is this the case?