1. 什么是 Babel AST Format?
The Babel parser generates AST according to Babel AST format. It is based on ESTree spec with some deviations.
2. 本期涉及哪些 AST node types?
本期涉及:
3. 示例
3.1. Identifier
AST:An identifier.
export interface Identifier extends BaseNode {
type: "Identifier";
name: string;
decorators: Array<Decorator> | null;
optional: boolean | null;
typeAnnotation: TypeAnnotation | TSTypeAnnotation | Noop | null;
}
3.2. Program
AST:A complete program source tree.
export interface Program extends BaseNode {
type: "Program";
body: Array<Statement>;
directives: Array<Directive>;
sourceType: "script" | "module";
interpreter: InterpreterDirective | null;
sourceFile: string;
}
3.3. Literals
AST:A literal token. May or may not represent an expression.
export type Literal = StringLiteral | NumericLiteral | NullLiteral | BooleanLiteral | RegExpLiteral | TemplateLiteral | BigIntLiteral | DecimalLiteral;
export interface StringLiteral extends BaseNode {
type: "StringLiteral";
value: string;
}
export interface NumericLiteral extends BaseNode {
type: "NumericLiteral";
value: number;
}
export interface NullLiteral extends BaseNode {
type: "NullLiteral";
}
export interface BooleanLiteral extends BaseNode {
type: "BooleanLiteral";
value: boolean;
}
export interface RegExpLiteral extends BaseNode {
type: "RegExpLiteral";
pattern: string;
flags: string;
}
export interface TemplateLiteral extends BaseNode {
type: "TemplateLiteral";
quasis: Array<TemplateElement>;
expressions: Array<Expression>;
}
export interface BigIntLiteral extends BaseNode {
type: "BigIntLiteral";
value: string;
}
export interface DecimalLiteral extends BaseNode {
type: "DecimalLiteral";
value: string;
}
示例:
"webj2ee", 123, null, true, /^regex$/, 456n, `template`
3.4. Function
AST:A function declaration or expression.
export type Function = FunctionDeclaration | FunctionExpression | ObjectMethod | ArrowFunctionExpression | ClassMethod | ClassPrivateMethod;
3.5. Statements——ExpressionStatement
AST:An expression statement, i.e., a statement consisting of a single expression.
export type Statement = BlockStatement | BreakStatement | ContinueStatement | DebuggerStatement | DoWhileStatement | EmptyStatement | ExpressionStatement | ForInStatement | ForStatement | FunctionDeclaration | IfStatement | LabeledStatement | ReturnStatement | SwitchStatement | ThrowStatement | TryStatement | VariableDeclaration | WhileStatement | WithStatement | ClassDeclaration | ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration | ForOfStatement | ImportDeclaration | DeclareClass | DeclareFunction | DeclareInterface | DeclareModule | DeclareModuleExports | DeclareTypeAlias | DeclareOpaqueType | DeclareVariable | DeclareExportDeclaration | DeclareExportAllDeclaration | InterfaceDeclaration | OpaqueType | TypeAlias | EnumDeclaration | TSDeclareFunction | TSInterfaceDeclaration | TSTypeAliasDeclaration | TSEnumDeclaration | TSModuleDeclaration | TSImportEqualsDeclaration | TSExportAssignment | TSNamespaceExportDeclaration;
export interface ExpressionStatement extends BaseNode {
type: "ExpressionStatement";
expression: Expression;
}
示例:
x++
3.6. Statements——BlockStatement
AST:A block statement, i.e., a sequence of statements surrounded by braces.
export interface BlockStatement extends BaseNode {
type: "BlockStatement";
body: Array<Statement>;
directives: Array<Directive>;
}
示例:
{}
3.7. Statements——EmptyStatement
AST:An empty statement, i.e., a solitary semicolon.
export interface EmptyStatement extends BaseNode {
type: "EmptyStatement";
}
示例:
;;;;
3.8. Statements——DebuggerStatement
AST:A debugger statement.
export interface DebuggerStatement extends BaseNode {
type: "DebuggerStatement";
}
示例:
debugger;
3.9. Statements——WithStatement
AST:A with statement.
export interface WithStatement extends BaseNode {
type: "WithStatement";
object: Expression;
body: Statement;
}
示例:
with (expression) {}
3.10. Statements——Control flow
3.11. Statements——Choice
AST:
// Control flow
export interface ReturnStatement extends BaseNode {
type: "ReturnStatement";
argument: Expression | null;
}
export interface LabeledStatement extends BaseNode {
type: "LabeledStatement";
label: Identifier;
body: Statement;
}
export interface BreakStatement extends BaseNode {
type: "BreakStatement";
label: Identifier | null;
}
export interface ContinueStatement extends BaseNode {
type: "ContinueStatement";
label: Identifier | null;
}
// Choice
export interface IfStatement extends BaseNode {
type: "IfStatement";
test: Expression;
consequent: Statement;
alternate: Statement | null;
}
export interface SwitchStatement extends BaseNode {
type: "SwitchStatement";
discriminant: Expression;
cases: Array<SwitchCase>;
}
export interface SwitchCase extends BaseNode {
type: "SwitchCase";
test: Expression | null;
consequent: Array<Statement>;
}
示例1:
switch(condition){
case v1:
break;
}
示例2:
function fn(){
for(;;){
if(v1)
return;
if(v2)
break;
if(v3)
continue;
}
}
3.12. Statements——Exceptions
AST:
export interface TryStatement extends BaseNode {
type: "TryStatement";
block: BlockStatement;
handler: CatchClause | null;
finalizer: BlockStatement | null;
}
export interface ThrowStatement extends BaseNode {
type: "ThrowStatement";
argument: Expression;
}
export interface CatchClause extends BaseNode {
type: "CatchClause";
param: Identifier | ArrayPattern | ObjectPattern | null;
body: BlockStatement;
}
示例:
try{
}catch(ex){
throw ex;
}finally{
}
3.13. Statements——Loops
AST:
export type Loop = DoWhileStatement | ForInStatement | ForStatement | WhileStatement | ForOfStatement;
export type While = DoWhileStatement | WhileStatement;
export interface ForStatement extends BaseNode {
type: "ForStatement";
init: VariableDeclaration | Expression | null;
test: Expression | null;
update: Expression | null;
body: Statement;
}
export interface ForOfStatement extends BaseNode {
type: "ForOfStatement";
left: VariableDeclaration | LVal;
right: Expression;
body: Statement;
await: boolean;
}
export interface ForInStatement extends BaseNode {
type: "ForInStatement";
left: VariableDeclaration | LVal;
right: Expression;
body: Statement;
}
export interface DoWhileStatement extends BaseNode {
type: "DoWhileStatement";
test: Expression;
body: Statement;
}
export interface WhileStatement extends BaseNode {
type: "WhileStatement";
test: Expression;
body: Statement;
}
示例:
for(;;){}
for(let k in o){}
for(let k of o){}
while(condition){}
do{}while(condition)
3.14. Statements——Declarations
AST:
export interface FunctionDeclaration extends BaseNode {
type: "FunctionDeclaration";
id: Identifier | null;
params: Array<Identifier | Pattern | RestElement | TSParameterProperty>;
body: BlockStatement;
generator: boolean;
async: boolean;
declare: boolean | null;
returnType: TypeAnnotation | TSTypeAnnotation | Noop | null;
typeParameters: TypeParameterDeclaration | TSTypeParameterDeclaration | Noop | null;
}
export interface VariableDeclaration extends BaseNode {
type: "VariableDeclaration";
kind: "var" | "let" | "const";
declarations: Array<VariableDeclarator>;
declare: boolean | null;
}
export interface VariableDeclarator extends BaseNode {
type: "VariableDeclarator";
id: LVal;
init: Expression | null;
definite: boolean | null;
}
示例:
function fn(){}
var x, y, z;
参考资料1:
Babel AST Format: https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md