Skip to content

Commit ad4fdbd

Browse files
committed
fix: typeArguments on decorator structure should output type arguments
Ref #1205
1 parent dabba30 commit ad4fdbd

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/ts-morph/src/structurePrinters/decorator/DecoratorStructurePrinter.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,23 @@ export class DecoratorStructurePrinter extends NodePrinter<OptionalKind<Decorato
1313

1414
protected printTextInternal(writer: CodeBlockWriter, structure: OptionalKind<DecoratorStructure>) {
1515
writer.write(`@${structure.name}`);
16+
this.printTypeArguments(writer, structure);
1617
this.printArguments(writer, structure);
1718
}
1819

19-
printArguments(writer: CodeBlockWriter, structure: OptionalKind<DecoratorStructure>) {
20+
private printTypeArguments(writer: CodeBlockWriter, structure: OptionalKind<DecoratorStructure>) {
21+
if (structure.typeArguments == null || structure.typeArguments.length === 0)
22+
return;
23+
24+
writer.write("<");
25+
for (let i = 0; i < structure.typeArguments.length; i++) {
26+
writer.conditionalWrite(i > 0, ", ");
27+
writer.write(this.getTextWithQueuedChildIndentation(writer, structure.typeArguments[i]));
28+
}
29+
writer.write(">");
30+
}
31+
32+
private printArguments(writer: CodeBlockWriter, structure: OptionalKind<DecoratorStructure>) {
2033
if (structure.arguments == null)
2134
return;
2235

packages/ts-morph/src/tests/structurePrinters/decorator/decoratorStructurePrinterTests.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,19 @@ describe(nameof(DecoratorStructurePrinter), () => {
2727
doTest({ name: "dec", arguments: writer => writer.writeLine("1,").write("2") }, `@dec(1,\n 2)`);
2828
});
2929
});
30+
31+
describe("type arguments", () => {
32+
it("should not write when empty", () => {
33+
doTest({ name: "dec", typeArguments: [] }, `@dec`);
34+
});
35+
36+
it("should write", () => {
37+
doTest({ name: "dec", typeArguments: ["string"], arguments: ["1"] }, `@dec<string>(1)`);
38+
});
39+
40+
it("should write multiple", () => {
41+
doTest({ name: "dec", typeArguments: ["string", "number"] }, `@dec<string, number>`);
42+
});
43+
});
3044
});
3145
});

0 commit comments

Comments
 (0)