Skip to content

Commit

Permalink
chore: apollo_parser upgrade init (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 authored Dec 20, 2023
2 parents 1b4e266 + feaf7de commit b3701f6
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 30 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-tag-swc-plugin",
"version": "0.1.7-0",
"version": "0.1.7-1",
"description": "SWC plugin to expand gql tags at build time",
"author": "rishabh3112 <rishabh31121999@gmail.com>",
"license": "ISC",
Expand Down
10 changes: 10 additions & 0 deletions tests/graphql_tag/dynamicSegments/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ const QUERY_WITH_DYNAMIC_SEGMENT = gql`
${DYNAMIC_FRAGMENT}
`;

const QUERY_WITH_DYNAMIC_FRAGMENT_SPREAD = gql`
query testQuery {
getEntity {
...${NAME}
}
}
${DYNAMIC_FRAGMENT}
`;

const STATIC_QUERY = gql`
query testQuery {
getEntity {
Expand Down
9 changes: 9 additions & 0 deletions tests/graphql_tag/dynamicSegments/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ const QUERY_WITH_DYNAMIC_SEGMENT = gql`
${DYNAMIC_FRAGMENT}
`;
const QUERY_WITH_DYNAMIC_FRAGMENT_SPREAD = gql`
query testQuery {
getEntity {
...${NAME}
}
}
${DYNAMIC_FRAGMENT}
`;
const STATIC_QUERY = {
"kind": "Document",
"definitions": /*#__PURE__*/ unique(/*#__PURE__*/ [
Expand Down
9 changes: 9 additions & 0 deletions tests/graphql_tag/dynamicSegments/strip-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ const QUERY_WITH_DYNAMIC_SEGMENT = gql`
${DYNAMIC_FRAGMENT}
`;
const QUERY_WITH_DYNAMIC_FRAGMENT_SPREAD = gql`
query testQuery {
getEntity {
...${NAME}
}
}
${DYNAMIC_FRAGMENT}
`;
const STATIC_QUERY = {
"kind": "Document",
"definitions": /*#__PURE__*/ unique(/*#__PURE__*/ [
Expand Down
2 changes: 1 addition & 1 deletion transforms/graphql_tag/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
apollo-parser = "0.2.10"
apollo-parser = "0.7.5"
swc_common = { version = "0.33.8", features = ["concurrent"] }
swc_ecma_ast = "0.110.9"
swc_ecma_visit = "0.96.9"
Expand Down
16 changes: 15 additions & 1 deletion transforms/graphql_tag/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion transforms/graphql_tag/src/parser/nodes/arguments/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// libs
use apollo_parser::ast::FragmentDefinition;
use apollo_parser::cst::FragmentDefinition;
use swc_common::Span;
use swc_ecma_ast::*;

Expand Down
4 changes: 2 additions & 2 deletions transforms/graphql_tag/src/parser/nodes/definitions/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn create_definition(definition: Definition, span: Span) -> Option<ExprOrSpr
})
}

pub fn create_definitions(definitions: AstChildren<Definition>, span: Span) -> Expr {
pub fn create_definitions(definitions: CstChildren<Definition>, span: Span) -> Expr {
let mut all_definitions = vec![];
for def in definitions {
all_definitions.push(create_definition(def, span));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// libs
use apollo_parser::ast::OperationDefinition;
use apollo_parser::cst::OperationDefinition;
use swc_common::Span;
use swc_ecma_ast::*;

Expand Down
2 changes: 1 addition & 1 deletion transforms/graphql_tag/src/parser/nodes/directive/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
2 changes: 1 addition & 1 deletion transforms/graphql_tag/src/parser/nodes/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
4 changes: 2 additions & 2 deletions transforms/graphql_tag/src/parser/nodes/selection_set/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down Expand Up @@ -32,7 +32,7 @@ pub fn create_selection_set(selection_set: Option<SelectionSet>, span: Span) ->
Expr::Object(sel_set)
}

fn create_selections(selections: AstChildren<Selection>, span: Span) -> Expr {
fn create_selections(selections: CstChildren<Selection>, span: Span) -> Expr {
let mut all_selections = vec![];
for selection in selections {
all_selections.push(create_selection(selection, span));
Expand Down
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
2 changes: 1 addition & 1 deletion transforms/graphql_tag/src/parser/nodes/types/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
15 changes: 6 additions & 9 deletions transforms/graphql_tag/src/parser/nodes/value/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,15 +32,12 @@ pub fn create_value(value: Option<Value>, 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<str>[^"]*)""#).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,
Expand Down Expand Up @@ -147,7 +144,7 @@ fn create_object_value(object: ObjectValue, span: Span) -> Expr {
Expr::Object(object_val)
}

fn create_object_fields(object_fields: AstChildren<ObjectField>, span: Span) -> Expr {
fn create_object_fields(object_fields: CstChildren<ObjectField>, span: Span) -> Expr {
let mut all_fields = vec![];
for field in object_fields.into_iter() {
all_fields.push(Some(ExprOrSpread {
Expand Down Expand Up @@ -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<Value>, span: Span) -> Expr {
fn create_list_value_values(values: CstChildren<Value>, span: Span) -> Expr {
let mut all_values = vec![];
for value in values.into_iter() {
all_values.push(Some(ExprOrSpread {
Expand Down
2 changes: 1 addition & 1 deletion transforms/graphql_tag/src/parser/nodes/variables/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
13 changes: 9 additions & 4 deletions transforms/graphql_tag/src/parser/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use apollo_parser::{ast::OperationType, Lexer, TokenKind};
use apollo_parser::{cst::OperationType, Error, Lexer, TokenKind};

use swc_ecma_ast::*;

Expand Down Expand Up @@ -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<String, Vec<Error>> {
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,
Expand All @@ -73,5 +78,5 @@ pub fn strip_ignored_characters(source: String) -> String {
}
}

stripped_body
Ok(stripped_body)
}

0 comments on commit b3701f6

Please sign in to comment.