Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
`,
},
],
});
Loading