Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-unsafe-enum-comparison] exempt bit shift operators #7074

13 changes: 13 additions & 0 deletions packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts
Expand Up @@ -66,6 +66,19 @@ export default util.createRule({
'BinaryExpression[operator=/=|<|>/]'(
Copy link
Member

@Josh-Cena Josh-Cena May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'BinaryExpression[operator=/=|<|>/]'(
'BinaryExpression[operator=/^(<>!)?={0,2}$/]'(

...Much simpler? Otherwise you will also need to remember to deal with >>>.

Copy link
Contributor Author

@Melandra Melandra May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, please see 68938c5, I've replaced the regex with ^[<>!=]?={0,2}$ instead to also match ===

node: TSESTree.BinaryExpression,
): void {
// Exempt bit shift operators since they are not comparisons
//
// ```ts
// enum Fruit {
// Apple,
// }
//
// const myBitShift = 1 << Fruit.Apple;
// ```
Melandra marked this conversation as resolved.
Show resolved Hide resolved
if (node.operator === '>>' || node.operator === '<<') {
Melandra marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const left = getTypeFromNode(node.left);
const right = getTypeFromNode(node.right);

Expand Down
Expand Up @@ -290,6 +290,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: [
{
Expand Down