1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getCursor = void 0;
|
4 | const babylon = require("babylon");
|
5 | const babel_traverse_1 = require("babel-traverse");
|
6 | const t = require("babel-types");
|
7 | function isCursorHere(nodeLocation, cursorLocation) {
|
8 | const isThatLine = (nodeLocation.start.line <= cursorLocation.line &&
|
9 | nodeLocation.end.line >= cursorLocation.line);
|
10 | const isThatColumn = (nodeLocation.start.column <= cursorLocation.column &&
|
11 | nodeLocation.end.column >= cursorLocation.column);
|
12 | return isThatLine && isThatColumn;
|
13 | }
|
14 | function getCursor(code, currentPosition) {
|
15 | const ast = babylon.parse(code);
|
16 | let wasCursorFound;
|
17 | babel_traverse_1.default(ast, {
|
18 | enter(path) {
|
19 | if (isCursorHere(path.node.loc, currentPosition)) {
|
20 | wasCursorFound = path;
|
21 | }
|
22 | },
|
23 | });
|
24 | if (wasCursorFound) {
|
25 | if (t.isArrowFunctionExpression(wasCursorFound.node)) {
|
26 | const arrowExpression = wasCursorFound.findParent((parentPath) => t.isExpressionStatement(parentPath.node));
|
27 | return arrowExpression.node.loc.end;
|
28 | }
|
29 | const externalNode = wasCursorFound.findParent((parentPath) => (t.isBlockStatement(parentPath.parent) || t.isProgram(parentPath.parent)));
|
30 | return externalNode.node.loc.end;
|
31 | }
|
32 | return null;
|
33 | }
|
34 | exports.getCursor = getCursor;
|