UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

64 lines (62 loc) 2.39 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const utils_1 = require("./../../../utils"); const TextSpan_1 = require("./TextSpan"); /** * Document span. */ class DocumentSpan { /** * @internal */ constructor(global, compilerObject) { this.global = global; this._compilerObject = compilerObject; // get the parent most, non-syntax tree, non-source file at the given position // todo: could make this faster by only going down the tree instead of going down then up let node = this.getSourceFile().getDescendantAtPos(this.getTextSpan().getStart()); let nodeParent = node.getParent(); while (nodeParent != null && nodeParent.getStart() === node.getStart() && nodeParent.getKind() !== ts.SyntaxKind.SourceFile) { node = nodeParent; nodeParent = node.getParent(); } // store this node so that it's start doesn't go out of date because of manipulation (though the text span may) this.node = node; } /** * Gets the compiler object. */ get compilerObject() { return this._compilerObject; } /** * Gets the source file this reference is in. */ getSourceFile() { return this.global.compilerFactory.getSourceFileFromFilePath(this.compilerObject.fileName); } /** * Gets the text span. */ getTextSpan() { return new TextSpan_1.TextSpan(this.compilerObject.textSpan); } /** * Gets the node at the start of the text span. */ getNode() { return this.node; } } __decorate([ utils_1.Memoize ], DocumentSpan.prototype, "getTextSpan", null); exports.DocumentSpan = DocumentSpan; //# sourceMappingURL=DocumentSpan.js.map