@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
137 lines (136 loc) • 3.95 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var no_duplicate_fields_exports = {};
__export(no_duplicate_fields_exports, {
rule: () => rule
});
module.exports = __toCommonJS(no_duplicate_fields_exports);
var import_graphql = require("graphql");
const RULE_ID = "no-duplicate-fields", rule = {
meta: {
type: "suggestion",
hasSuggestions: !0,
docs: {
description: "Checks for duplicate fields in selection set, variables in operation definition, or in arguments set of a field.",
category: "Operations",
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
recommended: !0,
examples: [
{
title: "Incorrect",
code: (
/* GraphQL */
`
query {
user {
name
email
name # duplicate field
}
}
`
)
},
{
title: "Incorrect",
code: (
/* GraphQL */
`
query {
users(
first: 100
skip: 50
after: "cji629tngfgou0b73kt7vi5jo"
first: 100 # duplicate argument
) {
id
}
}
`
)
},
{
title: "Incorrect",
code: (
/* GraphQL */
`
query (
$first: Int!
$first: Int! # duplicate variable
) {
users(first: $first, skip: 50) {
id
}
}
`
)
}
]
},
messages: {
[RULE_ID]: "{{ type }} `{{ fieldName }}` defined multiple times."
},
schema: []
},
create(context) {
function checkNode(usedFields, node) {
const fieldName = node.value;
if (usedFields.has(fieldName)) {
const { parent } = node;
context.report({
node,
messageId: RULE_ID,
data: {
type: parent.type,
fieldName
},
suggest: [
{
desc: `Remove \`${fieldName}\` ${parent.type.toLowerCase()}`,
fix(fixer) {
return fixer.remove(
parent.type === import_graphql.Kind.VARIABLE ? parent.parent : parent
);
}
}
]
});
} else
usedFields.add(fieldName);
}
return {
OperationDefinition(node) {
const set = /* @__PURE__ */ new Set();
for (const varDef of node.variableDefinitions || [])
checkNode(set, varDef.variable.name);
},
Field(node) {
const set = /* @__PURE__ */ new Set();
for (const arg of node.arguments || [])
checkNode(set, arg.name);
},
SelectionSet(node) {
const set = /* @__PURE__ */ new Set();
for (const selection of node.selections)
selection.kind === import_graphql.Kind.FIELD && checkNode(set, selection.alias || selection.name);
}
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
rule
});