ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
68 lines (67 loc) • 2.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var errors = require("../../../errors");
var manipulation_1 = require("../../../manipulation");
var typescript_1 = require("../../../typescript");
var callBaseSet_1 = require("../callBaseSet");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
function GeneratorableNode(Base) {
return /** @class */ (function (_super) {
tslib_1.__extends(class_1, _super);
function class_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
class_1.prototype.isGenerator = function () {
return this.compilerNode.asteriskToken != null;
};
class_1.prototype.getAsteriskToken = function () {
return this._getNodeFromCompilerNodeIfExists(this.compilerNode.asteriskToken);
};
class_1.prototype.getAsteriskTokenOrThrow = function () {
return errors.throwIfNullOrUndefined(this.getAsteriskToken(), "Expected to find an asterisk token.");
};
class_1.prototype.setIsGenerator = function (value) {
var asteriskToken = this.getAsteriskToken();
var isSet = asteriskToken != null;
if (isSet === value)
return this;
if (asteriskToken == null) {
manipulation_1.insertIntoParentTextRange({
insertPos: getAsteriskInsertPos(this),
parent: this,
newText: "*"
});
}
else {
manipulation_1.removeChildrenWithFormatting({
children: [asteriskToken],
getSiblingFormatting: function () { return manipulation_1.FormattingKind.Space; }
});
}
return this;
};
class_1.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(Base.prototype, this, structure);
if (structure.isGenerator != null)
this.setIsGenerator(structure.isGenerator);
return this;
};
class_1.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, {
isGenerator: this.isGenerator()
});
};
return class_1;
}(Base));
}
exports.GeneratorableNode = GeneratorableNode;
function getAsteriskInsertPos(node) {
if (node.getKind() === typescript_1.SyntaxKind.FunctionDeclaration)
return node.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.FunctionKeyword).getEnd();
var namedNode = node;
/* istanbul ignore if */
if (namedNode.getName == null)
throw new errors.NotImplementedError("Expected a name node for a non-function declaration.");
return namedNode.getNameNode().getStart();
}