@graphql-eslint/eslint-plugin
Version:
GraphQL plugin for ESLint
59 lines (58 loc) • 2.45 kB
JavaScript
import { parseGraphQLSDL } from '@graphql-tools/utils';
import { GraphQLError, GraphQLSchema } from 'graphql';
import debugFactory from 'debug';
import { convertToESTree, extractComments, extractTokens } from './estree-converter/index.js';
import { getSchema } from './schema.js';
import { getDocuments } from './documents.js';
import { loadGraphQLConfig } from './graphql-config.js';
import { CWD, VIRTUAL_DOCUMENT_REGEX } from './utils.js';
const debug = debugFactory('graphql-eslint:parser');
debug('cwd %o', CWD);
export 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 } = parseGraphQLSDL(filePath, code, {
...options.graphQLParserOptions,
noLocation: false,
});
const gqlConfig = loadGraphQLConfig(options);
const realFilepath = filePath.replace(VIRTUAL_DOCUMENT_REGEX, '');
const project = gqlConfig.getProjectForFile(realFilepath);
const schema = getSchema(project, options.schemaOptions);
const rootTree = convertToESTree(document, schema instanceof GraphQLSchema ? schema : undefined);
return {
services: {
schema,
siblingOperations: getDocuments(project),
},
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}`;
}
// 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 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;
}
}