@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
181 lines (161 loc) • 8 kB
JavaScript
Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _graphql = require('graphql');
var _lodashlowercase = require('lodash.lowercase'); var _lodashlowercase2 = _interopRequireDefault(_lodashlowercase);
function requireGraphQLOperations(ruleId, context) {
const { siblingOperations } = context.sourceCode.parserServices;
if (!siblingOperations.available) {
throw new Error(
`Rule \`${ruleId}\` requires graphql-config \`documents\` field to be set and loaded. See https://the-guild.dev/graphql/eslint/docs/usage#providing-operations for more info`
);
}
return siblingOperations;
}
function requireGraphQLSchema(ruleId, context) {
const { schema } = context.sourceCode.parserServices;
if (!schema) {
throw new Error(
`Rule \`${ruleId}\` requires graphql-config \`schema\` field to be set and loaded. See https://the-guild.dev/graphql/eslint/docs/usage#providing-schema for more info`
);
}
return schema;
}
const chalk = {
red: (str) => `\x1B[31m${str}\x1B[39m`,
yellow: (str) => `\x1B[33m${str}\x1B[39m`
};
const logger = {
error: (...args) => (
// eslint-disable-next-line no-console
console.error(chalk.red("error"), "[graphql-eslint]", ...args)
),
warn: (...args) => (
// eslint-disable-next-line no-console
console.warn(chalk.yellow("warning"), "[graphql-eslint]", ...args)
)
};
const slash = (path) => path.replaceAll("\\", "/");
const VIRTUAL_DOCUMENT_REGEX = /[/\\]\d+_document.graphql$/;
const CWD = process.cwd();
const getTypeName = (node) => "type" in node ? getTypeName(node.type) : "name" in node && node.name ? node.name.value : "";
const TYPES_KINDS = [
_graphql.Kind.OBJECT_TYPE_DEFINITION,
_graphql.Kind.INTERFACE_TYPE_DEFINITION,
_graphql.Kind.ENUM_TYPE_DEFINITION,
_graphql.Kind.SCALAR_TYPE_DEFINITION,
_graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION,
_graphql.Kind.UNION_TYPE_DEFINITION
];
const pascalCase = (str) => _lodashlowercase2.default.call(void 0, str).split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
const camelCase = (str) => {
const result = pascalCase(str);
return result.charAt(0).toLowerCase() + result.slice(1);
};
const convertCase = (style, str) => {
switch (style) {
case "camelCase":
return camelCase(str);
case "PascalCase":
return pascalCase(str);
case "snake_case":
return _lodashlowercase2.default.call(void 0, str).replace(/ /g, "_");
case "UPPER_CASE":
return _lodashlowercase2.default.call(void 0, str).replace(/ /g, "_").toUpperCase();
case "kebab-case":
return _lodashlowercase2.default.call(void 0, str).replace(/ /g, "-");
}
};
function getLocation(start, fieldName = "") {
const { line, column } = start;
return {
start: {
line,
column
},
end: {
line,
column: column + fieldName.length
}
};
}
const REPORT_ON_FIRST_CHARACTER = { column: 0, line: 1 };
const ARRAY_DEFAULT_OPTIONS = {
type: "array",
uniqueItems: true,
minItems: 1,
items: {
type: "string"
}
};
const englishJoinWords = (words) => new Intl.ListFormat("en-US", { type: "disjunction" }).format(words);
const DisplayNodeNameMap = {
[_graphql.Kind.ARGUMENT]: "argument",
[_graphql.Kind.BOOLEAN]: "boolean",
[_graphql.Kind.DIRECTIVE_DEFINITION]: "directive",
[_graphql.Kind.DIRECTIVE]: "directive",
[_graphql.Kind.DOCUMENT]: "document",
[_graphql.Kind.ENUM_TYPE_DEFINITION]: "enum",
[_graphql.Kind.ENUM_TYPE_EXTENSION]: "enum",
[_graphql.Kind.ENUM_VALUE_DEFINITION]: "enum value",
[_graphql.Kind.ENUM]: "enum",
[_graphql.Kind.FIELD_DEFINITION]: "field",
[_graphql.Kind.FIELD]: "field",
[_graphql.Kind.FLOAT]: "float",
[_graphql.Kind.FRAGMENT_DEFINITION]: "fragment",
[_graphql.Kind.FRAGMENT_SPREAD]: "fragment spread",
[_graphql.Kind.INLINE_FRAGMENT]: "inline fragment",
[_graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION]: "input",
[_graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION]: "input",
[_graphql.Kind.INPUT_VALUE_DEFINITION]: "input value",
[_graphql.Kind.INT]: "int",
[_graphql.Kind.INTERFACE_TYPE_DEFINITION]: "interface",
[_graphql.Kind.INTERFACE_TYPE_EXTENSION]: "interface",
[_graphql.Kind.LIST_TYPE]: "list type",
[_graphql.Kind.LIST]: "list",
[_graphql.Kind.NAME]: "name",
[_graphql.Kind.NAMED_TYPE]: "named type",
[_graphql.Kind.NON_NULL_TYPE]: "non-null type",
[_graphql.Kind.NULL]: "null",
[_graphql.Kind.OBJECT_FIELD]: "object field",
[_graphql.Kind.OBJECT_TYPE_DEFINITION]: "type",
[_graphql.Kind.OBJECT_TYPE_EXTENSION]: "type",
[_graphql.Kind.OBJECT]: "object",
[_graphql.Kind.OPERATION_DEFINITION]: "operation",
[_graphql.Kind.OPERATION_TYPE_DEFINITION]: "operation type",
[_graphql.Kind.SCALAR_TYPE_DEFINITION]: "scalar",
[_graphql.Kind.SCALAR_TYPE_EXTENSION]: "scalar",
[_graphql.Kind.SCHEMA_DEFINITION]: "schema",
[_graphql.Kind.SCHEMA_EXTENSION]: "schema",
[_graphql.Kind.SELECTION_SET]: "selection set",
[_graphql.Kind.STRING]: "string",
[_graphql.Kind.UNION_TYPE_DEFINITION]: "union",
[_graphql.Kind.UNION_TYPE_EXTENSION]: "union",
[_graphql.Kind.VARIABLE_DEFINITION]: "variable",
[_graphql.Kind.VARIABLE]: "variable"
};
function displayNodeName(node) {
return `${node.kind === _graphql.Kind.OPERATION_DEFINITION ? node.operation : DisplayNodeNameMap[node.kind]} "${"alias" in node && _optionalChain([node, 'access', _ => _.alias, 'optionalAccess', _2 => _2.value]) || "name" in node && _optionalChain([node, 'access', _3 => _3.name, 'optionalAccess', _4 => _4.value]) || node.value}"`;
}
function getNodeName(node) {
switch (node.kind) {
case _graphql.Kind.OBJECT_TYPE_DEFINITION:
case _graphql.Kind.OBJECT_TYPE_EXTENSION:
case _graphql.Kind.INTERFACE_TYPE_DEFINITION:
case _graphql.Kind.ENUM_TYPE_DEFINITION:
case _graphql.Kind.SCALAR_TYPE_DEFINITION:
case _graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION:
case _graphql.Kind.UNION_TYPE_DEFINITION:
case _graphql.Kind.DIRECTIVE_DEFINITION:
return displayNodeName(node);
case _graphql.Kind.FIELD_DEFINITION:
case _graphql.Kind.INPUT_VALUE_DEFINITION:
case _graphql.Kind.ENUM_VALUE_DEFINITION:
return `${displayNodeName(node)} in ${displayNodeName(node.parent)}`;
case _graphql.Kind.OPERATION_DEFINITION:
return node.name ? displayNodeName(node) : node.operation;
}
return "";
}
const eslintSelectorsTip = `> [!TIP]
>
> These fields are defined by ESLint [\`selectors\`](https://eslint.org/docs/developer-guide/selectors).
> Paste or drop code into the editor in [ASTExplorer](https://astexplorer.net) and inspect the generated AST to compose your selector.`;
exports.ARRAY_DEFAULT_OPTIONS = ARRAY_DEFAULT_OPTIONS; exports.CWD = CWD; exports.REPORT_ON_FIRST_CHARACTER = REPORT_ON_FIRST_CHARACTER; exports.TYPES_KINDS = TYPES_KINDS; exports.VIRTUAL_DOCUMENT_REGEX = VIRTUAL_DOCUMENT_REGEX; exports.camelCase = camelCase; exports.convertCase = convertCase; exports.displayNodeName = displayNodeName; exports.englishJoinWords = englishJoinWords; exports.eslintSelectorsTip = eslintSelectorsTip; exports.getLocation = getLocation; exports.getNodeName = getNodeName; exports.getTypeName = getTypeName; exports.logger = logger; exports.pascalCase = pascalCase; exports.requireGraphQLOperations = requireGraphQLOperations; exports.requireGraphQLSchema = requireGraphQLSchema; exports.slash = slash;
;