Skip to content

Commit

Permalink
fix(apollo-parser): bump BANG token when creating NON_NULL_TYPE
Browse files Browse the repository at this point in the history
We are missing BANG token in the AST when a NON_NULL_TYPE gets created. Although the node created is indeed NON_NULL_TYPE, it's also important to keep the original set of tokens. The NON_NULL_TYPE after this commit looks like this:

```txt
- [email protected]
    - [email protected] "\n    "
    - [email protected]
        - [email protected]
            - [email protected] "Int"
    - [email protected] "!"
```

fixes #142
  • Loading branch information
lrlna committed Jan 21, 2022
1 parent ec44c44 commit 494285c
Show file tree
Hide file tree
Showing 8 changed files with 4,320 additions and 4,122 deletions.
2 changes: 1 addition & 1 deletion crates/apollo-parser/examples/annotate_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn parse_schema() -> ast::Document {
// each err comes with the two pieces of data you need for diagnostics:
// - message (err.message())
// - index (err.index())
for err in ast.errors().into_iter() {
for err in ast.errors() {
let snippet = Snippet {
title: Some(Annotation {
label: Some(err.message()),
Expand Down
36 changes: 35 additions & 1 deletion crates/apollo-parser/src/parser/grammar/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ pub(crate) fn ty(p: &mut Parser) {
process(types, p);
}
}
Some((kind @ SyntaxKind::NON_NULL_TYPE, _)) => {
Some((kind @ SyntaxKind::NON_NULL_TYPE, token)) => {
let _non_null_g = p.start_node(kind);
process(types, p);
p.push_ast(S![!], token)
}
// Cannot use `name::name` or `named_type` function here as we
// cannot bump from this function. Instead, the process function has
Expand Down Expand Up @@ -103,3 +104,36 @@ fn peek<T>(target: &VecDeque<T>) -> Option<&T> {
len => target.get(len - 1),
}
}

#[cfg(test)]
mod test {
use crate::{ast, Parser};

#[test]
fn it_parses_nested_wrapped_types_in_op_def_and_returns_matching_stringified_doc() {
let mutation = r#"
mutation MyMutation($custId: [Int!]!) {
myMutation(custId: $custId)
}"#;
let parser = Parser::new(mutation);
let ast = parser.parse();
assert!(ast.errors.is_empty());

let doc = ast.document();
assert_eq!(&mutation, &doc.to_string());

for definition in doc.definitions() {
if let ast::Definition::OperationDefinition(op_type) = definition {
for var in op_type
.variable_definitions()
.unwrap()
.variable_definitions()
{
if let ast::Type::NamedType(name) = var.ty().unwrap() {
assert_eq!(name.to_string(), "[Int!]!")
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
- DOCUMENT@0..34
- INPUT_OBJECT_TYPE_DEFINITION@0..34
- DOCUMENT@0..35
- INPUT_OBJECT_TYPE_DEFINITION@0..35
- [email protected] "input"
- [email protected] " "
- INPUT_FIELDS_DEFINITION@6..34
- INPUT_FIELDS_DEFINITION@6..35
- [email protected] "{"
- [email protected] "\n "
- [email protected]
Expand All @@ -14,15 +14,16 @@
- [email protected] "\n "
- [email protected]
- [email protected] "String"
- INPUT_VALUE_DEFINITION@26..33
- INPUT_VALUE_DEFINITION@26..34
- [email protected]
- [email protected] "b"
- [email protected] ":"
- [email protected] " "
- NON_NULL_TYPE@29..33
- NON_NULL_TYPE@29..34
- [email protected] "\n"
- [email protected]
- [email protected]
- [email protected] "Int"
- [email protected] "}"
- [email protected] "!"
- [email protected] "}"
- ERROR@6:7 "expected a Name" {
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
- DOCUMENT@0..53
- INPUT_OBJECT_TYPE_DEFINITION@0..53
- DOCUMENT@0..54
- INPUT_OBJECT_TYPE_DEFINITION@0..54
- [email protected] "input"
- [email protected] " "
- [email protected]
- [email protected] "ExampleInputObject"
- [email protected] " "
- INPUT_FIELDS_DEFINITION@25..53
- INPUT_FIELDS_DEFINITION@25..54
- [email protected] "{"
- [email protected] "\n "
- [email protected]
Expand All @@ -17,14 +17,15 @@
- [email protected] "\n "
- [email protected]
- [email protected] "String"
- INPUT_VALUE_DEFINITION@45..52
- INPUT_VALUE_DEFINITION@45..53
- [email protected]
- [email protected] "b"
- [email protected] ":"
- [email protected] " "
- NON_NULL_TYPE@48..52
- NON_NULL_TYPE@48..53
- [email protected] "\n"
- [email protected]
- [email protected]
- [email protected] "Int"
- [email protected] "}"
- [email protected] "!"
- [email protected] "}"
Loading

0 comments on commit 494285c

Please sign in to comment.