Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [dot-notation] false positive with optional chaining #3711

Merged
merged 1 commit into from Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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