ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
101 lines (100 loc) • 5.14 kB
JavaScript
;
var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var typescript_1 = require("./../../typescript");
var errors = require("./../../errors");
var manipulation_1 = require("./../../manipulation");
var utils_1 = require("./../../utils");
var callBaseFill_1 = require("./../callBaseFill");
function TypeParameteredNode(Base) {
return /** @class */ (function (_super) {
__extends(class_1, _super);
function class_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
class_1.prototype.getTypeParameter = function (nameOrFindFunction) {
return utils_1.getNamedNodeByNameOrFindFunction(this.getTypeParameters(), nameOrFindFunction);
};
class_1.prototype.getTypeParameterOrThrow = function (nameOrFindFunction) {
return errors.throwIfNullOrUndefined(this.getTypeParameter(nameOrFindFunction), function () { return utils_1.getNotFoundErrorMessageForNameOrFindFunction("type parameter", nameOrFindFunction); });
};
class_1.prototype.getTypeParameters = function () {
var _this = this;
var typeParameters = this.compilerNode.typeParameters;
if (typeParameters == null)
return [];
return typeParameters.map(function (t) { return _this.getNodeFromCompilerNode(t); });
};
class_1.prototype.addTypeParameter = function (structure) {
return this.addTypeParameters([structure])[0];
};
class_1.prototype.addTypeParameters = function (structures) {
return this.insertTypeParameters(manipulation_1.getEndIndexFromArray(this.compilerNode.typeParameters), structures);
};
class_1.prototype.insertTypeParameter = function (index, structure) {
return this.insertTypeParameters(index, [structure])[0];
};
class_1.prototype.insertTypeParameters = function (index, structures) {
if (utils_1.ArrayUtils.isNullOrEmpty(structures))
return [];
var typeParameters = this.getTypeParameters();
var typeParamCodes = structures.map(function (s) { return getStructureCode(s); });
index = manipulation_1.verifyAndGetIndex(index, typeParameters.length);
if (typeParameters.length === 0) {
var _a = getInsertInfo(this), insertPos = _a.insertPos, childIndex = _a.childIndex;
manipulation_1.insertIntoParent({
insertPos: insertPos,
childIndex: childIndex,
insertItemsCount: 3,
parent: this,
newText: "<" + typeParamCodes.join(", ") + ">"
});
}
else {
manipulation_1.insertIntoCommaSeparatedNodes({
parent: this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.FirstBinaryOperator).getNextSiblingIfKindOrThrow(typescript_1.SyntaxKind.SyntaxList),
currentNodes: typeParameters,
insertIndex: index,
newTexts: typeParamCodes
});
}
return this.getTypeParameters().slice(index, index + structures.length);
};
class_1.prototype.fill = function (structure) {
callBaseFill_1.callBaseFill(Base.prototype, this, structure);
if (structure.typeParameters != null && structure.typeParameters.length > 0)
this.addTypeParameters(structure.typeParameters);
return this;
};
return class_1;
}(Base));
}
exports.TypeParameteredNode = TypeParameteredNode;
function getStructureCode(structure) {
var code = structure.name;
if (structure.constraint != null && structure.constraint.length > 0)
code += " extends " + structure.constraint;
return code;
}
function getInsertInfo(node) {
var namedNode = node;
if (namedNode.getNameNode != null) {
var nameNode = namedNode.getNameNode();
return { insertPos: nameNode.getEnd(), childIndex: nameNode.getChildIndex() + 1 };
}
else if (utils_1.TypeGuards.isCallSignatureDeclaration(node) || utils_1.TypeGuards.isFunctionTypeNode(node)) {
var openParenToken = node.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.OpenParenToken);
return { insertPos: openParenToken.getStart(), childIndex: openParenToken.getChildIndex() };
}
else
throw new errors.NotImplementedError("Not implemented scenario inserting type parameters for node with kind " + node.getKindName() + ".");
}