ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
85 lines (83 loc) • 3.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const callBaseFill_1 = require("./../callBaseFill");
const manipulation_1 = require("./../../manipulation");
const utils_1 = require("./../../utils");
function DecoratableNode(Base) {
return class extends Base {
getDecorators() {
if (this.compilerNode.decorators == null)
return [];
return this.compilerNode.decorators.map(d => this.global.compilerFactory.getNodeFromCompilerNode(d, this.sourceFile));
}
addDecorator(structure) {
return this.insertDecorator(manipulation_1.getEndIndexFromArray(this.compilerNode.decorators), structure);
}
addDecorators(structures) {
return this.insertDecorators(manipulation_1.getEndIndexFromArray(this.compilerNode.decorators), structures);
}
insertDecorator(index, structure) {
return this.insertDecorators(index, [structure])[0];
}
insertDecorators(index, structures) {
if (utils_1.ArrayUtils.isNullOrEmpty(structures))
return [];
const decoratorLines = getDecoratorLines(structures);
const decorators = this.getDecorators();
index = manipulation_1.verifyAndGetIndex(index, decorators.length);
const formattingKind = getDecoratorFormattingKind(this, decorators);
const previousDecorator = decorators[index - 1];
const decoratorCode = manipulation_1.getNewInsertCode({
structures,
newCodes: decoratorLines,
parent: this,
indentationText: this.getIndentationText(),
getSeparator: () => formattingKind,
previousFormattingKind: previousDecorator == null ? manipulation_1.FormattingKind.None : formattingKind,
nextFormattingKind: previousDecorator == null ? formattingKind : manipulation_1.FormattingKind.None
});
manipulation_1.insertIntoCreatableSyntaxList({
parent: this,
insertPos: decorators[index - 1] == null ? this.getStart() : decorators[index - 1].getEnd(),
childIndex: index,
insertItemsCount: structures.length,
newText: decoratorCode,
syntaxList: decorators.length === 0 ? undefined : decorators[0].getParentSyntaxListOrThrow()
});
return this.getDecorators().slice(index, index + structures.length);
}
fill(structure) {
callBaseFill_1.callBaseFill(Base.prototype, this, structure);
if (structure.decorators != null && structure.decorators.length > 0)
this.addDecorators(structure.decorators);
return this;
}
};
}
exports.DecoratableNode = DecoratableNode;
function getDecoratorLines(structures) {
const lines = [];
for (const structure of structures) {
let line = `@${structure.name}`;
if (structure.arguments != null)
line += `(${structure.arguments.join(", ")})`;
lines.push(line);
}
return lines;
}
function getDecoratorFormattingKind(parent, currentDecorators) {
const sameLine = areDecoratorsOnSameLine(parent, currentDecorators);
return sameLine ? manipulation_1.FormattingKind.Space : manipulation_1.FormattingKind.Newline;
}
function areDecoratorsOnSameLine(parent, currentDecorators) {
if (currentDecorators.length <= 1)
return parent.getKind() === ts.SyntaxKind.Parameter;
const startLinePos = currentDecorators[0].getStartLinePos();
for (let i = 1; i < currentDecorators.length; i++) {
if (currentDecorators[i].getStartLinePos() !== startLinePos)
return false;
}
return true;
}
//# sourceMappingURL=DecoratableNode.js.map