Skip to content

Commit c239626

Browse files
a-tarasyuksheetalkamat
authored andcommitted
fix(36140): handle quotesPreference option in interface implementation (microsoft#36398)
1 parent 20e8eba commit c239626

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3956,7 +3956,7 @@ namespace ts {
39563956
}
39573957
if (type.flags & TypeFlags.StringLiteral) {
39583958
context.approximateLength += ((<StringLiteralType>type).value.length + 2);
3959-
return createLiteralTypeNode(setEmitFlags(createLiteral((<StringLiteralType>type).value), EmitFlags.NoAsciiEscaping));
3959+
return createLiteralTypeNode(setEmitFlags(createLiteral((<StringLiteralType>type).value, !!(context.flags & NodeBuilderFlags.UseSingleQuotesForStringLiteralType)), EmitFlags.NoAsciiEscaping));
39603960
}
39613961
if (type.flags & TypeFlags.NumberLiteral) {
39623962
const value = (<NumberLiteralType>type).value;

src/compiler/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3630,6 +3630,7 @@ namespace ts {
36303630
UseTypeOfFunction = 1 << 12, // Build using typeof instead of function type literal
36313631
OmitParameterModifiers = 1 << 13, // Omit modifiers on parameters
36323632
UseAliasDefinedOutsideCurrentScope = 1 << 14, // Allow non-visible aliases
3633+
UseSingleQuotesForStringLiteralType = 1 << 28, // Use single quotes for string literal type
36333634

36343635
// Error handling
36353636
AllowThisInObjectLiteral = 1 << 15,
@@ -3672,6 +3673,7 @@ namespace ts {
36723673
OmitParameterModifiers = 1 << 13, // Omit modifiers on parameters
36733674

36743675
UseAliasDefinedOutsideCurrentScope = 1 << 14, // For a `type T = ... ` defined in a different file, write `T` instead of its value, even though `T` can't be accessed in the current scope.
3676+
UseSingleQuotesForStringLiteralType = 1 << 28, // Use single quotes for string literal type
36753677

36763678
// Error Handling
36773679
AllowUniqueESSymbolType = 1 << 20, // This is bit 20 to align with the same bit in `NodeBuilderFlags`
@@ -3690,7 +3692,8 @@ namespace ts {
36903692

36913693
NodeBuilderFlagsMask = NoTruncation | WriteArrayAsGenericType | UseStructuralFallback | WriteTypeArgumentsOfSignature |
36923694
UseFullyQualifiedType | SuppressAnyReturnType | MultilineObjectLiterals | WriteClassExpressionAsTypeLiteral |
3693-
UseTypeOfFunction | OmitParameterModifiers | UseAliasDefinedOutsideCurrentScope | AllowUniqueESSymbolType | InTypeAlias,
3695+
UseTypeOfFunction | OmitParameterModifiers | UseAliasDefinedOutsideCurrentScope | AllowUniqueESSymbolType | InTypeAlias |
3696+
UseSingleQuotesForStringLiteralType,
36943697
}
36953698

36963699
export const enum SymbolFormatFlags {

src/services/codefixes/helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ namespace ts.codefix {
6060
switch (declaration.kind) {
6161
case SyntaxKind.PropertySignature:
6262
case SyntaxKind.PropertyDeclaration:
63-
const typeNode = checker.typeToTypeNode(type, enclosingDeclaration, /*flags*/ undefined, getNoopSymbolTrackerWithResolver(context));
63+
const flags = preferences.quotePreference === "single" ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : undefined;
64+
const typeNode = checker.typeToTypeNode(type, enclosingDeclaration, flags, getNoopSymbolTrackerWithResolver(context));
6465
out(createProperty(
6566
/*decorators*/undefined,
6667
modifiers,

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,7 @@ declare namespace ts {
21182118
UseTypeOfFunction = 4096,
21192119
OmitParameterModifiers = 8192,
21202120
UseAliasDefinedOutsideCurrentScope = 16384,
2121+
UseSingleQuotesForStringLiteralType = 268435456,
21212122
AllowThisInObjectLiteral = 32768,
21222123
AllowQualifedNameInPlaceOfIdentifier = 65536,
21232124
AllowAnonymousIdentifier = 131072,
@@ -2145,6 +2146,7 @@ declare namespace ts {
21452146
UseTypeOfFunction = 4096,
21462147
OmitParameterModifiers = 8192,
21472148
UseAliasDefinedOutsideCurrentScope = 16384,
2149+
UseSingleQuotesForStringLiteralType = 268435456,
21482150
AllowUniqueESSymbolType = 1048576,
21492151
AddUndefined = 131072,
21502152
WriteArrowStyleSignature = 262144,
@@ -2153,7 +2155,7 @@ declare namespace ts {
21532155
InFirstTypeArgument = 4194304,
21542156
InTypeAlias = 8388608,
21552157
/** @deprecated */ WriteOwnNameForAnyLike = 0,
2156-
NodeBuilderFlagsMask = 9469291
2158+
NodeBuilderFlagsMask = 277904747
21572159
}
21582160
export enum SymbolFormatFlags {
21592161
None = 0,

tests/baselines/reference/api/typescript.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,7 @@ declare namespace ts {
21182118
UseTypeOfFunction = 4096,
21192119
OmitParameterModifiers = 8192,
21202120
UseAliasDefinedOutsideCurrentScope = 16384,
2121+
UseSingleQuotesForStringLiteralType = 268435456,
21212122
AllowThisInObjectLiteral = 32768,
21222123
AllowQualifedNameInPlaceOfIdentifier = 65536,
21232124
AllowAnonymousIdentifier = 131072,
@@ -2145,6 +2146,7 @@ declare namespace ts {
21452146
UseTypeOfFunction = 4096,
21462147
OmitParameterModifiers = 8192,
21472148
UseAliasDefinedOutsideCurrentScope = 16384,
2149+
UseSingleQuotesForStringLiteralType = 268435456,
21482150
AllowUniqueESSymbolType = 1048576,
21492151
AddUndefined = 131072,
21502152
WriteArrowStyleSignature = 262144,
@@ -2153,7 +2155,7 @@ declare namespace ts {
21532155
InFirstTypeArgument = 4194304,
21542156
InTypeAlias = 8388608,
21552157
/** @deprecated */ WriteOwnNameForAnyLike = 0,
2156-
NodeBuilderFlagsMask = 9469291
2158+
NodeBuilderFlagsMask = 277904747
21572159
}
21582160
export enum SymbolFormatFlags {
21592161
None = 0,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////interface IFoo {
4+
//// a: 'string';
5+
//// c: { d: 'string'; };
6+
////}
7+
////class Foo implements IFoo {}
8+
9+
verify.codeFix({
10+
description: [ts.Diagnostics.Implement_interface_0.message, "IFoo"],
11+
newFileContent:
12+
`interface IFoo {
13+
a: 'string';
14+
c: { d: 'string'; };
15+
}
16+
class Foo implements IFoo {
17+
a: 'string';
18+
c: { d: 'string'; };
19+
}`,
20+
preferences: { quotePreference: "single" }
21+
});

0 commit comments

Comments
 (0)