From de139e880928aa52c1064d5eddd206d27971c552 Mon Sep 17 00:00:00 2001 From: Omri Luzon Date: Tue, 15 Nov 2022 15:17:52 +0200 Subject: [PATCH 1/2] fix/issue5991-fix-prefer-optional-chain-BinaryExpression-in-negated-or --- packages/eslint-plugin/src/rules/prefer-optional-chain.ts | 4 ++++ .../eslint-plugin/tests/rules/prefer-optional-chain.test.ts | 2 ++ 2 files changed, 6 insertions(+) diff --git a/packages/eslint-plugin/src/rules/prefer-optional-chain.ts b/packages/eslint-plugin/src/rules/prefer-optional-chain.ts index 89eb72094b6..4ff494f394b 100644 --- a/packages/eslint-plugin/src/rules/prefer-optional-chain.ts +++ b/packages/eslint-plugin/src/rules/prefer-optional-chain.ts @@ -407,6 +407,10 @@ export default util.createRule({ propertyText = getMemberExpressionText(node.property); break; + case AST_NODE_TYPES.BinaryExpression: + propertyText = sourceCode.getText(node.property); + break; + /* istanbul ignore next */ default: throw new Error( diff --git a/packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts b/packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts index 58b309edcc8..e1a1467d80f 100644 --- a/packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts @@ -198,6 +198,8 @@ ruleTester.run('prefer-optional-chain', rule, { 'foo && foo[bar as string] && foo[bar as string].baz;', 'foo && foo[1 + 2] && foo[1 + 2].baz;', 'foo && foo[typeof bar] && foo[typeof bar].baz;', + '!foo[1 + 1] || !foo[1 + 2];', + '!foo[1 + 1] || !foo[1 + 1].foo;', '!foo || !foo[bar as string] || !foo[bar as string].baz;', '!foo || !foo[1 + 2] || !foo[1 + 2].baz;', '!foo || !foo[typeof bar] || !foo[typeof bar].baz;', From a9f1a05754ab8737202dc81fca26366b74d5e998 Mon Sep 17 00:00:00 2001 From: Omri Luzon Date: Tue, 15 Nov 2022 20:10:14 +0200 Subject: [PATCH 2/2] CR --- packages/eslint-plugin/src/rules/prefer-optional-chain.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/prefer-optional-chain.ts b/packages/eslint-plugin/src/rules/prefer-optional-chain.ts index 4ff494f394b..73fbcc27af3 100644 --- a/packages/eslint-plugin/src/rules/prefer-optional-chain.ts +++ b/packages/eslint-plugin/src/rules/prefer-optional-chain.ts @@ -400,6 +400,7 @@ export default util.createRule({ case AST_NODE_TYPES.Literal: case AST_NODE_TYPES.TemplateLiteral: + case AST_NODE_TYPES.BinaryExpression: propertyText = sourceCode.getText(node.property); break; @@ -407,10 +408,6 @@ export default util.createRule({ propertyText = getMemberExpressionText(node.property); break; - case AST_NODE_TYPES.BinaryExpression: - propertyText = sourceCode.getText(node.property); - break; - /* istanbul ignore next */ default: throw new Error(