Skip to content

Commit e82698c

Browse files
authored
fix(eslint-plugin): [no-extra-non-null-assertion] false positive with non-nullable computed key (typescript-eslint#2737)
1 parent c8a4dad commit e82698c

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

packages/eslint-plugin/src/rules/no-extra-non-null-assertion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default util.createRule({
3232

3333
return {
3434
'TSNonNullExpression > TSNonNullExpression': checkExtraNonNullAssertion,
35-
'MemberExpression[optional = true] > TSNonNullExpression': checkExtraNonNullAssertion,
35+
'MemberExpression[optional = true] > TSNonNullExpression.object': checkExtraNonNullAssertion,
3636
'CallExpression[optional = true] > TSNonNullExpression.callee': checkExtraNonNullAssertion,
3737
};
3838
},

packages/eslint-plugin/tests/rules/no-extra-non-null-assertion.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ function foo(bar?: { n: number }) {
3333
checksCounter?.textContent!.trim();
3434
`,
3535
},
36+
// https://github.com/typescript-eslint/typescript-eslint/issues/2732
37+
{
38+
code: `
39+
function foo(key: string | null) {
40+
const obj = {};
41+
return obj?.[key!];
42+
}
43+
`,
44+
},
3645
],
3746
invalid: [
3847
{

0 commit comments

Comments
 (0)