@Dale-Lin
2022-09-18T14:01:31.000000Z
字数 6014
阅读 837
Babel
@babel/parser 将 js 代码解析生成 AST 节点:
import * as babelParser from '@babel/parser'const parseAST = (code: string, options: babelParser.ParserOptions) => babelParser.parse(code, opt)
ParserOptions.sourceType 指定了代码被处理的模式。可以是 "script","module","unambiguous"。默认是 "script";"unambiguous" 模式下 babel 会根据 ES6 import/export 语句推测;使用了 ES6 import/export 的文件被判断成 "module"。解析出来的根节点 type 为
File。
babel 使用基于 ESTree 修改的 AST。
一个 AST 可以有一个或多个节点构成,它们组成可用于静态分析的程序语法。
AST 的节点基础定义:
interface Node {type: string;loc: SourceLocation | null;}
type 表示 AST 类型,可以用来判断这个 AST 节点实现了哪些接口;所有类型的节点都会继承 Node 类。
loc 表示源代码里这个节点的位置,如果节点不包含源代码位置信息,则为 null:
interface SourceLocation {source: string | nullstart: Positionend: Position}interface Position {line: number // >= 1column: number // >= 0
<:符号表示继承的父接口
interface Identifier {type: "Identifier";name: string;}
标识符可以是声明或解构。
interface PrivateName {type: "PrivateName";id: Identifier}
私有名标识符可以是声明或解构
字面量token,可能代表一个表达式定义的常量,例如数组、布尔、浮点数、整数、对象、正则、字符串。
interface RegExpLiteral {type: "RegExpLiteral";pattern: string;flag: string;}
interface NullLiteral {type: "NullLiteral"}
interface StringLiteral {type: "StringLiteral";value: string;}
interface BooleanLiteral {type: "BooleanLiteral";value: boolean}
interface NumericLiteral {type: "NumericLiteral";value: number;}
BigIntLiteral 的 value 字符串不包含后缀 n。
interface BigIntLiteral {type: "BigIntLiteral";value: string}
一个完整的程序树。
如果源码被解析成 ES6 模块,Parser 必须指定 sourceType 为 "module";否则 sourceType 必须是 "script"。
interface Programs {type: "Program";interpreter: InterpreterDirective | null;sourceType: "script" | "module";body: [ Statement | ModuleDeclaration ];directives: [ Directive ];}
函数声明或者表达式
interface Function {id: Identifier | null;params: [ Pattern ];body: BlockStatement;generator: boolean;async: boolean;}
表达式语句,例如单行表达式。
interface ExpressionStatement {type: "ExpressionStatement";expression: Expression;}
块语句,例如花括号里的一系列语句。
interface BlockStatement {type: "BlockStatement";body: [ Statement ];directives: [ Directive ];}
例如一个单独的分号
interface EmptyStatement {type: "EmptyStatement";}
debugger 语句。
interface DebuggerStatement {type: "DebuggerStatement";}
with 语句。
interface WithStatement {type: "WithStatement";object: Expression;body: Statement;}
interface ReturnStatement {type: "ReturnStatement";argument: Expression | null}
标记语句,例如 break/continue 标记后面的语句。
interface LabeledStatement {type: "LabeledStatement";label: Identifier;body: Statement}
interface BreakStatement {type: "BreakStatement";label: Identifier | null;}
interface ContinueStatement {type: "ContinueStatement";label: Identifier | null}
interface IfStatement {type: "IfStatement";test: Expression;consequent: Statement;alternate: Statement | null;}
interface SwitchStatement {type: "SwitchStatement";discriminant: Expression;cases: [ SwitchCase ];}
interface SwitchCase {type: "SwitchCase";test: Expression | null;consequent: [ Statement ];}
interface ThrowStatement {type: "ThrowStatement";argument: Expression;}
interface TryStatement {type: "TryStatement";block: BlockStatement;handler: CatchClause | null;finalizer: BlockStatement | null;}
interface CatchClause {type: "CatchClause";param?: Pattern;body: BlockStatement;}
interface WhileStatement {type: "WhileStatement";test: Expression;body: Statement;}
interface DoWhileStatement {type: "DoWhileStatement";body: Statement;test: Expression;}
interface ForStatement {type: "ForStatement";init: VariableDeclaration | Expression | null;test: Expression | null;update: Expression | null;body: Statement;}
interface ForInStatement {type: "ForInStatement";left: VariableDeclaration | Expression;right: Expression;body: Statement;}
interface ForOfStatement {type: "ForOfStatement";await: boolean;}
声明节点,声明被视为语句,因为声明可以出现在任意语句上下文中。
和
Function类型的区别是id不能为null,除非是ExportDefaultDeclaration的子类。
interface FunctionDeclaration {type: "FunctionDeclaration";id: Identifier;}
interface VariableDeclaration {type: "VariableDeclaration";declarations: [ VariableDeclarator ];kind: "var" | "let" | "const";}
interface VariableDeclarator {type: "VariableDeclarator";id: Pattern;init: Expression | null;}
表达式节点,赋值语句左侧必须是表达式,一个表达式也可以是一个 pattern
interface Super {type: "Super";}
interface {type: "Import";}
to be continue: https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md
模块 import 或 export 声明。
interface ModuleDeclaration {}
在 import 或 export 声明中的 local 变量名。
interface ModuleSpecifier {local: Identifier;}
import 声明,例如 import foo from "mod";
importKind 只有在 babel-parser 启用了
flowplugin 的时候才会有值。
interface ImportDeclaration {type: "ImportDeclaration";importKind: null | "type" | "typeof" | "value";specifiers: [ ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ];source: Literal;attributes?: [ ImportAttribute ];}
imported 变量绑定赋值,例如:
import {foo} from "mod"; 的 {foo};import {foo as bar} from "mod"; 的 {foo as bar};imported 字段表示从被引模块导出的变量名称,local 字段表示当前模块绑定的变量名称。
interface ImportSpecifier {type: "ImportSpecifier";imported: Identifier;}
默认导入标识变量,例如 import foo from "mod.js" 的 foo。
interface ImportDefaultSpecifier {type: "ImportDefaultSpecifier";}
命名空间导入,例如 import * as foo from "mod.js" 的 * as foo。
interface ImportNamespaceSpecifier {type: "ImportNamespaceSpecifier";}
ImportDeclaration 上的属性标识。
interface ImportAttribute {type: "ImportAttribute";key: Identifier;value: StringLiteral;}