UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

111 lines (109 loc) 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const errors = require("./../../errors"); const manipulation_1 = require("./../../manipulation"); const utils_1 = require("./../../utils"); const common_1 = require("./../common"); exports.DecoratorBase = common_1.Node; class Decorator extends exports.DecoratorBase { /** * Gets the decorator name. */ getName() { return this.getNameIdentifier().getText(); } /** * Gets the name identifier of the decorator. */ getNameIdentifier() { const sourceFile = this.getSourceFile(); if (this.isDecoratorFactory()) { const callExpression = this.getCallExpression(); return getIdentifierFromName(callExpression.getExpression()); } return getIdentifierFromName(this.getExpression()); function getIdentifierFromName(expression) { const identifier = getNameFromExpression(expression); if (!utils_1.TypeGuards.isIdentifier(identifier)) { throw new errors.NotImplementedError(`Expected the decorator expression '${identifier.getText()}' to be an identifier, ` + `but it wasn't. Please report this as a bug.`); } return identifier; } function getNameFromExpression(expression) { if (utils_1.TypeGuards.isPropertyAccessExpression(expression)) return expression.getNameIdentifier(); return expression; } } /** * 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.getExpression(); } /** * Gets the expression. */ getExpression() { 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