From de7a2db12acf734e1f68b336847d166335c04139 Mon Sep 17 00:00:00 2001 From: James <5511220+Zamiell@users.noreply.github.com> Date: Mon, 18 Sep 2023 11:35:15 -0400 Subject: [PATCH] fix: bug --- .../src/rules/no-unsafe-enum-comparison.ts | 17 ++++++++++------- .../rules/no-unsafe-enum-comparison.test.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) 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 677eb918831f..b5754100fe5a 100644 --- a/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts +++ b/packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts @@ -10,7 +10,6 @@ import { getEnumTypes } from './enum-utils/shared'; */ function typeViolates(leftTypeParts: ts.Type[], right: ts.Type): boolean { const leftValueKinds = new Set(leftTypeParts.map(getEnumValueType)); - return ( (leftValueKinds.has(ts.TypeFlags.Number) && tsutils.isTypeFlagSet( @@ -85,8 +84,10 @@ export default util.createRule({ } } - const leftTypeParts = tsutils.unionTypeParts(left); - const rightTypeParts = tsutils.unionTypeParts(right); + const leftTypeParts1 = tsutils.unionTypeParts(left); + const rightTypeParts1 = tsutils.unionTypeParts(right); + const leftTypeParts2 = tsutils.intersectionTypeParts(left); + const rightTypeParts2 = tsutils.intersectionTypeParts(right); // If a type exists in both sides, we consider this comparison safe: // @@ -94,15 +95,17 @@ export default util.createRule({ // declare const fruit: Fruit.Apple | 0; // fruit === 0; // ``` - for (const leftTypePart of leftTypeParts) { - if (rightTypeParts.includes(leftTypePart)) { + for (const leftTypePart of leftTypeParts1) { + if (rightTypeParts2.includes(leftTypePart)) { return; } } if ( - typeViolates(leftTypeParts, right) || - typeViolates(rightTypeParts, left) + typeViolates(leftTypeParts1, right) || + typeViolates(leftTypeParts2, right) || + typeViolates(rightTypeParts1, right) || + typeViolates(rightTypeParts2, left) ) { context.report({ messageId: 'mismatched', 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 c31b742160ce..82daa94d83cf 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 @@ -565,5 +565,16 @@ ruleTester.run('strict-enums-comparison', rule, { `, errors: [{ messageId: 'mismatched' }], }, + { + code: ` + enum Fruit { + Apple, + } + declare const foo: number & {}; + if (foo === Fruit.Apple) { + } + `, + errors: [{ messageId: 'mismatched' }], + }, ], });