@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
315 lines (314 loc) • 10.2 kB
JavaScript
import {
Kind
} from "graphql";
import lowerCase from "lodash.lowercase";
import { ARRAY_DEFAULT_OPTIONS, displayNodeName, truthy } from "../../utils.js";
const RULE_ID = "alphabetize", fieldsEnum = [
Kind.OBJECT_TYPE_DEFINITION,
Kind.INTERFACE_TYPE_DEFINITION,
Kind.INPUT_OBJECT_TYPE_DEFINITION
], selectionsEnum = [
Kind.OPERATION_DEFINITION,
Kind.FRAGMENT_DEFINITION
], argumentsEnum = [
Kind.FIELD_DEFINITION,
Kind.FIELD,
Kind.DIRECTIVE_DEFINITION,
Kind.DIRECTIVE
], schema = {
type: "array",
minItems: 1,
maxItems: 1,
items: {
type: "object",
additionalProperties: !1,
minProperties: 1,
properties: {
fields: {
...ARRAY_DEFAULT_OPTIONS,
items: {
enum: fieldsEnum
},
description: "Fields of `type`, `interface`, and `input`."
},
values: {
type: "boolean",
description: "Values of `enum`."
},
selections: {
...ARRAY_DEFAULT_OPTIONS,
items: {
enum: selectionsEnum
},
description: "Selections of `fragment` and operations `query`, `mutation` and `subscription`."
},
variables: {
type: "boolean",
description: "Variables of operations `query`, `mutation` and `subscription`."
},
arguments: {
...ARRAY_DEFAULT_OPTIONS,
items: {
enum: argumentsEnum
},
description: "Arguments of fields and directives."
},
definitions: {
type: "boolean",
description: "Definitions \u2013 `type`, `interface`, `enum`, `scalar`, `input`, `union` and `directive`."
},
groups: {
...ARRAY_DEFAULT_OPTIONS,
minItems: 2,
description: [
"Order group. Example: `['...', 'id', '*', '{']` where:",
"- `...` stands for fragment spreads",
"- `id` stands for field with name `id`",
"- `*` stands for everything else",
"- `{` stands for fields `selection set`"
].join(`
`)
}
}
}
}, rule = {
meta: {
type: "suggestion",
fixable: "code",
docs: {
category: ["Schema", "Operations"],
description: "Enforce arrange in alphabetical order for type fields, enum values, input object fields, operation selections and more.",
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
examples: [
{
title: "Incorrect",
usage: [{ fields: [Kind.OBJECT_TYPE_DEFINITION] }],
code: (
/* GraphQL */
`
type User {
password: String
firstName: String! # should be before "password"
age: Int # should be before "firstName"
lastName: String!
}
`
)
},
{
title: "Correct",
usage: [{ fields: [Kind.OBJECT_TYPE_DEFINITION] }],
code: (
/* GraphQL */
`
type User {
age: Int
firstName: String!
lastName: String!
password: String
}
`
)
},
{
title: "Incorrect",
usage: [{ values: !0 }],
code: (
/* GraphQL */
`
enum Role {
SUPER_ADMIN
ADMIN # should be before "SUPER_ADMIN"
USER
GOD # should be before "USER"
}
`
)
},
{
title: "Correct",
usage: [{ values: !0 }],
code: (
/* GraphQL */
`
enum Role {
ADMIN
GOD
SUPER_ADMIN
USER
}
`
)
},
{
title: "Incorrect",
usage: [{ selections: [Kind.OPERATION_DEFINITION] }],
code: (
/* GraphQL */
`
query {
me {
firstName
lastName
email # should be before "lastName"
}
}
`
)
},
{
title: "Correct",
usage: [{ selections: [Kind.OPERATION_DEFINITION] }],
code: (
/* GraphQL */
`
query {
me {
email
firstName
lastName
}
}
`
)
}
],
configOptions: {
schema: [
{
definitions: !0,
fields: fieldsEnum,
values: !0,
arguments: argumentsEnum,
groups: ["id", "*", "createdAt", "updatedAt"]
}
],
operations: [
{
definitions: !0,
selections: selectionsEnum,
variables: !0,
arguments: [Kind.FIELD, Kind.DIRECTIVE],
groups: ["...", "id", "*", "{"]
}
]
}
},
messages: {
[RULE_ID]: "{{ currNode }} should be before {{ prevNode }}"
},
schema
},
create(context) {
const sourceCode = context.getSourceCode();
function isNodeAndCommentOnSameLine(node, comment) {
return node.loc.end.line === comment.loc.start.line;
}
function getBeforeComments(node) {
const commentsBefore = sourceCode.getCommentsBefore(node);
if (commentsBefore.length === 0)
return [];
const tokenBefore = sourceCode.getTokenBefore(node);
if (tokenBefore)
return commentsBefore.filter((comment) => !isNodeAndCommentOnSameLine(tokenBefore, comment));
const filteredComments = [], nodeLine = node.loc.start.line;
for (let i = commentsBefore.length - 1; i >= 0; i -= 1) {
const comment = commentsBefore[i];
if (nodeLine - comment.loc.start.line - filteredComments.length > 1)
break;
filteredComments.unshift(comment);
}
return filteredComments;
}
function getRangeWithComments(node) {
node.kind === Kind.VARIABLE && (node = node.parent);
const [firstBeforeComment] = getBeforeComments(node), [firstAfterComment] = sourceCode.getCommentsAfter(node), from = firstBeforeComment || node, to = firstAfterComment && isNodeAndCommentOnSameLine(node, firstAfterComment) ? firstAfterComment : node;
return [from.range[0], to.range[1]];
}
function checkNodes(nodes = []) {
for (let i = 1; i < nodes.length; i += 1) {
const currNode = nodes[i], currName = getName(currNode);
if (!currName)
continue;
const prevNode = nodes[i - 1], prevName = getName(prevNode);
if (prevName) {
const compareResult = prevName.localeCompare(currName), { groups } = opts;
let shouldSortByGroup = !1;
if (groups?.length) {
if (!groups.includes("*"))
throw new Error("`groups` option should contain `*` string.");
const indexForPrev = getIndex({ node: prevNode, groups }), indexForCurr = getIndex({ node: currNode, groups });
if (shouldSortByGroup = indexForPrev - indexForCurr > 0, indexForPrev < indexForCurr)
continue;
}
if (!shouldSortByGroup && !(compareResult === 1) && (!(compareResult === 0) || !prevNode.kind.endsWith("Extension") || currNode.kind.endsWith("Extension")))
continue;
}
context.report({
// @ts-expect-error can't be undefined
node: "alias" in currNode && currNode.alias || currNode.name,
messageId: RULE_ID,
data: {
currNode: displayNodeName(currNode),
prevNode: prevName ? displayNodeName(prevNode) : lowerCase(prevNode.kind)
},
*fix(fixer) {
const prevRange = getRangeWithComments(prevNode), currRange = getRangeWithComments(currNode);
yield fixer.replaceTextRange(
prevRange,
sourceCode.getText({ range: currRange })
), yield fixer.replaceTextRange(
currRange,
sourceCode.getText({ range: prevRange })
);
}
});
}
}
const opts = context.options[0], fields = new Set(opts.fields ?? []), listeners = {}, fieldsSelector = [
fields.has(Kind.OBJECT_TYPE_DEFINITION) && [
Kind.OBJECT_TYPE_DEFINITION,
Kind.OBJECT_TYPE_EXTENSION
],
fields.has(Kind.INTERFACE_TYPE_DEFINITION) && [
Kind.INTERFACE_TYPE_DEFINITION,
Kind.INTERFACE_TYPE_EXTENSION
],
fields.has(Kind.INPUT_OBJECT_TYPE_DEFINITION) && [
Kind.INPUT_OBJECT_TYPE_DEFINITION,
Kind.INPUT_OBJECT_TYPE_EXTENSION
]
].filter(truthy).flat().join(","), selectionsSelector = opts.selections?.join(","), argumentsSelector = opts.arguments?.join(",");
if (fieldsSelector && (listeners[fieldsSelector] = (node) => {
checkNodes(node.fields);
}), opts.values) {
const enumValuesSelector = [Kind.ENUM_TYPE_DEFINITION, Kind.ENUM_TYPE_EXTENSION].join(",");
listeners[enumValuesSelector] = (node) => {
checkNodes(node.values);
};
}
return selectionsSelector && (listeners[`:matches(${selectionsSelector}) SelectionSet`] = (node) => {
checkNodes(node.selections);
}), opts.variables && (listeners.OperationDefinition = (node) => {
checkNodes(node.variableDefinitions?.map((varDef) => varDef.variable));
}), argumentsSelector && (listeners[argumentsSelector] = (node) => {
checkNodes(node.arguments);
}), opts.definitions && (listeners.Document = (node) => {
checkNodes(node.definitions);
}), listeners;
}
};
function getIndex({
node,
groups
}) {
let index = groups.indexOf(getName(node));
return index === -1 && "selectionSet" in node && node.selectionSet && (index = groups.indexOf("{")), index === -1 && node.kind === Kind.FRAGMENT_SPREAD && (index = groups.indexOf("...")), index === -1 && (index = groups.indexOf("*")), index;
}
function getName(node) {
return "alias" in node && node.alias?.value || //
"name" in node && node.name?.value || "";
}
export {
rule
};