Skip to content

fix(eslint-plugin): [strict-boolean-expressions] support mixed enums in allowNullableEnum option #6740

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,12 @@ export default util.createRule<Options, MessageId>({
is('nullish', 'number', 'enum') ||
is('nullish', 'string', 'enum') ||
is('nullish', 'truthy number', 'enum') ||
is('nullish', 'truthy string', 'enum')
is('nullish', 'truthy string', 'enum') ||
// mixed enums
is('nullish', 'truthy number', 'truthy string', 'enum') ||
is('nullish', 'truthy number', 'string', 'enum') ||
is('nullish', 'truthy string', 'number', 'enum') ||
is('nullish', 'number', 'string', 'enum')
) {
if (!options.allowNullableEnum) {
if (isLogicalNegationExpression(node.parent!)) {
Expand Down
158 changes: 158 additions & 0 deletions packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,53 @@ ruleTester.run('strict-boolean-expressions', rule, {
`,
options: [{ allowNullableEnum: true }],
},

// nullable mixed enum in boolean context
{
// falsy number and truthy string
code: `
enum ExampleEnum {
This = 0,
That = 'one',
}
(value?: ExampleEnum) => (value ? 1 : 0);
`,
options: [{ allowNullableEnum: true }],
},
{
// falsy string and truthy number
code: `
enum ExampleEnum {
This = '',
That = 1,
}
(value?: ExampleEnum) => (!value ? 1 : 0);
`,
options: [{ allowNullableEnum: true }],
},
{
// truthy string and truthy number
code: `
enum ExampleEnum {
This = 'this',
That = 1,
}
(value?: ExampleEnum) => (!value ? 1 : 0);
`,
options: [{ allowNullableEnum: true }],
},
{
// falsy string and falsy number
code: `
enum ExampleEnum {
This = '',
That = 0,
}
(value?: ExampleEnum) => (!value ? 1 : 0);
`,
options: [{ allowNullableEnum: true }],
},

{
code: `
declare const x: string[] | null;
Expand Down Expand Up @@ -1287,6 +1334,117 @@ if (y) {
}
`,
},

// nullable mixed enum in boolean context
{
// falsy number and truthy string
options: [{ allowNullableEnum: false }],
code: `
enum ExampleEnum {
This = 0,
That = 'one',
}
(value?: ExampleEnum) => (value ? 1 : 0);
`,
errors: [
{
line: 6,
column: 35,
messageId: 'conditionErrorNullableEnum',
endLine: 6,
endColumn: 40,
},
],
output: `
enum ExampleEnum {
This = 0,
That = 'one',
}
(value?: ExampleEnum) => ((value != null) ? 1 : 0);
`,
},
{
// falsy string and truthy number
options: [{ allowNullableEnum: false }],
code: `
enum ExampleEnum {
This = '',
That = 1,
}
(value?: ExampleEnum) => (!value ? 1 : 0);
`,
errors: [
{
line: 6,
column: 36,
messageId: 'conditionErrorNullableEnum',
endLine: 6,
endColumn: 41,
},
],
output: `
enum ExampleEnum {
This = '',
That = 1,
}
(value?: ExampleEnum) => ((value == null) ? 1 : 0);
`,
},
{
// truthy string and truthy number
options: [{ allowNullableEnum: false }],
code: `
enum ExampleEnum {
This = 'this',
That = 1,
}
(value?: ExampleEnum) => (!value ? 1 : 0);
`,
errors: [
{
line: 6,
column: 36,
messageId: 'conditionErrorNullableEnum',
endLine: 6,
endColumn: 41,
},
],
output: `
enum ExampleEnum {
This = 'this',
That = 1,
}
(value?: ExampleEnum) => ((value == null) ? 1 : 0);
`,
},
{
// falsy string and falsy number
options: [{ allowNullableEnum: false }],
code: `
enum ExampleEnum {
This = '',
That = 0,
}
(value?: ExampleEnum) => (!value ? 1 : 0);
`,
errors: [
{
line: 6,
column: 36,
messageId: 'conditionErrorNullableEnum',
endLine: 6,
endColumn: 41,
},
],
output: `
enum ExampleEnum {
This = '',
That = 0,
}
(value?: ExampleEnum) => ((value == null) ? 1 : 0);
`,
},

// any in boolean context
...batchedSingleLineTests<MessageId, Options>({
code: noFormat`
Expand Down