eslint-plugin-sonarjs
Version:
SonarJS rules for ESLint
78 lines (77 loc) • 2.94 kB
JavaScript
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* 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/S134/javascript
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = void 0;
const index_js_1 = require("../helpers/index.js");
const meta_js_1 = require("./meta.js");
const DEFAULT_MAXIMUM_NESTING_LEVEL = 3;
exports.rule = {
meta: (0, index_js_1.generateMeta)(meta_js_1.meta, { schema: meta_js_1.schema }, true),
create(context) {
const sourceCode = context.sourceCode;
const threshold = context.options[0]?.maximumNestingLevel ??
DEFAULT_MAXIMUM_NESTING_LEVEL;
const nodeStack = [];
function push(n) {
nodeStack.push(n);
}
function pop() {
return nodeStack.pop();
}
function check(node) {
if (nodeStack.length === threshold) {
(0, index_js_1.report)(context, {
message: `Refactor this code to not nest more than ${threshold} if/for/while/switch/try statements.`,
loc: sourceCode.getFirstToken(node).loc,
}, nodeStack.map(n => (0, index_js_1.toSecondaryLocation)(n, '+1')));
}
}
function isElseIf(node) {
const parent = (0, index_js_1.last)(context.sourceCode.getAncestors(node));
return (node.type === 'IfStatement' && parent.type === 'IfStatement' && node === parent.alternate);
}
const controlFlowNodes = [
'ForStatement',
'ForInStatement',
'ForOfStatement',
'WhileStatement',
'DoWhileStatement',
'IfStatement',
'TryStatement',
'SwitchStatement',
].join(',');
return {
[controlFlowNodes]: (node) => {
if (isElseIf(node)) {
pop();
push(sourceCode.getFirstToken(node));
}
else {
check(node);
push(sourceCode.getFirstToken(node));
}
},
[`${controlFlowNodes}:exit`]: (node) => {
if (!isElseIf(node)) {
pop();
}
},
};
},
};
;