|
| 1 | +/* |
| 2 | + * SonarQube JavaScript Plugin |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * You can redistribute and/or modify this program under the terms of |
| 7 | + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +import type { Rule } from 'eslint'; |
| 18 | +import type estree from 'estree'; |
| 19 | +import type { TSESTree } from '@typescript-eslint/utils'; |
| 20 | +import ts from 'typescript'; |
| 21 | +import { generateMeta } from '../helpers/generate-meta.js'; |
| 22 | +import { interceptReport } from '../helpers/decorators/interceptor.js'; |
| 23 | +import { |
| 24 | + isRequiredParserServices, |
| 25 | + type RequiredParserServices, |
| 26 | +} from '../helpers/parser-services.js'; |
| 27 | +import { getTypeFromTreeNode } from '../helpers/type.js'; |
| 28 | +import * as meta from './generated-meta.js'; |
| 29 | + |
| 30 | +export function decorate(rule: Rule.RuleModule): Rule.RuleModule { |
| 31 | + return interceptReport( |
| 32 | + { |
| 33 | + ...rule, |
| 34 | + meta: generateMeta(meta, rule.meta), |
| 35 | + }, |
| 36 | + reportExempting, |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +function reportExempting(context: Rule.RuleContext, descriptor: Rule.ReportDescriptor) { |
| 41 | + if (!('node' in descriptor)) { |
| 42 | + context.report(descriptor); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const services = context.sourceCode.parserServices; |
| 47 | + if (!isRequiredParserServices(services)) { |
| 48 | + // No TypeScript type information available; pass through unchanged (conservative fallback) |
| 49 | + context.report(descriptor); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const { node } = descriptor; |
| 54 | + const tsNode = node as TSESTree.Node; |
| 55 | + const parent = tsNode.parent; |
| 56 | + |
| 57 | + // Determine the callee node whose call signatures we'll inspect: |
| 58 | + // - method call (push, add, remove): the MemberExpression is the callee |
| 59 | + // - direct function call (importScripts): the reported Identifier is the callee |
| 60 | + let callee: TSESTree.Node; |
| 61 | + if (parent?.type === 'MemberExpression') { |
| 62 | + callee = parent; |
| 63 | + } else if (parent?.type === 'CallExpression') { |
| 64 | + callee = tsNode; |
| 65 | + } else { |
| 66 | + context.report(descriptor); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (calleeAcceptsMultipleArguments(callee, services)) { |
| 71 | + context.report(descriptor); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Returns true if the callee can accept more than one argument. |
| 77 | + * When signatures are unresolved (empty), returns true as a conservative fallback |
| 78 | + * to avoid false negatives. Single-argument callees with resolved signatures are |
| 79 | + * suppressed as false positives. |
| 80 | + */ |
| 81 | +function calleeAcceptsMultipleArguments( |
| 82 | + callee: TSESTree.Node, |
| 83 | + services: RequiredParserServices, |
| 84 | +): boolean { |
| 85 | + const calleeType = getTypeFromTreeNode(callee as unknown as estree.Node, services); |
| 86 | + const signatures = calleeType.getCallSignatures(); |
| 87 | + // If signatures cannot be resolved, fall back to reporting (conservative behavior) |
| 88 | + if (signatures.length === 0) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + return signatures.some(sig => { |
| 92 | + const params = sig.parameters; |
| 93 | + const lastParam = params.at(-1); |
| 94 | + if (lastParam === undefined) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + const decl = lastParam.valueDeclaration; |
| 98 | + return ( |
| 99 | + (decl !== undefined && ts.isParameter(decl) && !!decl.dotDotDotToken) || params.length > 1 |
| 100 | + ); |
| 101 | + }); |
| 102 | +} |
0 commit comments