Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix: loose strategy when suffer trailing comma in json #4379

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions crates/rome_json_parser/src/syntax.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,]
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ expression: snapshot

```json
[1,]

```


Expand All @@ -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
Expand All @@ -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,]
│ ^

```


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,]
Original file line number Diff line number Diff line change
@@ -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")] []

```


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":0,}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ expression: snapshot

```json
{"id":0,}

```


Expand All @@ -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
Expand All @@ -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,}
│ ^

```