eslint-plugin-sonarjs
Version:
101 lines (100 loc) • 5.11 kB
JavaScript
;
/*
* SonarQube JavaScript Plugin
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
// https://sonarsource.github.io/rspec/#/rspec/S1541/javascript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const generate_meta_js_1 = require("../helpers/generate-meta.js");
const complexity_js_1 = require("./complexity.js");
const immediate_invocation_js_1 = require("./immediate-invocation.js");
const module_initializers_js_1 = require("./module-initializers.js");
const helpers_js_1 = require("./helpers.js");
const meta = __importStar(require("./generated-meta.js"));
const DEFAULT_THRESHOLD = 10;
exports.rule = {
meta: (0, generate_meta_js_1.generateMeta)(meta),
create(context) {
const threshold = context.options[0]?.threshold ?? DEFAULT_THRESHOLD;
const complexity = new complexity_js_1.ComplexityTracker(context);
const moduleInitializers = new module_initializers_js_1.ModuleInitializerTracker((node, name) => (0, helpers_js_1.hasRuntimeBinding)(context, node, name));
const immediatelyExecuted = new Set();
function excludeIfExecuted(functionNode) {
if (functionNode) {
immediatelyExecuted.add(functionNode);
}
}
return {
'FunctionDeclaration, FunctionExpression, ArrowFunctionExpression': (node) => complexity.enterFunction(node),
':matches(FunctionDeclaration, FunctionExpression, ArrowFunctionExpression):exit': () => complexity.leaveFunction(),
'ConditionalExpression, LogicalExpression, SwitchCase, IfStatement, ForStatement, ForInStatement, ForOfStatement, WhileStatement, DoWhileStatement': (node) => complexity.addBranch(node),
// File-wide writes can invalidate a loader exception after its call.
AssignmentExpression: (node) => moduleInitializers.recordWrite(node.left),
UpdateExpression: (node) => moduleInitializers.recordWrite(node.argument),
UnaryExpression: (node) => {
const unary = node;
if (unary.operator === 'delete') {
moduleInitializers.recordWrite(unary.argument);
}
},
'ForInStatement, ForOfStatement': (node) => moduleInitializers.recordWrite(node.left),
CallExpression: (node) => {
const call = node;
moduleInitializers.recordCall(call);
excludeIfExecuted((0, immediate_invocation_js_1.getImmediatelyInvokedFunction)(call.callee));
},
NewExpression: (node) => excludeIfExecuted((0, immediate_invocation_js_1.getImmediatelyConstructedFunction)(node.callee)),
TaggedTemplateExpression: (node) => excludeIfExecuted((0, immediate_invocation_js_1.getImmediatelyInvokedFunction)(node.tag)),
'Program:exit': () => {
const moduleFactories = moduleInitializers.getExcludedFactories(node => complexity.hasNestedFunction(node));
complexity.reportOverThreshold(threshold, node => immediatelyExecuted.has(node) || moduleFactories.has(node));
},
};
},
};