UNPKG

@graphql-eslint/eslint-plugin

Version:
97 lines (96 loc) 4.29 kB
"use strict"; 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 parser_exports = {}; __export(parser_exports, { parseForESLint: () => parseForESLint }); module.exports = __toCommonJS(parser_exports); var import_utils = require("@graphql-tools/utils"), import_debug = __toESM(require("debug")), import_graphql = require("graphql"), import_estree_converter = require("./estree-converter/index.js"), import_graphql_config = require("./graphql-config.js"), import_schema = require("./schema.js"), import_siblings = require("./siblings.js"), import_utils2 = require("./utils.js"), import_documents = require("./documents.js"); const debug = (0, import_debug.default)("graphql-eslint:parser"); debug("cwd %o", import_utils2.CWD); const LEGACY_PARSER_OPTIONS_KEYS = [ "schema", "documents", "extensions", "include", "exclude", "projects", "schemaOptions", "graphQLParserOptions", "skipGraphQLConfig", "operations" ]; function parseForESLint(code, options) { for (const key of LEGACY_PARSER_OPTIONS_KEYS) if (key in options) throw new Error( `\`parserOptions.${key}\` was removed in graphql-eslint@4. Use physical graphql-config for setting schema and documents or \`parserOptions.graphQLConfig\` for programmatic usage.` ); try { const { filePath } = options, { document } = (0, import_utils.parseGraphQLSDL)(filePath, code, { noLocation: !1 }); let project, schema, documents; { const gqlConfig = (0, import_graphql_config.loadGraphQLConfig)(options), realFilepath = filePath.replace(import_utils2.VIRTUAL_DOCUMENT_REGEX, ""); project = gqlConfig.getProjectForFile(realFilepath), documents = (0, import_documents.getDocuments)(project); } try { schema = (0, import_schema.getSchema)(project); } catch (error) { throw error instanceof Error && (error.message = `Error while loading schema: ${error.message}`), error; } const rootTree = (0, import_estree_converter.convertToESTree)(document, schema); return { services: { schema, siblingOperations: (0, import_siblings.getSiblings)(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}`), error instanceof import_graphql.GraphQLError) { const location = error.locations?.[0]; throw { index: error.positions?.[0], ...location && { lineNumber: location.line, column: location.column - 1 }, message: error.message }; } throw error; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { parseForESLint });