Skip to content

Commit 8965ea3

Browse files
committed
fix(39332): handle quotes preference in interface method signatures
1 parent 619658b commit 8965ea3

File tree

3 files changed

+27
-33
lines changed

3 files changed

+27
-33
lines changed

src/services/codefixes/helpers.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ namespace ts.codefix {
121121
if (declarations.length === 1) {
122122
Debug.assert(signatures.length === 1, "One declaration implies one signature");
123123
const signature = signatures[0];
124-
outputMethod(signature, modifiers, name, ambient ? undefined : createStubbedMethodBody(preferences));
124+
outputMethod(preferences, signature, modifiers, name, ambient ? undefined : createStubbedMethodBody(preferences));
125125
break;
126126
}
127127

128128
for (const signature of signatures) {
129129
// Need to ensure nodes are fresh each time so they can have different positions.
130-
outputMethod(signature, getSynthesizedDeepClones(modifiers, /*includeTrivia*/ false), getSynthesizedDeepClone(name, /*includeTrivia*/ false));
130+
outputMethod(preferences, signature, getSynthesizedDeepClones(modifiers, /*includeTrivia*/ false), getSynthesizedDeepClone(name, /*includeTrivia*/ false));
131131
}
132132

133133
if (!ambient) {
134134
if (declarations.length > signatures.length) {
135135
const signature = checker.getSignatureFromDeclaration(declarations[declarations.length - 1] as SignatureDeclaration)!;
136-
outputMethod(signature, modifiers, name, createStubbedMethodBody(preferences));
136+
outputMethod(preferences, signature, modifiers, name, createStubbedMethodBody(preferences));
137137
}
138138
else {
139139
Debug.assert(declarations.length === signatures.length, "Declarations and signatures should match count");
@@ -143,14 +143,15 @@ namespace ts.codefix {
143143
break;
144144
}
145145

146-
function outputMethod(signature: Signature, modifiers: NodeArray<Modifier> | undefined, name: PropertyName, body?: Block): void {
147-
const method = signatureToMethodDeclaration(context, signature, enclosingDeclaration, modifiers, name, optional, body, importAdder);
146+
function outputMethod(preferences: UserPreferences, signature: Signature, modifiers: NodeArray<Modifier> | undefined, name: PropertyName, body?: Block): void {
147+
const method = signatureToMethodDeclaration(context, preferences, signature, enclosingDeclaration, modifiers, name, optional, body, importAdder);
148148
if (method) addClassElement(method);
149149
}
150150
}
151151

152152
function signatureToMethodDeclaration(
153153
context: TypeConstructionContext,
154+
preferences: UserPreferences,
154155
signature: Signature,
155156
enclosingDeclaration: ClassLikeDeclaration,
156157
modifiers: NodeArray<Modifier> | undefined,
@@ -162,7 +163,8 @@ namespace ts.codefix {
162163
const program = context.program;
163164
const checker = program.getTypeChecker();
164165
const scriptTarget = getEmitScriptTarget(program.getCompilerOptions());
165-
const signatureDeclaration = <MethodDeclaration>checker.signatureToSignatureDeclaration(signature, SyntaxKind.MethodDeclaration, enclosingDeclaration, NodeBuilderFlags.NoTruncation | NodeBuilderFlags.SuppressAnyReturnType, getNoopSymbolTrackerWithResolver(context));
166+
const flags = NodeBuilderFlags.NoTruncation | NodeBuilderFlags.SuppressAnyReturnType | (preferences.quotePreference === "single" ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : 0);
167+
const signatureDeclaration = <MethodDeclaration>checker.signatureToSignatureDeclaration(signature, SyntaxKind.MethodDeclaration, enclosingDeclaration, flags, getNoopSymbolTrackerWithResolver(context));
166168
if (!signatureDeclaration) {
167169
return undefined;
168170
}

tests/cases/fourslash/codeFixClassImplementInterface1_optionQuote.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
/// <reference path='fourslash.ts' />
22

33
////interface I {
4-
//// m(): void;
4+
//// a(): void;
5+
//// b(x: 'x', y: 'a' | 'b'): 'b';
6+
////
7+
//// c: 'c';
8+
//// d: { e: 'e'; };
59
////}
6-
////class C implements I {}
10+
////class Foo implements I {}
711

812
verify.codeFix({
9-
description: "Implement interface 'I'",
13+
description: [ts.Diagnostics.Implement_interface_0.message, "I"],
1014
newFileContent:
1115
`interface I {
12-
m(): void;
16+
a(): void;
17+
b(x: 'x', y: 'a' | 'b'): 'b';
18+
19+
c: 'c';
20+
d: { e: 'e'; };
1321
}
14-
class C implements I {
15-
m(): void {
22+
class Foo implements I {
23+
a(): void {
24+
throw new Error('Method not implemented.');
25+
}
26+
b(x: 'x', y: 'a' | 'b'): 'b' {
1627
throw new Error('Method not implemented.');
1728
}
29+
c: 'c';
30+
d: { e: 'e'; };
1831
}`,
1932
preferences: { quotePreference: "single" }
2033
});

0 commit comments

Comments
 (0)