ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
70 lines (69 loc) • 3.15 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 callBaseFill_1 = require("./../callBaseFill");
var common_1 = require("./../common");
var base_1 = require("./../base");
exports.VariableDeclarationBase = base_1.TypedNode(base_1.InitializerExpressionableNode(base_1.BindingNamedNode(common_1.Node)));
var VariableDeclaration = /** @class */ (function (_super) {
__extends(VariableDeclaration, _super);
function VariableDeclaration() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Fills this node with the specified structure.
* @param structure - Structure to fill.
*/
VariableDeclaration.prototype.fill = function (structure) {
callBaseFill_1.callBaseFill(exports.VariableDeclarationBase.prototype, this, structure);
return this;
};
/**
* Removes this variable declaration.
*/
VariableDeclaration.prototype.remove = function () {
var parent = this.getParentOrThrow();
switch (parent.getKind()) {
case typescript_1.SyntaxKind.VariableDeclarationList:
removeFromDeclarationList(this);
break;
case typescript_1.SyntaxKind.CatchClause:
removeFromCatchClause(this);
break;
default:
throw new errors.NotImplementedError("Not implemented for syntax kind: " + parent.getKindName());
}
function removeFromDeclarationList(node) {
var variableStatement = parent.getParentIfKindOrThrow(typescript_1.SyntaxKind.VariableStatement);
var declarations = variableStatement.getDeclarations();
if (declarations.length === 1)
variableStatement.remove();
else
manipulation_1.removeCommaSeparatedChild(node, { removePrecedingSpaces: declarations[0] === node ? false : undefined });
}
function removeFromCatchClause(node) {
manipulation_1.removeChildren({
children: [
node.getPreviousSiblingIfKindOrThrow(typescript_1.SyntaxKind.OpenParenToken),
node,
node.getNextSiblingIfKindOrThrow(typescript_1.SyntaxKind.CloseParenToken)
],
removePrecedingSpaces: true
});
}
};
return VariableDeclaration;
}(exports.VariableDeclarationBase));
exports.VariableDeclaration = VariableDeclaration;