Skip to content

Commit 75c4280

Browse files
committed
feat: JSONをinterfaceに変換する
1 parent a5444b5 commit 75c4280

16 files changed

+311
-21
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
1-
# @himenon/node-lib-template
1+
# @himenon/typescript-codegen
2+
3+
```bash
4+
AST --> TypeScript
5+
```
6+
7+
## References
8+
9+
TypeScript
10+
11+
- https://ts-ast-viewer.com
12+
- http://akito0107.hatenablog.com/entry/2018/12/23/020323
13+
14+
JavaScript
15+
16+
- https://astexplorer.net/
17+
18+
Flow
19+
20+
- https://talks.leko.jp/transform-flow-to-typescript-using-ast/#0
21+
22+
Babel
23+
24+
- https://blog.ikeryo1182.com/typescript-transpiler/
225

326
## Usage
427

28+
## Development
29+
530
| scripts | description |
631
| :------------------------ | :------------------------------------------ |
732
| `build` | typescript build and create proxy directory |
@@ -26,4 +51,4 @@
2651

2752
## LICENCE
2853

29-
[@himenon-node-lib-template](https://github.com/Himenon/node-lib-template)・MIT
54+
[@himenon-typescript-codegen](https://github.com/Himenon/typescript-codegen)・MIT

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = {
6060
// A set of global variables that need to be available in all test environments
6161
globals: {
6262
"ts-jest": {
63-
tsConfig: "tsconfig.json",
63+
tsconfig: "tsconfig.json",
6464
diagnostics: false,
6565
},
6666
},

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
"kind-of": "6.0.3",
6262
"node-fetch": "2.6.1"
6363
},
64+
"dependencies": {
65+
"@babel/generator": "^7.12.11",
66+
"@babel/parser": "^7.12.11",
67+
"@types/json-schema": "^7.0.6"
68+
},
6469
"devDependencies": {
6570
"@commitlint/cli": "11.0.0",
6671
"@commitlint/config-conventional": "11.0.0",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { generateInterface } from "../generateInterface";
2+
3+
describe("Interface Generator", () => {
4+
it("Change Interface Name", () => {
5+
const name = "MyTestInterface";
6+
const expectResult = `interface ${name} {\n}\n`;
7+
const result = generateInterface({ name, schemas: {} });
8+
expect(result).toBe(expectResult);
9+
});
10+
});

src/__tests__/index.test.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { JSONSchema4 } from "json-schema";
2+
import { generateInterface } from "../generateInterface";
3+
4+
const schemas: JSONSchema4 = {
5+
type: "object",
6+
properties: {
7+
name: {
8+
type: "string",
9+
required: false,
10+
},
11+
age: {
12+
type: "number",
13+
},
14+
body: {
15+
type: "object",
16+
properties: {
17+
child: {
18+
type: "string",
19+
},
20+
},
21+
},
22+
},
23+
};
24+
25+
describe("Interface Generator", () => {
26+
it("Change Interface Name", () => {
27+
const name = "MyTestInterface";
28+
const expectResult = `interface ${name} {\n}\n`;
29+
const result = generateInterface({ name, schemas });
30+
console.log(JSON.stringify(result));
31+
expect(result).toBe(expectResult);
32+
});
33+
});

src/convertAstToTypeScriptCode.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as ts from "typescript";
2+
3+
export const convertAstToTypeScriptCode = (sourceFile: ts.SourceFile): string => {
4+
const printer = ts.createPrinter(); // AST -> TypeScriptに変換
5+
return printer.printFile(sourceFile);
6+
};

src/generateInterface.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { transform } from "./transform";
2+
import * as Traverse from "./traverse";
3+
import { JSONSchema4 } from "json-schema";
4+
5+
interface Params {
6+
name: string;
7+
schemas: JSONSchema4;
8+
}
9+
10+
const code = `
11+
interface DummyInterface {
12+
13+
}
14+
`;
15+
16+
export const generateInterface = (params: Params): string => {
17+
const result = transform(code, [
18+
Traverse.InterfaceName.traverse({ name: params.name }),
19+
Traverse.InterfaceAppendMembers.traverse({
20+
schemas: params.schemas,
21+
}),
22+
]);
23+
console.log({ result });
24+
return result;
25+
};

src/index.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
export const hello = (name: string): string => {
2-
const params = {
3-
hoge: 1,
4-
fuga: 2,
5-
};
6-
return `Hello ${name} ${JSON.stringify(params)}`;
1+
import * as ts from "typescript";
2+
3+
const code = `
4+
interface Hoge {
5+
name: string;
6+
}
7+
8+
const hoge: Hoge = {
9+
name: "hoge",
710
};
11+
`;
12+
13+
const source = ts.createSourceFile("", code, ts.ScriptTarget.ESNext);
14+
15+
const result = ts.transform(source, []);
16+
result.dispose();
17+
18+
const printer = ts.createPrinter();
819

9-
console.log(hello("Your name"));
20+
console.log(printer.printFile(result.transformed[0] as ts.SourceFile));

src/transform.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as ts from "typescript";
2+
import { convertAstToTypeScriptCode } from "./convertAstToTypeScriptCode";
3+
import * as Types from "./types";
4+
5+
export const transform = (code: string, transformers: Types.TransformerFactory<ts.SourceFile>[] = []) => {
6+
const source = ts.createSourceFile("", code, ts.ScriptTarget.ESNext);
7+
const result = ts.transform(source, transformers);
8+
result.dispose();
9+
if (result.transformed.length > 1) {
10+
console.error(result.transformed);
11+
throw new Error("1個以上あるよ");
12+
}
13+
return convertAstToTypeScriptCode(result.transformed[0]);
14+
};

0 commit comments

Comments
 (0)