Skip to content

Commit

Permalink
feat: Ability to set a DecoratorStructure's arguments using a writer …
Browse files Browse the repository at this point in the history
…function.
  • Loading branch information
dsherret committed Apr 30, 2018
1 parent e7e0158 commit 585793c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/structurePrinters/decorator/DecoratorStructurePrinter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CodeBlockWriter } from "../../codeBlockWriter";
import { printTextFromStringOrWriter } from "../../utils";
import { DecoratorStructure } from "../../structures";
import { FactoryStructurePrinter } from "../FactoryStructurePrinter";

Expand All @@ -13,8 +14,19 @@ export class DecoratorStructurePrinter extends FactoryStructurePrinter<Decorator

printText(writer: CodeBlockWriter, structure: DecoratorStructure) {
writer.write(`@${structure.name}`);
if (structure.arguments != null)
writer.write(`(${structure.arguments.join(", ")})`);
this.printArguments(writer, structure);
}

printArguments(writer: CodeBlockWriter, structure: DecoratorStructure) {
if (structure.arguments == null)
return;

writer.write("(");
for (let i = 0; i < structure.arguments.length; i++) {
writer.conditionalWrite(i > 0, ", ");
printTextFromStringOrWriter(writer, structure.arguments[0]);
}
writer.write(")");
}

private printMultiple(writer: CodeBlockWriter, structures: DecoratorStructure[] | undefined, separator: () => void) {
Expand Down
6 changes: 4 additions & 2 deletions src/structures/decorator/DecoratorStructure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface DecoratorStructure {
import { CodeBlockWriter } from "../../codeBlockWriter";

export interface DecoratorStructure {
name: string;
arguments?: string[];
arguments?: (string | ((writer: CodeBlockWriter) => void))[];
}
5 changes: 4 additions & 1 deletion src/tests/compiler/base/decoratableNodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ describe(nameof(DecoratableNode), () => {
});

it("should insert with arguments", () => {
doTest("class MyClass {}", 0, [{ name: "dec", arguments: [] }, { name: "dec2", arguments: ["1"] }, { name: "dec3", arguments: ["1", "2"] }],
doTest("class MyClass {}", 0, [
{ name: "dec", arguments: [] }, { name: "dec2", arguments: ["1"] },
{ name: "dec3", arguments: ["1", writer => writer.write("2")] }
],
"@dec()\n@dec2(1)\n@dec3(1, 2)\nclass MyClass {}");
});

Expand Down

0 comments on commit 585793c

Please sign in to comment.