UNPKG

@graphql-eslint/eslint-plugin

Version:
62 lines (61 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseForESLint = void 0; const tslib_1 = require("tslib"); const utils_1 = require("@graphql-tools/utils"); const graphql_1 = require("graphql"); const debug_1 = tslib_1.__importDefault(require("debug")); const index_js_1 = require("./estree-converter/index.js"); const schema_js_1 = require("./schema.js"); const documents_js_1 = require("./documents.js"); const graphql_config_js_1 = require("./graphql-config.js"); const utils_js_1 = require("./utils.js"); const debug = (0, debug_1.default)('graphql-eslint:parser'); debug('cwd %o', utils_js_1.CWD); function parseForESLint(code, options) { try { const { filePath } = options; // First parse code from file, in case of syntax error do not try load schema, // documents or even graphql-config instance const { document } = (0, utils_1.parseGraphQLSDL)(filePath, code, { ...options.graphQLParserOptions, noLocation: false, }); const gqlConfig = (0, graphql_config_js_1.loadGraphQLConfig)(options); const realFilepath = filePath.replace(utils_js_1.VIRTUAL_DOCUMENT_REGEX, ''); const project = gqlConfig.getProjectForFile(realFilepath); const schema = (0, schema_js_1.getSchema)(project, options.schemaOptions); const rootTree = (0, index_js_1.convertToESTree)(document, schema instanceof graphql_1.GraphQLSchema ? schema : null); return { services: { schema, siblingOperations: (0, documents_js_1.getDocuments)(project), }, ast: { comments: (0, index_js_1.extractComments)(document.loc), tokens: (0, index_js_1.extractTokens)(filePath, code), loc: rootTree.loc, range: rootTree.range, type: 'Program', sourceType: 'script', body: [rootTree], }, }; } catch (error) { error.message = `[graphql-eslint] ${error.message}`; // In case of GraphQL parser error, we report it to ESLint as a parser error that matches the requirements // of ESLint. This will make sure to display it correctly in IDEs and lint results. if (error instanceof graphql_1.GraphQLError) { const eslintError = { index: error.positions[0], lineNumber: error.locations[0].line, column: error.locations[0].column - 1, message: error.message, }; throw eslintError; } throw error; } } exports.parseForESLint = parseForESLint;