Skip to content

Commit 9549a9d

Browse files
Andaristsnovader
authored andcommitted
Fixed string completions that require escaping (microsoft#55118)
1 parent fdb2124 commit 9549a9d

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
@@ -33,6 +33,7 @@ import {
3333
endsWith,
3434
ensureTrailingDirectorySeparator,
3535
equateStringsCaseSensitive,
36+
escapeString,
3637
Extension,
3738
fileExtensionIsOneOf,
3839
filter,
@@ -64,6 +65,7 @@ import {
6465
getSupportedExtensions,
6566
getSupportedExtensionsWithJsonIfResolveJsonModule,
6667
getTextOfJsxAttributeName,
68+
getTextOfNode,
6769
getTokenAtPosition,
6870
hasIndexSignature,
6971
hasProperty,
@@ -263,8 +265,13 @@ function convertStringLiteralCompletions(
263265
return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: completion.hasIndexSignature, optionalReplacementSpan, entries };
264266
}
265267
case StringLiteralCompletionKind.Types: {
268+
const quoteChar = contextToken.kind === SyntaxKind.NoSubstitutionTemplateLiteral
269+
? CharacterCodes.backtick
270+
: startsWith(getTextOfNode(contextToken), "'")
271+
? CharacterCodes.singleQuote
272+
: CharacterCodes.doubleQuote;
266273
const entries = completion.types.map(type => ({
267-
name: type.value,
274+
name: escapeString(type.value, quoteChar),
268275
kindModifiers: ScriptElementKindModifier.none,
269276
kind: ScriptElementKind.string,
270277
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)