前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【编译技术】:解读 Babel AST Format——06

【编译技术】:解读 Babel AST Format——06

作者头像
WEBJ2EE
发布2020-11-05 10:24:57
4050
发布2020-11-05 10:24:57
举报
文章被收录于专栏:WebJ2EEWebJ2EE

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?

本期涉及:

  • Identifier
  • Literals
    • RegExpLiteral
    • NullLiteral
    • StringLiteral
    • BooleanLiteral
    • NumericLiteral
    • BigIntLiteral
  • Programs
  • Function
  • Statements
    • ExpressionStatement
    • BlockStatement
    • EmptyStatement
    • DebuggerStatement
    • WithStatement
    • Control flow
      • ReturnStatement
      • LabeledStatement
      • BreakStatement
      • ContinueStatement
    • Choice
      • IfStatement
      • SwitchStatement
        • SwitchCase
    • Exceptions
      • ThrowStatement
      • TryStatement
        • CatchClause
    • Loops
      • ‍WhileStatement
      • DoWhileStatement
      • ForStatement
      • ForInStatement
      • ForOfStatement
    • Declarations
      • FunctionDeclaration
      • VariableDeclaration
        • VariableDeclarator

3. 示例

3.1. Identifier

AST:An identifier.

代码语言:javascript
复制
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.

  • Parsers must specify sourceType as "module" if the source has been parsed as an ES6 module. Otherwise, sourceType must be "script".
代码语言:javascript
复制
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.

代码语言:javascript
复制
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;
}

示例:

代码语言:javascript
复制
"webj2ee", 123, null, true, /^regex$/, 456n, `template`

3.4. Function

AST:A function declaration or expression.

代码语言:javascript
复制
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.

代码语言:javascript
复制
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;
}

示例:

代码语言:javascript
复制
x++

3.6. Statements——BlockStatement

AST:A block statement, i.e., a sequence of statements surrounded by braces.

代码语言:javascript
复制
export interface BlockStatement extends BaseNode {
  type: "BlockStatement";
  body: Array<Statement>;
  directives: Array<Directive>;
}

示例:

代码语言:javascript
复制
{}

3.7. Statements——EmptyStatement

AST:An empty statement, i.e., a solitary semicolon.

代码语言:javascript
复制
export interface EmptyStatement extends BaseNode {
  type: "EmptyStatement";
}

示例:

代码语言:javascript
复制
;;;;

3.8. Statements——DebuggerStatement

AST:A debugger statement.

代码语言:javascript
复制
export interface DebuggerStatement extends BaseNode {
  type: "DebuggerStatement";
}

示例:

代码语言:javascript
复制
debugger;

3.9. Statements——WithStatement

AST:A with statement.

代码语言:javascript
复制
export interface WithStatement extends BaseNode {
  type: "WithStatement";
  object: Expression;
  body: Statement;
}

示例:

代码语言:javascript
复制
with (expression) {}

3.10. Statements——Control flow

3.11. Statements——Choice

AST:

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
switch(condition){
    case v1:
        break;
}

示例2:

代码语言:javascript
复制
function fn(){
    for(;;){
        if(v1)
            return;
        if(v2)
            break;
        if(v3)
            continue;
    }
}    

3.12. Statements——Exceptions

AST:

代码语言:javascript
复制
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;
}

示例:

代码语言:javascript
复制
try{
}catch(ex){
    throw ex;
}finally{
}

3.13. Statements——Loops

AST:

代码语言:javascript
复制
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;
}

示例:

代码语言:javascript
复制
for(;;){}
for(let k in o){}
for(let k of o){}
while(condition){}
do{}while(condition)

3.14. Statements——Declarations

AST:

代码语言:javascript
复制
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;
}

示例:

代码语言:javascript
复制
function fn(){}
var x, y, z;

参考资料1:

Babel AST Format: https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-10-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WebJ2EE 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档