ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
152 lines (151 loc) • 7.03 kB
JavaScript
"use strict";
var __assign = (this && this.__assign)/* istanbul ignore next */ || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var textManipulators_1 = require("./../textManipulators");
var nodeHandlers_1 = require("./../nodeHandlers");
var doManipulation_1 = require("./doManipulation");
var helpers_1 = require("./../helpers");
function insertIntoParent(opts) {
var insertPos = opts.insertPos, newText = opts.newText, parent = opts.parent, childIndex = opts.childIndex, insertItemsCount = opts.insertItemsCount, customMappings = opts.customMappings;
doManipulation_1.doManipulation(parent.sourceFile, new textManipulators_1.InsertionTextManipulator({
insertPos: insertPos,
newText: newText,
replacingLength: opts.replacing == null ? undefined : opts.replacing.textLength
}), new nodeHandlers_1.NodeHandlerFactory().getForChildIndex({
parent: parent,
childCount: insertItemsCount,
childIndex: childIndex,
replacingNodes: opts.replacing == null ? undefined : opts.replacing.nodes,
customMappings: customMappings
}));
}
exports.insertIntoParent = insertIntoParent;
function insertSyntaxList(opts) {
var insertPos = opts.insertPos, newText = opts.newText, parent = opts.parent;
doManipulation_1.doManipulation(parent.sourceFile, new textManipulators_1.InsertionTextManipulator({
insertPos: insertPos,
newText: newText
}), new nodeHandlers_1.NodeHandlerFactory().getForCreatingSyntaxList({
parent: parent,
insertPos: insertPos
}));
}
exports.insertSyntaxList = insertSyntaxList;
/**
* Inserts a text range into a parent.
*/
function insertIntoParentTextRange(opts) {
var insertPos = opts.insertPos, newText = opts.newText, parent = opts.parent;
doManipulation_1.doManipulation(parent.sourceFile, new textManipulators_1.InsertionTextManipulator({
insertPos: insertPos,
newText: newText
}), new nodeHandlers_1.NodeHandlerFactory().getForRange({
parent: parent,
start: insertPos,
end: insertPos + newText.length
}));
}
exports.insertIntoParentTextRange = insertIntoParentTextRange;
function insertIntoCreatableSyntaxList(opts) {
var insertPos = opts.insertPos, newText = opts.newText, parent = opts.parent, syntaxList = opts.syntaxList, childIndex = opts.childIndex, insertItemsCount = opts.insertItemsCount;
if (syntaxList == null)
insertSyntaxList({
parent: parent,
insertPos: insertPos,
newText: newText
});
else
insertIntoParent({
insertPos: insertPos,
newText: newText,
parent: syntaxList,
insertItemsCount: insertItemsCount,
childIndex: childIndex
});
}
exports.insertIntoCreatableSyntaxList = insertIntoCreatableSyntaxList;
function insertIntoCommaSeparatedNodes(opts) {
// todo: this needs to be fixed/cleaned up in the future, but this is good enough for now
var currentNodes = opts.currentNodes, insertIndex = opts.insertIndex, newTexts = opts.newTexts, parent = opts.parent;
var nextNode = currentNodes[insertIndex];
var previousNode = currentNodes[insertIndex - 1];
var numberOfSyntaxListItemsInserting = newTexts.length * 2 - 1;
var separator = getSeparator();
var newText = newTexts.join("," + (opts.useNewlines ? parent.global.manipulationSettings.getNewLineKind() : " ")).replace(/^\s+/, "");
if (nextNode != null) {
insertIntoParent({
insertPos: nextNode.getStart(),
newText: newText + "," + separator,
parent: parent,
childIndex: nextNode.getChildIndex(),
insertItemsCount: numberOfSyntaxListItemsInserting + 1 // extra comma
});
}
else if (previousNode != null) {
insertIntoParent({
insertPos: previousNode.getEnd(),
newText: "," + separator + newText,
parent: parent,
childIndex: previousNode.getChildIndex() + 1,
insertItemsCount: numberOfSyntaxListItemsInserting + 1 // extra comma
});
}
else {
if (opts.useNewlines && currentNodes.length === 0)
newText = separator + newText + parent.global.manipulationSettings.getNewLineKind() + parent.getIndentationText();
insertIntoParent({
insertPos: parent.getPos(),
parent: parent,
newText: newText,
childIndex: 0,
insertItemsCount: numberOfSyntaxListItemsInserting,
replacing: currentNodes.length === 0 ? { textLength: parent.getNextSiblingOrThrow().getStart() - parent.getPos(), nodes: [] } : undefined
});
}
function getSeparator() {
if (!opts.useNewlines)
return " ";
return parent.global.manipulationSettings.getNewLineKind() + parent.getParentOrThrow().getChildIndentationText();
}
}
exports.insertIntoCommaSeparatedNodes = insertIntoCommaSeparatedNodes;
/**
* Used to insert non-comma separated nodes into braces or a source file.
*/
function insertIntoBracesOrSourceFile(opts) {
var parent = opts.parent, index = opts.index, childCodes = opts.childCodes, separator = opts.separator, children = opts.children;
doManipulation_1.doManipulation(parent.sourceFile, new textManipulators_1.InsertIntoBracesTextManipulator(opts), new nodeHandlers_1.NodeHandlerFactory().getForChildIndex({
parent: parent.getChildSyntaxListOrThrow(),
childIndex: index,
childCount: childCodes.length
}));
}
exports.insertIntoBracesOrSourceFile = insertIntoBracesOrSourceFile;
/**
* Glues together insertIntoBracesOrSourceFile and fillAndGetChildren.
* @param opts - Options to do this operation.
*/
function insertIntoBracesOrSourceFileWithFillAndGetChildren(opts) {
if (opts.structures.length === 0)
return [];
var startChildren = opts.getIndexedChildren();
var parentSyntaxList = opts.parent.getChildSyntaxListOrThrow();
var index = helpers_1.verifyAndGetIndex(opts.index, startChildren.length);
var childIndex = getChildIndex();
insertIntoBracesOrSourceFile(__assign({}, opts, { children: parentSyntaxList.getChildren(), separator: opts.sourceFile.global.manipulationSettings.getNewLineKind(), index: childIndex }));
return helpers_1.fillAndGetChildren(__assign({}, opts, { allChildren: opts.getIndexedChildren(), index: index }));
function getChildIndex() {
if (index === 0)
return 0;
// get the previous member in order to get the implementation signature + 1
return startChildren[index - 1].getChildIndex() + 1;
}
}
exports.insertIntoBracesOrSourceFileWithFillAndGetChildren = insertIntoBracesOrSourceFileWithFillAndGetChildren;