Skip to content

Commit

Permalink
Fixed string completions that require escaping (#55118)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Sep 21, 2023
1 parent 5b7b011 commit 56accb0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
endsWith,
ensureTrailingDirectorySeparator,
equateStringsCaseSensitive,
escapeString,
Extension,
fileExtensionIsOneOf,
filter,
Expand Down Expand Up @@ -63,6 +64,7 @@ import {
getSupportedExtensions,
getSupportedExtensionsWithJsonIfResolveJsonModule,
getTextOfJsxAttributeName,
getTextOfNode,
getTokenAtPosition,
hasIndexSignature,
hasProperty,
Expand Down Expand Up @@ -262,8 +264,13 @@ function convertStringLiteralCompletions(
return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: completion.hasIndexSignature, optionalReplacementSpan, entries };
}
case StringLiteralCompletionKind.Types: {
const quoteChar = contextToken.kind === SyntaxKind.NoSubstitutionTemplateLiteral
? CharacterCodes.backtick
: startsWith(getTextOfNode(contextToken), "'")
? CharacterCodes.singleQuote
: CharacterCodes.doubleQuote;
const entries = completion.types.map(type => ({
name: type.value,
name: escapeString(type.value, quoteChar),
kindModifiers: ScriptElementKindModifier.none,
kind: ScriptElementKind.string,
sortText: SortText.LocationPriority,
Expand Down
24 changes: 24 additions & 0 deletions tests/cases/fourslash/stringCompletionsVsEscaping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference path="fourslash.ts" />

//// type Value<P extends string> = `var(--\\\\, ${P})`
//// export const value: Value<'one' | 'two'> = "/*1*/"
////
//// export const test: `\ntest\n` = '/*2*/'
////
//// export const doubleQuoted1: `"double-quoted"` = '/*3*/'
//// export const doubleQuoted2: `"double-quoted"` = "/*4*/"
////
//// export const singleQuoted2: `'single-quoted'` = "/*5*/"
//// export const singleQuoted2: `'single-quoted'` = '/*6*/'
////
//// export const backtickQuoted1: '`backtick-quoted`' = "/*7*/"
//// export const backtickQuoted2: '`backtick-quoted`' = `/*8*/`

verify.completions({ marker: "1", exact: ["var(--\\\\\\\\, one)", "var(--\\\\\\\\, two)"] });
verify.completions({ marker: "2", exact: ["\\ntest\\n"] });
verify.completions({ marker: "3", exact: ['"double-quoted"'] });
verify.completions({ marker: "4", exact: ['\\\"double-quoted\\\"'] });
verify.completions({ marker: "5", exact: ["'single-quoted'"] });
verify.completions({ marker: "6", exact: ["\\'single-quoted\\'"] });
verify.completions({ marker: "7", exact: ["`backtick-quoted`"] });
verify.completions({ marker: "8", exact: ["\\`backtick-quoted\\`"] });

0 comments on commit 56accb0

Please sign in to comment.