Skip to content

Commit 56accb0

Browse files
authored
Fixed string completions that require escaping (#55118)
1 parent 5b7b011 commit 56accb0

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/services/stringCompletions.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
endsWith,
3333
ensureTrailingDirectorySeparator,
3434
equateStringsCaseSensitive,
35+
escapeString,
3536
Extension,
3637
fileExtensionIsOneOf,
3738
filter,
@@ -63,6 +64,7 @@ import {
6364
getSupportedExtensions,
6465
getSupportedExtensionsWithJsonIfResolveJsonModule,
6566
getTextOfJsxAttributeName,
67+
getTextOfNode,
6668
getTokenAtPosition,
6769
hasIndexSignature,
6870
hasProperty,
@@ -262,8 +264,13 @@ function convertStringLiteralCompletions(
262264
return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: completion.hasIndexSignature, optionalReplacementSpan, entries };
263265
}
264266
case StringLiteralCompletionKind.Types: {
267+
const quoteChar = contextToken.kind === SyntaxKind.NoSubstitutionTemplateLiteral
268+
? CharacterCodes.backtick
269+
: startsWith(getTextOfNode(contextToken), "'")
270+
? CharacterCodes.singleQuote
271+
: CharacterCodes.doubleQuote;
265272
const entries = completion.types.map(type => ({
266-
name: type.value,
273+
name: escapeString(type.value, quoteChar),
267274
kindModifiers: ScriptElementKindModifier.none,
268275
kind: ScriptElementKind.string,
269276
sortText: SortText.LocationPriority,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// type Value<P extends string> = `var(--\\\\, ${P})`
4+
//// export const value: Value<'one' | 'two'> = "/*1*/"
5+
////
6+
//// export const test: `\ntest\n` = '/*2*/'
7+
////
8+
//// export const doubleQuoted1: `"double-quoted"` = '/*3*/'
9+
//// export const doubleQuoted2: `"double-quoted"` = "/*4*/"
10+
////
11+
//// export const singleQuoted2: `'single-quoted'` = "/*5*/"
12+
//// export const singleQuoted2: `'single-quoted'` = '/*6*/'
13+
////
14+
//// export const backtickQuoted1: '`backtick-quoted`' = "/*7*/"
15+
//// export const backtickQuoted2: '`backtick-quoted`' = `/*8*/`
16+
17+
verify.completions({ marker: "1", exact: ["var(--\\\\\\\\, one)", "var(--\\\\\\\\, two)"] });
18+
verify.completions({ marker: "2", exact: ["\\ntest\\n"] });
19+
verify.completions({ marker: "3", exact: ['"double-quoted"'] });
20+
verify.completions({ marker: "4", exact: ['\\\"double-quoted\\\"'] });
21+
verify.completions({ marker: "5", exact: ["'single-quoted'"] });
22+
verify.completions({ marker: "6", exact: ["\\'single-quoted\\'"] });
23+
verify.completions({ marker: "7", exact: ["`backtick-quoted`"] });
24+
verify.completions({ marker: "8", exact: ["\\`backtick-quoted\\`"] });

0 commit comments

Comments
 (0)