From e602dd759aeb6109493b445f50f96b52d7b23525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 15 Jul 2024 16:52:34 +0900 Subject: [PATCH] fix(relay): Handle comments in graphql (#333) https://github.com/vercel/next.js/issues/67614 --- Cargo.lock | 2 +- packages/emotion/transform/tests/fixture.rs | 4 +-- .../transform/tests/fixture.rs | 4 +-- packages/relay/CHANGELOG.md | 6 +++++ packages/relay/README.md | 6 +++++ packages/relay/package.json | 2 +- packages/relay/transform/Cargo.toml | 2 +- packages/relay/transform/src/lib.rs | 27 ++++++++++++++++--- .../tests/fixture/simple/next-67614/input.js | 7 +++++ .../tests/fixture/simple/next-67614/output.js | 1 + .../remove-console/transform/tests/fixture.rs | 4 +-- .../transform/tests/fixture.rs | 4 +-- .../styled-jsx/transform/tests/fixture.rs | 4 +-- .../transform/tests/fixture.rs | 4 +-- 14 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 packages/relay/transform/tests/fixture/simple/next-67614/input.js create mode 100644 packages/relay/transform/tests/fixture/simple/next-67614/output.js diff --git a/Cargo.lock b/Cargo.lock index 44558817a..3f850199f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3149,7 +3149,7 @@ dependencies = [ [[package]] name = "swc_relay" -version = "0.44.19" +version = "0.44.20" dependencies = [ "once_cell", "regex", diff --git a/packages/emotion/transform/tests/fixture.rs b/packages/emotion/transform/tests/fixture.rs index e83996d33..36410cc58 100644 --- a/packages/emotion/transform/tests/fixture.rs +++ b/packages/emotion/transform/tests/fixture.rs @@ -1,14 +1,14 @@ use std::path::PathBuf; use swc_common::{chain, comments::SingleThreadedComments, Mark}; -use swc_ecma_parser::{Syntax, TsConfig}; +use swc_ecma_parser::{Syntax, TsSyntax}; use swc_ecma_transforms_react::{jsx, Runtime}; use swc_ecma_transforms_testing::test_fixture; use swc_emotion::EmotionOptions; use testing::fixture; fn ts_syntax() -> Syntax { - Syntax::Typescript(TsConfig { + Syntax::Typescript(TsSyntax { tsx: true, ..Default::default() }) diff --git a/packages/react-remove-properties/transform/tests/fixture.rs b/packages/react-remove-properties/transform/tests/fixture.rs index 12ea80ad3..13c2fe4dd 100644 --- a/packages/react-remove-properties/transform/tests/fixture.rs +++ b/packages/react-remove-properties/transform/tests/fixture.rs @@ -2,12 +2,12 @@ use std::path::PathBuf; use react_remove_properties::Options; use swc_common::{chain, Mark}; -use swc_ecma_parser::{EsConfig, Syntax}; +use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig}; fn syntax() -> Syntax { - Syntax::Es(EsConfig { + Syntax::Es(EsSyntax { jsx: true, ..Default::default() }) diff --git a/packages/relay/CHANGELOG.md b/packages/relay/CHANGELOG.md index d9d4ed617..06841bed4 100644 --- a/packages/relay/CHANGELOG.md +++ b/packages/relay/CHANGELOG.md @@ -1,5 +1,11 @@ # @swc/plugin-relay +## 2.0.12 + +### Patch Changes + +- 06e1e66: Fix operation name detection + ## 2.0.11 ### Patch Changes diff --git a/packages/relay/README.md b/packages/relay/README.md index a4f6f7111..262d78297 100644 --- a/packages/relay/README.md +++ b/packages/relay/README.md @@ -104,6 +104,12 @@ In this example typescript graphql files will output transpiled import path of ` # @swc/plugin-relay +## 2.0.12 + +### Patch Changes + +- 06e1e66: Fix operation name detection + ## 2.0.11 ### Patch Changes diff --git a/packages/relay/package.json b/packages/relay/package.json index 8ea999467..ba83bf617 100644 --- a/packages/relay/package.json +++ b/packages/relay/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-relay", - "version": "2.0.11", + "version": "2.0.12", "description": "SWC plugin for relay", "main": "swc_plugin_relay.wasm", "types": "./types.d.ts", diff --git a/packages/relay/transform/Cargo.toml b/packages/relay/transform/Cargo.toml index ec3501142..55ceba5bd 100644 --- a/packages/relay/transform/Cargo.toml +++ b/packages/relay/transform/Cargo.toml @@ -11,7 +11,7 @@ homepage = { workspace = true } license = { workspace = true } repository = { workspace = true } rust-version = { workspace = true } -version = "0.44.19" +version = "0.44.20" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/packages/relay/transform/src/lib.rs b/packages/relay/transform/src/lib.rs index 99553308e..1e7c0c0b1 100644 --- a/packages/relay/transform/src/lib.rs +++ b/packages/relay/transform/src/lib.rs @@ -3,6 +3,7 @@ //! TODO: Once refactoring next-swc is done, remove duplicated codes and import //! packages directly use std::{ + borrow::Cow, path::{Path, PathBuf}, sync::Arc, }; @@ -136,12 +137,32 @@ pub struct ProjectConfig { pub artifact_directory: Option, } +/// A line starting with `#` is a comment. +fn strip_comments(s: &str) -> Cow { + if s.contains('#') { + let mut buf = String::with_capacity(s.len()); + for line in s.lines() { + if let Some(idx) = line.find('#') { + buf.push_str(&line[..idx]); + } else { + buf.push_str(line); + } + buf.push('\n'); + } + buf.into() + } else { + s.into() + } +} + fn pull_first_operation_name_from_tpl(tpl: &TaggedTpl) -> Option { + static OPERATION_REGEX: Lazy = + Lazy::new(|| Regex::new(r"(fragment|mutation|query|subscription) (\w+)").unwrap()); + tpl.tpl.quasis.iter().find_map(|quasis| { - static OPERATION_REGEX: Lazy = - Lazy::new(|| Regex::new(r"(fragment|mutation|query|subscription) (\w+)").unwrap()); + let raw = strip_comments(&quasis.raw); - let capture_group = OPERATION_REGEX.captures_iter(&quasis.raw).next(); + let capture_group = OPERATION_REGEX.captures_iter(&raw).next(); capture_group.map(|capture_group| capture_group[2].to_string()) }) diff --git a/packages/relay/transform/tests/fixture/simple/next-67614/input.js b/packages/relay/transform/tests/fixture/simple/next-67614/input.js new file mode 100644 index 000000000..423d15f94 --- /dev/null +++ b/packages/relay/transform/tests/fixture/simple/next-67614/input.js @@ -0,0 +1,7 @@ +graphql` + # Pagination query to be fetched upon calling 'loadMore'. + # Notice that we re-use our fragment, and the shape of this query matches our fragment spec. + query UserListPaginationQuery { + ...UserList_query + } + ` \ No newline at end of file diff --git a/packages/relay/transform/tests/fixture/simple/next-67614/output.js b/packages/relay/transform/tests/fixture/simple/next-67614/output.js new file mode 100644 index 000000000..8f460dfb8 --- /dev/null +++ b/packages/relay/transform/tests/fixture/simple/next-67614/output.js @@ -0,0 +1 @@ +require("./__generated__/UserListPaginationQuery.graphql.ts"); diff --git a/packages/remove-console/transform/tests/fixture.rs b/packages/remove-console/transform/tests/fixture.rs index 2bad8db5a..74507f733 100644 --- a/packages/remove-console/transform/tests/fixture.rs +++ b/packages/remove-console/transform/tests/fixture.rs @@ -1,12 +1,12 @@ use std::path::PathBuf; use swc_common::{chain, Mark, SyntaxContext}; -use swc_ecma_parser::{EsConfig, Syntax}; +use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_base::resolver; use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig}; fn syntax() -> Syntax { - Syntax::Es(EsConfig { + Syntax::Es(EsSyntax { jsx: true, ..Default::default() }) diff --git a/packages/styled-components/transform/tests/fixture.rs b/packages/styled-components/transform/tests/fixture.rs index 41e99657f..30f55a3cb 100644 --- a/packages/styled-components/transform/tests/fixture.rs +++ b/packages/styled-components/transform/tests/fixture.rs @@ -4,7 +4,7 @@ use std::{fs::read_to_string, path::PathBuf}; use styled_components::{styled_components, Config}; use swc_common::{chain, Mark}; -use swc_ecma_parser::{EsConfig, Syntax}; +use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms::resolver; use swc_ecma_transforms_testing::test_fixture; @@ -16,7 +16,7 @@ fn fixture(input: PathBuf) { let config: Config = serde_json::from_str(&config).unwrap(); test_fixture( - Syntax::Es(EsConfig { + Syntax::Es(EsSyntax { jsx: true, ..Default::default() }), diff --git a/packages/styled-jsx/transform/tests/fixture.rs b/packages/styled-jsx/transform/tests/fixture.rs index fe15478e5..76920c14f 100644 --- a/packages/styled-jsx/transform/tests/fixture.rs +++ b/packages/styled-jsx/transform/tests/fixture.rs @@ -5,13 +5,13 @@ use lightningcss::stylesheet::ParserOptions; use preset_env_base::Versions; use styled_jsx::visitor::{styled_jsx, NativeConfig}; use swc_common::{chain, FileName, Mark, Span, DUMMY_SP}; -use swc_ecma_parser::{EsConfig, Syntax}; +use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms::resolver; use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig}; use testing::fixture; fn syntax() -> Syntax { - Syntax::Es(EsConfig { + Syntax::Es(EsSyntax { jsx: true, ..Default::default() }) diff --git a/packages/transform-imports/transform/tests/fixture.rs b/packages/transform-imports/transform/tests/fixture.rs index d3c5a995e..40dc521b9 100644 --- a/packages/transform-imports/transform/tests/fixture.rs +++ b/packages/transform-imports/transform/tests/fixture.rs @@ -1,12 +1,12 @@ use std::path::PathBuf; use modularize_imports::{modularize_imports, PackageConfig}; -use swc_ecma_parser::{EsConfig, Syntax}; +use swc_ecma_parser::{EsSyntax, Syntax}; use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig}; use testing::fixture; fn syntax() -> Syntax { - Syntax::Es(EsConfig { + Syntax::Es(EsSyntax { jsx: true, ..Default::default() })