@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
55 lines (54 loc) • 1.78 kB
JavaScript
import { valueFromNode } from '../estree-converter/index.js';
export const rule = {
meta: {
docs: {
description: 'Require all deprecation directives to specify a reason.',
category: 'Schema',
url: 'https://the-guild.dev/graphql/eslint/rules/require-deprecation-reason',
recommended: true,
examples: [
{
title: 'Incorrect',
code: /* GraphQL */ `
type MyType {
name: String @deprecated
}
`,
},
{
title: 'Incorrect',
code: /* GraphQL */ `
type MyType {
name: String @deprecated(reason: "")
}
`,
},
{
title: 'Correct',
code: /* GraphQL */ `
type MyType {
name: String @deprecated(reason: "no longer relevant, please use fullName field")
}
`,
},
],
},
type: 'suggestion',
schema: [],
},
create(context) {
return {
'Directive[name.value=deprecated]'(node) {
var _a;
const reasonArgument = (_a = node.arguments) === null || _a === void 0 ? void 0 : _a.find(arg => arg.name.value === 'reason');
const value = reasonArgument && String(valueFromNode(reasonArgument.value)).trim();
if (!value) {
context.report({
node: node.name,
message: 'Directive "@deprecated" must have a reason!',
});
}
},
};
},
};