@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
69 lines (66 loc) • 2.36 kB
JavaScript
Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _indexjs = require('../../estree-converter/index.js');
var _utilsjs = require('../../utils.js');
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) {
const reasonArgument = _optionalChain([node, 'access', _ => _.arguments, 'optionalAccess', _2 => _2.find, 'call', _3 => _3(
(arg) => arg.name.value === "reason"
)]);
const value = reasonArgument && String(_indexjs.valueFromNode.call(void 0, reasonArgument.value)).trim();
if (!value) {
context.report({
node: node.name,
message: `Deprecation reason is required for ${_utilsjs.getNodeName.call(void 0, node.parent)}.`
});
}
}
};
}
};
exports.rule = rule;
;