ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
101 lines (100 loc) • 4.47 kB
JavaScript
"use strict";
var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var callBaseFill_1 = require("./../callBaseFill");
var errors = require("./../../errors");
var manipulation_1 = require("./../../manipulation");
var utils_1 = require("./../../utils");
function TypedNode(Base) {
return /** @class */ (function (_super) {
__extends(class_1, _super);
function class_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
class_1.prototype.getType = function () {
return this.global.typeChecker.getTypeAtLocation(this);
};
class_1.prototype.getTypeNode = function () {
return this.compilerNode.type == null ? undefined : this.global.compilerFactory.getNodeFromCompilerNode(this.compilerNode.type, this.sourceFile);
};
class_1.prototype.getTypeNodeOrThrow = function () {
return errors.throwIfNullOrUndefined(this.getTypeNode(), "Expected to find a type node.");
};
class_1.prototype.setType = function (text) {
if (utils_1.StringUtils.isNullOrWhitespace(text))
return this.removeType();
var typeNode = this.getTypeNode();
if (typeNode != null && typeNode.getText() === text)
return this;
// remove previous type
var separatorSyntaxKind = getSeparatorSyntaxKindForNode(this);
var separatorNode = this.getFirstChildByKind(separatorSyntaxKind);
var insertPos;
var childIndex;
var insertItemsCount;
var newText;
if (separatorNode == null) {
var identifier = this.getFirstChildByKindOrThrow(ts.SyntaxKind.Identifier);
childIndex = identifier.getChildIndex() + 1;
insertPos = identifier.getEnd();
insertItemsCount = 2;
newText = (separatorSyntaxKind === ts.SyntaxKind.EqualsToken ? " = " : ": ") + text;
}
else {
childIndex = separatorNode.getChildIndex() + 1;
insertPos = typeNode.getStart();
insertItemsCount = 1;
newText = text;
}
// insert new type
manipulation_1.insertIntoParent({
parent: this,
childIndex: childIndex,
insertItemsCount: insertItemsCount,
insertPos: insertPos,
newText: newText,
replacing: {
textLength: typeNode == null ? 0 : typeNode.getWidth(),
nodes: typeNode == null ? [] : [typeNode]
}
});
return this;
};
class_1.prototype.fill = function (structure) {
callBaseFill_1.callBaseFill(Base.prototype, this, structure);
if (structure.type != null)
this.setType(structure.type);
return this;
};
class_1.prototype.removeType = function () {
if (this.getKind() === ts.SyntaxKind.TypeAliasDeclaration)
throw new errors.NotSupportedError("Cannot remove the type of a type alias. Use " + "setType" + " instead.");
var typeNode = this.getTypeNode();
if (typeNode == null)
return this;
var separatorToken = typeNode.getPreviousSiblingIfKindOrThrow(getSeparatorSyntaxKindForNode(this));
manipulation_1.removeChildren({ children: [separatorToken, typeNode], removePrecedingSpaces: true });
return this;
};
return class_1;
}(Base));
}
exports.TypedNode = TypedNode;
function getSeparatorSyntaxKindForNode(node) {
switch (node.getKind()) {
case ts.SyntaxKind.TypeAliasDeclaration:
return ts.SyntaxKind.EqualsToken;
default:
return ts.SyntaxKind.ColonToken;
}
}