@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
110 lines (109 loc) • 3.21 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: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__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: true }), mod);
var description_style_exports = {};
__export(description_style_exports, {
rule: () => rule
});
module.exports = __toCommonJS(description_style_exports);
var import_utils = require("../utils.js");
const schema = {
type: "array",
maxItems: 1,
items: {
type: "object",
additionalProperties: false,
minProperties: 1,
properties: {
style: {
enum: ["block", "inline"],
default: "block"
}
}
}
};
const 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://the-guild.dev/graphql/eslint/rules/description-style",
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 for ${(0, import_utils.getNodeName)(
node.parent
)}`,
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);
}
}
]
});
}
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
rule
});