Skip to content

Commit

Permalink
fix: MethodDeclarationStructure should write an asterisk isGenerator …
Browse files Browse the repository at this point in the history
…= true (#1502)
  • Loading branch information
ajvincent authored Feb 11, 2024
1 parent 715bd0d commit 871cb46
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -7669,6 +7669,7 @@ class MethodDeclarationStructurePrinter extends NodePrinter {
if (structure.decorators != null)
this.factory.forDecorator().printTexts(writer, structure.decorators);
this.factory.forModifierableNode().printText(writer, structure);
writer.conditionalWrite(structure.isGenerator, "*");
writer.write(name);
writer.conditionalWrite(structure.hasQuestionToken, "?");
this.factory.forTypeParameterDeclaration().printTextsWithBrackets(writer, structure.typeParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class MethodDeclarationStructurePrinter extends NodePrinter<OptionalKind<
this.factory.forDecorator().printTexts(writer, (structure as MethodDeclarationStructure).decorators);

this.factory.forModifierableNode().printText(writer, structure);
writer.conditionalWrite(structure.isGenerator, "*");
writer.write(name);
writer.conditionalWrite(structure.hasQuestionToken, "?");
this.factory.forTypeParameterDeclaration().printTextsWithBrackets(writer, structure.typeParameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ describe("ClassLikeDeclarationBase", () => {
0,
[structure],
"class c {\n public static myMethod?();\n private myMethod();\n"
+ " /** Test */\n @dec\n public static override async myMethod?<T>(param): number {\n"
+ " /** Test */\n @dec\n public static override async *myMethod?<T>(param): number {\n"
+ " class C {\n }\n\n console.log('here');\n"
+ " }\n}",
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { nameof } from "@ts-morph/common";
import { expect } from "chai";
import { FormatCodeSettings, Scope } from "../../../compiler";
import { MethodDeclarationStructurePrinter } from "../../../structurePrinters";
import { MethodDeclarationStructure, OptionalKind } from "../../../structures";
import { OptionalKindAndTrivia } from "../../compiler/testHelpers";
import { getStructureFactoryAndWriter } from "../../testHelpers";

describe("MethodDeclarationStructurePrinter", () => {
interface Options {
formatCodeSettings?: FormatCodeSettings;
isAmbient?: boolean;
}

function doTest(structure: OptionalKind<MethodDeclarationStructure>, expectedOutput: string, options: Options = {}) {
const { writer, factory } = getStructureFactoryAndWriter(options.formatCodeSettings);
factory.forMethodDeclaration({ isAmbient: options.isAmbient || false }).printText(writer, structure);
expect(writer.toString()).to.equal(expectedOutput);
}

describe(nameof<MethodDeclarationStructurePrinter>("printText"), () => {
it("should write a method when the structure has public scope, hasQuestionToken: true, isStatic: true, isGenerator: true, and statements", () => {
const structure: OptionalKindAndTrivia<MakeRequired<MethodDeclarationStructure>> = {
decorators: [{ name: "dec" }],
docs: [{ description: "test" }],
hasOverrideKeyword: false,
hasQuestionToken: true,
isAbstract: false,
isAsync: false,
isGenerator: true,
isStatic: true,
name: "method",
overloads: [],
parameters: [{ name: "p", type: "number" }],
returnType: "IterableIterator<number>",
scope: Scope.Public,
statements: ["yield p;"],
typeParameters: undefined,
};

doTest(
structure,
[
"/** test */",
"@dec",
"public static *method?(p: number): IterableIterator<number> {\n yield p;\n}",
].join("\n"),
);
});

it("should write a method when the structure has isAsync: true, and statements", () => {
const structure: OptionalKindAndTrivia<MakeRequired<MethodDeclarationStructure>> = {
decorators: [],
docs: [],
hasOverrideKeyword: false,
hasQuestionToken: false,
isAbstract: false,
isAsync: true,
isGenerator: false,
isStatic: false,
name: "method",
overloads: [],
parameters: [{ name: "p", type: "number" }],
returnType: "Promise<number>",
scope: undefined,
statements: ["return Promise.resolve(p);"],
typeParameters: undefined,
};

doTest(
structure,
[
"async method(p: number): Promise<number> {\n return Promise.resolve(p);\n}",
].join("\n"),
);
});

it("should write a method when the structure has isAbstract: true", () => {
const structure: OptionalKindAndTrivia<MakeRequired<MethodDeclarationStructure>> = {
decorators: [],
docs: [],
hasOverrideKeyword: false,
hasQuestionToken: false,
isAbstract: true,
isAsync: false,
isGenerator: false,
isStatic: false,
name: "method",
overloads: [],
parameters: [{ name: "p", type: "number" }],
returnType: "number",
scope: undefined,
statements: ["return Promise.resolve(p);"],
typeParameters: undefined,
};

doTest(
structure,
[
"abstract method(p: number): number;",
].join("\n"),
);
});
});
});

0 comments on commit 871cb46

Please sign in to comment.