diff --git a/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts b/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts index d80ad566449f..59045a512eeb 100644 --- a/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts +++ b/packages/eslint-plugin/tests/rules/strict-boolean-expressions.test.ts @@ -117,6 +117,20 @@ ruleTester.run('strict-boolean-expressions', rule, { (x: T) => x ? 1 : 0; `, }), + + // logical operator + ...batchedSingleLineTests({ + options: [{ allowString: true, allowNumber: true }], + code: ` + 1 && true && "x" && {}; + let x = 0 || false || "" || null; + if (1 && true && "x") {} + if (0 || false || "") {} + 1 && true && "x" ? {} : null; + 0 || false || "" ? null : {}; + `, + }), + { code: ` declare const x: string[] | null; @@ -304,7 +318,7 @@ if (y) { ], }, - // a chain of logical operators should check every operand except the last one in a chain + // shouldn't check last logical operand when used for control flow { options: [{ allowString: false, allowNumber: false }], code: "'asd' && 123 && [] && null;", @@ -325,35 +339,37 @@ if (y) { }, { options: [{ allowString: false, allowNumber: false }], - code: "(1 && 'a' && null) || 0 || '' || {};", + code: "let x = (1 && 'a' && null) || 0 || '' || {};", errors: [ - { messageId: 'conditionErrorNumber', line: 1, column: 2 }, - { messageId: 'conditionErrorString', line: 1, column: 7 }, - { messageId: 'conditionErrorNullish', line: 1, column: 14 }, - { messageId: 'conditionErrorNumber', line: 1, column: 23 }, - { messageId: 'conditionErrorString', line: 1, column: 28 }, + { messageId: 'conditionErrorNumber', line: 1, column: 10 }, + { messageId: 'conditionErrorString', line: 1, column: 15 }, + { messageId: 'conditionErrorNullish', line: 1, column: 22 }, + { messageId: 'conditionErrorNumber', line: 1, column: 31 }, + { messageId: 'conditionErrorString', line: 1, column: 36 }, ], }, { options: [{ allowString: false, allowNumber: false }], - code: "(1 || 'a' || null) && 0 && '' && {};", + code: "return (1 || 'a' || null) && 0 && '' && {};", errors: [ - { messageId: 'conditionErrorNumber', line: 1, column: 2 }, - { messageId: 'conditionErrorString', line: 1, column: 7 }, - { messageId: 'conditionErrorNullish', line: 1, column: 14 }, - { messageId: 'conditionErrorNumber', line: 1, column: 23 }, - { messageId: 'conditionErrorString', line: 1, column: 28 }, + { messageId: 'conditionErrorNumber', line: 1, column: 9 }, + { messageId: 'conditionErrorString', line: 1, column: 14 }, + { messageId: 'conditionErrorNullish', line: 1, column: 21 }, + { messageId: 'conditionErrorNumber', line: 1, column: 30 }, + { messageId: 'conditionErrorString', line: 1, column: 35 }, ], }, { options: [{ allowString: false, allowNumber: false }], - code: "(1 && []) || ('a' && {});", + code: "console.log((1 && []) || ('a' && {}));", errors: [ - { messageId: 'conditionErrorNumber', line: 1, column: 2 }, - { messageId: 'conditionErrorObject', line: 1, column: 7 }, - { messageId: 'conditionErrorString', line: 1, column: 15 }, + { messageId: 'conditionErrorNumber', line: 1, column: 14 }, + { messageId: 'conditionErrorObject', line: 1, column: 19 }, + { messageId: 'conditionErrorString', line: 1, column: 27 }, ], }, + + // should check all logical operands when used in a condition { options: [{ allowString: false, allowNumber: false }], code: "if ((1 && []) || ('a' && {})) void 0;", @@ -364,6 +380,26 @@ if (y) { { messageId: 'conditionErrorObject', line: 1, column: 26 }, ], }, + { + options: [{ allowString: false, allowNumber: false }], + code: "let x = null || 0 || 'a' || [] ? {} : undefined;", + errors: [ + { messageId: 'conditionErrorNullish', line: 1, column: 9 }, + { messageId: 'conditionErrorNumber', line: 1, column: 17 }, + { messageId: 'conditionErrorString', line: 1, column: 22 }, + { messageId: 'conditionErrorObject', line: 1, column: 29 }, + ], + }, + { + options: [{ allowString: false, allowNumber: false }], + code: "return !(null || 0 || 'a' || []);", + errors: [ + { messageId: 'conditionErrorNullish', line: 1, column: 10 }, + { messageId: 'conditionErrorNumber', line: 1, column: 18 }, + { messageId: 'conditionErrorString', line: 1, column: 23 }, + { messageId: 'conditionErrorObject', line: 1, column: 30 }, + ], + }, // nullish in boolean context ...batchedSingleLineTests({