@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
97 lines (96 loc) • 2.46 kB
JavaScript
import "../chunk-BMTV3EA2.js";
import { ARRAY_DEFAULT_OPTIONS, getLocation, pascalCase } from "../utils.js";
const RULE_ID = "lone-executable-definition";
const definitionTypes = ["fragment", "query", "mutation", "subscription"];
const schema = {
type: "array",
maxItems: 1,
items: {
type: "object",
minProperties: 1,
additionalProperties: false,
properties: {
ignore: {
...ARRAY_DEFAULT_OPTIONS,
maxItems: 3,
// ignore all 4 types is redundant
items: {
enum: definitionTypes
},
description: "Allow certain definitions to be placed alongside others."
}
}
}
};
const rule = {
meta: {
type: "suggestion",
docs: {
category: "Operations",
description: "Require queries, mutations, subscriptions or fragments to be located in separate files.",
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
examples: [
{
title: "Incorrect",
code: (
/* GraphQL */
`
query Foo {
id
}
fragment Bar on Baz {
id
}
`
)
},
{
title: "Correct",
code: (
/* GraphQL */
`
query Foo {
id
}
`
)
}
]
},
messages: {
[RULE_ID]: "{{name}} should be in a separate file."
},
schema
},
create(context) {
var _a;
const ignore = new Set(((_a = context.options[0]) == null ? void 0 : _a.ignore) || []);
const definitions = [];
return {
":matches(OperationDefinition, FragmentDefinition)"(node) {
const type = "operation" in node ? node.operation : "fragment";
if (!ignore.has(type)) {
definitions.push({ type, node });
}
},
"Document:exit"() {
var _a2, _b;
for (const { node, type } of definitions.slice(1)) {
let name = pascalCase(type);
const definitionName = (_a2 = node.name) == null ? void 0 : _a2.value;
if (definitionName) {
name += ` "${definitionName}"`;
}
context.report({
loc: ((_b = node.name) == null ? void 0 : _b.loc) || getLocation(node.loc.start, type),
messageId: RULE_ID,
data: { name }
});
}
}
};
}
};
export {
rule
};