UNPKG

eslint-plugin-sonarjs

Version:
101 lines (100 loc) 4.02 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/S6439/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 detectReactNativeSelector = [ ':matches(', [ 'CallExpression[callee.name="require"][arguments.0.value="react-native"]', 'ImportDeclaration[source.value="react-native"]', ].join(','), ')', ].join(''); exports.rule = { meta: (0, index_js_1.generateMeta)(meta_js_1.meta, { hasSuggestions: true, messages: { nonBooleanMightRender: 'Convert the conditional to a boolean to avoid leaked value', suggestConversion: 'Convert the conditional to a boolean', }, }), create(context) { if (!(0, index_js_1.isRequiredParserServices)(context.sourceCode.parserServices)) { return {}; } let usesReactNative = false; return { [detectReactNativeSelector]() { usesReactNative = true; }, 'JSXExpressionContainer > LogicalExpression[operator="&&"]'(node) { const leftSide = node.left; checkNonBoolean(context, usesReactNative ? isStringOrNumber : isNumber, leftSide); }, }; }, }; function report(node, context) { context.report({ messageId: 'nonBooleanMightRender', node, suggest: [ { messageId: 'suggestConversion', fix: fixer => { const sourceCode = context.sourceCode; const previousToken = sourceCode.getTokenBefore(node); const nextToken = sourceCode.getTokenAfter(node); const fixes = []; if (!!previousToken && !!nextToken && node.range !== undefined && previousToken.value === '(' && previousToken.range[1] <= node.range[0] && nextToken.value === ')' && nextToken.range[0] >= node.range[1]) { fixes.push(fixer.remove(previousToken)); fixes.push(fixer.remove(nextToken)); } fixes.push(fixer.replaceText(node, `!!(${sourceCode.getText(node)})`)); return fixes; }, }, ], }); } function isStringOrNumber(node, context) { const type = (0, index_js_1.getTypeFromTreeNode)(node, context.sourceCode.parserServices); return (0, index_js_1.isStringType)(type) || (0, index_js_1.isBigIntType)(type) || (0, index_js_1.isNumberType)(type); } function isNumber(node, context) { const type = (0, index_js_1.getTypeFromTreeNode)(node, context.sourceCode.parserServices); return (0, index_js_1.isBigIntType)(type) || (0, index_js_1.isNumberType)(type); } function checkNonBoolean(context, isLeakingType, node) { if (node.type === 'LogicalExpression') { checkNonBoolean(context, isLeakingType, node.left); checkNonBoolean(context, isLeakingType, node.right); } else if (isLeakingType(node, context)) { report(node, context); } }