ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
145 lines (144 loc) • 6.42 kB
JavaScript
"use strict";
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 manipulation_1 = require("./../../manipulation");
var errors = require("./../../errors");
var common_1 = require("./../common");
var base_1 = require("./../base");
var callBaseFill_1 = require("./../callBaseFill");
var VariableDeclarationType_1 = require("./VariableDeclarationType");
exports.VariableDeclarationListBase = base_1.ModifierableNode(common_1.Node);
var VariableDeclarationList = /** @class */ (function (_super) {
__extends(VariableDeclarationList, _super);
function VariableDeclarationList() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Get the variable declarations.
*/
VariableDeclarationList.prototype.getDeclarations = function () {
var _this = this;
return this.compilerNode.declarations.map(function (d) { return _this.getNodeFromCompilerNode(d); });
};
/**
* Gets the variable declaration type.
*/
VariableDeclarationList.prototype.getDeclarationType = function () {
var nodeFlags = this.compilerNode.flags;
if (nodeFlags & typescript_1.ts.NodeFlags.Let)
return VariableDeclarationType_1.VariableDeclarationType.Let;
else if (nodeFlags & typescript_1.ts.NodeFlags.Const)
return VariableDeclarationType_1.VariableDeclarationType.Const;
else
return VariableDeclarationType_1.VariableDeclarationType.Var;
};
/**
* Gets the variable declaration type keyword.
*/
VariableDeclarationList.prototype.getDeclarationTypeKeyword = function () {
var declarationType = this.getDeclarationType();
switch (declarationType) {
case VariableDeclarationType_1.VariableDeclarationType.Const:
return this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.ConstKeyword);
case VariableDeclarationType_1.VariableDeclarationType.Let:
return this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.LetKeyword);
case VariableDeclarationType_1.VariableDeclarationType.Var:
return this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.VarKeyword);
default:
throw errors.getNotImplementedForNeverValueError(declarationType);
}
};
/**
* Sets the variable declaration type.
* @param type - Type to set.
*/
VariableDeclarationList.prototype.setDeclarationType = function (type) {
if (this.getDeclarationType() === type)
return this;
var keyword = this.getDeclarationTypeKeyword();
manipulation_1.insertIntoParent({
childIndex: keyword.getChildIndex(),
insertItemsCount: 1,
insertPos: keyword.getStart(),
newText: type,
parent: this,
replacing: {
nodes: [keyword],
textLength: keyword.getWidth()
}
});
return this;
};
/**
* Add a variable declaration to the statement.
* @param structure - Structure representing the variable declaration to add.
*/
VariableDeclarationList.prototype.addDeclaration = function (structure) {
return this.addDeclarations([structure])[0];
};
/**
* Adds variable declarations to the statement.
* @param structures - Structures representing the variable declarations to add.
*/
VariableDeclarationList.prototype.addDeclarations = function (structures) {
return this.insertDeclarations(this.getDeclarations().length, structures);
};
/**
* Inserts a variable declaration at the specified index within the statement.
* @param index - Index to insert.
* @param structure - Structure representing the variable declaration to insert.
*/
VariableDeclarationList.prototype.insertDeclaration = function (index, structure) {
return this.insertDeclarations(index, [structure])[0];
};
/**
* Inserts variable declarations at the specified index within the statement.
* @param index - Index to insert.
* @param structures - Structures representing the variable declarations to insert.
*/
VariableDeclarationList.prototype.insertDeclarations = function (index, structures) {
var indentationText = this.getChildIndentationText();
var texts = structures.map(function (structure) {
var text = structure.name;
if (structure.type != null)
text += ": " + structure.type;
if (structure.initializer != null)
text += " = " + structure.initializer;
return text;
});
manipulation_1.insertIntoCommaSeparatedNodes({
parent: this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.SyntaxList),
currentNodes: this.getDeclarations(),
insertIndex: index,
newTexts: texts
});
var declarations = this.getDeclarations().slice(index, index + texts.length);
for (var i = 0; i < structures.length; i++)
declarations[i].fill(structures[i]);
return declarations;
};
/**
* Fills the node from a structure.
* @param structure - Structure to fill.
*/
VariableDeclarationList.prototype.fill = function (structure) {
callBaseFill_1.callBaseFill(exports.VariableDeclarationListBase.prototype, this, structure);
if (structure.declarationType != null)
this.setDeclarationType(structure.declarationType);
if (structure.declarations != null)
this.addDeclarations(structure.declarations);
return this;
};
return VariableDeclarationList;
}(exports.VariableDeclarationListBase));
exports.VariableDeclarationList = VariableDeclarationList;