@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
79 lines (78 loc) • 2.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const schema = {
type: 'array',
maxItems: 1,
items: {
type: 'object',
additionalProperties: false,
minProperties: 1,
properties: {
style: {
enum: ['block', 'inline'],
default: 'block',
},
},
},
};
exports.rule = {
meta: {
type: 'suggestion',
hasSuggestions: true,
docs: {
examples: [
{
title: 'Incorrect',
usage: [{ style: 'inline' }],
code: /* GraphQL */ `
""" Description """
type someTypeName {
# ...
}
`,
},
{
title: 'Correct',
usage: [{ style: 'inline' }],
code: /* GraphQL */ `
" Description "
type someTypeName {
# ...
}
`,
},
],
description: 'Require all comments to follow the same style (either block or inline).',
category: 'Schema',
url: 'https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/description-style.md',
recommended: true,
},
schema,
},
create(context) {
const { style = 'block' } = context.options[0] || {};
const isBlock = style === 'block';
return {
[`.description[type=StringValue][block!=${isBlock}]`](node) {
context.report({
loc: isBlock ? node.loc : node.loc.start,
message: `Unexpected ${isBlock ? 'inline' : 'block'} description.`,
suggest: [
{
desc: `Change to ${isBlock ? 'block' : 'inline'} style description`,
fix(fixer) {
const sourceCode = context.getSourceCode();
const originalText = sourceCode.getText(node);
const newText = isBlock
? originalText.replace(/(^")|("$)/g, '"""')
: originalText.replace(/(^""")|("""$)/g, '"').replace(/\s+/g, ' ');
return fixer.replaceText(node, newText);
},
},
],
});
},
};
},
};