@babel/parser

These are the core @babel/parser (babylon) AST node types.

Node objects

AST nodes are represented as Node objects, which may have any prototype inheritance but which implement the following interface:

interface Node {
  type: string;
  loc: SourceLocation | null;
}
复制代码

The type field is a string representing the AST variant type. Each subtype of Node is documented below with the specific string of its type field. You can use this field to determine which interface a node implements.

The loc field represents the source location information of the node. If the node contains no information about the source location, the field is null; otherwise it is an object consisting of a start position (the position of the first character of the parsed source region) and an end position (the position of the first character after the parsed source region):

interface SourceLocation {
  source: string | null;
  start: Position;
  end: Position;
}
复制代码

Each Position object consists of a line number (1-indexed) and a column number (0-indexed):

interface Position {
  line: number; // >= 1
  column: number; // >= 0
}
复制代码

Changes

@babel/parser (Babylon) v7

Flow: Node renamed from ExistentialTypeParam to ExistsTypeAnnotation #322

Flow: Node renamed from NumericLiteralTypeAnnotation to NumberLiteralTypeAnnotation babel/babylon#332

Flow: Node Variance which replaces the string value of the variance field on several nodes babel/babylon#333

Flow: ObjectTypeIndexer location info matches Flow's better babel/babylon#228

Node ForAwaitStatement has been removed #349 in favor of modifying ForOfStatement

RestProperty and SpreadProperty have been dropped in favor of RestElement and SpreadElement.

Identifier

interface Identifier <: Expression, Pattern {
  type: "Identifier";
  name: string;
}
复制代码

An identifier. Note that an identifier may be an expression or a destructuring pattern.

PrivateName

interface PrivateName <: Expression, Pattern {
  type: "PrivateName";
  id: Identifier;
}
复制代码

A Private Name Identifier.

Literals

interface Literal <: Expression { }
复制代码

A literal token. May or may not represent an expression.

RegExpLiteral

interface RegExpLiteral <: Literal {
  type: "RegExpLiteral";
  pattern: string;
  flags: string;
}
复制代码

NullLiteral

interface NullLiteral <: Literal {
  type: "NullLiteral";
}
复制代码

StringLiteral

interface StringLiteral <: Literal {
  type: "StringLiteral";
  value: string;
}
复制代码

BooleanLiteral

interface BooleanLiteral <: Literal {
  type: "BooleanLiteral";
  value: boolean;
}
复制代码

NumericLiteral

interface NumericLiteral <: Literal {
  type: "NumericLiteral";
  value: number;
}
复制代码

Programs

interface Program <: Node {
  type: "Program";
  interpreter: InterpreterDirective | null;
  sourceType: "script" | "module";
  body: [ Statement | ModuleDeclaration ];
  directives: [ Directive ];
}
复制代码

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".

Functions

interface Function <: Node {
  id: Identifier | null;
  params: [ Pattern ];
  body: BlockStatement;
  generator: boolean;
  async: boolean;
}
复制代码

A function declaration or expression.

Statements

interface Statement <: Node { }
复制代码

Any statement.

ExpressionStatement

interface ExpressionStatement <: Statement {
  type: "ExpressionStatement";
  expression: Expression;
}
复制代码

An expression statement, i.e., a statement consisting of a single expression.

BlockStatement

interface BlockStatement <: Statement {
  type: "BlockStatement";
  body: [ Statement ];
  directives: [ Directive ];
}
复制代码

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

EmptyStatement

interface EmptyStatement <: Statement {
  type: "EmptyStatement";
}
复制代码

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

DebuggerStatement

interface DebuggerStatement <: Statement {
  type: "DebuggerStatement";
}
复制代码

A debugger statement.

WithStatement

interface WithStatement <: Statement {
  type: "WithStatement";
  object: Expression;
  body: Statement;
}
复制代码

A with statement.

Control flow

ReturnStatement

interface ReturnStatement <: Statement {
  type: "ReturnStatement";
  argument: Expression | null;
}
复制代码

A return statement.

LabeledStatement

interface LabeledStatement <: Statement {
  type: "LabeledStatement";
  label: Identifier;
  body: Statement;
}
复制代码

A labeled statement, i.e., a statement prefixed by a break/continue label.

BreakStatement

interface BreakStatement <: Statement {
  type: "BreakStatement";
  label: Identifier | null;
}
复制代码

A break statement.

ContinueStatement

interface ContinueStatement <: Statement {
  type: "ContinueStatement";
  label: Identifier | null;
}
复制代码

A continue statement.

Choice

IfStatement

interface IfStatement <: Statement {
  type: "IfStatement";
  test: Expression;
  consequent: Statement;
  alternate: Statement | null;
}
复制代码

An if statement.

SwitchStatement

interface SwitchStatement <: Statement {
  type: "SwitchStatement";
  discriminant: Expression;
  cases: [ SwitchCase ];
}
复制代码

A switch statement.

SwitchCase
interface SwitchCase <: Node {
  type: "SwitchCase";
  test: Expression | null;
  consequent: [ Statement ];
}
复制代码

A case (if test is an Expression) or default (if test === null) clause in the body of a switch statement.

Exceptions

ThrowStatement

interface ThrowStatement <: Statement {
  type: "ThrowStatement";
  argument: Expression;
}
复制代码

A throw statement.

TryStatement

interface TryStatement <: Statement {
  type: "TryStatement";
  block: BlockStatement;
  handler: CatchClause | null;
  finalizer: BlockStatement | null;
}
复制代码

A try statement. If handler is null then finalizer must be a BlockStatement.

CatchClause
interface CatchClause <: Node {
  type: "CatchClause";
  param: Pattern | null;
  body: BlockStatement;
}
复制代码

A catch clause following a try block.

Loops

WhileStatement

interface WhileStatement <: Statement {
  type: "WhileStatement";
  test: Expression;
  body: Statement;
}
复制代码

A while statement.

DoWhileStatement

interface DoWhileStatement <: Statement {
  type: "DoWhileStatement";
  body: Statement;
  test: Expression;
}
复制代码

A do/while statement.

ForStatement

interface ForStatement <: Statement {
  type: "ForStatement";
  init: VariableDeclaration | Expression | null;
  test: Expression | null;
  update: Expression | null;
  body: Statement;
}
复制代码

A for statement.

ForInStatement

interface ForInStatement <: Statement {
  type: "ForInStatement";
  left: VariableDeclaration |  Expression;
  right: Expression;
  body: Statement;
}
复制代码

A for/in statement.

ForOfStatement

interface ForOfStatement <: ForInStatement {
  type: "ForOfStatement";
  await: boolean;
}
复制代码

Declarations

interface Declaration <: Statement { }
复制代码

Any declaration node. Note that declarations are considered statements; this is because declarations can appear in any statement context.

FunctionDeclaration

interface FunctionDeclaration <: Function, Declaration {
  type: "FunctionDeclaration";
  id: Identifier;
}
复制代码

A function declaration. Note that unlike in the parent interface Function, the id cannot be null, except when this is the child of an ExportDefaultDeclaration.

VariableDeclaration

interface VariableDeclaration <: Declaration {
  type: "VariableDeclaration";
  declarations: [ VariableDeclarator ];
  kind: "var" | "let" | "const";
}
复制代码

A variable declaration.

VariableDeclarator

interface VariableDeclarator <: Node {
  type: "VariableDeclarator";
  id: Pattern;
  init: Expression | null;
}
复制代码

A variable declarator.

Misc

Decorator

interface Decorator <: Node {
  type: "Decorator";
  expression: Expression;
}
复制代码

Directive

interface Directive <: Node {
  type: "Directive";
  value: DirectiveLiteral;
}
复制代码

DirectiveLiteral

interface DirectiveLiteral <: StringLiteral {
  type: "DirectiveLiteral";
}
复制代码

InterpreterDirective

interface InterpreterDirective <: StringLiteral {
  type: "InterpreterDirective";
}
复制代码

Expressions

interface Expression <: Node { }
复制代码

Any expression node. Since the left-hand side of an assignment may be any expression in general, an expression can also be a pattern.

Super

interface Super <: Node {
    type: "Super";
}
复制代码

A super pseudo-expression.

Import

interface Import <: Node {
    type: "Import";
}
复制代码

A import pseudo-expression.

ThisExpression

interface ThisExpression <: Expression {
  type: "ThisExpression";
}
复制代码

A this expression.

ArrowFunctionExpression

interface ArrowFunctionExpression <: Function, Expression {
  type: "ArrowFunctionExpression";
  body: BlockStatement | Expression;
}
复制代码

A fat arrow function expression, e.g., let foo = (bar) => { /* body */ }.

YieldExpression

interface YieldExpression <: Expression {
  type: "YieldExpression";
  argument: Expression | null;
  delegate: boolean;
}
复制代码

A yield expression.

AwaitExpression

interface AwaitExpression <: Expression {
  type: "AwaitExpression";
  argument: Expression | null;
}
复制代码

A await expression.

ArrayExpression

interface ArrayExpression <: Expression {
  type: "ArrayExpression";
  elements: [ Expression | SpreadElement | null ];
}
复制代码

An array expression.

ObjectExpression

interface ObjectExpression <: Expression {
  type: "ObjectExpression";
  properties: [ ObjectProperty | ObjectMethod | SpreadElement ];
}
复制代码

An object expression.

ObjectMember

interface ObjectMember <: Node {
  key: Expression;
  computed: boolean;
  decorators: [ Decorator ];
}
复制代码
ObjectProperty
interface ObjectProperty <: ObjectMember {
  type: "ObjectProperty";
  shorthand: boolean;
  value: Expression;
}
复制代码
ObjectMethod
interface ObjectMethod <: ObjectMember, Function {
  type: "ObjectMethod";
  kind: "get" | "set" | "method";
}
复制代码

FunctionExpression

interface FunctionExpression <: Function, Expression {
  type: "FunctionExpression";
}
复制代码

A function expression.

Unary operations

UnaryExpression

interface UnaryExpression <: Expression {
  type: "UnaryExpression";
  operator: UnaryOperator;
  prefix: boolean;
  argument: Expression;
}
复制代码

A unary operator expression.

UnaryOperator
enum UnaryOperator {
  "-" | "+" | "!" | "~" | "typeof" | "void" | "delete" | "throw"
}
复制代码

A unary operator token.

UpdateExpression

interface UpdateExpression <: Expression {
  type: "UpdateExpression";
  operator: UpdateOperator;
  argument: Expression;
  prefix: boolean;
}
复制代码

An update (increment or decrement) operator expression.

UpdateOperator
enum UpdateOperator {
  "++" | "--"
}
复制代码

An update (increment or decrement) operator token.

Binary operations

BinaryExpression

interface BinaryExpression <: Expression {
  type: "BinaryExpression";
  operator: BinaryOperator;
  left: Expression;
  right: Expression;
}
复制代码

A binary operator expression.

BinaryOperator
enum BinaryOperator {
  "==" | "!=" | "===" | "!=="
     | "<" | "<=" | ">" | ">="
     | "<<" | ">>" | ">>>"
     | "+" | "-" | "*" | "/" | "%"
     | "**" | "|" | "^" | "&" | "in"
     | "instanceof"
     | "|>"
}
复制代码

A binary operator token.

AssignmentExpression

interface AssignmentExpression <: Expression {
  type: "AssignmentExpression";
  operator: AssignmentOperator;
  left: Pattern | Expression;
  right: Expression;
}
复制代码

An assignment operator expression.

AssignmentOperator
enum AssignmentOperator {
  "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "**="
    | "<<=" | ">>=" | ">>>="
    | "|=" | "^=" | "&="
}
复制代码

An assignment operator token.

LogicalExpression

interface LogicalExpression <: Expression {
  type: "LogicalExpression";
  operator: LogicalOperator;
  left: Expression;
  right: Expression;
}
复制代码

A logical operator expression.

LogicalOperator
enum LogicalOperator {
  "||" | "&&" | "??"
}
复制代码

A logical operator token.

SpreadElement

interface SpreadElement <: Node {
  type: "SpreadElement";
  argument: Expression;
}
复制代码

MemberExpression

interface MemberExpression <: Expression, Pattern {
  type: "MemberExpression";
  object: Expression | Super;
  property: Expression;
  computed: boolean;
  optional: boolean | null;
}
复制代码

A member expression. If computed is true, the node corresponds to a computed (a[b]) member expression and property is an Expression. If computed is false, the node corresponds to a static (a.b) member expression and property is an Identifier. The optional flags indicates that the member expression can be called even if the object is null or undefined. If this is the object value (null/undefined) should be returned.

BindExpression

interface BindExpression <: Expression {
    type: "BindExpression";
    object: Expression | null;
    callee: Expression;
}
复制代码

If object is null, then callee should be a MemberExpression.

ConditionalExpression

interface ConditionalExpression <: Expression {
  type: "ConditionalExpression";
  test: Expression;
  alternate: Expression;
  consequent: Expression;
}
复制代码

A conditional expression, i.e., a ternary ?/: expression.

CallExpression

interface CallExpression <: Expression {
  type: "CallExpression";
  callee: Expression | Super | Import;
  arguments: [ Expression | SpreadElement ];
  optional: boolean | null;
}
复制代码

A function or method call expression.

NewExpression

interface NewExpression <: CallExpression {
  type: "NewExpression";
  optional: boolean | null;
}
复制代码

A new expression.

SequenceExpression

interface SequenceExpression <: Expression {
  type: "SequenceExpression";
  expressions: [ Expression ];
}
复制代码

A sequence expression, i.e., a comma-separated sequence of expressions.

DoExpression

interface DoExpression <: Expression {
  type: "DoExpression";
  body: BlockStatement
}
复制代码

Template Literals

TemplateLiteral

interface TemplateLiteral <: Expression {
  type: "TemplateLiteral";
  quasis: [ TemplateElement ];
  expressions: [ Expression ];
}
复制代码

TaggedTemplateExpression

interface TaggedTemplateExpression <: Expression {
  type: "TaggedTemplateExpression";
  tag: Expression;
  quasi: TemplateLiteral;
}
复制代码

TemplateElement

interface TemplateElement <: Node {
  type: "TemplateElement";
  tail: boolean;
  value: {
    cooked: string | null;
    raw: string;
  };
}
复制代码

Patterns

interface Pattern <: Node { }
复制代码

ObjectPattern

interface AssignmentProperty <: ObjectProperty {
  value: Pattern;
}

interface ObjectPattern <: Pattern {
  type: "ObjectPattern";
  properties: [ AssignmentProperty | RestElement ];
}
复制代码

ArrayPattern

interface ArrayPattern <: Pattern {
  type: "ArrayPattern";
  elements: [ Pattern | null ];
}
复制代码

RestElement

interface RestElement <: Pattern {
  type: "RestElement";
  argument: Pattern;
}
复制代码

AssignmentPattern

interface AssignmentPattern <: Pattern {
  type: "AssignmentPattern";
  left: Pattern;
  right: Expression;
}
复制代码

Classes

interface Class <: Node {
  id: Identifier | null;
  superClass: Expression | null;
  body: ClassBody;
  decorators: [ Decorator ];
}
复制代码

ClassBody

interface ClassBody <: Node {
  type: "ClassBody";
  body: [ ClassMethod | ClassPrivateMethod | ClassProperty | ClassPrivateProperty ];
}
复制代码

ClassMethod

interface ClassMethod <: Function {
  type: "ClassMethod";
  key: Expression;
  kind: "constructor" | "method" | "get" | "set";
  computed: boolean;
  static: boolean;
  decorators: [ Decorator ];
}
复制代码

ClassPrivateMethod

interface ClassPrivateMethod <: Function {
  type: "ClassPrivateMethod";
  key: PrivateName;
  kind: "method" | "get" | "set";
  static: boolean;
  decorators: [ Decorator ];
}
复制代码

ClassProperty

interface ClassProperty <: Node {
  type: "ClassProperty";
  key: Expression;
  value: Expression;
  static: boolean;
  computed: boolean;
}
复制代码

ClassPrivateProperty

interface ClassPrivateProperty <: Node {
  type: "ClassPrivateProperty";
  key: PrivateName;
  value: Expression;
  static: boolean;
}
复制代码

ClassDeclaration

interface ClassDeclaration <: Class, Declaration {
  type: "ClassDeclaration";
  id: Identifier;
}
复制代码

ClassExpression

interface ClassExpression <: Class, Expression {
  type: "ClassExpression";
}
复制代码

MetaProperty

interface MetaProperty <: Expression {
  type: "MetaProperty";
  meta: Identifier;
  property: Identifier;
}
复制代码

Modules

ModuleDeclaration

interface ModuleDeclaration <: Node { }
复制代码

A module import or export declaration.

ModuleSpecifier

interface ModuleSpecifier <: Node {
  local: Identifier;
}
复制代码

A specifier in an import or export declaration.

Imports

ImportDeclaration

interface ImportDeclaration <: ModuleDeclaration {
  type: "ImportDeclaration";
  specifiers: [ ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ];
  source: Literal;
}
复制代码

An import declaration, e.g., import foo from "mod";.

ImportSpecifier

interface ImportSpecifier <: ModuleSpecifier {
  type: "ImportSpecifier";
  imported: Identifier;
}
复制代码

An imported variable binding, e.g., {foo} in import {foo} from "mod" or {foo as bar} in import {foo as bar} from "mod". The imported field refers to the name of the export imported from the module. The local field refers to the binding imported into the local module scope. If it is a basic named import, such as in import {foo} from "mod", both imported and local are equivalent Identifier nodes; in this case an Identifier node representing foo. If it is an aliased import, such as in import {foo as bar} from "mod", the imported field is an Identifier node representing foo, and the local field is an Identifier node representing bar.

ImportDefaultSpecifier

interface ImportDefaultSpecifier <: ModuleSpecifier {
  type: "ImportDefaultSpecifier";
}
复制代码

A default import specifier, e.g., foo in import foo from "mod.js".

ImportNamespaceSpecifier

interface ImportNamespaceSpecifier <: ModuleSpecifier {
  type: "ImportNamespaceSpecifier";
}
复制代码

A namespace import specifier, e.g., * as foo in import * as foo from "mod.js".

Exports

ExportNamedDeclaration

interface ExportNamedDeclaration <: ModuleDeclaration {
  type: "ExportNamedDeclaration";
  declaration: Declaration | null;
  specifiers: [ ExportSpecifier ];
  source: Literal | null;
}
复制代码

An export named declaration, e.g., export {foo, bar};, export {foo} from "mod";, export var foo = 1; or export * as foo from "bar";.

Note: Having declaration populated with non-empty specifiers or non-null source results in an invalid state.

ExportSpecifier

interface ExportSpecifier <: ModuleSpecifier {
  type: "ExportSpecifier";
  exported: Identifier;
}
复制代码

An exported variable binding, e.g., {foo} in export {foo} or {bar as foo} in export {bar as foo}. The exported field refers to the name exported in the module. The local field refers to the binding into the local module scope. If it is a basic named export, such as in export {foo}, both exported and local are equivalent Identifier nodes; in this case an Identifier node representing foo. If it is an aliased export, such as in export {bar as foo}, the exported field is an Identifier node representing foo, and the local field is an Identifier node representing bar.

ExportDefaultDeclaration

interface OptFunctionDeclaration <: FunctionDeclaration {
  id: Identifier | null;
}

interface OptClassDeclaration <: ClassDeclaration {
  id: Identifier | null;
}

interface ExportDefaultDeclaration <: ModuleDeclaration {
  type: "ExportDefaultDeclaration";
  declaration: OptFunctionDeclaration | OptClassDeclaration | Expression;
}
复制代码

An export default declaration, e.g., export default function () {}; or export default 1;.

ExportAllDeclaration

interface ExportAllDeclaration <: ModuleDeclaration {
  type: "ExportAllDeclaration";
  source: Literal;
}
复制代码

An export batch declaration, e.g., export * from "mod";.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值