diff --git a/src/services/codefixes/fixSpelling.ts b/src/services/codefixes/fixSpelling.ts index 851b749d29b..59f5e64f212 100644 --- a/src/services/codefixes/fixSpelling.ts +++ b/src/services/codefixes/fixSpelling.ts @@ -13,13 +13,15 @@ namespace ts.codefix { const info = getInfo(sourceFile, context.span.start, context); if (!info) return undefined; const { node, suggestion } = info; - const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node, suggestion)); + const { target } = context.host.getCompilationSettings(); + const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node, suggestion, target)); return [createCodeFixAction(changes, [Diagnostics.Change_spelling_to_0, suggestion], fixId, Diagnostics.Fix_all_detected_spelling_errors)]; }, fixIds: [fixId], getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { const info = getInfo(diag.file!, diag.start!, context); - if (info) doChange(changes, context.sourceFile, info.node, info.suggestion); + const { target } = context.host.getCompilationSettings(); + if (info) doChange(changes, context.sourceFile, info.node, info.suggestion, target); }), }); @@ -54,8 +56,13 @@ namespace ts.codefix { return suggestion === undefined ? undefined : { node, suggestion }; } - function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: Node, suggestion: string) { - changes.replaceNode(sourceFile, node, createIdentifier(suggestion)); + function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, node: Node, suggestion: string, target: ScriptTarget) { + if (!isIdentifierText(suggestion, target) && isPropertyAccessExpression(node.parent)) { + changes.replaceNode(sourceFile, node.parent, createElementAccess(node.parent.expression, createLiteral(suggestion))); + } + else { + changes.replaceNode(sourceFile, node, createIdentifier(suggestion)); + } } function convertSemanticMeaningToSymbolFlags(meaning: SemanticMeaning): SymbolFlags { diff --git a/tests/cases/fourslash/codeFixSpelling_all.ts b/tests/cases/fourslash/codeFixSpelling_all.ts index 973f1e1503c..e4c3a5ef940 100644 --- a/tests/cases/fourslash/codeFixSpelling_all.ts +++ b/tests/cases/fourslash/codeFixSpelling_all.ts @@ -4,6 +4,11 @@ //// s.toStrang(); //// s.toStrong(); ////} +//// +////type A = { "foo-bar": number; "barbaz": string; }; +////var a: A; +////a.foobar; +////a.barbz verify.codeFixAll({ fixId: "fixSpelling", @@ -12,5 +17,10 @@ verify.codeFixAll({ `function f(s: string) { s.toString(); s.toString(); -}`, +} + +type A = { "foo-bar": number; "barbaz": string; }; +var a: A; +a["foo-bar"]; +a.barbaz`, });