Skip to content

Commit

Permalink
fix(relay): Handle comments in graphql (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jul 15, 2024
1 parent c34b259 commit e602dd7
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions packages/emotion/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
@@ -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()
})
Expand Down
4 changes: 2 additions & 2 deletions packages/react-remove-properties/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
6 changes: 6 additions & 0 deletions packages/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-relay

## 2.0.12

### Patch Changes

- 06e1e66: Fix operation name detection

## 2.0.11

### Patch Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/relay/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions packages/relay/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -136,12 +137,32 @@ pub struct ProjectConfig {
pub artifact_directory: Option<PathBuf>,
}

/// A line starting with `#` is a comment.
fn strip_comments(s: &str) -> Cow<str> {
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<String> {
static OPERATION_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(fragment|mutation|query|subscription) (\w+)").unwrap());

tpl.tpl.quasis.iter().find_map(|quasis| {
static OPERATION_REGEX: Lazy<Regex> =
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())
})
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("./__generated__/UserListPaginationQuery.graphql.ts");
4 changes: 2 additions & 2 deletions packages/remove-console/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
@@ -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()
})
Expand Down
4 changes: 2 additions & 2 deletions packages/styled-components/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/styled-jsx/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
4 changes: 2 additions & 2 deletions packages/transform-imports/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
@@ -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()
})
Expand Down

0 comments on commit e602dd7

Please sign in to comment.