Skip to content

fix(eslint-plugin): [no-unsafe-enum-comparison] add logic to see through intersections #9777

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

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions packages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,37 @@ import {
/**
* @returns Whether the right type is an unsafe comparison against any left type.
*/
function typeViolates(leftTypeParts: ts.Type[], right: ts.Type): boolean {
const leftValueKinds = new Set(leftTypeParts.map(getEnumValueType));
function typeViolates(leftTypeParts: ts.Type[], rightType: ts.Type): boolean {
const leftEnumValueTypes = new Set(leftTypeParts.map(getEnumValueType));

return (
(leftValueKinds.has(ts.TypeFlags.Number) &&
tsutils.isTypeFlagSet(
right,
ts.TypeFlags.Number | ts.TypeFlags.NumberLike,
)) ||
(leftValueKinds.has(ts.TypeFlags.String) &&
tsutils.isTypeFlagSet(
right,
ts.TypeFlags.String | ts.TypeFlags.StringLike,
))
(leftEnumValueTypes.has(ts.TypeFlags.Number) && isNumberLike(rightType)) ||
(leftEnumValueTypes.has(ts.TypeFlags.String) && isStringLike(rightType))
);
}

function isNumberLike(type: ts.Type): boolean {
const typeParts = tsutils.intersectionTypeParts(type);

return typeParts.some(typePart => {
return tsutils.isTypeFlagSet(
typePart,
ts.TypeFlags.Number | ts.TypeFlags.NumberLike,
);
});
}

function isStringLike(type: ts.Type): boolean {
const typeParts = tsutils.intersectionTypeParts(type);

return typeParts.some(typePart => {
return tsutils.isTypeFlagSet(
typePart,
ts.TypeFlags.String | ts.TypeFlags.StringLike,
);
});
}

/**
* @returns What type a type's enum value is (number or string), if either.
*/
Expand Down Expand Up @@ -90,6 +104,13 @@ export default createRule({
}
}

// We need to split the type into the union type parts in order to find
// valid enum comparisons like:
//
// ```ts
// declare const something: Fruit | Vegetable;
// something === Fruit.Apple;
// ```
const leftTypeParts = tsutils.unionTypeParts(leftType);
const rightTypeParts = tsutils.unionTypeParts(rightType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,6 @@ ruleTester.run('no-unsafe-enum-comparison', rule, {
declare const right: string | Vegetable2;
left === right;
`,
`
enum Vegetable {
Asparagus = 'asparagus',
Beet = 'beet',
Celery = 'celery',
}
type WeirdString = string & { __someBrand: void };
declare const weirdString: WeirdString;
Vegetable.Asparagus === weirdString;
`,
`
enum Vegetable {
Asparagus = 'asparagus',
Expand Down Expand Up @@ -1212,5 +1202,49 @@ ruleTester.run('no-unsafe-enum-comparison', rule, {
},
],
},
{
code: `
enum Fruit {
Apple,
}
declare const foo: number & {};
if (foo === Fruit.Apple) {
}
`,
errors: [{ messageId: 'mismatchedCondition' }],
},
{
code: `
enum Fruit {
Apple,
}
declare const foo: number & { __someBrand: void };
if (foo === Fruit.Apple) {
}
`,
errors: [{ messageId: 'mismatchedCondition' }],
},
{
code: `
enum Vegetable {
Asparagus = 'asparagus',
}
declare const foo: string & {};
if (foo === Vegetable.Asparagus) {
}
`,
errors: [{ messageId: 'mismatchedCondition' }],
},
{
code: `
enum Vegetable {
Asparagus = 'asparagus',
}
declare const foo: string & { __someBrand: void };
if (foo === Vegetable.Asparagus) {
}
`,
errors: [{ messageId: 'mismatchedCondition' }],
},
],
});
Loading