Skip to content
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

fix(eslint-plugin): [strict-boolean-expressions] handle truthy enums #6618

Merged
merged 1 commit into from
Mar 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,12 @@ export default util.createRule<Options, MessageId>({
}

// nullable enum
if (is('nullish', 'number', 'enum') || is('nullish', 'string', 'enum')) {
if (
is('nullish', 'number', 'enum') ||
is('nullish', 'string', 'enum') ||
is('nullish', 'truthy number', 'enum') ||
is('nullish', 'truthy string', 'enum')
) {
if (!options.allowNullableEnum) {
if (isLogicalNegationExpression(node.parent!)) {
context.report({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ ruleTester.run('strict-boolean-expressions', rule, {
`,
options: [{ allowNullableEnum: true }],
},
{
code: `
enum ExampleEnum {
This = 1,
That = 2,
}
const rand = Math.random();
let theEnum: ExampleEnum | null = null;
if (rand < 0.3) {
theEnum = ExampleEnum.This;
}
if (!theEnum) {
}
`,
options: [{ allowNullableEnum: true }],
},
{
code: `
enum ExampleEnum {
This = 'one',
That = 'two',
}
const rand = Math.random();
let theEnum: ExampleEnum | null = null;
if (rand < 0.3) {
theEnum = ExampleEnum.This;
}
if (!theEnum) {
}
`,
options: [{ allowNullableEnum: true }],
},
{
code: `
declare const x: string[] | null;
Expand Down Expand Up @@ -1149,6 +1181,66 @@ if (y) {
}
`,
},
{
options: [{ allowNullableEnum: false }],
code: `
enum ExampleEnum {
This = 'one',
That = 'two',
}
const theEnum = Math.random() < 0.3 ? ExampleEnum.This : null;
if (!theEnum) {
}
`,
errors: [
{
line: 7,
column: 14,
messageId: 'conditionErrorNullableEnum',
endLine: 7,
endColumn: 21,
},
],
output: `
enum ExampleEnum {
This = 'one',
That = 'two',
}
const theEnum = Math.random() < 0.3 ? ExampleEnum.This : null;
if (theEnum == null) {
}
`,
},
{
options: [{ allowNullableEnum: false }],
code: `
enum ExampleEnum {
This = 1,
That = 2,
}
const theEnum = Math.random() < 0.3 ? ExampleEnum.This : null;
if (!theEnum) {
}
`,
errors: [
{
line: 7,
column: 14,
messageId: 'conditionErrorNullableEnum',
endLine: 7,
endColumn: 21,
},
],
output: `
enum ExampleEnum {
This = 1,
That = 2,
}
const theEnum = Math.random() < 0.3 ? ExampleEnum.This : null;
if (theEnum == null) {
}
`,
},
// any in boolean context
...batchedSingleLineTests<MessageId, Options>({
code: noFormat`
Expand Down