@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
189 lines (188 loc) • 7.86 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
mod
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var utils_exports = {};
__export(utils_exports, {
ARRAY_DEFAULT_OPTIONS: () => ARRAY_DEFAULT_OPTIONS,
CWD: () => CWD,
REPORT_ON_FIRST_CHARACTER: () => REPORT_ON_FIRST_CHARACTER,
TYPES_KINDS: () => TYPES_KINDS,
VIRTUAL_DOCUMENT_REGEX: () => VIRTUAL_DOCUMENT_REGEX,
camelCase: () => camelCase,
convertCase: () => convertCase,
displayNodeName: () => displayNodeName,
englishJoinWords: () => englishJoinWords,
getLocation: () => getLocation,
getNodeName: () => getNodeName,
getTypeName: () => getTypeName,
logger: () => logger,
normalizePath: () => normalizePath,
pascalCase: () => pascalCase,
requireGraphQLSchemaFromContext: () => requireGraphQLSchemaFromContext,
requireSiblingsOperations: () => requireSiblingsOperations,
truthy: () => truthy
});
module.exports = __toCommonJS(utils_exports);
var import_graphql = require("graphql"), import_lodash = __toESM(require("lodash.lowercase"));
function requireSiblingsOperations(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/config/docs/user/documents for more info`
);
return siblingOperations;
}
function requireGraphQLSchemaFromContext(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/config/docs/user/schema for more info`
);
return schema;
}
const chalk = {
red: (str) => `\x1B[31m${str}\x1B[39m`,
yellow: (str) => `\x1B[33m${str}\x1B[39m`
}, 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)
)
}, normalizePath = (path) => (path || "").replace(/\\/g, "/"), VIRTUAL_DOCUMENT_REGEX = /\/\d+_document.graphql$/, CWD = process.cwd(), getTypeName = (node) => "type" in node ? getTypeName(node.type) : "name" in node && node.name ? node.name.value : "", TYPES_KINDS = [
import_graphql.Kind.OBJECT_TYPE_DEFINITION,
import_graphql.Kind.INTERFACE_TYPE_DEFINITION,
import_graphql.Kind.ENUM_TYPE_DEFINITION,
import_graphql.Kind.SCALAR_TYPE_DEFINITION,
import_graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION,
import_graphql.Kind.UNION_TYPE_DEFINITION
], pascalCase = (str) => (0, import_lodash.default)(str).split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(""), camelCase = (str) => {
const result = pascalCase(str);
return result.charAt(0).toLowerCase() + result.slice(1);
}, convertCase = (style, str) => {
switch (style) {
case "camelCase":
return camelCase(str);
case "PascalCase":
return pascalCase(str);
case "snake_case":
return (0, import_lodash.default)(str).replace(/ /g, "_");
case "UPPER_CASE":
return (0, import_lodash.default)(str).replace(/ /g, "_").toUpperCase();
case "kebab-case":
return (0, import_lodash.default)(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 }, ARRAY_DEFAULT_OPTIONS = {
type: "array",
uniqueItems: !0,
minItems: 1,
items: {
type: "string"
}
}, englishJoinWords = (words) => new Intl.ListFormat("en-US", { type: "disjunction" }).format(words);
function truthy(value) {
return !!value;
}
const DisplayNodeNameMap = {
[import_graphql.Kind.OBJECT_TYPE_DEFINITION]: "type",
[import_graphql.Kind.OBJECT_TYPE_EXTENSION]: "type",
[import_graphql.Kind.INTERFACE_TYPE_DEFINITION]: "interface",
[import_graphql.Kind.INTERFACE_TYPE_EXTENSION]: "interface",
[import_graphql.Kind.ENUM_TYPE_DEFINITION]: "enum",
[import_graphql.Kind.ENUM_TYPE_EXTENSION]: "enum",
[import_graphql.Kind.SCALAR_TYPE_DEFINITION]: "scalar",
[import_graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION]: "input",
[import_graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION]: "input",
[import_graphql.Kind.UNION_TYPE_DEFINITION]: "union",
[import_graphql.Kind.UNION_TYPE_EXTENSION]: "union",
[import_graphql.Kind.DIRECTIVE_DEFINITION]: "directive",
[import_graphql.Kind.FIELD_DEFINITION]: "field",
[import_graphql.Kind.ENUM_VALUE_DEFINITION]: "enum value",
[import_graphql.Kind.INPUT_VALUE_DEFINITION]: "input value",
[import_graphql.Kind.ARGUMENT]: "argument",
[import_graphql.Kind.VARIABLE]: "variable",
[import_graphql.Kind.FRAGMENT_DEFINITION]: "fragment",
[import_graphql.Kind.OPERATION_DEFINITION]: "operation",
[import_graphql.Kind.FIELD]: "field"
};
function displayNodeName(node) {
return `${node.kind === import_graphql.Kind.OPERATION_DEFINITION ? node.operation : DisplayNodeNameMap[node.kind]} "${"alias" in node && node.alias?.value || "name" in node && node.name?.value}"`;
}
function getNodeName(node) {
switch (node.kind) {
case import_graphql.Kind.OBJECT_TYPE_DEFINITION:
case import_graphql.Kind.OBJECT_TYPE_EXTENSION:
case import_graphql.Kind.INTERFACE_TYPE_DEFINITION:
case import_graphql.Kind.ENUM_TYPE_DEFINITION:
case import_graphql.Kind.SCALAR_TYPE_DEFINITION:
case import_graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION:
case import_graphql.Kind.UNION_TYPE_DEFINITION:
case import_graphql.Kind.DIRECTIVE_DEFINITION:
return displayNodeName(node);
case import_graphql.Kind.FIELD_DEFINITION:
case import_graphql.Kind.INPUT_VALUE_DEFINITION:
case import_graphql.Kind.ENUM_VALUE_DEFINITION:
return `${displayNodeName(node)} in ${displayNodeName(node.parent)}`;
case import_graphql.Kind.OPERATION_DEFINITION:
return node.name ? displayNodeName(node) : node.operation;
}
return "";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ARRAY_DEFAULT_OPTIONS,
CWD,
REPORT_ON_FIRST_CHARACTER,
TYPES_KINDS,
VIRTUAL_DOCUMENT_REGEX,
camelCase,
convertCase,
displayNodeName,
englishJoinWords,
getLocation,
getNodeName,
getTypeName,
logger,
normalizePath,
pascalCase,
requireGraphQLSchemaFromContext,
requireSiblingsOperations,
truthy
});