Skip to content

Commit 3c7ac73

Browse files
authored
Merge pull request #41793 from Kingwl/noPropertyAccessFromIndexSignature_fix
No property access from index signature fix
2 parents 387b6dc + 870f5b6 commit 3c7ac73

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25904,7 +25904,7 @@ namespace ts {
2590425904

2590525905
propType = (compilerOptions.noUncheckedIndexedAccess && !isAssignmentTarget(node)) ? getUnionType([indexInfo.type, undefinedType]) : indexInfo.type;
2590625906
if (compilerOptions.noPropertyAccessFromIndexSignature && isPropertyAccessExpression(node)) {
25907-
error(node, Diagnostics.Property_0_comes_from_an_index_signature_so_it_must_be_accessed_with_0, unescapeLeadingUnderscores(right.escapedText));
25907+
error(right, Diagnostics.Property_0_comes_from_an_index_signature_so_it_must_be_accessed_with_0, unescapeLeadingUnderscores(right.escapedText));
2590825908
}
2590925909
}
2591025910
else {

src/compiler/commandLineParser.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,6 @@ namespace ts {
662662
{
663663
name: "noPropertyAccessFromIndexSignature",
664664
type: "boolean",
665-
affectsBindDiagnostics: true,
666-
affectsSemanticDiagnostics: true,
667665
showInSimplifiedHelpView: false,
668666
category: Diagnostics.Additional_Checks,
669667
description: Diagnostics.Require_undeclared_properties_from_index_signatures_to_use_element_accesses

src/services/codefixes/fixNoPropertyAccessFromIndexSignature.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ namespace ts.codefix {
99
errorCodes,
1010
fixIds: [fixId],
1111
getCodeActions(context) {
12-
const { sourceFile, span } = context;
12+
const { sourceFile, span, preferences } = context;
1313
const property = getPropertyAccessExpression(sourceFile, span.start);
14-
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property));
14+
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property, preferences));
1515
return [createCodeFixAction(fixId, changes, [Diagnostics.Use_element_access_for_0, property.name.text], fixId, Diagnostics.Use_element_access_for_all_undeclared_properties)];
1616
},
1717
getAllCodeActions: context =>
18-
codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getPropertyAccessExpression(diag.file, diag.start)))
18+
codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getPropertyAccessExpression(diag.file, diag.start), context.preferences))
1919
});
2020

21-
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: PropertyAccessExpression): void {
22-
const argumentsExpression = factory.createStringLiteral(node.name.text);
21+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: PropertyAccessExpression, preferences: UserPreferences): void {
22+
const quotePreference = getQuotePreference(sourceFile, preferences);
23+
const argumentsExpression = factory.createStringLiteral(node.name.text, quotePreference === QuotePreference.Single);
2324
changes.replaceNode(
2425
sourceFile,
2526
node,

tests/baselines/reference/noPropertyAccessFromIndexSignature1.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(24,1): error TS4111: Property 'foo' comes from an index signature, so it must be accessed with ['foo'].
2-
tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(32,1): error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
3-
tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(40,1): error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
1+
tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(24,3): error TS4111: Property 'foo' comes from an index signature, so it must be accessed with ['foo'].
2+
tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(32,3): error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
3+
tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(40,4): error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
44

55

66
==== tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts (3 errors) ====
@@ -28,7 +28,7 @@ tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(
2828

2929
// access index signature
3030
b.foo;
31-
~~~~~
31+
~~~
3232
!!! error TS4111: Property 'foo' comes from an index signature, so it must be accessed with ['foo'].
3333
b["foo"];
3434

@@ -38,7 +38,7 @@ tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(
3838

3939
// access index signature
4040
c.bar;
41-
~~~~~
41+
~~~
4242
!!! error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
4343
c["bar"];
4444

@@ -48,7 +48,7 @@ tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(
4848

4949
// optional access index signature
5050
d?.bar;
51-
~~~~~~
51+
~~~
5252
!!! error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
5353
d?.["bar"];
5454

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @noPropertyAccessFromIndexSignature: true
4+
//// declare let x: { y: { [x: string]: string } };
5+
//// x.y.yadda;
6+
7+
verify.codeFix({
8+
description: [ts.Diagnostics.Use_element_access_for_0.message, 'yadda'],
9+
index: 0,
10+
newFileContent:
11+
`declare let x: { y: { [x: string]: string } };
12+
x.y["yadda"];`,
13+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @noPropertyAccessFromIndexSignature: true
4+
//// declare let x: { y: { [x: string]: string } };
5+
//// x.y.yadda;
6+
7+
verify.codeFix({
8+
description: [ts.Diagnostics.Use_element_access_for_0.message, 'yadda'],
9+
index: 0,
10+
preferences: {
11+
quotePreference: 'single'
12+
},
13+
newFileContent:
14+
`declare let x: { y: { [x: string]: string } };
15+
x.y['yadda'];`,
16+
});

0 commit comments

Comments
 (0)