Skip to content

Commit 3677fe4

Browse files
RostaminikooRostaminikoo
Rostaminikoo
authored and
Rostaminikoo
committedAug 15, 2023
added npm ignore
1 parent eda210a commit 3677fe4

File tree

6 files changed

+37
-42
lines changed

6 files changed

+37
-42
lines changed
 

‎.npmignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test
2+
src
3+
public
4+
5+
webpack.config.js
6+
tsconfig.json
7+
jest.config.js

‎index.html

-13
This file was deleted.

‎src/api.ts

-22
This file was deleted.

‎src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ export * from "./parameters";
1010
export * from "./properties";
1111
export * from "./statements";
1212
export * from "./types";
13-
export * from "./api";
13+
export * from "./writer";

‎src/writer.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import ts from "typescript";
2+
3+
export class Writer {
4+
5+
private _printer: ts.Printer;
6+
private _nodes: Array<ts.Node> = [];
7+
8+
constructor(...nodes: Array<ts.Node>) {
9+
this._nodes = nodes || this._nodes;
10+
this._printer = ts.createPrinter({ newLine: ts.NewLineKind.CarriageReturnLineFeed });
11+
}
12+
13+
setNodes(...nodes: Array<ts.Node>) {
14+
this._nodes = nodes || this._nodes;
15+
}
16+
17+
print(): string {
18+
const result: Array<string> = [];
19+
this._nodes.forEach(n =>
20+
result.push(this._printer.printNode(ts.EmitHint.Unspecified, n, undefined))
21+
)
22+
return result.join("\n\n")
23+
}
24+
}

‎test/compilerAPI.spec.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
21
// import "ts-polyfill";
32

43
import ts, { SyntaxKind, factory } from "typescript";
5-
import { nodeText, ConstructorGenerator, IdentifierGenerator, TypeGenerator, ParameterGenerator, ClassGenerator, stringType, assignToClassProperty, expOrNull, numberType } from "../src";
4+
import { IdentifierGenerator, TypeGenerator, ClassGenerator, stringType, assignToClassProperty, expOrNull, numberType, Writer } from "../src";
65

76
describe("test typescript compiler API directly", () => {
87
test("idendifier Generatore", () => {
98
const cg = new IdentifierGenerator("hamid");
10-
expect(nodeText(cg.generate())).toEqual("hamid")
9+
expect(new Writer(cg.generate()).print()).toEqual("hamid")
1110
});
1211

1312
test("TypeGenerator testing", () => {
@@ -21,12 +20,12 @@ describe("test typescript compiler API directly", () => {
2120
const cg = new TypeGenerator("MyType");
2221
cg.setType(arrayString);
2322

24-
expect(nodeText(cg.generate())).toEqual("type MyType = string[];")
23+
expect(new Writer(cg.generate()).print()).toEqual("type MyType = string[];")
2524

2625
const f = new TypeGenerator("IfcText");
2726
f.Modifiers.export();
2827
f.setType(cg.toArrayTypeNode())
29-
expect(nodeText(f.generate())).toEqual("export type IfcText = MyType[];")
28+
expect(new Writer(f.generate()).print()).toEqual("export type IfcText = MyType[];")
3029
});
3130

3231
test("class Generator", () => {
@@ -51,7 +50,7 @@ describe("test typescript compiler API directly", () => {
5150
.setBody()
5251
.returnsProperty(property.Identifier);
5352

53+
console.log(new Writer(c.generate()).print());
5454

55-
console.log(nodeText(c.generate()));
5655
})
5756
});

0 commit comments

Comments
 (0)