Typescript AST(抽象语法树)
TypeScript AST
的读取是一个挑战,这个库封装了 TypeScript compiler API,并且它很简单。
手动安装
yarn add @zeronejs/ast-ts
1
npm install @zeronejs/ast-ts
1
解释器
根据不同的声明方式 Zerone
提供了多种解释器
Imports
假设您有一个文件 source.ts
,并提供以下三种导入方式
import * as ts from 'typescript';
import { readFileSync } from 'fs';
import path, { join } from 'path';
1
2
3
4
2
3
4
创建一个文件 interpret.ts
, 并引入@zeronejs/ast-ts
提示
InterpretCore
是解释器的核心依赖
import { ImportsInterpret, InterpretCore } from '@zeronejs/ast-ts';
import { join } from 'path';
const interpretCore = new InterpretCore(join(__dirname, 'source.ts'));
const imports = new ImportsInterpret(interpretCore).interpret();
console.log(imports)
// => [
// { from: 'typescript', elements: [], namespaceImport: 'ts' },
// { from: 'fs', elements: [ 'readFileSync' ] },
// { from: 'path', elements: [ 'join' ], defalutImport: 'path' }
// ]
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Classes
在您的源文件中声明一个类
/**
* 用户表
*/
@Entity()
export class UserEntity {
/**
* id
*/
@PrimaryGeneratedColumn()
id: number;
/**
* 用户名
*/
@Index({ unique: true })
@Column({ unique: true })
username: string;
/**
* 备注
*/
@Column({ default: 'desc' })
desc?: string;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use
import { ClassesInterpret, InterpretCore } from '@zeronejs/ast-ts';
import { join } from 'path';
const interpretCore = new InterpretCore(join(__dirname, 'source.ts'));
const classes = new ClassesInterpret(interpretCore).interpret();
console.log(imports)
/**
* => [
{
"name": "UserEntity",
"decorators": [{"name": "Entity",...}],
"documentation": "用户表",
"properties": [
...
{
"name": "username",
"documentation": "用户名",
"isOptional": false,
"type": {"value": "string","typeReferences": []}
"decorators": [
{
"name": "Index",
"expression": {
"args": [{"unique": "true"}]
}
},
...
],
},
...
]
}
]
*
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
enums
获取所有枚举
import { EnumsInterpret, InterpretCore } from '@zeronejs/ast-ts';
import { join } from 'path';
const interpretCore = new InterpretCore(join(__dirname, 'source.ts'));
const enums = new EnumsInterpret(interpretCore).interpret();
console.log(enums)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
variables
获取所有变量
import { VariableStatement, InterpretCore } from '@zeronejs/ast-ts';
import { join } from 'path';
const interpretCore = new InterpretCore(join(__dirname, 'source.ts'));
const vars = new VariableStatement(interpretCore).interpret();
console.log(vars)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
typeAlias
获取所有类型别名
import { TypeAliasDeclaration, InterpretCore } from '@zeronejs/ast-ts';
import { join } from 'path';
const interpretCore = new InterpretCore(join(__dirname, 'source.ts'));
const typeAlias = new TypeAliasDeclaration(interpretCore).interpret();
console.log(typeAlias)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
interfaces
获取所有接口
import { InterfaceDeclaration, InterpretCore } from '@zeronejs/ast-ts';
import { join } from 'path';
const interpretCore = new InterpretCore(join(__dirname, 'source.ts'));
const interfaces = new InterfaceDeclaration(interpretCore).interpret();
console.log(interfaces)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8