From ba22ea7f604b236828ce4dcff75831ec1da01ec1 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Tue, 3 Mar 2020 10:52:40 +0200 Subject: [PATCH] fix(eslint-plugin): fix autofixer for computed properties (#1662) --- .../src/rules/no-unnecessary-condition.ts | 2 +- .../tests/rules/no-unnecessary-condition.test.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts index 22a9fed5ba6..45af1e80706 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts @@ -403,7 +403,7 @@ export default createRule({ function checkOptionalMemberExpression( node: TSESTree.OptionalMemberExpression, ): void { - checkOptionalChain(node, node.object, '.'); + checkOptionalChain(node, node.object, node.computed ? '' : '.'); } function checkOptionalCallExpression( diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts index 0728b9202b9..5af22f00ca0 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -222,6 +222,7 @@ anyValue?.(); let unknownValue: unknown; unknownValue?.(); `, + 'const foo = [1, 2, 3][0];', ], invalid: [ // Ensure that it's checking in all the right places @@ -575,5 +576,18 @@ foo }, ], }, + { + code: 'const foo = [1, 2, 3]?.[0];', + output: 'const foo = [1, 2, 3][0];', + errors: [ + { + messageId: 'neverOptionalChain', + line: 1, + endLine: 1, + column: 22, + endColumn: 24, + }, + ], + }, ], });