Skip to content

Commit

Permalink
fix(eslint-plugin): [dot-notation] handle noPropertyAccessFromIndexSi…
Browse files Browse the repository at this point in the history
…gnature true (#10644)

* fix(eslint-plugin): [dot-notation] handle noPropertyAccessFromIndexSignature true

* add test cases

* add test cases

* apply reviews

* Add testcases
  • Loading branch information
yeonjuan authored Feb 3, 2025
1 parent 252e11b commit 3e44913
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/eslint-plugin/src/rules/dot-notation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default createRule<Options, MessageIds>({
create(context, [options]) {
const rules = baseRule.create(context);
const services = getParserServices(context);

const checker = services.program.getTypeChecker();
const allowPrivateClassPropertyAccess =
options.allowPrivateClassPropertyAccess;
const allowProtectedClassPropertyAccess =
Expand Down Expand Up @@ -126,11 +126,15 @@ export default createRule<Options, MessageIds>({
return;
}
if (propertySymbol == null && allowIndexSignaturePropertyAccess) {
const objectType = services.getTypeAtLocation(node.object);
const indexType = objectType
.getNonNullableType()
.getStringIndexType();
if (indexType != null) {
const objectType = services
.getTypeAtLocation(node.object)
.getNonNullableType();
const indexInfos = checker.getIndexInfosOfType(objectType);
if (
indexInfos.some(
info => info.keyType.flags & ts.TypeFlags.StringLike,
)
) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noPropertyAccessFromIndexSignature": true
}
}
100 changes: 100 additions & 0 deletions packages/eslint-plugin/tests/rules/dot-notation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,62 @@ console.log(x?.['priv_prop']);
`,
options: [{ allowProtectedClassPropertyAccess: true }],
},
{
code: `
type Foo = {
bar: boolean;
[key: \`key_\${string}\`]: number;
};
declare const foo: Foo;
foo['key_baz'];
`,
languageOptions: {
parserOptions: {
project: './tsconfig.noPropertyAccessFromIndexSignature.json',
projectService: false,
tsconfigRootDir: rootPath,
},
},
},
{
code: `
type Key = Lowercase<string>;
type Foo = {
BAR: boolean;
[key: Lowercase<string>]: number;
};
declare const foo: Foo;
foo['bar'];
`,
languageOptions: {
parserOptions: {
project: './tsconfig.noPropertyAccessFromIndexSignature.json',
projectService: false,
tsconfigRootDir: rootPath,
},
},
},
{
code: `
type ExtraKey = \`extra\${string}\`;
type Foo = {
foo: string;
[extraKey: ExtraKey]: number;
};
function f<T extends Foo>(x: T) {
x['extraKey'];
}
`,
languageOptions: {
parserOptions: {
project: './tsconfig.noPropertyAccessFromIndexSignature.json',
projectService: false,
tsconfigRootDir: rootPath,
},
},
},
],
invalid: [
{
Expand Down Expand Up @@ -384,5 +440,49 @@ const x = new X();
x.prop = 'hello';
`,
},
{
code: `
type Foo = {
bar: boolean;
[key: \`key_\${string}\`]: number;
};
foo['key_baz'];
`,
errors: [{ messageId: 'useDot' }],
output: `
type Foo = {
bar: boolean;
[key: \`key_\${string}\`]: number;
};
foo.key_baz;
`,
},
{
code: `
type ExtraKey = \`extra\${string}\`;
type Foo = {
foo: string;
[extraKey: ExtraKey]: number;
};
function f<T extends Foo>(x: T) {
x['extraKey'];
}
`,
errors: [{ messageId: 'useDot' }],
output: `
type ExtraKey = \`extra\${string}\`;
type Foo = {
foo: string;
[extraKey: ExtraKey]: number;
};
function f<T extends Foo>(x: T) {
x.extraKey;
}
`,
},
],
});

0 comments on commit 3e44913

Please sign in to comment.