From f0a6154e9b43561112f37285eaeccaf9582f6543 Mon Sep 17 00:00:00 2001 From: Jesse Trinity Date: Wed, 6 Feb 2019 10:04:13 -0800 Subject: [PATCH 1/2] Added auto setting to quotePreference --- src/compiler/types.ts | 2 +- src/server/protocol.ts | 2 +- src/services/codefixes/helpers.ts | 1 + src/services/utilities.ts | 14 ++++++++------ 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 60444cc4f3e..277b5a4ebe9 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5862,7 +5862,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..8d48c32782a 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1315,10 +1315,9 @@ 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 { + } else { const firstModuleSpecifier = sourceFile.imports && find(sourceFile.imports, isStringLiteral); return firstModuleSpecifier ? quotePreferenceFromString(firstModuleSpecifier, sourceFile) : QuotePreference.Double; } @@ -1868,15 +1867,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); } } From 7fd1e62a63caec46cf2f52f99bdcbf453a1debab Mon Sep 17 00:00:00 2001 From: Jesse Trinity Date: Wed, 6 Feb 2019 14:36:17 -0800 Subject: [PATCH 2/2] accepted new baselines --- src/services/utilities.ts | 3 ++- tests/baselines/reference/api/tsserverlibrary.d.ts | 4 ++-- tests/baselines/reference/api/typescript.d.ts | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 8d48c32782a..285ca0cc6a9 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1317,7 +1317,8 @@ namespace ts { export function getQuotePreference(sourceFile: SourceFile, preferences: UserPreferences): QuotePreference { if (preferences.quotePreference && preferences.quotePreference !== "auto") { return preferences.quotePreference === "single" ? QuotePreference.Single : QuotePreference.Double; - } else { + } + else { const firstModuleSpecifier = sourceFile.imports && find(sourceFile.imports, isStringLiteral); return firstModuleSpecifier ? quotePreferenceFromString(firstModuleSpecifier, sourceFile) : QuotePreference.Double; } 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";