UNPKG

@graphql-eslint/eslint-plugin

Version:
131 lines (128 loc) 4.68 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _nodepath = require('node:path'); var _nodepath2 = _interopRequireDefault(_nodepath); var _utilsjs = require('../../utils.js'); const RULE_ID = "require-import-fragment"; const SUGGESTION_ID = "add-import-expression"; const rule = { meta: { type: "suggestion", docs: { category: "Operations", description: "Require fragments to be imported via an import expression.", url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`, 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 }, 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 = _utilsjs.requireGraphQLOperations.call(void 0, RULE_ID, context); const filePath = context.filename; return { "FragmentSpread > .name"(node) { const fragmentName = node.value; const fragmentsFromSiblings = siblings.getFragment(fragmentName); for (const comment of comments) { if (comment.type !== "Line") continue; const isPossibleImported = new RegExp( `^\\s*import\\s+(${fragmentName}\\s+from\\s+)?['"]` ).test(comment.value); if (!isPossibleImported) continue; const extractedImportPath = _optionalChain([comment, 'access', _ => _.value, 'access', _2 => _2.match, 'call', _3 => _3(/(["'])((?:\1|.)*?)\1/), 'optionalAccess', _4 => _4[2]]); if (!extractedImportPath) continue; const importPath = _nodepath2.default.join(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) => ( // Use always forward slash for suggested import path _utilsjs.slash.call(void 0, _nodepath2.default.relative(_nodepath2.default.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}' ` ) })) }); } }; } }; exports.rule = rule;