UNPKG

eslint-plugin-sonarjs

Version:
102 lines (101 loc) 4.58 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/S4623/javascript var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.rule = void 0; const index_js_1 = require("../helpers/index.js"); const typescript_1 = __importDefault(require("typescript")); const meta_js_1 = require("./meta.js"); exports.rule = { meta: (0, index_js_1.generateMeta)(meta_js_1.meta, { hasSuggestions: true, messages: { removeUndefined: 'Remove this redundant "undefined".', suggestRemoveUndefined: 'Remove this redundant argument', }, }), create(context) { const services = context.sourceCode.parserServices; if ((0, index_js_1.isRequiredParserServices)(services)) { return { CallExpression: (node) => { const call = node; const { arguments: args } = call; if (args.length === 0) { return; } const lastArgument = args[args.length - 1]; if ((0, index_js_1.isUndefined)(lastArgument) && isOptionalParameter(args.length - 1, call, services)) { context.report({ messageId: 'removeUndefined', node: lastArgument, suggest: [ { messageId: 'suggestRemoveUndefined', fix: fixer => { if (call.arguments.length === 1) { const openingParen = context.sourceCode.getTokenAfter(call.callee); const closingParen = context.sourceCode.getLastToken(node); const [, begin] = openingParen.range; const [end] = closingParen.range; return fixer.removeRange([begin, end]); } else { const [, begin] = args[args.length - 2].range; const [, end] = lastArgument.range; return fixer.removeRange([begin, end]); } }, }, ], }); } }, }; } return {}; }, }; function isOptionalParameter(paramIndex, node, services) { const signature = services.program .getTypeChecker() .getResolvedSignature(services.esTreeNodeToTSNodeMap.get(node)); if (signature) { const declaration = signature.declaration; if (declaration && isFunctionLikeDeclaration(declaration)) { const { parameters } = declaration; const parameter = parameters[paramIndex]; return parameter && (parameter.initializer || parameter.questionToken); } } return false; } function isFunctionLikeDeclaration(declaration) { return [ typescript_1.default.SyntaxKind.FunctionDeclaration, typescript_1.default.SyntaxKind.FunctionExpression, typescript_1.default.SyntaxKind.ArrowFunction, typescript_1.default.SyntaxKind.MethodDeclaration, typescript_1.default.SyntaxKind.Constructor, typescript_1.default.SyntaxKind.GetAccessor, typescript_1.default.SyntaxKind.SetAccessor, ].includes(declaration.kind); }