@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
167 lines (166 loc) • 5.52 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var require_import_fragment_exports = {};
__export(require_import_fragment_exports, {
rule: () => rule
});
module.exports = __toCommonJS(require_import_fragment_exports);
var import_path = __toESM(require("path"));
var import_utils = 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,
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 = (0, import_utils.requireSiblingsOperations)(RULE_ID, context);
const filePath = context.getFilename();
return {
"FragmentSpread > .name"(node) {
var _a;
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 = (_a = comment.value.match(/(["'])((?:\1|.)*?)\1/)) == null ? void 0 : _a[2];
if (!extractedImportPath)
continue;
const importPath = import_path.default.join(import_path.default.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) => import_path.default.relative(import_path.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}'
`
)
}))
});
}
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
rule
});