From b3e0e7571f1abb5dae347d3701844324232b1431 Mon Sep 17 00:00:00 2001 From: Melandra Date: Mon, 17 Jul 2023 21:13:37 +0200 Subject: [PATCH] fix(eslint-plugin): [no-unsafe-enum-comparison] exempt bit shift operators (#7074) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: exempt bit shift operators * Add test for other bit shift operator * fix: extend regex to replace if statement * fix: use more precise regex --------- Co-authored-by: Josh Goldberg ✨ --- .../src/rules/no-unsafe-enum-comparison.ts | 2 +- .../tests/rules/no-unsafe-enum-comparison.test.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts b/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts index 0a068b577e7..888924e3bc8 100644 --- a/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts +++ b/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts @@ -63,7 +63,7 @@ export default util.createRule({ } return { - 'BinaryExpression[operator=/=|<|>/]'( + 'BinaryExpression[operator=/^[<>!=]?={0,2}$/]'( node: TSESTree.BinaryExpression, ): void { const left = getTypeFromNode(node.left); diff --git a/packages/eslint-plugin/tests/rules/no-unsafe-enum-comparison.test.ts b/packages/eslint-plugin/tests/rules/no-unsafe-enum-comparison.test.ts index adb3630354e..c31b742160c 100644 --- a/packages/eslint-plugin/tests/rules/no-unsafe-enum-comparison.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unsafe-enum-comparison.test.ts @@ -292,6 +292,20 @@ ruleTester.run('strict-enums-comparison', rule, { num === someFunction; mixed === someFunction; `, + ` + enum Fruit { + Apple, + } + + const bitShift = 1 << Fruit.Apple; + `, + ` + enum Fruit { + Apple, + } + + const bitShift = 1 >> Fruit.Apple; + `, ], invalid: [ {