diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 84c365a3968..9c710d5f8cd 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5864,7 +5864,7 @@ namespace ts { export interface UserPreferences { readonly disableSuggestions?: boolean; - readonly quotePreference?: "double" | "single"; + readonly quotePreference?: "auto" | "double" | "single"; readonly includeCompletionsForModuleExports?: boolean; readonly includeCompletionsWithInsertText?: boolean; readonly importModuleSpecifierPreference?: "relative" | "non-relative"; diff --git a/src/server/protocol.ts b/src/server/protocol.ts index 11930de4d8c..1fd8d98b570 100644 --- a/src/server/protocol.ts +++ b/src/server/protocol.ts @@ -2891,7 +2891,7 @@ namespace ts.server.protocol { export interface UserPreferences { readonly disableSuggestions?: boolean; - readonly quotePreference?: "double" | "single"; + readonly quotePreference?: "auto" | "double" | "single"; /** * If enabled, TypeScript will search through all external modules' exports and add them to the completions list. * This affects lone identifier completions but not completions on the right hand side of `obj.`. diff --git a/src/services/codefixes/helpers.ts b/src/services/codefixes/helpers.ts index 4309e6ec35d..ec86415b9fa 100644 --- a/src/services/codefixes/helpers.ts +++ b/src/services/codefixes/helpers.ts @@ -264,6 +264,7 @@ namespace ts.codefix { createNew( createIdentifier("Error"), /*typeArguments*/ undefined, + // TODO Handle auto quote preference. [createLiteral("Method not implemented.", /*isSingleQuote*/ preferences.quotePreference === "single")]))], /*multiline*/ true); } diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 2f78ef2324b..285ca0cc6a9 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1315,7 +1315,7 @@ namespace ts { } export function getQuotePreference(sourceFile: SourceFile, preferences: UserPreferences): QuotePreference { - if (preferences.quotePreference) { + if (preferences.quotePreference && preferences.quotePreference !== "auto") { return preferences.quotePreference === "single" ? QuotePreference.Single : QuotePreference.Double; } else { @@ -1868,15 +1868,18 @@ namespace ts { if (/^\d+$/.test(text)) { return text; } + // Editors can pass in undefined or empty string - we want to infer the preference in those cases. + const quotePreference = preferences.quotePreference || "auto"; const quoted = JSON.stringify(text); - switch (preferences.quotePreference) { - case undefined: + switch (quotePreference) { + // TODO use getQuotePreference to infer the actual quote style. + case "auto": case "double": return quoted; case "single": return `'${stripQuotes(quoted).replace("'", "\\'").replace('\\"', '"')}'`; default: - return Debug.assertNever(preferences.quotePreference); + return Debug.assertNever(quotePreference); } } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index ee13a377f5e..53c40047e5d 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -3010,7 +3010,7 @@ declare namespace ts { } interface UserPreferences { readonly disableSuggestions?: boolean; - readonly quotePreference?: "double" | "single"; + readonly quotePreference?: "auto" | "double" | "single"; readonly includeCompletionsForModuleExports?: boolean; readonly includeCompletionsWithInsertText?: boolean; readonly importModuleSpecifierPreference?: "relative" | "non-relative"; @@ -7927,7 +7927,7 @@ declare namespace ts.server.protocol { } interface UserPreferences { readonly disableSuggestions?: boolean; - readonly quotePreference?: "double" | "single"; + readonly quotePreference?: "auto" | "double" | "single"; /** * If enabled, TypeScript will search through all external modules' exports and add them to the completions list. * This affects lone identifier completions but not completions on the right hand side of `obj.`. diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 49238bd3d6d..7e451446b54 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -3010,7 +3010,7 @@ declare namespace ts { } interface UserPreferences { readonly disableSuggestions?: boolean; - readonly quotePreference?: "double" | "single"; + readonly quotePreference?: "auto" | "double" | "single"; readonly includeCompletionsForModuleExports?: boolean; readonly includeCompletionsWithInsertText?: boolean; readonly importModuleSpecifierPreference?: "relative" | "non-relative";