ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
92 lines (90 loc) • 3.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const errors = require("./../../errors");
const manipulation_1 = require("./../../manipulation");
const common_1 = require("./../common");
exports.DecoratorBase = common_1.Node;
class Decorator extends exports.DecoratorBase {
/**
* Gets the decorator name.
*/
getName() {
const sourceFile = this.getSourceFile();
if (this.isDecoratorFactory()) {
const callExpression = this.compilerNode.expression;
return getNameFromExpression(callExpression.expression);
}
return getNameFromExpression(this.compilerNode.expression);
function getNameFromExpression(expression) {
if (expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
const propAccess = expression;
return propAccess.name.getText(sourceFile.compilerNode);
}
return expression.getText(sourceFile.compilerNode);
}
}
/**
* Gets the full decorator name.
*/
getFullName() {
const sourceFile = this.getSourceFile();
if (this.isDecoratorFactory())
return this.getCallExpression().getExpression().getText();
return this.compilerNode.expression.getText(sourceFile.compilerNode);
}
/**
* Gets if the decorator is a decorator factory.
*/
isDecoratorFactory() {
return this.compilerNode.expression.kind === ts.SyntaxKind.CallExpression;
}
/**
* Gets the compiler call expression if a decorator factory.
*/
getCallExpression() {
if (!this.isDecoratorFactory())
return undefined;
return this.global.compilerFactory.getNodeFromCompilerNode(this.compilerNode.expression, this.sourceFile);
}
/**
* Gets the decorator's arguments from its call expression.
*/
getArguments() {
const callExpression = this.getCallExpression();
return callExpression == null ? [] : callExpression.getArguments();
}
/**
* Gets the decorator's type arguments from its call expression.
*/
getTypeArguments() {
const callExpression = this.getCallExpression();
return callExpression == null ? [] : callExpression.getTypeArguments();
}
removeTypeArgument(typeArgOrIndex) {
const callExpression = this.getCallExpression();
if (callExpression == null)
throw new errors.InvalidOperationError("Cannot remove a type argument from a decorator that has no type arguments.");
callExpression.removeTypeArgument(typeArgOrIndex);
}
/**
* Removes this decorator.
*/
remove() {
const thisStartLinePos = this.getStartLinePos();
const previousDecorator = this.getPreviousSiblingIfKind(ts.SyntaxKind.Decorator);
if (previousDecorator != null && previousDecorator.getStartLinePos() === thisStartLinePos) {
manipulation_1.removeChildren({
children: [this],
removePrecedingSpaces: true
});
}
else
manipulation_1.removeChildrenWithFormattingFromCollapsibleSyntaxList({
children: [this],
getSiblingFormatting: (parent, sibling) => sibling.getStartLinePos() === thisStartLinePos ? manipulation_1.FormattingKind.Space : manipulation_1.FormattingKind.Newline
});
}
}
exports.Decorator = Decorator;
//# sourceMappingURL=Decorator.js.map