UNPKG

1.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.pointToOffset = exports.getASTNodeAtPosition = void 0;
4const graphql_1 = require("graphql");
5function getASTNodeAtPosition(query, ast, point) {
6 const offset = pointToOffset(query, point);
7 let nodeContainingPosition;
8 graphql_1.visit(ast, {
9 enter(node) {
10 if (node.kind !== 'Name' &&
11 node.loc &&
12 node.loc.start <= offset &&
13 offset <= node.loc.end) {
14 nodeContainingPosition = node;
15 }
16 else {
17 return false;
18 }
19 },
20 leave(node) {
21 if (node.loc && node.loc.start <= offset && offset <= node.loc.end) {
22 return false;
23 }
24 },
25 });
26 return nodeContainingPosition;
27}
28exports.getASTNodeAtPosition = getASTNodeAtPosition;
29function pointToOffset(text, point) {
30 const linesUntilPosition = text.split('\n').slice(0, point.line);
31 return (point.character +
32 linesUntilPosition
33 .map(line => line.length + 1)
34 .reduce((a, b) => a + b, 0));
35}
36exports.pointToOffset = pointToOffset;
37//# sourceMappingURL=getASTNodeAtPosition.js.map
\No newline at end of file