Skip to content
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

Merged
merged 10 commits into from
Sep 22, 2023

Conversation

nissy-dev
Copy link
Contributor

@nissy-dev nissy-dev commented Sep 18, 2023

Summary

Fix #129

Test Plan

I add and update some snapshot tests

@nissy-dev nissy-dev temporarily deployed to Website deployment September 18, 2023 10:19 — with GitHub Actions Inactive
@github-actions github-actions bot added A-CLI Area: CLI A-Project Area: project A-Parser Area: parser A-Formatter Area: formatter A-Website Area: website L-JSON Language: JSON and super languages A-Changelog Area: changelog labels Sep 18, 2023
@nissy-dev nissy-dev changed the title feat(rome_json_parser): support allowTrailingCommas options feat(rome_json_parser): support allowTrailingCommas option Sep 18, 2023
@denbezrukov
Copy link
Contributor

Do we need to tweak the formatter's behavior?

@nissy-dev nissy-dev temporarily deployed to Website deployment September 18, 2023 12:12 — with GitHub Actions Inactive
@nissy-dev
Copy link
Contributor Author

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.

@nissy-dev nissy-dev temporarily deployed to Website deployment September 18, 2023 12:49 — with GitHub Actions Inactive
@nissy-dev nissy-dev temporarily deployed to Website deployment September 18, 2023 14:18 — with GitHub Actions Inactive
@github-actions github-actions bot removed the A-Formatter Area: formatter label Sep 18, 2023
Copy link
Member

@ematipico ematipico left a 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:

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:

JsArrayElementList = (AnyJsArrayElement (',' AnyJsArrayElement)* ','?)

Look at the '.'? at the end.

Comment on lines 1889 to 1900
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],
}
"#;
Copy link
Member

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,
Copy link
Member

@ematipico ematipico Sep 19, 2023

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.

@nissy-dev nissy-dev temporarily deployed to Website deployment September 19, 2023 15:22 — with GitHub Actions Inactive
@github-actions github-actions bot added the A-Tooling Area: internal tools label Sep 19, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Sep 19, 2023

Parser conformance results on

js/262

Test result main count This PR count Difference
Total 48863 48863 0
Passed 47808 47808 0
Failed 1055 1055 0
Panics 0 0 0
Coverage 97.84% 97.84% 0.00%

jsx/babel

Test result main count This PR count Difference
Total 40 40 0
Passed 37 37 0
Failed 3 3 0
Panics 0 0 0
Coverage 92.50% 92.50% 0.00%

symbols/microsoft

Test result main count This PR count Difference
Total 6212 6212 0
Passed 1767 1767 0
Failed 4445 4445 0
Panics 0 0 0
Coverage 28.44% 28.44% 0.00%

ts/babel

Test result main count This PR count Difference
Total 639 639 0
Passed 571 571 0
Failed 68 68 0
Panics 0 0 0
Coverage 89.36% 89.36% 0.00%

ts/microsoft

Test result main count This PR count Difference
Total 17224 17224 0
Passed 13123 13123 0
Failed 4101 4101 0
Panics 0 0 0
Coverage 76.19% 76.19% 0.00%

@nissy-dev
Copy link
Contributor Author

Thank you for reviews! I updated the codes by your reviews.

Copy link
Member

@ematipico ematipico left a 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.

Comment on lines 1 to 6
{
"prop1": [
1,
2,
],
"prop2": 0,
Copy link
Member

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


@nissy-dev nissy-dev temporarily deployed to Website deployment September 20, 2023 13:30 — with GitHub Actions Inactive
Copy link
Member

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 😄

@nissy-dev nissy-dev temporarily deployed to Website deployment September 20, 2023 15:29 — with GitHub Actions Inactive
Copy link
Member

@ematipico ematipico left a 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

@nissy-dev nissy-dev temporarily deployed to Website deployment September 22, 2023 12:50 — with GitHub Actions Inactive
@nissy-dev
Copy link
Contributor Author

nissy-dev commented Sep 22, 2023

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.

Copy link
Member

@ematipico ematipico left a 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 🚀

@nissy-dev nissy-dev merged commit 04de818 into main Sep 22, 2023
18 checks passed
@nissy-dev nissy-dev deleted the nissy-dev/json-parser-comma branch September 22, 2023 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-CLI Area: CLI A-Parser Area: parser A-Project Area: project A-Tooling Area: internal tools A-Website Area: website L-JSON Language: JSON and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📎 Implement json.parser.allowTrailingCommas
3 participants