TsSimpleAst
Decorators
Decorators can be retrieved from class related nodes by calling the getDecorators() method.
const decorators = classDeclaration.getDecorators();
Name
You can get the name or fully qualified name of a decorator by using the getName() or getFullName() functions respectively.
For example, given the following code:
@obj.decorator
function myFunction() {
}
The following happens:
decorator.getName(); // decorator
decorator.getFullName(); // obj.decorator
Decorator factory
Decorators with parenthesis (ex. @decorator(3)) are decorator factories, while decorators without (ex. @decorator) are not.
decorator.isDecoratorFactory(); // returns: boolean
Arguments
Decorators with parenthesis are call expressions.
Call expressions are currently not implemented in this library, so you will need to access the information about it by getting the call expression’s underlying compiler node:
// must be a decorator factory, otherwise getCallExpression will return undefined
if (!decorators.isDecoratorFactory())
return;
const callExpression = decorator.getCallExpression()!.getCompilerNode();
for (const arg of callExpression.arguments) {
// use arg here
}