|
| 1 | +import path from 'path'; |
| 2 | +import { NameNode } from 'graphql'; |
| 3 | +import { requireSiblingsOperations } from '../utils.js'; |
| 4 | +import { GraphQLESTreeNode } from '../estree-converter/index.js'; |
| 5 | +import { GraphQLESLintRule } from '../types.js'; |
| 6 | + |
| 7 | +const RULE_ID = 'require-import-fragment'; |
| 8 | +const SUGGESTION_ID = 'add-import-expression'; |
| 9 | + |
| 10 | +export const rule: GraphQLESLintRule = { |
| 11 | + meta: { |
| 12 | + type: 'suggestion', |
| 13 | + docs: { |
| 14 | + category: 'Operations', |
| 15 | + description: 'Require fragments to be imported via an import expression.', |
| 16 | + url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`, |
| 17 | + examples: [ |
| 18 | + { |
| 19 | + title: 'Incorrect', |
| 20 | + code: /* GraphQL */ ` |
| 21 | + query { |
| 22 | + user { |
| 23 | + ...UserFields |
| 24 | + } |
| 25 | + } |
| 26 | + `, |
| 27 | + }, |
| 28 | + { |
| 29 | + title: 'Incorrect', |
| 30 | + code: /* GraphQL */ ` |
| 31 | + # import 'post-fields.fragment.graphql' |
| 32 | + query { |
| 33 | + user { |
| 34 | + ...UserFields |
| 35 | + } |
| 36 | + } |
| 37 | + `, |
| 38 | + }, |
| 39 | + { |
| 40 | + title: 'Incorrect', |
| 41 | + code: /* GraphQL */ ` |
| 42 | + # import UserFields from 'post-fields.fragment.graphql' |
| 43 | + query { |
| 44 | + user { |
| 45 | + ...UserFields |
| 46 | + } |
| 47 | + } |
| 48 | + `, |
| 49 | + }, |
| 50 | + { |
| 51 | + title: 'Correct', |
| 52 | + code: /* GraphQL */ ` |
| 53 | + # import UserFields from 'user-fields.fragment.graphql' |
| 54 | + query { |
| 55 | + user { |
| 56 | + ...UserFields |
| 57 | + } |
| 58 | + } |
| 59 | + `, |
| 60 | + }, |
| 61 | + ], |
| 62 | + requiresSiblings: true, |
| 63 | + isDisabledForAllConfig: true, |
| 64 | + }, |
| 65 | + hasSuggestions: true, |
| 66 | + messages: { |
| 67 | + [RULE_ID]: 'Expected "{{fragmentName}}" fragment to be imported.', |
| 68 | + [SUGGESTION_ID]: 'Add import expression for "{{fragmentName}}".', |
| 69 | + }, |
| 70 | + schema: [], |
| 71 | + }, |
| 72 | + create(context) { |
| 73 | + const comments = context.getSourceCode().getAllComments(); |
| 74 | + const siblings = requireSiblingsOperations(RULE_ID, context); |
| 75 | + const filePath = context.getFilename(); |
| 76 | + |
| 77 | + return { |
| 78 | + 'FragmentSpread > .name'(node: GraphQLESTreeNode<NameNode>) { |
| 79 | + const fragmentName = node.value; |
| 80 | + const fragmentsFromSiblings = siblings.getFragment(fragmentName); |
| 81 | + |
| 82 | + for (const comment of comments) { |
| 83 | + if (comment.type !== 'Line') continue; |
| 84 | + |
| 85 | + // 1. could start with extra whitespace |
| 86 | + // 2. match both named/default import |
| 87 | + const isPossibleImported = new RegExp( |
| 88 | + `^\\s*import\\s+(${fragmentName}\\s+from\\s+)?['"]`, |
| 89 | + ).test(comment.value); |
| 90 | + if (!isPossibleImported) continue; |
| 91 | + |
| 92 | + const extractedImportPath = comment.value.match(/(["'])((?:\1|.)*?)\1/)?.[2]; |
| 93 | + if (!extractedImportPath) continue; |
| 94 | + |
| 95 | + const importPath = path.join(path.dirname(filePath), extractedImportPath); |
| 96 | + const hasInSiblings = fragmentsFromSiblings.some( |
| 97 | + source => source.filePath === importPath, |
| 98 | + ); |
| 99 | + if (hasInSiblings) return; |
| 100 | + } |
| 101 | + |
| 102 | + const fragmentInSameFile = fragmentsFromSiblings.some( |
| 103 | + source => source.filePath === filePath, |
| 104 | + ); |
| 105 | + if (fragmentInSameFile) return; |
| 106 | + |
| 107 | + const suggestedFilePaths = fragmentsFromSiblings.length |
| 108 | + ? fragmentsFromSiblings.map(o => path.relative(path.dirname(filePath), o.filePath)) |
| 109 | + : ['CHANGE_ME.graphql']; |
| 110 | + |
| 111 | + context.report({ |
| 112 | + node, |
| 113 | + messageId: RULE_ID, |
| 114 | + data: { fragmentName }, |
| 115 | + suggest: suggestedFilePaths.map(suggestedPath => ({ |
| 116 | + messageId: SUGGESTION_ID, |
| 117 | + data: { fragmentName }, |
| 118 | + fix: fixer => |
| 119 | + fixer.insertTextBeforeRange( |
| 120 | + [0, 0], |
| 121 | + `# import ${fragmentName} from '${suggestedPath}'\n`, |
| 122 | + ), |
| 123 | + })), |
| 124 | + }); |
| 125 | + }, |
| 126 | + }; |
| 127 | + }, |
| 128 | +}; |
0 commit comments