cognitive-complexity-ts
Version:
This program analyses TypeScript and JavaScript code according to the [Cognitive Complexity metric](https://www.sonarsource.com/docs/CognitiveComplexity.pdf). It produces a JSON summary and a GUI for exploring the complexity of your codebase.
58 lines • 2.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scope = void 0;
const typescript_1 = __importDefault(require("typescript"));
const node_naming_1 = require("./node-naming");
/**
* Contains all names that could be recursively referenced.
*/
class Scope {
constructor(local, object) {
this.local = local;
this.object = object;
}
includes(name) {
return this.local.includes(name) || this.object.includes(name);
}
maybeAdd(node, variableBeingDefined) {
const { local, object } = this.scopeToAdd(node, variableBeingDefined);
if (local.length !== 0 || object.length !== 0) {
return new Scope([...this.local, ...local], [...this.object, ...object]);
}
return this;
}
scopeToAdd(node, variableBeingDefined) {
const local = [];
const object = [];
const introducedLocal = (0, node_naming_1.getIntroducedLocalName)(node);
if (introducedLocal !== undefined) {
local.push(introducedLocal);
}
if (typescript_1.default.isPropertyAssignment(node)
|| typescript_1.default.isPropertyDeclaration(node)
|| typescript_1.default.isMethodDeclaration(node)
|| typescript_1.default.isAccessor(node)) {
if (variableBeingDefined !== undefined) {
const name = node.getChildAt(0).getText();
object.push(variableBeingDefined + "." + name);
}
}
const maybeExpression = (0, node_naming_1.getExpressionToAccessObjectMember)(node);
if (maybeExpression !== undefined) {
object.push(maybeExpression);
if (variableBeingDefined) {
object.push(variableBeingDefined + "." + maybeExpression);
local.push(variableBeingDefined + "." + maybeExpression);
}
}
return {
local,
object,
};
}
}
exports.Scope = Scope;
//# sourceMappingURL=Scope.js.map