-
Notifications
You must be signed in to change notification settings - Fork 109
Add require-import-fragment rule
#1443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dimaMachina
merged 15 commits into
graphql-hive:master
from
FloEdelmann:require-import-fragment
Feb 9, 2023
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0a3bf5c
Add `require-import-fragment` rule
FloEdelmann 19b442f
Generate configs
FloEdelmann e2ba549
Add changeset
FloEdelmann 1ecc373
Also allow locally defined fragments
FloEdelmann 4033be1
Add suggestion
FloEdelmann e939550
Pass only name node
FloEdelmann d9b0a8b
Use `trimStart` instead of `trim`
FloEdelmann ca6085a
Adjust test case name
FloEdelmann 3b7513f
apply fixes
dimaMachina 012c5e4
this is no needed
dimaMachina f33745e
fix tests
dimaMachina 9a708e9
some fixes
dimaMachina 65f0a45
prettify
dimaMachina 1660617
simplify
dimaMachina 007cbf3
last refactor
dimaMachina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@graphql-eslint/eslint-plugin': minor | ||
| --- | ||
|
|
||
| Add new `require-import-fragment` rule that reports fragments which were not imported via an import | ||
| expression. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import path from 'path'; | ||
| import { NameNode } from 'graphql'; | ||
| import { requireSiblingsOperations } from '../utils.js'; | ||
| import { GraphQLESTreeNode } from '../estree-converter/index.js'; | ||
| import { GraphQLESLintRule } from '../types.js'; | ||
|
|
||
| const RULE_ID = 'require-import-fragment'; | ||
| const SUGGESTION_ID = 'add-import-expression'; | ||
|
|
||
| export const rule: GraphQLESLintRule = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| category: 'Operations', | ||
| description: 'Require fragments to be imported via an import expression.', | ||
dimaMachina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`, | ||
| examples: [ | ||
| { | ||
| title: 'Incorrect', | ||
| code: /* GraphQL */ ` | ||
| query { | ||
| user { | ||
| ...UserFields | ||
| } | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| title: 'Incorrect', | ||
| code: /* GraphQL */ ` | ||
| # import 'post-fields.fragment.graphql' | ||
| query { | ||
| user { | ||
| ...UserFields | ||
| } | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| title: 'Incorrect', | ||
| code: /* GraphQL */ ` | ||
| # import UserFields from 'post-fields.fragment.graphql' | ||
| query { | ||
| user { | ||
| ...UserFields | ||
| } | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| title: 'Correct', | ||
| code: /* GraphQL */ ` | ||
| # import UserFields from 'user-fields.fragment.graphql' | ||
| query { | ||
| user { | ||
| ...UserFields | ||
| } | ||
| } | ||
| `, | ||
| }, | ||
| ], | ||
| requiresSiblings: true, | ||
| isDisabledForAllConfig: true, | ||
| }, | ||
| hasSuggestions: true, | ||
| messages: { | ||
| [RULE_ID]: 'Expected "{{fragmentName}}" fragment to be imported.', | ||
| [SUGGESTION_ID]: 'Add import expression for "{{fragmentName}}".', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| create(context) { | ||
| const comments = context.getSourceCode().getAllComments(); | ||
| const siblings = requireSiblingsOperations(RULE_ID, context); | ||
| const filePath = context.getFilename(); | ||
|
|
||
| return { | ||
| 'FragmentSpread > .name'(node: GraphQLESTreeNode<NameNode>) { | ||
| const fragmentName = node.value; | ||
| const fragmentsFromSiblings = siblings.getFragment(fragmentName); | ||
|
|
||
| for (const comment of comments) { | ||
| if (comment.type !== 'Line') continue; | ||
|
|
||
| // 1. could start with extra whitespace | ||
| // 2. match both named/default import | ||
| const isPossibleImported = new RegExp( | ||
| `^\\s*import\\s+(${fragmentName}\\s+from\\s+)?['"]`, | ||
| ).test(comment.value); | ||
| if (!isPossibleImported) continue; | ||
|
|
||
| const extractedImportPath = comment.value.match(/(["'])((?:\1|.)*?)\1/)?.[2]; | ||
| if (!extractedImportPath) continue; | ||
|
|
||
| const importPath = path.join(path.dirname(filePath), extractedImportPath); | ||
| const hasInSiblings = fragmentsFromSiblings.some( | ||
| source => source.filePath === importPath, | ||
| ); | ||
| if (hasInSiblings) return; | ||
| } | ||
|
|
||
| const fragmentInSameFile = fragmentsFromSiblings.some( | ||
| source => source.filePath === filePath, | ||
| ); | ||
| if (fragmentInSameFile) return; | ||
|
|
||
| const suggestedFilePaths = fragmentsFromSiblings.length | ||
| ? fragmentsFromSiblings.map(o => path.relative(path.dirname(filePath), o.filePath)) | ||
| : ['CHANGE_ME.graphql']; | ||
|
|
||
| context.report({ | ||
| node, | ||
| messageId: RULE_ID, | ||
| data: { fragmentName }, | ||
| suggest: suggestedFilePaths.map(suggestedPath => ({ | ||
| messageId: SUGGESTION_ID, | ||
| data: { fragmentName }, | ||
| fix: fixer => | ||
| fixer.insertTextBeforeRange( | ||
| [0, 0], | ||
| `# import ${fragmentName} from '${suggestedPath}'\n`, | ||
| ), | ||
| })), | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/plugin/tests/__snapshots__/require-import-fragment.spec.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Vitest Snapshot v1 | ||
|
|
||
| exports[`should report fragments when there are no import expressions 1`] = ` | ||
| #### ⌨️ Code | ||
|
|
||
| 1 | { | ||
| 2 | foo { | ||
| 3 | ...FooFields | ||
| 4 | } | ||
| 5 | } | ||
|
|
||
| #### ❌ Error | ||
|
|
||
| 2 | foo { | ||
| > 3 | ...FooFields | ||
| | ^^^^^^^^^ Expected "FooFields" fragment to be imported. | ||
| 4 | } | ||
|
|
||
| #### 💡 Suggestion: Add import expression for "FooFields". | ||
|
|
||
| 1 | # import FooFields from 'foo-fragment.gql' | ||
| 2 | { | ||
| 3 | foo { | ||
| 4 | ...FooFields | ||
| 5 | } | ||
| 6 | } | ||
| `; | ||
|
|
||
| exports[`should report with default import 1`] = ` | ||
| #### ⌨️ Code | ||
|
|
||
| 1 | #import 'bar-fragment.gql' | ||
| 2 | query { | ||
| 3 | foo { | ||
| 4 | ...FooFields | ||
| 5 | } | ||
| 6 | } | ||
|
|
||
| #### ❌ Error | ||
|
|
||
| 3 | foo { | ||
| > 4 | ...FooFields | ||
| | ^^^^^^^^^ Expected "FooFields" fragment to be imported. | ||
| 5 | } | ||
|
|
||
| #### 💡 Suggestion: Add import expression for "FooFields". | ||
|
|
||
| 1 | # import FooFields from 'foo-fragment.gql' | ||
| 2 | #import 'bar-fragment.gql' | ||
| 3 | query { | ||
| 4 | foo { | ||
| 5 | ...FooFields | ||
| 6 | } | ||
| 7 | } | ||
| `; | ||
|
|
||
| exports[`should report with named import 1`] = ` | ||
| #### ⌨️ Code | ||
|
|
||
| 1 | #import FooFields from "bar-fragment.gql" | ||
| 2 | query { | ||
| 3 | foo { | ||
| 4 | ...FooFields | ||
| 5 | } | ||
| 6 | } | ||
|
|
||
| #### ❌ Error | ||
|
|
||
| 3 | foo { | ||
| > 4 | ...FooFields | ||
| | ^^^^^^^^^ Expected "FooFields" fragment to be imported. | ||
| 5 | } | ||
|
|
||
| #### 💡 Suggestion: Add import expression for "FooFields". | ||
|
|
||
| 1 | # import FooFields from 'foo-fragment.gql' | ||
| 2 | #import FooFields from "bar-fragment.gql" | ||
| 3 | query { | ||
| 4 | foo { | ||
| 5 | ...FooFields | ||
| 6 | } | ||
| 7 | } | ||
| `; |
3 changes: 3 additions & 0 deletions
3
packages/plugin/tests/mocks/import-fragments/bar-fragment.gql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| fragment BarFields on Bar { | ||
| id | ||
| } |
3 changes: 3 additions & 0 deletions
3
packages/plugin/tests/mocks/import-fragments/foo-fragment.gql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| fragment FooFields on Foo { | ||
| id | ||
| } |
6 changes: 6 additions & 0 deletions
6
packages/plugin/tests/mocks/import-fragments/invalid-query-default.gql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #import 'bar-fragment.gql' | ||
| query { | ||
| foo { | ||
| ...FooFields | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
packages/plugin/tests/mocks/import-fragments/invalid-query.gql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #import FooFields from "bar-fragment.gql" | ||
| query { | ||
| foo { | ||
| ...FooFields | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
packages/plugin/tests/mocks/import-fragments/missing-import.gql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| foo { | ||
| ...FooFields | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| foo { | ||
| ...FooFields | ||
| } | ||
| } | ||
|
|
||
| fragment FooFields on Foo { | ||
| id | ||
| } |
9 changes: 9 additions & 0 deletions
9
packages/plugin/tests/mocks/import-fragments/valid-query-default.gql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Imports could have extra whitespace and double/single quotes | ||
|
|
||
| # import 'foo-fragment.gql' | ||
|
|
||
| query { | ||
| foo { | ||
| ...FooFields | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Imports could have extra whitespace and double/single quotes | ||
|
|
||
| # import FooFields from "foo-fragment.gql" | ||
|
|
||
| query { | ||
| foo { | ||
| ...FooFields | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { join } from 'node:path'; | ||
| import { GraphQLInvalidTestCase, GraphQLRuleTester } from '../src'; | ||
| import { rule } from '../src/rules/require-import-fragment'; | ||
| import { Linter } from 'eslint'; | ||
| import ParserOptions = Linter.ParserOptions; | ||
|
|
||
| const ruleTester = new GraphQLRuleTester(); | ||
|
|
||
| function withMocks({ | ||
| name, | ||
| filename, | ||
| errors, | ||
| }: { | ||
| name: string; | ||
| filename: string; | ||
| errors?: GraphQLInvalidTestCase['errors']; | ||
| }): { | ||
| name: string; | ||
| filename: string; | ||
| code: string; | ||
| parserOptions: { | ||
| documents: ParserOptions['documents']; | ||
| }; | ||
| errors: any; | ||
| } { | ||
| return { | ||
| name, | ||
| filename, | ||
| code: ruleTester.fromMockFile(filename.split('/mocks')[1]), | ||
| parserOptions: { | ||
| documents: [ | ||
| filename, | ||
| join(__dirname, 'mocks/import-fragments/foo-fragment.gql'), | ||
| join(__dirname, 'mocks/import-fragments/bar-fragment.gql'), | ||
| ], | ||
| }, | ||
| errors, | ||
| }; | ||
| } | ||
|
|
||
| ruleTester.runGraphQLTests('require-import-fragment', rule, { | ||
| valid: [ | ||
| withMocks({ | ||
| name: 'should not report with named import', | ||
| filename: join(__dirname, 'mocks/import-fragments/valid-query.gql'), | ||
| }), | ||
| withMocks({ | ||
| name: 'should not report with default import', | ||
| filename: join(__dirname, 'mocks/import-fragments/valid-query-default.gql'), | ||
| }), | ||
| withMocks({ | ||
| name: 'should not report fragments from the same file', | ||
| filename: join(__dirname, 'mocks/import-fragments/same-file.gql'), | ||
| }), | ||
| ], | ||
| invalid: [ | ||
| withMocks({ | ||
| name: 'should report with named import', | ||
| filename: join(__dirname, 'mocks/import-fragments/invalid-query.gql'), | ||
| errors: [{ message: 'Expected "FooFields" fragment to be imported.' }], | ||
| }), | ||
| withMocks({ | ||
| name: 'should report with default import', | ||
| filename: join(__dirname, 'mocks/import-fragments/invalid-query-default.gql'), | ||
| errors: [{ message: 'Expected "FooFields" fragment to be imported.' }], | ||
| }), | ||
| withMocks({ | ||
| name: 'should report fragments when there are no import expressions', | ||
| filename: join(__dirname, 'mocks/import-fragments/missing-import.gql'), | ||
| errors: [{ message: 'Expected "FooFields" fragment to be imported.' }], | ||
| }), | ||
| ], | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.