UNPKG

graphql-language-service-server

Version:
52 lines 2.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.astroParser = void 0; const graphql_language_service_1 = require("graphql-language-service"); const babel_1 = require("./babel"); const compiler_1 = require("@astrojs/compiler"); async function parseAstro(source) { var _a, _b; const { ast, diagnostics } = await (0, compiler_1.parse)(source, { position: false, }); if (diagnostics.some(d => d.severity === 1)) { return { type: 'error', errors: diagnostics.map(d => JSON.stringify(d)), }; } for (const node of ast.children) { if (node.type === 'frontmatter') { try { return { type: 'ok', scriptOffset: ((_b = (_a = node.position) === null || _a === void 0 ? void 0 : _a.start.line) !== null && _b !== void 0 ? _b : 1) - 1, scriptAst: [(0, babel_1.babelParser)(node.value, ['typescript'])], }; } catch (error) { return { type: 'error', errors: [String(error)], }; } } } return { type: 'error', errors: ['Could not find frontmatter block'] }; } const astroParser = async (text, uri, logger) => { const parseAstroResult = await parseAstro(text); if (parseAstroResult.type === 'error') { logger.info(`Could not parse the astro file at ${uri} to extract the graphql tags:`); for (const error of parseAstroResult.errors) { logger.info(String(error)); } return null; } const rangeMapper = range => { return new graphql_language_service_1.Range(new graphql_language_service_1.Position(range.start.line + parseAstroResult.scriptOffset, range.start.character), new graphql_language_service_1.Position(range.end.line + parseAstroResult.scriptOffset, range.end.character)); }; return { asts: parseAstroResult.scriptAst, rangeMapper }; }; exports.astroParser = astroParser; //# sourceMappingURL=astro.js.map