UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

113 lines (112 loc) 3.96 kB
"use strict"; var __decorate = (this && this.__decorate)/* istanbul ignore next */ || 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; }; var __values = (this && this.__values)/* istanbul ignore next */ || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); var ts = require("typescript"); var utils_1 = require("./../../../utils"); var TextSpan_1 = require("./TextSpan"); /** * Definition info. */ var DefinitionInfo = /** @class */ (function () { /** * @internal */ function DefinitionInfo(global, compilerObject) { this.global = global; this._compilerObject = compilerObject; } Object.defineProperty(DefinitionInfo.prototype, "compilerObject", { /** * Gets the compiler object. */ get: function () { return this._compilerObject; }, enumerable: true, configurable: true }); /** * Gets the source file this reference is in. */ DefinitionInfo.prototype.getSourceFile = function () { return this.global.compilerFactory.getSourceFileFromFilePath(this.compilerObject.fileName); }; /** * Gets the text span. */ DefinitionInfo.prototype.getTextSpan = function () { return new TextSpan_1.TextSpan(this.compilerObject.textSpan); }; /** * Gets the kind. */ DefinitionInfo.prototype.getKind = function () { return this.compilerObject.kind; }; /** * Gets the name. */ DefinitionInfo.prototype.getName = function () { return this.compilerObject.name; }; /** * Gets the container kind. */ DefinitionInfo.prototype.getContainerKind = function () { return this.compilerObject.containerKind; }; /** * Gets the container name. */ DefinitionInfo.prototype.getContainerName = function () { return this.compilerObject.containerName; }; /** * Gets the definition node. */ DefinitionInfo.prototype.getNode = function () { var start = this.getTextSpan().getStart(); var identifier = findIdentifier(this.getSourceFile()); return identifier == null ? undefined : identifier.getParentOrThrow(); function findIdentifier(node) { if (node.getKind() === ts.SyntaxKind.Identifier && node.getStart() === start) return node; try { for (var _a = __values(node.getChildrenIterator()), _b = _a.next(); !_b.done; _b = _a.next()) { var child = _b.value; if (child.getPos() <= start && child.getEnd() >= start) return findIdentifier(child); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } } return undefined; var e_1, _c; } }; __decorate([ utils_1.Memoize ], DefinitionInfo.prototype, "getTextSpan", null); return DefinitionInfo; }()); exports.DefinitionInfo = DefinitionInfo;