Skip to content

Commit

Permalink
feat: unique fragment definition handling init
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh Chawla authored and rishabh3112 committed Dec 13, 2023
1 parent fe86616 commit 4de50d9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// libs
use serde::Deserialize;
use swc_core::plugin::{plugin_transform, proxies::TransformPluginProgramMetadata};
use swc_ecma_ast::Program;
use swc_ecma_visit::{as_folder, FoldWith};
use swc_ecma_ast::{Ident, Program};
use swc_ecma_visit::{as_folder, FoldWith, VisitMut};

// structs
use graphql_tag::structs::{GraphQLTagConfig, TransformVisitor};
Expand All @@ -15,12 +15,48 @@ pub struct Config {
pub strip: Option<bool>,
}

// TODO: move this to transform folder or create it's own folder
// So that we can test this
struct UniqueIdentifierVisitor {
identifier: String,
count: i64,
}

impl UniqueIdentifierVisitor {
pub fn new() -> Self {
Self {
identifier: "unique".into(),
count: 0,
}
}
}

impl VisitMut for UniqueIdentifierVisitor {
fn visit_mut_ident(&mut self, node: &mut Ident) {
if node.sym.as_str() == self.identifier {
self.count = self.count + 1;
}
}
}

#[plugin_transform]
pub fn process_transform(program: Program, data: TransformPluginProgramMetadata) -> Program {
let mut program = program;
let mut unique_visitor = UniqueIdentifierVisitor::new();

program = program.fold_with(&mut as_folder(&mut unique_visitor));

let unique_fn_name = if unique_visitor.count > 0 {
format!("{}{}", unique_visitor.identifier, unique_visitor.count)
} else {
unique_visitor.identifier
};

let default_config = GraphQLTagConfig {
import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()],
gql_tag_identifiers: vec!["gql".to_string()],
strip: false,
unique_fn_name: unique_fn_name.clone(),
};

let config = match data.get_transform_plugin_config() {
Expand All @@ -35,6 +71,7 @@ pub fn process_transform(program: Program, data: TransformPluginProgramMetadata)
.gql_tag_identifiers
.unwrap_or(default_config.gql_tag_identifiers),
strip: config.strip.unwrap_or(false),
unique_fn_name,
},
Err(_) => {
println!("Got invalid config for graphql-tag-swc-plugin, using default config instead");
Expand All @@ -45,5 +82,7 @@ pub fn process_transform(program: Program, data: TransformPluginProgramMetadata)
None => default_config,
};

program.fold_with(&mut as_folder(TransformVisitor::new(config)))
program = program.fold_with(&mut as_folder(TransformVisitor::new(config)));

program
}
1 change: 1 addition & 0 deletions tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn graphql_tag_fixture(input: PathBuf) {
import_sources: vec!["@apollo/client".to_string(), "graphql-tag".into()],
gql_tag_identifiers: vec!["gql".to_string()],
strip: true,
unique_fn_name: "unique".into(),
}))
},
&input,
Expand Down
2 changes: 2 additions & 0 deletions transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ impl TransformVisitor {

impl VisitMut for TransformVisitor {
fn visit_mut_program(&mut self, node: &mut Program) {
// TODO: use unique_fn_name for creating unique function
println!("{}", self.config.unique_fn_name);
node.visit_mut_children_with(self);
self.active_gql_tag_identifiers.clear()
}
Expand Down
1 change: 1 addition & 0 deletions transform/src/structs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct GraphQLTagConfig {
pub import_sources: Vec<String>,
pub gql_tag_identifiers: Vec<String>,
pub strip: bool,
pub unique_fn_name: String,
}

pub struct TransformVisitor {
Expand Down

0 comments on commit 4de50d9

Please sign in to comment.