@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
104 lines (103 loc) • 4.16 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __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: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var parser_exports = {};
__export(parser_exports, {
parseForESLint: () => parseForESLint
});
module.exports = __toCommonJS(parser_exports);
var import_utils = require("@graphql-tools/utils");
var import_debug = __toESM(require("debug"));
var import_graphql = require("graphql");
var import_estree_converter = require("./estree-converter/index.js");
var import_graphql_config = require("./graphql-config.js");
var import_schema = require("./schema.js");
var import_siblings = require("./siblings.js");
var import_utils2 = require("./utils.js");
const debug = (0, import_debug.default)("graphql-eslint:parser");
debug("cwd %o", import_utils2.CWD);
function parseForESLint(code, options) {
var _a, _b;
try {
const { filePath } = options;
options.documents || (options.documents = options.operations);
const { document } = (0, import_utils.parseGraphQLSDL)(filePath, code, {
...options.graphQLParserOptions,
noLocation: false
});
const gqlConfig = (0, import_graphql_config.loadGraphQLConfig)(options);
const realFilepath = filePath.replace(import_utils2.VIRTUAL_DOCUMENT_REGEX, "");
const project = gqlConfig.getProjectForFile(realFilepath);
let schema = null;
try {
schema = project ? (0, import_schema.getSchema)(project, options.schemaOptions) : typeof options.schema === "string" ? (0, import_graphql.buildSchema)(options.schema) : null;
} catch (error) {
if (error instanceof Error) {
error.message = `Error while loading schema: ${error.message}`;
}
throw error;
}
const rootTree = (0, import_estree_converter.convertToESTree)(document, schema);
return {
services: {
schema,
siblingOperations: (0, import_siblings.getSiblings)(project, options.documents)
},
ast: {
comments: (0, import_estree_converter.extractComments)(document.loc),
tokens: (0, import_estree_converter.extractTokens)(filePath, code),
loc: rootTree.loc,
range: rootTree.range,
type: "Program",
sourceType: "script",
body: [rootTree]
}
};
} catch (error) {
if (error instanceof Error) {
error.message = `[graphql-eslint] ${error.message}`;
}
if (error instanceof import_graphql.GraphQLError) {
const location = (_a = error.locations) == null ? void 0 : _a[0];
const eslintError = {
index: (_b = error.positions) == null ? void 0 : _b[0],
...location && {
lineNumber: location.line,
column: location.column - 1
},
message: error.message
};
throw eslintError;
}
throw error;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
parseForESLint
});