From 775686538da984fcaba4520daa7abaa4e9cbe5b1 Mon Sep 17 00:00:00 2001 From: Daniel Cassidy Date: Thu, 5 Aug 2021 10:01:42 +0100 Subject: [PATCH] fix(eslint-plugin): dot-notation: false positive with optional chaining closes #3510 --- .../eslint-plugin/src/rules/dot-notation.ts | 7 +++---- .../tests/rules/dot-notation.test.ts | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/dot-notation.ts b/packages/eslint-plugin/src/rules/dot-notation.ts index d0757f55a08..becc2a92727 100644 --- a/packages/eslint-plugin/src/rules/dot-notation.ts +++ b/packages/eslint-plugin/src/rules/dot-notation.ts @@ -110,10 +110,9 @@ export default createRule({ const objectType = typeChecker.getTypeAtLocation( esTreeNodeToTSNodeMap.get(node.object), ); - const indexType = typeChecker.getIndexTypeOfType( - objectType, - ts.IndexKind.String, - ); + const indexType = objectType + .getNonNullableType() + .getStringIndexType(); if (indexType != undefined) { return; } diff --git a/packages/eslint-plugin/tests/rules/dot-notation.test.ts b/packages/eslint-plugin/tests/rules/dot-notation.test.ts index cbdb2d52342..fe25e4de1f1 100644 --- a/packages/eslint-plugin/tests/rules/dot-notation.test.ts +++ b/packages/eslint-plugin/tests/rules/dot-notation.test.ts @@ -99,6 +99,25 @@ x['hello'] = 3; `, options: [{ allowIndexSignaturePropertyAccess: true }], }, + { + code: ` +interface Nested { + property: string; + [key: string]: number | string; +} + +class Dingus { + nested: Nested; +} + +let dingus: Dingus | undefined; + +dingus?.nested.property; +dingus?.nested['hello']; + `, + options: [{ allowIndexSignaturePropertyAccess: true }], + parserOptions: { ecmaVersion: 2020 }, + }, ], invalid: [ {