Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba22ea7

Browse files
authored
fix(eslint-plugin): fix autofixer for computed properties (typescript-eslint#1662)
1 parent 608a750 commit ba22ea7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/eslint-plugin/src/rules/no-unnecessary-condition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ export default createRule<Options, MessageId>({
403403
function checkOptionalMemberExpression(
404404
node: TSESTree.OptionalMemberExpression,
405405
): void {
406-
checkOptionalChain(node, node.object, '.');
406+
checkOptionalChain(node, node.object, node.computed ? '' : '.');
407407
}
408408

409409
function checkOptionalCallExpression(

packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ anyValue?.();
222222
let unknownValue: unknown;
223223
unknownValue?.();
224224
`,
225+
'const foo = [1, 2, 3][0];',
225226
],
226227
invalid: [
227228
// Ensure that it's checking in all the right places
@@ -575,5 +576,18 @@ foo
575576
},
576577
],
577578
},
579+
{
580+
code: 'const foo = [1, 2, 3]?.[0];',
581+
output: 'const foo = [1, 2, 3][0];',
582+
errors: [
583+
{
584+
messageId: 'neverOptionalChain',
585+
line: 1,
586+
endLine: 1,
587+
column: 22,
588+
endColumn: 24,
589+
},
590+
],
591+
},
578592
],
579593
});

0 commit comments

Comments
 (0)