[关闭]
@Dale-Lin 2022-09-18T22:01:31.000000Z 字数 6014 阅读 517

@babel/parser

Babel


@babel/parser 将 js 代码解析生成 AST 节点:

  1. import * as babelParser from '@babel/parser'
  2. const parseAST = (code: string, options: babelParser.ParserOptions) => babelParser.parse(code, opt)

解析出来的根节点 type 为 File


ASTs Node

babel 使用基于 ESTree 修改的 AST。

一个 AST 可以有一个或多个节点构成,它们组成可用于静态分析的程序语法。

AST 的节点基础定义:

  1. interface Node {
  2. type: string;
  3. loc: SourceLocation | null;
  4. }

type 表示 AST 类型,可以用来判断这个 AST 节点实现了哪些接口;所有类型的节点都会继承 Node 类。

loc 表示源代码里这个节点的位置,如果节点不包含源代码位置信息,则为 null:

  1. interface SourceLocation {
  2. source: string | null
  3. start: Position
  4. end: Position
  5. }
  6. interface Position {
  7. line: number // >= 1
  8. column: number // >= 0

Node type

<: 符号表示继承的父接口

Identifier <: Expression, Pattern

  1. interface Identifier {
  2. type: "Identifier";
  3. name: string;
  4. }

标识符可以是声明或解构。


PrivateName <: Expression, Pattern

  1. interface PrivateName {
  2. type: "PrivateName";
  3. id: Identifier
  4. }

私有名标识符可以是声明或解构


Literals <: Expression

字面量token,可能代表一个表达式定义的常量,例如数组、布尔、浮点数、整数、对象、正则、字符串。

RegExpLiteral <: Literal

  1. interface RegExpLiteral {
  2. type: "RegExpLiteral";
  3. pattern: string;
  4. flag: string;
  5. }

NullLiteral <: Literal

  1. interface NullLiteral {
  2. type: "NullLiteral"
  3. }

StringLiteral <: Literal

  1. interface StringLiteral {
  2. type: "StringLiteral";
  3. value: string;
  4. }

BooleanLiteral <: Literal

  1. interface BooleanLiteral {
  2. type: "BooleanLiteral";
  3. value: boolean
  4. }

NumericLiteral <: Literal

  1. interface NumericLiteral {
  2. type: "NumericLiteral";
  3. value: number;
  4. }

BigIntLiteral <: Literal

BigIntLiteral 的 value 字符串不包含后缀 n

  1. interface BigIntLiteral {
  2. type: "BigIntLiteral";
  3. value: string
  4. }

Programs <: Node

一个完整的程序树。
如果源码被解析成 ES6 模块,Parser 必须指定 sourceType"module";否则 sourceType 必须是 "script"

  1. interface Programs {
  2. type: "Program";
  3. interpreter: InterpreterDirective | null;
  4. sourceType: "script" | "module";
  5. body: [ Statement | ModuleDeclaration ];
  6. directives: [ Directive ];
  7. }

Function <: Node

函数声明或者表达式

  1. interface Function {
  2. id: Identifier | null;
  3. params: [ Pattern ];
  4. body: BlockStatement;
  5. generator: boolean;
  6. async: boolean;
  7. }

Statements <: Node

ExpressionStatement <: Statement

表达式语句,例如单行表达式。

  1. interface ExpressionStatement {
  2. type: "ExpressionStatement";
  3. expression: Expression;
  4. }

BlockStatement <: Statement

块语句,例如花括号里的一系列语句。

  1. interface BlockStatement {
  2. type: "BlockStatement";
  3. body: [ Statement ];
  4. directives: [ Directive ];
  5. }

EmptyStatement <: Statement

例如一个单独的分号

  1. interface EmptyStatement {
  2. type: "EmptyStatement";
  3. }

DebuggerStatement <: Statement

debugger 语句。

  1. interface DebuggerStatement {
  2. type: "DebuggerStatement";
  3. }

WithStatement <: Statement

with 语句。

  1. interface WithStatement {
  2. type: "WithStatement";
  3. object: Expression;
  4. body: Statement;
  5. }

Control flow

ReturnStatement <: Statement

  1. interface ReturnStatement {
  2. type: "ReturnStatement";
  3. argument: Expression | null
  4. }

LabeledStatement <: Statement

标记语句,例如 break/continue 标记后面的语句。

  1. interface LabeledStatement {
  2. type: "LabeledStatement";
  3. label: Identifier;
  4. body: Statement
  5. }

BreakStatement <: Statement

  1. interface BreakStatement {
  2. type: "BreakStatement";
  3. label: Identifier | null;
  4. }

ContinueStatement <: Statement

  1. interface ContinueStatement {
  2. type: "ContinueStatement";
  3. label: Identifier | null
  4. }

Choice

IfStatement <: Statement

  1. interface IfStatement {
  2. type: "IfStatement";
  3. test: Expression;
  4. consequent: Statement;
  5. alternate: Statement | null;
  6. }

SwitchStatement <: Statement

  1. interface SwitchStatement {
  2. type: "SwitchStatement";
  3. discriminant: Expression;
  4. cases: [ SwitchCase ];
  5. }
SwitchCase <: Node
  1. interface SwitchCase {
  2. type: "SwitchCase";
  3. test: Expression | null;
  4. consequent: [ Statement ];
  5. }

Exceptions

ThrowStatement <: Statement

  1. interface ThrowStatement {
  2. type: "ThrowStatement";
  3. argument: Expression;
  4. }

TryStatement <: Statement

  1. interface TryStatement {
  2. type: "TryStatement";
  3. block: BlockStatement;
  4. handler: CatchClause | null;
  5. finalizer: BlockStatement | null;
  6. }
CatchClause <: Node
  1. interface CatchClause {
  2. type: "CatchClause";
  3. param?: Pattern;
  4. body: BlockStatement;
  5. }

Loops

WhileStatement <: Statement

  1. interface WhileStatement {
  2. type: "WhileStatement";
  3. test: Expression;
  4. body: Statement;
  5. }

DoWhileStatement <: Statement

  1. interface DoWhileStatement {
  2. type: "DoWhileStatement";
  3. body: Statement;
  4. test: Expression;
  5. }

ForStatement <: Statement

  1. interface ForStatement {
  2. type: "ForStatement";
  3. init: VariableDeclaration | Expression | null;
  4. test: Expression | null;
  5. update: Expression | null;
  6. body: Statement;
  7. }

ForInStatement <: Statement

  1. interface ForInStatement {
  2. type: "ForInStatement";
  3. left: VariableDeclaration | Expression;
  4. right: Expression;
  5. body: Statement;
  6. }

ForOfStatement <: ForInStatement

  1. interface ForOfStatement {
  2. type: "ForOfStatement";
  3. await: boolean;
  4. }

Declarations <: Statement

声明节点,声明被视为语句,因为声明可以出现在任意语句上下文中。

FunctionDeclaration <: Function, Declaration

Function 类型的区别是 id 不能为 null,除非是 ExportDefaultDeclaration 的子类。

  1. interface FunctionDeclaration {
  2. type: "FunctionDeclaration";
  3. id: Identifier;
  4. }

VariableDeclaration <: Declaration

  1. interface VariableDeclaration {
  2. type: "VariableDeclaration";
  3. declarations: [ VariableDeclarator ];
  4. kind: "var" | "let" | "const";
  5. }
VariableDeclarator <: Node
  1. interface VariableDeclarator {
  2. type: "VariableDeclarator";
  3. id: Pattern;
  4. init: Expression | null;
  5. }

Misc

todo//


Expression <: Node

表达式节点,赋值语句左侧必须是表达式,一个表达式也可以是一个 pattern

Super <: Node

  1. interface Super {
  2. type: "Super";
  3. }

Import <: Node

  1. interface {
  2. type: "Import";
  3. }

to be continue: https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md


Modules

ModuleDeclaration <: Node

模块 importexport 声明。

  1. interface ModuleDeclaration {}

MoudleSpecifier <: Node

在 import 或 export 声明中的 local 变量名。

  1. interface ModuleSpecifier {
  2. local: Identifier;
  3. }

Imports

ImportDeclaration <: ModuleDeclaration

import 声明,例如 import foo from "mod";

importKind 只有在 babel-parser 启用了 flow plugin 的时候才会有值。

  1. interface ImportDeclaration {
  2. type: "ImportDeclaration";
  3. importKind: null | "type" | "typeof" | "value";
  4. specifiers: [ ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ];
  5. source: Literal;
  6. attributes?: [ ImportAttribute ];
  7. }
ImportSpecifier <: ModuleSpecifier

imported 变量绑定赋值,例如:

imported 字段表示从被引模块导出的变量名称,local 字段表示当前模块绑定的变量名称。

  1. interface ImportSpecifier {
  2. type: "ImportSpecifier";
  3. imported: Identifier;
  4. }
ImportDefaultSpecifier <: ModuleSpecifier

默认导入标识变量,例如 import foo from "mod.js"foo

  1. interface ImportDefaultSpecifier {
  2. type: "ImportDefaultSpecifier";
  3. }
ImportNamespaceSpecifier <: ModuleSpecifier

命名空间导入,例如 import * as foo from "mod.js"* as foo

  1. interface ImportNamespaceSpecifier {
  2. type: "ImportNamespaceSpecifier";
  3. }
ImportAttribute <: Node

ImportDeclaration 上的属性标识。

  1. interface ImportAttribute {
  2. type: "ImportAttribute";
  3. key: Identifier;
  4. value: StringLiteral;
  5. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注