@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
180 lines (178 loc) • 5.77 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 require_description_exports = {};
__export(require_description_exports, {
RULE_ID: () => RULE_ID,
rule: () => rule
});
module.exports = __toCommonJS(require_description_exports);
var import_utils = require("@graphql-tools/utils"), import_graphql = require("graphql"), import_utils2 = require("../utils.js");
const RULE_ID = "require-description", ALLOWED_KINDS = [
...import_utils2.TYPES_KINDS,
import_graphql.Kind.DIRECTIVE_DEFINITION,
import_graphql.Kind.FIELD_DEFINITION,
import_graphql.Kind.INPUT_VALUE_DEFINITION,
import_graphql.Kind.ENUM_VALUE_DEFINITION,
import_graphql.Kind.OPERATION_DEFINITION
], schema = {
type: "array",
minItems: 1,
maxItems: 1,
items: {
type: "object",
additionalProperties: !1,
minProperties: 1,
properties: {
types: {
type: "boolean",
description: `Includes:
${import_utils2.TYPES_KINDS.map((kind) => `- \`${kind}\``).join(`
`)}`
},
rootField: {
type: "boolean",
description: "Definitions within `Query`, `Mutation`, and `Subscription` root types."
},
...Object.fromEntries(
[...ALLOWED_KINDS].sort().map((kind) => {
let description = `Read more about this kind on [spec.graphql.org](https://spec.graphql.org/October2021/#${kind}).`;
return kind === import_graphql.Kind.OPERATION_DEFINITION && (description += '\n> You must use only comment syntax `#` and not description syntax `"""` or `"`.'), [kind, { type: "boolean", description }];
})
)
}
}
}, rule = {
meta: {
docs: {
category: "Schema",
description: "Enforce descriptions in type definitions and operations.",
url: `https://the-guild.dev/graphql/eslint/rules/${RULE_ID}`,
examples: [
{
title: "Incorrect",
usage: [{ types: !0, FieldDefinition: !0 }],
code: (
/* GraphQL */
`
type someTypeName {
name: String
}
`
)
},
{
title: "Correct",
usage: [{ types: !0, FieldDefinition: !0 }],
code: (
/* GraphQL */
`
"""
Some type description
"""
type someTypeName {
"""
Name description
"""
name: String
}
`
)
},
{
title: "Correct",
usage: [{ OperationDefinition: !0 }],
code: (
/* GraphQL */
`
# Create a new user
mutation createUser {
# ...
}
`
)
},
{
title: "Correct",
usage: [{ rootField: !0 }],
code: (
/* GraphQL */
`
type Mutation {
"Create a new user"
createUser: User
}
type User {
name: String
}
`
)
}
],
configOptions: [
{
types: !0,
[import_graphql.Kind.DIRECTIVE_DEFINITION]: !0,
rootField: !0
}
],
recommended: !0
},
type: "suggestion",
messages: {
[RULE_ID]: "Description is required for {{ nodeName }}"
},
schema
},
create(context) {
const { types, rootField, ...restOptions } = context.options[0] || {}, kinds = new Set(types ? import_utils2.TYPES_KINDS : []);
for (const [kind, isEnabled] of Object.entries(restOptions))
isEnabled ? kinds.add(kind) : kinds.delete(kind);
if (rootField) {
const schema2 = (0, import_utils2.requireGraphQLSchemaFromContext)(RULE_ID, context), rootTypeNames = (0, import_utils.getRootTypeNames)(schema2);
kinds.add(
`:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/^(${[
...rootTypeNames
].join(",")})$/] > FieldDefinition`
);
}
return {
[[...kinds].join(",")](node) {
let description = "";
const isOperation = node.kind === import_graphql.Kind.OPERATION_DEFINITION;
if (isOperation) {
const rawNode = node.rawNode(), { prev, line } = rawNode.loc.startToken;
if (prev?.kind === import_graphql.TokenKind.COMMENT) {
const value = prev.value.trim(), linesBefore = line - prev.line;
!value.startsWith("eslint") && linesBefore === 1 && (description = value);
}
} else
description = node.description?.value.trim() || "";
description.length === 0 && context.report({
loc: isOperation ? (0, import_utils2.getLocation)(node.loc.start, node.operation) : node.name.loc,
messageId: RULE_ID,
data: {
nodeName: (0, import_utils2.getNodeName)(node)
}
});
}
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
RULE_ID,
rule
});