ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
138 lines (137 loc) • 6.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var errors = require("../../../errors");
var manipulation_1 = require("../../../manipulation");
var structurePrinters_1 = require("../../../structurePrinters");
var typescript_1 = require("../../../typescript");
var base_1 = require("../base");
var callBaseSet_1 = require("../callBaseSet");
var common_1 = require("../common");
var VariableDeclarationKind_1 = require("./VariableDeclarationKind");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
exports.VariableDeclarationListBase = base_1.ModifierableNode(common_1.Node);
var VariableDeclarationList = /** @class */ (function (_super) {
tslib_1.__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 kind.
*/
VariableDeclarationList.prototype.getDeclarationKind = function () {
var nodeFlags = this.compilerNode.flags;
if (nodeFlags & typescript_1.ts.NodeFlags.Let)
return VariableDeclarationKind_1.VariableDeclarationKind.Let;
else if (nodeFlags & typescript_1.ts.NodeFlags.Const)
return VariableDeclarationKind_1.VariableDeclarationKind.Const;
else
return VariableDeclarationKind_1.VariableDeclarationKind.Var;
};
/**
* Gets the variable declaration kind keyword.
*/
VariableDeclarationList.prototype.getDeclarationKindKeyword = function () {
var declarationKind = this.getDeclarationKind();
switch (declarationKind) {
case VariableDeclarationKind_1.VariableDeclarationKind.Const:
return this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.ConstKeyword);
case VariableDeclarationKind_1.VariableDeclarationKind.Let:
return this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.LetKeyword);
case VariableDeclarationKind_1.VariableDeclarationKind.Var:
return this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.VarKeyword);
default:
throw errors.getNotImplementedForNeverValueError(declarationKind);
}
};
/**
* Sets the variable declaration kind.
* @param type - Type to set.
*/
VariableDeclarationList.prototype.setDeclarationKind = function (type) {
if (this.getDeclarationKind() === type)
return this;
var keyword = this.getDeclarationKindKeyword();
manipulation_1.insertIntoParentTextRange({
insertPos: keyword.getStart(),
newText: type,
parent: this,
replacing: {
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 - Child index to insert at.
* @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 - Child index to insert at.
* @param structures - Structures representing the variable declarations to insert.
*/
VariableDeclarationList.prototype.insertDeclarations = function (index, structures) {
var writer = this._getWriterWithQueuedChildIndentation();
var structurePrinter = new structurePrinters_1.CommaSeparatedStructuresPrinter(this._context.structurePrinterFactory.forVariableDeclaration());
structurePrinter.printText(writer, structures);
manipulation_1.insertIntoCommaSeparatedNodes({
parent: this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.SyntaxList),
currentNodes: this.getDeclarations(),
insertIndex: index,
newText: writer.toString()
});
return manipulation_1.getNodesToReturn(this.getDeclarations(), index, structures.length);
};
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
VariableDeclarationList.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(exports.VariableDeclarationListBase.prototype, this, structure);
if (structure.declarationKind != null)
this.setDeclarationKind(structure.declarationKind);
if (structure.declarations != null) {
var existingDeclarations = this.getDeclarations();
this.addDeclarations(structure.declarations);
existingDeclarations.forEach(function (d) { return d.remove(); });
}
return this;
};
/**
* Gets the structure equivalent to this node.
*/
VariableDeclarationList.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(exports.VariableDeclarationListBase.prototype, this, {
declarationKind: this.getDeclarationKind(),
declarations: this.getDeclarations().map(function (declaration) { return declaration.getStructure(); })
});
};
return VariableDeclarationList;
}(exports.VariableDeclarationListBase));
exports.VariableDeclarationList = VariableDeclarationList;