diff --git a/Cargo.lock b/Cargo.lock index fb93c88..e06251f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,11 +64,13 @@ checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" [[package]] name = "apollo-parser" -version = "0.2.12" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f901785c93a542be795c2703370ddb5f84d4e38606357fc56fefa4b42ab25151" +checksum = "db2953239e146be68f42333c2eaff528884fd300d0d1ed306b374f2a619a7d85" dependencies = [ + "memchr", "rowan", + "thiserror", ] [[package]] diff --git a/transforms/graphql_tag/Cargo.toml b/transforms/graphql_tag/Cargo.toml index 2851d01..b1cbb5c 100644 --- a/transforms/graphql_tag/Cargo.toml +++ b/transforms/graphql_tag/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" edition = "2021" [dependencies] -apollo-parser = "0.2.10" +apollo-parser = "0.7.4" swc_common = { version = "0.33.8", features = ["concurrent"] } swc_ecma_ast = "0.110.9" swc_ecma_visit = "0.96.9" diff --git a/transforms/graphql_tag/src/lib.rs b/transforms/graphql_tag/src/lib.rs index f00cebd..68924f6 100644 --- a/transforms/graphql_tag/src/lib.rs +++ b/transforms/graphql_tag/src/lib.rs @@ -132,7 +132,21 @@ where let gql_raw_string = data.to_string(); let gql_text = if self.config.strip { - strip_ignored_characters(gql_raw_string) + let strip_result = strip_ignored_characters(gql_raw_string.clone()); + match strip_result { + Ok(gql_strip_text) => gql_strip_text, + Err(errors) => { + for error in errors { + println!( + "GraphQL Error: At index {}, {} got \"{}\" instead\n", + error.index(), + error.message(), + error.data() + ) + } + gql_raw_string + } + } } else { gql_raw_string }; diff --git a/transforms/graphql_tag/src/parser/nodes/arguments/mod.rs b/transforms/graphql_tag/src/parser/nodes/arguments/mod.rs index 4dd95d0..bd33eb2 100644 --- a/transforms/graphql_tag/src/parser/nodes/arguments/mod.rs +++ b/transforms/graphql_tag/src/parser/nodes/arguments/mod.rs @@ -1,5 +1,5 @@ // libs -use apollo_parser::ast::{Argument, Arguments}; +use apollo_parser::cst::{Argument, Arguments}; use swc_common::Span; use swc_ecma_ast::*; diff --git a/transforms/graphql_tag/src/parser/nodes/definitions/fragment.rs b/transforms/graphql_tag/src/parser/nodes/definitions/fragment.rs index 2c95494..28e4b1a 100644 --- a/transforms/graphql_tag/src/parser/nodes/definitions/fragment.rs +++ b/transforms/graphql_tag/src/parser/nodes/definitions/fragment.rs @@ -1,5 +1,5 @@ // libs -use apollo_parser::ast::FragmentDefinition; +use apollo_parser::cst::FragmentDefinition; use swc_common::Span; use swc_ecma_ast::*; diff --git a/transforms/graphql_tag/src/parser/nodes/definitions/mod.rs b/transforms/graphql_tag/src/parser/nodes/definitions/mod.rs index 3666687..2f92bc9 100644 --- a/transforms/graphql_tag/src/parser/nodes/definitions/mod.rs +++ b/transforms/graphql_tag/src/parser/nodes/definitions/mod.rs @@ -1,5 +1,5 @@ // libs -use apollo_parser::ast::{AstChildren, Definition}; +use apollo_parser::cst::{CstChildren, Definition}; use swc_common::Span; use swc_ecma_ast::*; @@ -40,7 +40,7 @@ pub fn create_definition(definition: Definition, span: Span) -> Option, span: Span) -> Expr { +pub fn create_definitions(definitions: CstChildren, span: Span) -> Expr { let mut all_definitions = vec![]; for def in definitions { all_definitions.push(create_definition(def, span)); diff --git a/transforms/graphql_tag/src/parser/nodes/definitions/operation.rs b/transforms/graphql_tag/src/parser/nodes/definitions/operation.rs index 414908c..b176bc1 100644 --- a/transforms/graphql_tag/src/parser/nodes/definitions/operation.rs +++ b/transforms/graphql_tag/src/parser/nodes/definitions/operation.rs @@ -1,5 +1,5 @@ // libs -use apollo_parser::ast::OperationDefinition; +use apollo_parser::cst::OperationDefinition; use swc_common::Span; use swc_ecma_ast::*; diff --git a/transforms/graphql_tag/src/parser/nodes/directive/mod.rs b/transforms/graphql_tag/src/parser/nodes/directive/mod.rs index 96674a3..1bb9593 100644 --- a/transforms/graphql_tag/src/parser/nodes/directive/mod.rs +++ b/transforms/graphql_tag/src/parser/nodes/directive/mod.rs @@ -1,5 +1,5 @@ // libs -use apollo_parser::ast::{Directive, Directives}; +use apollo_parser::cst::{Directive, Directives}; use swc_common::Span; use swc_ecma_ast::*; diff --git a/transforms/graphql_tag/src/parser/nodes/document/mod.rs b/transforms/graphql_tag/src/parser/nodes/document/mod.rs index 3a771c6..e6c3a1c 100644 --- a/transforms/graphql_tag/src/parser/nodes/document/mod.rs +++ b/transforms/graphql_tag/src/parser/nodes/document/mod.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; // libs -use apollo_parser::ast::Document; +use apollo_parser::cst::Document; use swc_common::{comments::Comments, BytePos, Span}; use swc_ecma_ast::*; diff --git a/transforms/graphql_tag/src/parser/nodes/selection_set/mod.rs b/transforms/graphql_tag/src/parser/nodes/selection_set/mod.rs index cbdc97c..7e0cd35 100644 --- a/transforms/graphql_tag/src/parser/nodes/selection_set/mod.rs +++ b/transforms/graphql_tag/src/parser/nodes/selection_set/mod.rs @@ -1,5 +1,5 @@ // libs -use apollo_parser::ast::{AstChildren, Selection, SelectionSet}; +use apollo_parser::cst::{CstChildren, Selection, SelectionSet}; use swc_common::Span; use swc_ecma_ast::*; @@ -32,7 +32,7 @@ pub fn create_selection_set(selection_set: Option, span: Span) -> Expr::Object(sel_set) } -fn create_selections(selections: AstChildren, span: Span) -> Expr { +fn create_selections(selections: CstChildren, span: Span) -> Expr { let mut all_selections = vec![]; for selection in selections { all_selections.push(create_selection(selection, span)); diff --git a/transforms/graphql_tag/src/parser/nodes/selection_set/selection.rs b/transforms/graphql_tag/src/parser/nodes/selection_set/selection.rs index 1618a31..3b33668 100644 --- a/transforms/graphql_tag/src/parser/nodes/selection_set/selection.rs +++ b/transforms/graphql_tag/src/parser/nodes/selection_set/selection.rs @@ -1,5 +1,5 @@ // libs -use apollo_parser::ast::{Field, FragmentSpread, InlineFragment, Selection}; +use apollo_parser::cst::{Field, FragmentSpread, InlineFragment, Selection}; use swc_common::Span; use swc_ecma_ast::*; diff --git a/transforms/graphql_tag/src/parser/nodes/types/mod.rs b/transforms/graphql_tag/src/parser/nodes/types/mod.rs index cf30e50..658725c 100644 --- a/transforms/graphql_tag/src/parser/nodes/types/mod.rs +++ b/transforms/graphql_tag/src/parser/nodes/types/mod.rs @@ -1,5 +1,5 @@ // libs -use apollo_parser::ast::{ListType, NamedType, NonNullType, Type, TypeCondition}; +use apollo_parser::cst::{ListType, NamedType, NonNullType, Type, TypeCondition}; use swc_common::Span; use swc_ecma_ast::*; diff --git a/transforms/graphql_tag/src/parser/nodes/value/mod.rs b/transforms/graphql_tag/src/parser/nodes/value/mod.rs index a78b59d..1e3bdaf 100644 --- a/transforms/graphql_tag/src/parser/nodes/value/mod.rs +++ b/transforms/graphql_tag/src/parser/nodes/value/mod.rs @@ -1,6 +1,6 @@ // libs -use apollo_parser::ast::{ - AstChildren, BooleanValue, EnumValue, FloatValue, IntValue, ListValue, NullValue, ObjectField, +use apollo_parser::cst::{ + BooleanValue, CstChildren, EnumValue, FloatValue, IntValue, ListValue, NullValue, ObjectField, ObjectValue, StringValue, Value, }; use regex::Regex; @@ -32,15 +32,12 @@ pub fn create_value(value: Option, span: Span) -> Expr { fn create_string_value(str: StringValue, span: Span) -> Expr { let kind = get_key_value_node("kind".into(), "StringValue".into()); - let mut string_token = str.to_string(); + let mut string_token: String = str.into(); let re = Regex::new(r#""(?P[^"]*)""#).unwrap(); for cap in re.captures_iter(string_token.clone().as_str()) { string_token = cap[0].to_string(); } - let value = get_key_value_node( - "value".into(), - string_token[1..string_token.len() - 1].into(), - ); + let value = get_key_value_node("value".into(), string_token.into()); let str_value = ObjectLit { span, @@ -147,7 +144,7 @@ fn create_object_value(object: ObjectValue, span: Span) -> Expr { Expr::Object(object_val) } -fn create_object_fields(object_fields: AstChildren, span: Span) -> Expr { +fn create_object_fields(object_fields: CstChildren, span: Span) -> Expr { let mut all_fields = vec![]; for field in object_fields.into_iter() { all_fields.push(Some(ExprOrSpread { @@ -178,7 +175,7 @@ fn create_object_field(field: ObjectField, span: Span) -> Expr { Expr::Object(object_field_value) } -fn create_list_value_values(values: AstChildren, span: Span) -> Expr { +fn create_list_value_values(values: CstChildren, span: Span) -> Expr { let mut all_values = vec![]; for value in values.into_iter() { all_values.push(Some(ExprOrSpread { diff --git a/transforms/graphql_tag/src/parser/nodes/variables/mod.rs b/transforms/graphql_tag/src/parser/nodes/variables/mod.rs index f1d8c84..c3f7494 100644 --- a/transforms/graphql_tag/src/parser/nodes/variables/mod.rs +++ b/transforms/graphql_tag/src/parser/nodes/variables/mod.rs @@ -1,5 +1,5 @@ // libs -use apollo_parser::ast::{DefaultValue, Variable, VariableDefinition, VariableDefinitions}; +use apollo_parser::cst::{DefaultValue, Variable, VariableDefinition, VariableDefinitions}; use swc_common::Span; use swc_ecma_ast::*; diff --git a/transforms/graphql_tag/src/parser/utils.rs b/transforms/graphql_tag/src/parser/utils.rs index 733d271..3a35d7d 100644 --- a/transforms/graphql_tag/src/parser/utils.rs +++ b/transforms/graphql_tag/src/parser/utils.rs @@ -1,4 +1,4 @@ -use apollo_parser::{ast::OperationType, Lexer, TokenKind}; +use apollo_parser::{cst::OperationType, Error, Lexer, TokenKind}; use swc_ecma_ast::*; @@ -49,13 +49,18 @@ fn is_punctuator_token_kind(kind: TokenKind) -> bool { } } -pub fn strip_ignored_characters(source: String) -> String { +pub fn strip_ignored_characters(source: String) -> Result> { let lexer = Lexer::new(source.as_str()); let mut stripped_body = String::new(); let mut was_last_added_token_non_punctuator = false; + let (tokens, errors) = lexer.lex(); - for token in lexer.tokens() { + if errors.len() != 0 { + return Err(errors); + } + + for token in tokens { let kind = token.kind(); match kind { TokenKind::Whitespace | TokenKind::Comment | TokenKind::Eof => continue, @@ -73,5 +78,5 @@ pub fn strip_ignored_characters(source: String) -> String { } } - stripped_body + Ok(stripped_body) }