Skip to content

Commit

Permalink
feat: Ability to set a SourceFileStructure's bodyText 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 585793c commit 792c530
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class DecoratorStructurePrinter extends FactoryStructurePrinter<Decorator
writer.write("(");
for (let i = 0; i < structure.arguments.length; i++) {
writer.conditionalWrite(i > 0, ", ");
printTextFromStringOrWriter(writer, structure.arguments[0]);
printTextFromStringOrWriter(writer, structure.arguments[i]);
}
writer.write(")");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ArrayUtils } from "../../utils";
import { FactoryStructurePrinter } from "../FactoryStructurePrinter";
import { StructurePrinterFactory } from "../../factories";

export type BodyTextStructures = StatementedNodeStructure | { bodyText?: string; };
export type BodyTextStructures = StatementedNodeStructure | { bodyText?: string | ((writer: CodeBlockWriter) => void); };

export class BodyTextStructurePrinter extends FactoryStructurePrinter<BodyTextStructures> {
constructor(factory: StructurePrinterFactory, private readonly options: { isAmbient: boolean; }) {
Expand Down
5 changes: 3 additions & 2 deletions src/structures/file/SourceFileStructure.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { StatementedNodeStructure } from "../statement";
import { CodeBlockWriter } from "../../codeBlockWriter";
import { StatementedNodeStructure } from "../statement";
import { ImportDeclarationStructure } from "./ImportDeclarationStructure";
import { ExportDeclarationStructure } from "./ExportDeclarationStructure";

export interface SourceFileStructure extends SourceFileSpecificStructure, StatementedNodeStructure {
bodyText?: string;
bodyText?: string | ((writer: CodeBlockWriter) => void);
}

export interface SourceFileSpecificStructure {
Expand Down
5 changes: 3 additions & 2 deletions src/tests/projectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,10 @@ describe(nameof(Project), () => {
name: "MyEnum"
}],
imports: [{ moduleSpecifier: "./test" }],
exports: [{ moduleSpecifier: "./test" }]
exports: [{ moduleSpecifier: "./test" }],
bodyText: writer => writer.write("print('test');")
});
expect(sourceFile.getFullText()).to.equal(`import "./test";\n\nenum MyEnum {\n}\n\nexport * from "./test";\n`);
expect(sourceFile.getFullText()).to.equal(`import "./test";\n\nenum MyEnum {\n}\n\nprint('test');\n\nexport * from "./test";\n`);
});

it("should add for everything in the structure", () => {
Expand Down

0 comments on commit 792c530

Please sign in to comment.