@xtrek/ts-migrate-plugins
Version:
Set of codemods, which are doing transformation of js/jsx to ts/tsx
37 lines • 1.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const typescript_1 = __importDefault(require("typescript"));
/**
* Returns the token whose text contains the position.
* If the position is past the end of the file, then it returns the file node itself.
*
* This function is adapted from TypeScript:
* https://github.com/microsoft/TypeScript/blob/v4.1.3/src/services/utilities.ts#L1095
*/
function getTokenAtPosition(sourceFile, position) {
let current = sourceFile;
// eslint-disable-next-line no-restricted-syntax, no-labels
outer: while (true) {
// find the child that contains 'position'
// eslint-disable-next-line no-restricted-syntax
for (const child of current.getChildren(sourceFile)) {
const start = child.getFullStart();
if (start > position) {
// If this child begins after position, then all subsequent children will as well.
break;
}
const end = child.getEnd();
if (position < end || (position === end && child.kind === typescript_1.default.SyntaxKind.EndOfFileToken)) {
current = child;
// eslint-disable-next-line no-continue, no-labels
continue outer;
}
}
return current;
}
}
exports.default = getTokenAtPosition;
//# sourceMappingURL=token-pos.js.map