-
-
Notifications
You must be signed in to change notification settings - Fork 503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rome_json_parser): support allowTrailingCommas option #326
Conversation
Do we need to tweak the formatter's behavior? |
At this point, I think we do not need to do anything in particular. If we get feedback from users, we will consider it then. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an important thing missing, we need to update our grammar to take this new syntax into account.
At the moment our grammar doesn't allow trailing commas, for example:
biome/xtask/codegen/json.ungram
Line 59 in 9bc8a92
JsonArrayElementList = (AnyJsonValue (',' AnyJsonValue)* ) |
This means that we have to update the grammar to take that into account. Here's an example of how we do it in JavaScript grammar:
Line 424 in 9bc8a92
JsArrayElementList = (AnyJsArrayElement (',' AnyJsArrayElement)* ','?) |
Look at the '.'?
at the end.
let config_json = r#"{ | ||
"json": { | ||
"parser": { "allowTrailingCommas": true } | ||
} | ||
}"#; | ||
let biome_config = "biome.json"; | ||
let code = r#" | ||
/*test*/ [ | ||
|
||
/* some other comment*/1, 2, 3] | ||
"#; | ||
/*test*/{ | ||
/* some other comment */ | ||
"array": [1, 2, 3], | ||
} | ||
"#; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should create a new test, so we don't overlap with other tests
value_token: JSON_NUMBER_LITERAL@27..37 "2" [Newline("\n"), Whitespace(" ")] [], | ||
}, | ||
COMMA@37..38 "," [] [], | ||
missing element, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there's something wrong with the AST here. This is because our grammar hasn't been udpated, and the syntax factory hasn't generated the code to take ,
into account.
A new token means that the syntax factory must generate a new syntax where we have a new slot where the token ,
is missing and optional.
Parser conformance results onjs/262
jsx/babel
symbols/microsoft
ts/babel
ts/microsoft
|
Thank you for reviews! I updated the codes by your reviews. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, I think we should add more cases to avoid possible regressions or "unknown" in the future.
{ | ||
"prop1": [ | ||
1, | ||
2, | ||
], | ||
"prop2": 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add more cases, so we cover all the possible valid/not valid combinations:
true,
Valid or not? We should have a case for that
null,
Valid or not? We should have a case for that
{},
Valid or not? We should have a case for that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I actually meant having a json file that contains
true
(same for the rests)
They all are valid JSON files 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe these new cases should fail, although you should check what VSCode does. If VSCode does support trialing comma in these cases, then we need:
- to update the grammar
- correctly parse these tokens before
EOF
For example, at the moment there's an issue:
0: JSON_ROOT@0..6
0: JSON_NULL_VALUE@0..4
0: NULL_KW@0..4 "null" [] []
1: EOF@4..6 ",\n" [] []
The comma is inside the EOF token
Sorry, I didn't check properly. After checking jsonc-parser and VSCode, I confirm the new case need to fail. Therefore, I update codes and test cases. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's great! Looking forward to having this released 🚀
Summary
Fix #129
Test Plan
I add and update some snapshot tests