UNPKG

@graphql-eslint/eslint-plugin

Version:
82 lines (81 loc) 2.56 kB
import debugFactory from "debug"; import { GraphQLError } from "graphql"; import { parseGraphQLSDL } from "@graphql-tools/utils"; import { getDocuments } from "./documents.js"; import { convertToESTree, extractComments, extractTokens } from "./estree-converter/index.js"; import { getFirstExistingPath, loadGraphQLConfig } from "./graphql-config.js"; import { version } from "./meta.js"; import { getSchema } from "./schema.js"; import { getSiblings } from "./siblings.js"; import { CWD } from "./utils.js"; const debug = debugFactory("graphql-eslint:parser"); debug("cwd %o", 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 } = parseGraphQLSDL(filePath, code, { noLocation: !1 }); let project, schema, documents; project = loadGraphQLConfig(options).getProjectForFile(getFirstExistingPath(filePath)), documents = getDocuments(project); try { schema = getSchema(project); } catch (error) { throw error instanceof Error && (error.message = `Error while loading schema: ${error.message}`), error; } const rootTree = convertToESTree(document, schema); return { services: { schema, siblingOperations: getSiblings(documents) }, ast: { comments: extractComments(document.loc), tokens: 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 GraphQLError) { const location = error.locations?.[0]; throw { index: error.positions?.[0], ...location && { lineNumber: location.line, column: location.column - 1 }, message: error.message }; } throw error; } } const parser = { parseForESLint, meta: { name: "@graphql-eslint/parser", version } }; export { parseForESLint, parser };