Skip to content

Commit

Permalink
fix(eslint-plugin): [strict-boolean-expressions] support mixed enums …
Browse files Browse the repository at this point in the history
…in allowNullableEnum option (#6740)

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
kozlovvski and JoshuaKGoldberg committed Apr 2, 2023
1 parent 4256c10 commit 49be8a8
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 1 deletion.
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
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

0 comments on commit 49be8a8

Please sign in to comment.