Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 7 commits intotypescript-eslint:mainfromyeonjuan:fix/10627
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/eslint-plugin/src/rules/dot-notation.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noPropertyAccessFromIndexSignature": true
}
}
100 changes: 100 additions & 0 deletionspackages/eslint-plugin/tests/rules/dot-notation.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp