Repository: apollographql/apollo-rs
Pull request #653 (2023-09-25 07:24:00)

fix(parser): closing bracket for list type is not optional


Patch:
diff --git a/crates/apollo-parser/src/parser/grammar/ty.rs b/crates/apollo-parser/src/parser/grammar/ty.rs
index 3f2acdf..50294ad 100644
--- a/crates/apollo-parser/src/parser/grammar/ty.rs
+++ b/crates/apollo-parser/src/parser/grammar/ty.rs
@@ -41,9 +41,7 @@ fn parse<'a>(p: &mut Parser<'a>) -> Result<(), Token<'a>> {
                 // type, so both opening and closing brackets `[]`.
                 p.err_at_token(&token, "expected item type");
             }
-            if let Some(T![']']) = p.peek() {
-                p.eat(S![']']);
-            }
+            p.expect(T![']'], S![']'])
         }
         Some(TokenKind::Name) => {
             let _guard = p.start_node(SyntaxKind::NAMED_TYPE);


Test patch:
diff --git a/crates/apollo-parser/test_data/parser/err/0044_list_type_with_no_type.txt b/crates/apollo-parser/test_data/parser/err/0044_list_type_with_no_type.txt
index 1b5d513..1a693e2 100644
--- a/crates/apollo-parser/test_data/parser/err/0044_list_type_with_no_type.txt
+++ b/crates/apollo-parser/test_data/parser/err/0044_list_type_with_no_type.txt
@@ -47,6 +47,10 @@
     - ERROR@67..68 "}"
     - WHITESPACE@68..69 "\n"
 - ERROR@22:23 "expected item type" ]
+- ERROR@23:24 "expected R_BRACK, got  "  
+- ERROR@60:64 "expected R_BRACK, got Type" Type
+- ERROR@60:64 "expected R_BRACK, got Type" Type
+- ERROR@60:64 "expected R_BRACK, got Type" Type
 - ERROR@64:65 "expected a type" ]
 - ERROR@64:65 "expected R_CURLY, got ]" ]
 - ERROR@64:65 "expected a StringValue, Name or OperationDefinition" ]
diff --git a/crates/apollo-parser/test_data/parser/err/0048_unbalanced_list_type_1.graphql b/crates/apollo-parser/test_data/parser/err/0048_unbalanced_list_type_1.graphql
new file mode 100644
index 0000000..34f99c8
--- /dev/null
+++ b/crates/apollo-parser/test_data/parser/err/0048_unbalanced_list_type_1.graphql
@@ -0,0 +1,3 @@
+type Query {
+    unbalanced: [[Int]]]
+}
diff --git a/crates/apollo-parser/test_data/parser/err/0048_unbalanced_list_type_1.txt b/crates/apollo-parser/test_data/parser/err/0048_unbalanced_list_type_1.txt
new file mode 100644
index 0000000..85daa67
--- /dev/null
+++ b/crates/apollo-parser/test_data/parser/err/0048_unbalanced_list_type_1.txt
@@ -0,0 +1,32 @@
+- DOCUMENT@0..40
+    - OBJECT_TYPE_DEFINITION@0..36
+        - type_KW@0..4 "type"
+        - WHITESPACE@4..5 " "
+        - NAME@5..10
+            - IDENT@5..10 "Query"
+        - WHITESPACE@10..11 " "
+        - FIELDS_DEFINITION@11..36
+            - L_CURLY@11..12 "{"
+            - WHITESPACE@12..17 "\n    "
+            - FIELD_DEFINITION@17..36
+                - NAME@17..27
+                    - IDENT@17..27 "unbalanced"
+                - COLON@27..28 ":"
+                - WHITESPACE@28..29 " "
+                - LIST_TYPE@29..36
+                    - L_BRACK@29..30 "["
+                    - LIST_TYPE@30..35
+                        - L_BRACK@30..31 "["
+                        - NAMED_TYPE@31..34
+                            - NAME@31..34
+                                - IDENT@31..34 "Int"
+                        - R_BRACK@34..35 "]"
+                    - R_BRACK@35..36 "]"
+    - ERROR@36..37 "]"
+    - WHITESPACE@37..38 "\n"
+    - ERROR@38..39 "}"
+    - WHITESPACE@39..40 "\n"
+- ERROR@36:37 "expected R_CURLY, got ]" ]
+- ERROR@36:37 "expected a StringValue, Name or OperationDefinition" ]
+- ERROR@38:39 "expected a StringValue, Name or OperationDefinition" }
+recursion limit: 4096, high: 0
\ No newline at end of file
diff --git a/crates/apollo-parser/test_data/parser/err/0049_unbalanced_list_type_2.graphql b/crates/apollo-parser/test_data/parser/err/0049_unbalanced_list_type_2.graphql
new file mode 100644
index 0000000..b677251
--- /dev/null
+++ b/crates/apollo-parser/test_data/parser/err/0049_unbalanced_list_type_2.graphql
@@ -0,0 +1,3 @@
+type Query {
+    unbalanced: [[[Int]]
+}
diff --git a/crates/apollo-parser/test_data/parser/err/0049_unbalanced_list_type_2.txt b/crates/apollo-parser/test_data/parser/err/0049_unbalanced_list_type_2.txt
new file mode 100644
index 0000000..39b36ee
--- /dev/null
+++ b/crates/apollo-parser/test_data/parser/err/0049_unbalanced_list_type_2.txt
@@ -0,0 +1,31 @@
+- DOCUMENT@0..40
+    - OBJECT_TYPE_DEFINITION@0..39
+        - type_KW@0..4 "type"
+        - WHITESPACE@4..5 " "
+        - NAME@5..10
+            - IDENT@5..10 "Query"
+        - WHITESPACE@10..11 " "
+        - FIELDS_DEFINITION@11..39
+            - L_CURLY@11..12 "{"
+            - WHITESPACE@12..17 "\n    "
+            - FIELD_DEFINITION@17..37
+                - NAME@17..27
+                    - IDENT@17..27 "unbalanced"
+                - COLON@27..28 ":"
+                - WHITESPACE@28..29 " "
+                - LIST_TYPE@29..37
+                    - L_BRACK@29..30 "["
+                    - LIST_TYPE@30..37
+                        - L_BRACK@30..31 "["
+                        - LIST_TYPE@31..36
+                            - L_BRACK@31..32 "["
+                            - NAMED_TYPE@32..35
+                                - NAME@32..35
+                                    - IDENT@32..35 "Int"
+                            - R_BRACK@35..36 "]"
+                        - R_BRACK@36..37 "]"
+            - WHITESPACE@37..38 "\n"
+            - R_CURLY@38..39 "}"
+    - WHITESPACE@39..40 "\n"
+- ERROR@38:39 "expected R_BRACK, got }" }
+recursion limit: 4096, high: 0
\ No newline at end of file