UNPKG

1.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getCursor = void 0;
4const babylon = require("babylon");
5const babel_traverse_1 = require("babel-traverse");
6const t = require("babel-types");
7function 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}
14function 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}
34exports.getCursor = getCursor;