UNPKG

eslint-plugin-sonarjs

Version:
126 lines (125 loc) 5.59 kB
"use strict"; /* * 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/S1871 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 message = "This {{type}}'s code block is the same as the block for the {{type}} on line {{line}}."; exports.rule = { meta: (0, index_js_1.generateMeta)(meta_js_1.meta, { messages: { sameConditionalBlock: message, }, }, true), create(context) { return { IfStatement(node) { visitIfStatement(node); }, SwitchStatement(node) { visitSwitchStatement(node); }, }; function visitIfStatement(ifStmt) { if ((0, index_js_1.isIfStatement)(ifStmt.parent)) { return; } const { branches, endsWithElse } = (0, index_js_1.collectIfBranches)(ifStmt); if (allEquivalentWithoutDefault(branches, endsWithElse)) { branches.slice(1).forEach((branch, i) => reportIssue(branch, branches[i], 'branch')); return; } for (let i = 1; i < branches.length; i++) { if (hasRequiredSize([branches[i]])) { for (let j = 0; j < i; j++) { if (compareIfBranches(branches[i], branches[j])) { break; } } } } } function visitSwitchStatement(switchStmt) { const { cases } = switchStmt; const { endsWithDefault } = (0, index_js_1.collectSwitchBranches)(switchStmt); const nonEmptyCases = cases.filter(c => (0, index_js_1.takeWithoutBreak)(expandSingleBlockStatement(c.consequent)).length > 0); const casesWithoutBreak = nonEmptyCases.map(c => (0, index_js_1.takeWithoutBreak)(expandSingleBlockStatement(c.consequent))); if (allEquivalentWithoutDefault(casesWithoutBreak, endsWithDefault)) { nonEmptyCases .slice(1) .forEach((caseStmt, i) => reportIssue(caseStmt, nonEmptyCases[i], 'case')); return; } for (let i = 1; i < cases.length; i++) { const firstClauseWithoutBreak = (0, index_js_1.takeWithoutBreak)(expandSingleBlockStatement(cases[i].consequent)); if (hasRequiredSize(firstClauseWithoutBreak)) { for (let j = 0; j < i; j++) { const secondClauseWithoutBreak = (0, index_js_1.takeWithoutBreak)(expandSingleBlockStatement(cases[j].consequent)); if ((0, index_js_1.areEquivalent)(firstClauseWithoutBreak, secondClauseWithoutBreak, context.sourceCode)) { reportIssue(cases[i], cases[j], 'case'); break; } } } } } function hasRequiredSize(nodes) { if (nodes.length > 0) { const tokens = [ ...context.sourceCode.getTokens(nodes[0]), ...context.sourceCode.getTokens(nodes[nodes.length - 1]), ].filter(token => token.value !== '{' && token.value !== '}'); return (tokens.length > 0 && tokens[tokens.length - 1].loc.end.line > tokens[0].loc.start.line); } return false; } function compareIfBranches(a, b) { const equivalent = (0, index_js_1.areEquivalent)(a, b, context.sourceCode); if (equivalent && b.loc) { reportIssue(a, b, 'branch'); } return equivalent; } function expandSingleBlockStatement(nodes) { if (nodes.length === 1) { const node = nodes[0]; if (node.type === 'BlockStatement') { return node.body; } } return nodes; } function allEquivalentWithoutDefault(branches, endsWithDefault) { return (!endsWithDefault && branches.length > 1 && branches .slice(1) .every((branch, index) => (0, index_js_1.areEquivalent)(branch, branches[index], context.sourceCode))); } function reportIssue(node, equivalentNode, type) { const equivalentNodeLoc = equivalentNode.loc; (0, index_js_1.report)(context, { message, messageId: 'sameConditionalBlock', data: { type, line: String(equivalentNodeLoc.start.line) }, node, }, [(0, index_js_1.toSecondaryLocation)(equivalentNode, 'Original')]); } }, };