Skip to content

Commit 044fb53

Browse files
author
Andy
authored
Escape quotes in bracketed completions (#21676)
1 parent abe814f commit 044fb53

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/services/completions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,10 @@ namespace ts.Completions {
180180
let replacementSpan: TextSpan | undefined;
181181
if (includeInsertTextCompletions) {
182182
if (origin && origin.type === "this-type") {
183-
insertText = needsConvertPropertyAccess ? `this["${name}"]` : `this.${name}`;
183+
insertText = needsConvertPropertyAccess ? `this[${quote(name)}]` : `this.${name}`;
184184
}
185185
else if (needsConvertPropertyAccess) {
186-
// TODO: GH#20619 Use configured quote style
187-
insertText = `["${name}"]`;
186+
insertText = `[${quote(name)}]`;
188187
const dot = findChildOfKind(propertyAccessToConvert!, SyntaxKind.DotToken, sourceFile)!;
189188
// If the text after the '.' starts with this name, write over it. Else, add new text.
190189
const end = startsWith(name, propertyAccessToConvert!.name.text) ? propertyAccessToConvert!.name.end : dot.end;
@@ -222,6 +221,10 @@ namespace ts.Completions {
222221
};
223222
}
224223

224+
function quote(text: string): string {
225+
// TODO: GH#20619 Use configured quote style
226+
return JSON.stringify(text);
227+
}
225228

226229
function isRecommendedCompletionMatch(localSymbol: Symbol, recommendedCompletion: Symbol, checker: TypeChecker): boolean {
227230
return localSymbol === recommendedCompletion ||
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////declare const x: { '"': 0 };
4+
////x[|./**/|];
5+
6+
const replacementSpan = test.ranges()[0];
7+
verify.completionsAt("", [{ name: '"', insertText: '["\\""]', replacementSpan }], { includeInsertTextCompletions: true });

0 commit comments

Comments
 (0)