From 67828b749db873054c80963610b2611d18d97d9f Mon Sep 17 00:00:00 2001 From: Jesse Smith Date: Wed, 14 Feb 2024 16:27:37 -0800 Subject: [PATCH 1/2] Add failing test --- .../mocks/import-fragments/baz-fragment/index.gql | 3 +++ .../mocks/import-fragments/baz-fragment/package.json | 3 +++ .../__tests__/mocks/import-fragments/module-import.gql | 7 +++++++ packages/plugin/__tests__/require-import-fragment.spec.ts | 5 +++++ 4 files changed, 18 insertions(+) create mode 100644 packages/plugin/__tests__/mocks/import-fragments/baz-fragment/index.gql create mode 100644 packages/plugin/__tests__/mocks/import-fragments/baz-fragment/package.json create mode 100644 packages/plugin/__tests__/mocks/import-fragments/module-import.gql diff --git a/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/index.gql b/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/index.gql new file mode 100644 index 00000000000..b8d612d74aa --- /dev/null +++ b/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/index.gql @@ -0,0 +1,3 @@ +fragment BazFields on Foo { + id +} diff --git a/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/package.json b/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/package.json new file mode 100644 index 00000000000..c755cf07e39 --- /dev/null +++ b/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/package.json @@ -0,0 +1,3 @@ +{ + "main": "./index.gql" +} diff --git a/packages/plugin/__tests__/mocks/import-fragments/module-import.gql b/packages/plugin/__tests__/mocks/import-fragments/module-import.gql new file mode 100644 index 00000000000..8a3f6f694c3 --- /dev/null +++ b/packages/plugin/__tests__/mocks/import-fragments/module-import.gql @@ -0,0 +1,7 @@ +# import './baz-fragment' + +query { + foo { + ...BazFields + } +} diff --git a/packages/plugin/__tests__/require-import-fragment.spec.ts b/packages/plugin/__tests__/require-import-fragment.spec.ts index b594e68d836..ab1a1e1b519 100644 --- a/packages/plugin/__tests__/require-import-fragment.spec.ts +++ b/packages/plugin/__tests__/require-import-fragment.spec.ts @@ -13,6 +13,7 @@ function withMocks({ name, filename, errors }: { name: string; filename: string; filename, join(__dirname, 'mocks/import-fragments/foo-fragment.gql'), join(__dirname, 'mocks/import-fragments/bar-fragment.gql'), + join(__dirname, 'mocks/import-fragments/baz-fragment/index.gql'), ], }, } satisfies ParserOptionsForTests, @@ -34,6 +35,10 @@ ruleTester.run('require-import-fragment', rule, { name: 'should not report fragments from the same file', filename: join(__dirname, 'mocks/import-fragments/same-file.gql'), }), + withMocks({ + name: 'should not report with module import', + filename: join(__dirname, 'mocks/import-fragments/module-import.gql'), + }), ], invalid: [ withMocks({ From d33edb2dde1d8c6981bfb69907abb8010c3e2307 Mon Sep 17 00:00:00 2001 From: Jesse Smith Date: Wed, 14 Feb 2024 16:28:08 -0800 Subject: [PATCH 2/2] Add support for module paths to require-import-fragment --- packages/plugin/src/rules/require-import-fragment.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/plugin/src/rules/require-import-fragment.ts b/packages/plugin/src/rules/require-import-fragment.ts index d1579d86ff3..375d823e57b 100644 --- a/packages/plugin/src/rules/require-import-fragment.ts +++ b/packages/plugin/src/rules/require-import-fragment.ts @@ -91,7 +91,13 @@ export const rule: GraphQLESLintRule = { const extractedImportPath = comment.value.match(/(["'])((?:\1|.)*?)\1/)?.[2]; if (!extractedImportPath) continue; - const importPath = path.join(path.dirname(filePath), extractedImportPath); + let importPath: string; + try { + importPath = require.resolve(extractedImportPath, { paths: [path.dirname(filePath)] }); + } catch { + importPath = path.join(path.dirname(filePath), extractedImportPath); + } + const hasInSiblings = fragmentsFromSiblings.some( source => source.filePath === importPath, );