ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
70 lines (68 loc) • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const manipulation_1 = require("./../../manipulation");
const callBaseFill_1 = require("./../callBaseFill");
const utils_1 = require("./../../utils");
function DocumentationableNode(Base) {
return class extends Base {
getDocumentationComment() {
const docCommentNodes = this.getDocumentationCommentNodes();
if (docCommentNodes.length === 0)
return undefined;
const texts = docCommentNodes.map(n => (n.compilerNode.comment || "").trim());
return texts.filter(t => t.length > 0).join(this.global.manipulationSettings.getNewLineKind());
}
getDocumentationCommentNodes() {
const nodes = this.compilerNode.jsDoc || [];
return nodes.map(n => this.global.compilerFactory.getNodeFromCompilerNode(n, this.sourceFile));
}
addDoc(structure) {
return this.addDocs([structure])[0];
}
addDocs(structures) {
return this.insertDocs(manipulation_1.getEndIndexFromArray(this.compilerNode.jsDoc), structures);
}
insertDoc(index, structure) {
return this.insertDocs(index, [structure])[0];
}
insertDocs(index, structures) {
if (utils_1.ArrayUtils.isNullOrEmpty(structures))
return [];
const indentationText = this.getIndentationText();
const newLineText = this.global.manipulationSettings.getNewLineKind();
const code = `${getDocumentationCode(structures, indentationText, newLineText)}${newLineText}${indentationText}`;
const nodes = this.getDocumentationCommentNodes();
index = manipulation_1.verifyAndGetIndex(index, nodes.length);
const insertPos = index === nodes.length ? this.getStart() : nodes[index].getStart();
manipulation_1.insertIntoParent({
insertPos,
parent: this,
newText: code,
childIndex: nodes.length > 0 ? nodes[0].getChildIndex() + index : 0,
insertItemsCount: structures.length
});
return this.getDocumentationCommentNodes().slice(index, index + structures.length);
}
fill(structure) {
callBaseFill_1.callBaseFill(Base.prototype, this, structure);
if (structure.docs != null && structure.docs.length > 0)
this.addDocs(structure.docs);
return this;
}
};
}
exports.DocumentationableNode = DocumentationableNode;
function getDocumentationCode(structures, indentationText, newLineText) {
let code = "";
for (const structure of structures) {
if (code.length > 0)
code += `${newLineText}${indentationText}`;
code += getDocumentationCodeForStructure(structure, indentationText, newLineText);
}
return code;
}
function getDocumentationCodeForStructure(structure, indentationText, newLineText) {
const lines = structure.description.split(/\r?\n/);
return `/**${newLineText}` + lines.map(l => `${indentationText} * ${l}`).join(newLineText) + `${newLineText}${indentationText} */`;
}
//# sourceMappingURL=DocumentationableNode.js.map