ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
81 lines (80 loc) • 3.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var typescript_1 = require("../../../typescript");
var utils_1 = require("../../../utils");
var callBaseSet_1 = require("../callBaseSet");
var Scope_1 = require("../common/Scope");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
function ScopeableNode(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.getScope = function () {
var scope = getScopeForNode(this);
if (scope != null)
return scope;
if (utils_1.TypeGuards.isParameterDeclaration(this) && this.isReadonly())
return Scope_1.Scope.Public;
return undefined;
};
class_1.prototype.setScope = function (scope) {
setScopeForNode(this, scope);
return this;
};
// todo: make public
class_1.prototype.getScopeKeyword = function () {
return utils_1.ArrayUtils.find(this.getModifiers(), function (m) {
var text = m.getText();
return text === "public" || text === "protected" || text === "private";
});
};
class_1.prototype.hasScopeKeyword = function () {
return this.getScopeKeyword() != null;
};
class_1.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(Base.prototype, this, structure);
if (structure.hasOwnProperty("scope"))
this.setScope(structure.scope);
return this;
};
class_1.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, {
scope: this.getScope()
});
};
return class_1;
}(Base));
}
exports.ScopeableNode = ScopeableNode;
/**
* Gets the scope for a node.
* @internal
* @param node - Node to check for.
*/
function getScopeForNode(node) {
var modifierFlags = node.getCombinedModifierFlags();
if ((modifierFlags & typescript_1.ts.ModifierFlags.Private) !== 0)
return Scope_1.Scope.Private;
else if ((modifierFlags & typescript_1.ts.ModifierFlags.Protected) !== 0)
return Scope_1.Scope.Protected;
else if ((modifierFlags & typescript_1.ts.ModifierFlags.Public) !== 0)
return Scope_1.Scope.Public;
else
return undefined;
}
exports.getScopeForNode = getScopeForNode;
/**
* Sets the scope for a node.
* @internal
* @param node - Node to set the scope for.
* @param scope - Scope to be set to.
*/
function setScopeForNode(node, scope) {
node.toggleModifier("public", scope === Scope_1.Scope.Public); // always be explicit with scope
node.toggleModifier("protected", scope === Scope_1.Scope.Protected);
node.toggleModifier("private", scope === Scope_1.Scope.Private);
}
exports.setScopeForNode = setScopeForNode;