Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(eslint-plugin): [no-unsafe-enum-comparison] support unions of literals#11599

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
36 changes: 22 additions & 14 deletionspackages/eslint-plugin/src/rules/no-unsafe-enum-comparison.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,25 +23,33 @@ function typeViolates(leftTypeParts: ts.Type[], rightType: ts.Type): boolean {
}

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

return typeParts.some(typePart => {
return tsutils.isTypeFlagSet(
typePart,
ts.TypeFlags.Number | ts.TypeFlags.NumberLike,
return tsutils
.unionConstituents(type)
.every(unionPart =>
tsutils
.intersectionConstituents(unionPart)
.some(intersectionPart =>
tsutils.isTypeFlagSet(
intersectionPart,
ts.TypeFlags.Number | ts.TypeFlags.NumberLike,
),
),
);
});
}

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

return typeParts.some(typePart => {
return tsutils.isTypeFlagSet(
typePart,
ts.TypeFlags.String | ts.TypeFlags.StringLike,
return tsutils
.unionConstituents(type)
.every(unionPart =>
tsutils
.intersectionConstituents(unionPart)
.some(intersectionPart =>
tsutils.isTypeFlagSet(
intersectionPart,
ts.TypeFlags.String | ts.TypeFlags.StringLike,
),
),
);
});
}

/**
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1165,5 +1165,81 @@ ruleTester.run('no-unsafe-enum-comparison', rule, {
`,
errors: [{ messageId: 'mismatchedCondition' }],
},
{
code: `
enum NUMBER_ENUM {
First = 0,
Second = 1,
}

type NumberUnion = 0 | 1;

declare const numberUnion: NumberUnion;

switch (numberUnion) {
case NUMBER_ENUM.First:
case NUMBER_ENUM.Second:
break;
}
`,
errors: [
{
line: 12,
messageId: 'mismatchedCase',
},
{
line: 13,
messageId: 'mismatchedCase',
},
],
},
{
code: `
enum STRING_ENUM {
First = 'one',
Second = 'two',
}

type StringUnion = 'one' | 'two';

declare const stringUnion: StringUnion;

switch (stringUnion) {
case STRING_ENUM.First:
case STRING_ENUM.Second:
break;
}
`,
errors: [
{
line: 12,
messageId: 'mismatchedCase',
},
{
line: 13,
messageId: 'mismatchedCase',
},
],
},
{
code: `
declare const stringUnion: 'foo' | 'bar';

enum StringEnum {
FOO = 'foo',
BAR = 'bar',
}

declare const stringEnum: StringEnum;

stringUnion === stringEnum;
`,
errors: [
{
line: 11,
messageId: 'mismatchedCondition',
},
],
},
],
});
Loading

[8]ページ先頭

©2009-2025 Movatter.jp