From 4153edbe31d116b07deedec06fdd4df1838e5016 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 4 Feb 2021 12:36:42 -0800 Subject: [PATCH] Swapping the quote style of a string for completions should swap _all_ quotes in the string --- src/services/utilities.ts | 2 +- ...letionForStringLiteral_quotePreference8.ts | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/completionForStringLiteral_quotePreference8.ts diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 1fb8a23043ef7..d57f267ea977c 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -2495,7 +2495,7 @@ namespace ts { // Editors can pass in undefined or empty string - we want to infer the preference in those cases. const quotePreference = getQuotePreference(sourceFile, preferences); const quoted = JSON.stringify(text); - return quotePreference === QuotePreference.Single ? `'${stripQuotes(quoted).replace("'", "\\'").replace('\\"', '"')}'` : quoted; + return quotePreference === QuotePreference.Single ? `'${stripQuotes(quoted).replace(/'/g, "\\'").replace(/\\"/g, '"')}'` : quoted; } export function isEqualityOperatorKind(kind: SyntaxKind): kind is EqualityOperator { diff --git a/tests/cases/fourslash/completionForStringLiteral_quotePreference8.ts b/tests/cases/fourslash/completionForStringLiteral_quotePreference8.ts new file mode 100644 index 0000000000000..9e66dcb1fe953 --- /dev/null +++ b/tests/cases/fourslash/completionForStringLiteral_quotePreference8.ts @@ -0,0 +1,22 @@ +/// + +// @filename: /a.ts +////export const a = null; + +// @filename: /b.ts +////import { a } from './a'; +//// +////const foo = { '"a name\'s all good but it\'s better with more"': null }; +////foo[|./**/|] + +goTo.file("/b.ts"); +verify.completions({ + marker: "", + exact: [ + { name: "\"a name's all good but it's better with more\"", insertText: "['\"a name\\'s all good but it\\'s better with more\"']", replacementSpan: test.ranges()[0] }, + ], + preferences: { + includeInsertTextCompletions: true, + quotePreference: "auto" + } +});