|
| 1 | +/* |
| 2 | + * eslint-plugin-sonarjs |
| 3 | + * Copyright (C) 2018 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +import { RuleTester } from "eslint"; |
| 21 | + |
| 22 | +const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } }); |
| 23 | +import rule = require("../../src/rules/no-inverted-boolean-check"); |
| 24 | + |
| 25 | +ruleTester.run("no-inverted-boolean-check", rule, { |
| 26 | + valid: [ |
| 27 | + { |
| 28 | + code: `if (!x) {}`, |
| 29 | + }, |
| 30 | + { |
| 31 | + code: `if (x == 1) {}`, |
| 32 | + }, |
| 33 | + { |
| 34 | + code: `if (!(x + 1)) {}`, |
| 35 | + }, |
| 36 | + { |
| 37 | + code: `if (+(x == 1)) {}`, |
| 38 | + }, |
| 39 | + { |
| 40 | + code: `!x ? 2 : 3`, |
| 41 | + }, |
| 42 | + ], |
| 43 | + invalid: [ |
| 44 | + // `==` => `!=` |
| 45 | + { |
| 46 | + code: `if (!(x == 1)) {}`, |
| 47 | + errors: [ |
| 48 | + { |
| 49 | + message: message("!="), |
| 50 | + line: 1, |
| 51 | + endLine: 1, |
| 52 | + column: 5, |
| 53 | + endColumn: 14, |
| 54 | + }, |
| 55 | + ], |
| 56 | + output: `if (x != 1) {}`, |
| 57 | + }, |
| 58 | + // `!=` => `==` |
| 59 | + { |
| 60 | + code: `if (!(x != 1)) {}`, |
| 61 | + errors: [message("==")], |
| 62 | + output: `if (x == 1) {}`, |
| 63 | + }, |
| 64 | + // `===` => `!==` |
| 65 | + { |
| 66 | + code: `if (!(x === 1)) {}`, |
| 67 | + errors: [message("!==")], |
| 68 | + output: `if (x !== 1) {}`, |
| 69 | + }, |
| 70 | + // `!==` => `===` |
| 71 | + { |
| 72 | + code: `if (!(x !== 1)) {}`, |
| 73 | + errors: [message("===")], |
| 74 | + output: `if (x === 1) {}`, |
| 75 | + }, |
| 76 | + // `>` => `<=` |
| 77 | + { |
| 78 | + code: `if (!(x > 1)) {}`, |
| 79 | + errors: [message("<=")], |
| 80 | + output: `if (x <= 1) {}`, |
| 81 | + }, |
| 82 | + // `<` => `>=` |
| 83 | + { |
| 84 | + code: `if (!(x < 1)) {}`, |
| 85 | + errors: [message(">=")], |
| 86 | + output: `if (x >= 1) {}`, |
| 87 | + }, |
| 88 | + // `>=` => `<` |
| 89 | + { |
| 90 | + code: `if (!(x >= 1)) {}`, |
| 91 | + errors: [message("<")], |
| 92 | + output: `if (x < 1) {}`, |
| 93 | + }, |
| 94 | + // `<=` => `>` |
| 95 | + { |
| 96 | + code: `if (!(x <= 1)) {}`, |
| 97 | + errors: [message(">")], |
| 98 | + output: `if (x > 1) {}`, |
| 99 | + }, |
| 100 | + // ternary operator |
| 101 | + { |
| 102 | + code: `!(x != 1) ? 1 : 2`, |
| 103 | + errors: [message("==")], |
| 104 | + output: `x == 1 ? 1 : 2`, |
| 105 | + }, |
| 106 | + // not conditional |
| 107 | + { |
| 108 | + code: `foo(!(x === 1))`, |
| 109 | + errors: [message("!==")], |
| 110 | + output: `foo(x !== 1)`, |
| 111 | + }, |
| 112 | + { |
| 113 | + code: `let foo = !(x <= 4)`, |
| 114 | + errors: [message(">")], |
| 115 | + output: `let foo = x > 4`, |
| 116 | + }, |
| 117 | + ], |
| 118 | +}); |
| 119 | + |
| 120 | +function message(oppositeOperator: string) { |
| 121 | + return `Use the opposite operator (${oppositeOperator}) instead.`; |
| 122 | +} |
0 commit comments