ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
39 lines (37 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const errors = require("./../../errors");
const manipulation_1 = require("./../../manipulation");
function TypeArgumentedNode(Base) {
return class extends Base {
getTypeArguments() {
if (this.compilerNode.typeArguments == null)
return [];
return this.compilerNode.typeArguments.map(a => this.global.compilerFactory.getNodeFromCompilerNode(a, this.sourceFile));
}
removeTypeArgument(typeArgOrIndex) {
const typeArguments = this.getTypeArguments();
if (typeArguments.length === 0)
throw new errors.InvalidOperationError("Cannot remove a type argument when none exist.");
const typeArgToRemove = typeof typeArgOrIndex === "number" ? getTypeArgFromIndex(typeArgOrIndex) : typeArgOrIndex;
if (typeArguments.length === 1) {
const childSyntaxList = typeArguments[0].getParentSyntaxListOrThrow();
manipulation_1.removeChildren({
children: [
childSyntaxList.getPreviousSiblingIfKindOrThrow(ts.SyntaxKind.FirstBinaryOperator),
childSyntaxList,
childSyntaxList.getNextSiblingIfKindOrThrow(ts.SyntaxKind.GreaterThanToken)
]
});
}
else
manipulation_1.removeCommaSeparatedChild(typeArgToRemove);
function getTypeArgFromIndex(index) {
return typeArguments[manipulation_1.verifyAndGetIndex(index, typeArguments.length - 1)];
}
}
};
}
exports.TypeArgumentedNode = TypeArgumentedNode;
//# sourceMappingURL=TypeArgumentedNode.js.map