diff --git a/crates/rome_json_parser/src/syntax.rs b/crates/rome_json_parser/src/syntax.rs index 18f2b7e0935..c70d4ace7b9 100644 --- a/crates/rome_json_parser/src/syntax.rs +++ b/crates/rome_json_parser/src/syntax.rs @@ -1,7 +1,8 @@ use crate::prelude::*; +use rome_diagnostics::DiagnosticExt; use rome_json_syntax::JsonSyntaxKind; use rome_json_syntax::JsonSyntaxKind::*; -use rome_parser::diagnostic::{expected_any, expected_node}; +use rome_parser::diagnostic::{expected_any, expected_node, expected_token}; use rome_parser::parse_recovery::ParseRecovery; use rome_parser::parsed_syntax::ParsedSyntax::Absent; use rome_parser::prelude::ParsedSyntax::Present; @@ -171,10 +172,19 @@ fn parse_sequence(p: &mut JsonParser, root_kind: SequenceKind) -> ParsedSyntax { let mut progress = ParserProgress::default(); while !p.at(EOF) && !p.at(current.kind.close_paren()) { - if first { + let last_token = if first { first = false; + Some(current.kind.open_paren()) } else { - p.expect(T![,]); + let last_token = p.last(); + p.expect(T![,]); + last_token + }; + + if p.at(current.kind.close_paren()) && !matches!(last_token, Some(T![,]) | None) { + // SAFETY: we know that previous should not be none + // let builder = p.err_builder("Trailing comma is not allowed in json", p.cur_range()).with_severity(rome_diagnostics::Severity::Warning); + break; } progress.assert_progressing(p); @@ -214,6 +224,9 @@ fn parse_sequence(p: &mut JsonParser, root_kind: SequenceKind) -> ParsedSyntax { } } + // while p.at(T![,]) { + // p.bump_any(); + // } current.list.complete(p, current.kind.list_kind()); p.expect(current.kind.close_paren()); let node = current.node.complete(p, current.kind.node_kind()); diff --git a/crates/rome_json_parser/tests/json_test_suite/err/array_comma_and_number.json b/crates/rome_json_parser/tests/json_test_suite/err/array_comma_and_number.json deleted file mode 100755 index d2c84e374a2..00000000000 --- a/crates/rome_json_parser/tests/json_test_suite/err/array_comma_and_number.json +++ /dev/null @@ -1 +0,0 @@ -[,1] \ No newline at end of file diff --git a/crates/rome_json_parser/tests/json_test_suite/err/array_extra_comma.json b/crates/rome_json_parser/tests/json_test_suite/err/array_extra_comma.json deleted file mode 100644 index 5f8ce18e4b2..00000000000 --- a/crates/rome_json_parser/tests/json_test_suite/err/array_extra_comma.json +++ /dev/null @@ -1 +0,0 @@ -["",] \ No newline at end of file diff --git a/crates/rome_json_parser/tests/json_test_suite/err/array_extra_comma.json.snap b/crates/rome_json_parser/tests/json_test_suite/err/array_extra_comma.json.snap deleted file mode 100644 index f2cfb2ea5e0..00000000000 --- a/crates/rome_json_parser/tests/json_test_suite/err/array_extra_comma.json.snap +++ /dev/null @@ -1,65 +0,0 @@ ---- -source: crates/rome_json_parser/tests/spec_test.rs -expression: snapshot ---- - -## Input - -```json -["",] -``` - - -## AST - -``` -JsonRoot { - value: JsonArrayValue { - l_brack_token: L_BRACK@0..1 "[" [] [], - elements: JsonArrayElementList [ - JsonStringValue { - value_token: JSON_STRING_LITERAL@1..3 "\"\"" [] [], - }, - COMMA@3..4 "," [] [], - missing element, - ], - r_brack_token: R_BRACK@4..5 "]" [] [], - }, - eof_token: EOF@5..5 "" [] [], -} -``` - -## CST - -``` -0: JSON_ROOT@0..5 - 0: JSON_ARRAY_VALUE@0..5 - 0: L_BRACK@0..1 "[" [] [] - 1: JSON_ARRAY_ELEMENT_LIST@1..4 - 0: JSON_STRING_VALUE@1..3 - 0: JSON_STRING_LITERAL@1..3 "\"\"" [] [] - 1: COMMA@3..4 "," [] [] - 2: (empty) - 2: R_BRACK@4..5 "]" [] [] - 1: EOF@5..5 "" [] [] - -``` - -## Diagnostics - -``` -array_extra_comma.json:1:5 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × expected an array, an object, or a literal but instead found ']' - - > 1 │ ["",] - │ ^ - - i Expected an array, an object, or a literal here - - > 1 │ ["",] - │ ^ - -``` - - diff --git a/crates/rome_json_parser/tests/json_test_suite/err/array_just_comma.json.snap b/crates/rome_json_parser/tests/json_test_suite/err/array_just_comma.json.snap deleted file mode 100644 index 50ddacad8eb..00000000000 --- a/crates/rome_json_parser/tests/json_test_suite/err/array_just_comma.json.snap +++ /dev/null @@ -1,74 +0,0 @@ ---- -source: crates/rome_json_parser/tests/spec_test.rs -expression: snapshot ---- - -## Input - -```json -[,] -``` - - -## AST - -``` -JsonRoot { - value: JsonArrayValue { - l_brack_token: L_BRACK@0..1 "[" [] [], - elements: JsonArrayElementList [ - missing element, - COMMA@1..2 "," [] [], - missing element, - ], - r_brack_token: R_BRACK@2..3 "]" [] [], - }, - eof_token: EOF@3..3 "" [] [], -} -``` - -## CST - -``` -0: JSON_ROOT@0..3 - 0: JSON_ARRAY_VALUE@0..3 - 0: L_BRACK@0..1 "[" [] [] - 1: JSON_ARRAY_ELEMENT_LIST@1..2 - 0: (empty) - 1: COMMA@1..2 "," [] [] - 2: (empty) - 2: R_BRACK@2..3 "]" [] [] - 1: EOF@3..3 "" [] [] - -``` - -## Diagnostics - -``` -array_just_comma.json:1:2 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × expected an array, an object, or a literal but instead found ',' - - > 1 │ [,] - │ ^ - - i Expected an array, an object, or a literal here - - > 1 │ [,] - │ ^ - -array_just_comma.json:1:3 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × expected an array, an object, or a literal but instead found ']' - - > 1 │ [,] - │ ^ - - i Expected an array, an object, or a literal here - - > 1 │ [,] - │ ^ - -``` - - diff --git a/crates/rome_json_parser/tests/json_test_suite/err/array_number_and_comma.json b/crates/rome_json_parser/tests/json_test_suite/err/array_number_and_comma.json deleted file mode 100755 index 13f6f1d18a4..00000000000 --- a/crates/rome_json_parser/tests/json_test_suite/err/array_number_and_comma.json +++ /dev/null @@ -1 +0,0 @@ -[1,] \ No newline at end of file diff --git a/crates/rome_json_parser/tests/json_test_suite/err/object_trailing_comma.json b/crates/rome_json_parser/tests/json_test_suite/err/object_trailing_comma.json deleted file mode 100755 index a4b02509459..00000000000 --- a/crates/rome_json_parser/tests/json_test_suite/err/object_trailing_comma.json +++ /dev/null @@ -1 +0,0 @@ -{"id":0,} \ No newline at end of file diff --git a/crates/rome_json_parser/tests/json_test_suite/ok/array_number_and_comma.json b/crates/rome_json_parser/tests/json_test_suite/ok/array_number_and_comma.json new file mode 100755 index 00000000000..e8b1a170fd0 --- /dev/null +++ b/crates/rome_json_parser/tests/json_test_suite/ok/array_number_and_comma.json @@ -0,0 +1 @@ +[1,] diff --git a/crates/rome_json_parser/tests/json_test_suite/err/array_number_and_comma.json.snap b/crates/rome_json_parser/tests/json_test_suite/ok/array_number_and_comma.json.snap similarity index 59% rename from crates/rome_json_parser/tests/json_test_suite/err/array_number_and_comma.json.snap rename to crates/rome_json_parser/tests/json_test_suite/ok/array_number_and_comma.json.snap index c3e412f706f..dc52393960e 100644 --- a/crates/rome_json_parser/tests/json_test_suite/err/array_number_and_comma.json.snap +++ b/crates/rome_json_parser/tests/json_test_suite/ok/array_number_and_comma.json.snap @@ -7,6 +7,7 @@ expression: snapshot ```json [1,] + ``` @@ -25,14 +26,14 @@ JsonRoot { ], r_brack_token: R_BRACK@3..4 "]" [] [], }, - eof_token: EOF@4..4 "" [] [], + eof_token: EOF@4..5 "" [Newline("\n")] [], } ``` ## CST ``` -0: JSON_ROOT@0..4 +0: JSON_ROOT@0..5 0: JSON_ARRAY_VALUE@0..4 0: L_BRACK@0..1 "[" [] [] 1: JSON_ARRAY_ELEMENT_LIST@1..3 @@ -41,25 +42,8 @@ JsonRoot { 1: COMMA@2..3 "," [] [] 2: (empty) 2: R_BRACK@3..4 "]" [] [] - 1: EOF@4..4 "" [] [] - -``` - -## Diagnostics - -``` -array_number_and_comma.json:1:4 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + 1: EOF@4..5 "" [Newline("\n")] [] - × expected an array, an object, or a literal but instead found ']' - - > 1 │ [1,] - │ ^ - - i Expected an array, an object, or a literal here - - > 1 │ [1,] - │ ^ - ``` diff --git a/crates/rome_json_parser/tests/json_test_suite/ok/array_with_extra_one_comma.json b/crates/rome_json_parser/tests/json_test_suite/ok/array_with_extra_one_comma.json new file mode 100755 index 00000000000..e8b1a170fd0 --- /dev/null +++ b/crates/rome_json_parser/tests/json_test_suite/ok/array_with_extra_one_comma.json @@ -0,0 +1 @@ +[1,] diff --git a/crates/rome_json_parser/tests/json_test_suite/ok/array_with_extra_one_comma.json.snap b/crates/rome_json_parser/tests/json_test_suite/ok/array_with_extra_one_comma.json.snap new file mode 100644 index 00000000000..dc52393960e --- /dev/null +++ b/crates/rome_json_parser/tests/json_test_suite/ok/array_with_extra_one_comma.json.snap @@ -0,0 +1,49 @@ +--- +source: crates/rome_json_parser/tests/spec_test.rs +expression: snapshot +--- + +## Input + +```json +[1,] + +``` + + +## AST + +``` +JsonRoot { + value: JsonArrayValue { + l_brack_token: L_BRACK@0..1 "[" [] [], + elements: JsonArrayElementList [ + JsonNumberValue { + value_token: JSON_NUMBER_LITERAL@1..2 "1" [] [], + }, + COMMA@2..3 "," [] [], + missing element, + ], + r_brack_token: R_BRACK@3..4 "]" [] [], + }, + eof_token: EOF@4..5 "" [Newline("\n")] [], +} +``` + +## CST + +``` +0: JSON_ROOT@0..5 + 0: JSON_ARRAY_VALUE@0..4 + 0: L_BRACK@0..1 "[" [] [] + 1: JSON_ARRAY_ELEMENT_LIST@1..3 + 0: JSON_NUMBER_VALUE@1..2 + 0: JSON_NUMBER_LITERAL@1..2 "1" [] [] + 1: COMMA@2..3 "," [] [] + 2: (empty) + 2: R_BRACK@3..4 "]" [] [] + 1: EOF@4..5 "" [Newline("\n")] [] + +``` + + diff --git a/crates/rome_json_parser/tests/json_test_suite/ok/object_trailing_comma.json b/crates/rome_json_parser/tests/json_test_suite/ok/object_trailing_comma.json new file mode 100755 index 00000000000..28074759440 --- /dev/null +++ b/crates/rome_json_parser/tests/json_test_suite/ok/object_trailing_comma.json @@ -0,0 +1 @@ +{"id":0,} diff --git a/crates/rome_json_parser/tests/json_test_suite/err/object_trailing_comma.json.snap b/crates/rome_json_parser/tests/json_test_suite/ok/object_trailing_comma.json.snap similarity index 69% rename from crates/rome_json_parser/tests/json_test_suite/err/object_trailing_comma.json.snap rename to crates/rome_json_parser/tests/json_test_suite/ok/object_trailing_comma.json.snap index 697dbd1495b..bdecd707270 100644 --- a/crates/rome_json_parser/tests/json_test_suite/err/object_trailing_comma.json.snap +++ b/crates/rome_json_parser/tests/json_test_suite/ok/object_trailing_comma.json.snap @@ -7,6 +7,7 @@ expression: snapshot ```json {"id":0,} + ``` @@ -31,14 +32,14 @@ JsonRoot { ], r_curly_token: R_CURLY@8..9 "}" [] [], }, - eof_token: EOF@9..9 "" [] [], + eof_token: EOF@9..10 "" [Newline("\n")] [], } ``` ## CST ``` -0: JSON_ROOT@0..9 +0: JSON_ROOT@0..10 0: JSON_OBJECT_VALUE@0..9 0: L_CURLY@0..1 "{" [] [] 1: JSON_MEMBER_LIST@1..8 @@ -51,25 +52,8 @@ JsonRoot { 1: COMMA@7..8 "," [] [] 2: (empty) 2: R_CURLY@8..9 "}" [] [] - 1: EOF@9..9 "" [] [] - -``` - -## Diagnostics - -``` -object_trailing_comma.json:1:9 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + 1: EOF@9..10 "" [Newline("\n")] [] - × expected a property but instead found '}' - - > 1 │ {"id":0,} - │ ^ - - i Expected a property here - - > 1 │ {"id":0,} - │ ^ - ```