Typescript AST

Reading the TypeScript AST is a challenge. The library encapsulates the TypeScript Compiler API and is simple.

Installing

yarn add @zeronejs/ast-ts
1
npm install @zeronejs/ast-ts
1

Interprets

Zerone offers a variety of interpreters depending on how it is declared

Imports

Suppose you have a file source.ts that provides the following three import options

import * as ts from 'typescript';
import { readFileSync } from 'fs';
import path, { join } from 'path';

1
2
3
4

Create a file interpret.ts and import @zeronejs/ast-ts

TIP

InterpretCore is a core dependency for the interpreter

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

Classes

Declare a class to your source file

/**
 * 用户表
 */
@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

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

enums

Get all 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

variables

Get all 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

typeAlias

Get all 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

interfaces

Get all 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