Skip to content

Commit acbda14

Browse files
committed
addMethodDeclaration codefix creates a generator function when target is child of a YieldExpression, resolves microsoft#24728
1 parent d0ae03c commit acbda14

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/services/codefixes/helpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,18 @@ namespace ts.codefix {
111111
}
112112

113113
export function createMethodFromCallExpression(
114-
{ typeArguments, arguments: args }: CallExpression,
114+
{ typeArguments, arguments: args, parent: parent }: CallExpression,
115115
methodName: string,
116116
inJs: boolean,
117117
makeStatic: boolean,
118118
preferences: UserPreferences,
119119
): MethodDeclaration {
120+
const asterisk = parent.kind === SyntaxKind.YieldExpression ? createToken(SyntaxKind.AsteriskToken) : undefined;
121+
120122
return createMethod(
121123
/*decorators*/ undefined,
122124
/*modifiers*/ makeStatic ? [createToken(SyntaxKind.StaticKeyword)] : undefined,
123-
/*asteriskToken*/ undefined,
125+
/*asteriskToken*/ asterisk,
124126
methodName,
125127
/*questionToken*/ undefined,
126128
/*typeParameters*/ inJs ? undefined : map(typeArguments, (_, i) =>
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+
////class C {
4+
//// *method() {
5+
//// yield* this.y();
6+
//// }
7+
////}
8+
9+
verify.codeFixAll({
10+
fixId: "addMissingMember",
11+
fixAllDescription: "Add all missing members",
12+
newFileContent:
13+
`class C {
14+
*method() {
15+
yield* this.y();
16+
}
17+
*y(): any {
18+
throw new Error("Method not implemented.");
19+
}
20+
}`,
21+
});
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+
////class C {
4+
//// method() {
5+
//// yield* this.y();
6+
//// }
7+
////}
8+
9+
verify.codeFixAll({
10+
fixId: "addMissingMember",
11+
fixAllDescription: "Add all missing members",
12+
newFileContent:
13+
`class C {
14+
method() {
15+
yield* this.y();
16+
}
17+
y(): any {
18+
throw new Error("Method not implemented.");
19+
}
20+
}`,
21+
});

0 commit comments

Comments
 (0)