diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
index 413360d6d2a..6ef9a7525fd 100644
--- a/src/compiler/checker.ts
+++ b/src/compiler/checker.ts
@@ -25904,7 +25904,7 @@ namespace ts {
propType = (compilerOptions.noUncheckedIndexedAccess && !isAssignmentTarget(node)) ? getUnionType([indexInfo.type, undefinedType]) : indexInfo.type;
if (compilerOptions.noPropertyAccessFromIndexSignature && isPropertyAccessExpression(node)) {
- error(node, Diagnostics.Property_0_comes_from_an_index_signature_so_it_must_be_accessed_with_0, unescapeLeadingUnderscores(right.escapedText));
+ error(right, Diagnostics.Property_0_comes_from_an_index_signature_so_it_must_be_accessed_with_0, unescapeLeadingUnderscores(right.escapedText));
}
}
else {
diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts
index 51cf7923a10..61a3a43a81e 100644
--- a/src/compiler/commandLineParser.ts
+++ b/src/compiler/commandLineParser.ts
@@ -662,8 +662,6 @@ namespace ts {
{
name: "noPropertyAccessFromIndexSignature",
type: "boolean",
- affectsBindDiagnostics: true,
- affectsSemanticDiagnostics: true,
showInSimplifiedHelpView: false,
category: Diagnostics.Additional_Checks,
description: Diagnostics.Require_undeclared_properties_from_index_signatures_to_use_element_accesses
diff --git a/src/services/codefixes/fixNoPropertyAccessFromIndexSignature.ts b/src/services/codefixes/fixNoPropertyAccessFromIndexSignature.ts
index 7ac06f99635..4563731e01c 100644
--- a/src/services/codefixes/fixNoPropertyAccessFromIndexSignature.ts
+++ b/src/services/codefixes/fixNoPropertyAccessFromIndexSignature.ts
@@ -9,17 +9,18 @@ namespace ts.codefix {
errorCodes,
fixIds: [fixId],
getCodeActions(context) {
- const { sourceFile, span } = context;
+ const { sourceFile, span, preferences } = context;
const property = getPropertyAccessExpression(sourceFile, span.start);
- const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property));
+ const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, property, preferences));
return [createCodeFixAction(fixId, changes, [Diagnostics.Use_element_access_for_0, property.name.text], fixId, Diagnostics.Use_element_access_for_all_undeclared_properties)];
},
getAllCodeActions: context =>
- codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getPropertyAccessExpression(diag.file, diag.start)))
+ codeFixAll(context, errorCodes, (changes, diag) => doChange(changes, diag.file, getPropertyAccessExpression(diag.file, diag.start), context.preferences))
});
- function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: PropertyAccessExpression): void {
- const argumentsExpression = factory.createStringLiteral(node.name.text);
+ function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: PropertyAccessExpression, preferences: UserPreferences): void {
+ const quotePreference = getQuotePreference(sourceFile, preferences);
+ const argumentsExpression = factory.createStringLiteral(node.name.text, quotePreference === QuotePreference.Single);
changes.replaceNode(
sourceFile,
node,
diff --git a/tests/baselines/reference/noPropertyAccessFromIndexSignature1.errors.txt b/tests/baselines/reference/noPropertyAccessFromIndexSignature1.errors.txt
index bb6fb9b84ba..91bcb439f5b 100644
--- a/tests/baselines/reference/noPropertyAccessFromIndexSignature1.errors.txt
+++ b/tests/baselines/reference/noPropertyAccessFromIndexSignature1.errors.txt
@@ -1,6 +1,6 @@
-tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(24,1): error TS4111: Property 'foo' comes from an index signature, so it must be accessed with ['foo'].
-tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(32,1): error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
-tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(40,1): error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
+tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(24,3): error TS4111: Property 'foo' comes from an index signature, so it must be accessed with ['foo'].
+tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(32,3): error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
+tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(40,4): error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
==== tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts (3 errors) ====
@@ -28,7 +28,7 @@ tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(
// access index signature
b.foo;
- ~~~~~
+ ~~~
!!! error TS4111: Property 'foo' comes from an index signature, so it must be accessed with ['foo'].
b["foo"];
@@ -38,7 +38,7 @@ tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(
// access index signature
c.bar;
- ~~~~~
+ ~~~
!!! error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
c["bar"];
@@ -48,7 +48,7 @@ tests/cases/conformance/additionalChecks/noPropertyAccessFromIndexSignature1.ts(
// optional access index signature
d?.bar;
- ~~~~~~
+ ~~~
!!! error TS4111: Property 'bar' comes from an index signature, so it must be accessed with ['bar'].
d?.["bar"];
\ No newline at end of file
diff --git a/tests/cases/fourslash/codeFixNoPropertyAccessFromIndexSignature4.ts b/tests/cases/fourslash/codeFixNoPropertyAccessFromIndexSignature4.ts
new file mode 100644
index 00000000000..cb43005a3a7
--- /dev/null
+++ b/tests/cases/fourslash/codeFixNoPropertyAccessFromIndexSignature4.ts
@@ -0,0 +1,13 @@
+///
+
+// @noPropertyAccessFromIndexSignature: true
+//// declare let x: { y: { [x: string]: string } };
+//// x.y.yadda;
+
+verify.codeFix({
+ description: [ts.Diagnostics.Use_element_access_for_0.message, 'yadda'],
+ index: 0,
+ newFileContent:
+`declare let x: { y: { [x: string]: string } };
+x.y["yadda"];`,
+});
diff --git a/tests/cases/fourslash/codeFixNoPropertyAccessFromIndexSignature5.ts b/tests/cases/fourslash/codeFixNoPropertyAccessFromIndexSignature5.ts
new file mode 100644
index 00000000000..a2472e130d1
--- /dev/null
+++ b/tests/cases/fourslash/codeFixNoPropertyAccessFromIndexSignature5.ts
@@ -0,0 +1,16 @@
+///
+
+// @noPropertyAccessFromIndexSignature: true
+//// declare let x: { y: { [x: string]: string } };
+//// x.y.yadda;
+
+verify.codeFix({
+ description: [ts.Diagnostics.Use_element_access_for_0.message, 'yadda'],
+ index: 0,
+ preferences: {
+ quotePreference: 'single'
+ },
+ newFileContent:
+`declare let x: { y: { [x: string]: string } };
+x.y['yadda'];`,
+});