Skip to content

Commit

Permalink
fix(eslint-plugin): [dot-notation] false positive with optional chain…
Browse files Browse the repository at this point in the history
…ing (#3711)

closes #3510
  • Loading branch information
djcsdy committed Aug 30, 2021
1 parent 71dd273 commit c19fc6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions packages/eslint-plugin/src/rules/dot-notation.ts
Expand Up @@ -110,10 +110,9 @@ export default createRule<Options, MessageIds>({
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;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-plugin/tests/rules/dot-notation.test.ts
Expand Up @@ -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: [
{
Expand Down

0 comments on commit c19fc6e

Please sign in to comment.