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

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

作者头像
WEBJ2EE
发布2020-10-26 15:10:42
1.1K0
发布2020-10-26 15:10:42
举报
文章被收录于专栏:WebJ2EEWebJ2EE
代码语言:javascript
复制
目录
1. 什么是 Babel AST Format?
2. 本期涉及哪些 AST node types?
3. 语法规范回顾
    3.1. imports
    3.2. exports   

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?

本期涉及:

  • Modules
    • ModuleDeclaration
    • ModuleSpecifier
    • Imports
      • ImportDeclaration
      • ImportSpecifier
      • ImportDefaultSpecifier
      • ImportNamespaceSpecifier
      • ImportAttribute
    • Exports
      • ExportNamedDeclaration
      • ExportSpecifier
      • ExportDefaultDeclaration
      • ExportAllDeclaration

3. 语法规范回顾

3.1. imports

3.1.1. ES6 语法定义

3.1.2 AST Node

代码语言:javascript
复制
export type ModuleDeclaration = ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration | ImportDeclaration;

export interface ImportDeclaration extends BaseNode {
  type: "ImportDeclaration";
  specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>;
  source: StringLiteral;
  importKind: "type" | "typeof" | "value" | null;
}

export interface ImportSpecifier extends BaseNode {
  type: "ImportSpecifier";
  local: Identifier;
  imported: Identifier;
  importKind: "type" | "typeof" | null;
}

export interface ImportDefaultSpecifier extends BaseNode {
  type: "ImportDefaultSpecifier";
  local: Identifier;
}

export interface ImportNamespaceSpecifier extends BaseNode {
  type: "ImportNamespaceSpecifier";
  local: Identifier;
}

3.1.3. 示例1

语法:

代码语言:javascript
复制
import ModuleSpecifier

示例:

代码语言:javascript
复制
import "webj2ee.css"

AST:

3.1.4. 示例2

语法:

代码语言:javascript
复制
import ImportedDefaultBinding from ModuleSpecifier

示例:

代码语言:javascript
复制
import React from "react"

AST:

3.1.5. 示例3

语法:

代码语言:javascript
复制
import NamespaceImport from ModuleSpecifier

示例:

代码语言:javascript
复制
import * as express from 'express';

AST:

3.1.5. 示例4

语法:

代码语言:javascript
复制
import NamedImports from ModuleSpecifier

示例:

代码语言:javascript
复制
import {useState as MyUseState, useEffect} from 'React';

AST:

3.2. exports

3.2.1. ES6 语法定义

3.2.2 AST Node

代码语言:javascript
复制
export type ExportDeclaration = ExportAllDeclaration | ExportDefaultDeclaration | ExportNamedDeclaration;

export interface ExportAllDeclaration extends BaseNode {
  type: "ExportAllDeclaration";
  source: StringLiteral;
}

export interface ExportDefaultDeclaration extends BaseNode {
  type: "ExportDefaultDeclaration";
  declaration: FunctionDeclaration | TSDeclareFunction | ClassDeclaration | Expression;
}

export interface ExportNamedDeclaration extends BaseNode {
  type: "ExportNamedDeclaration";
  declaration: Declaration | null;
  specifiers: Array<ExportSpecifier | ExportDefaultSpecifier | ExportNamespaceSpecifier>;
  source: StringLiteral | null;
  exportKind: "type" | "value" | null;
}

export interface ExportSpecifier extends BaseNode {
  type: "ExportSpecifier";
  local: Identifier;
  exported: Identifier;
}

3.2.3. 示例1

语法:

代码语言:javascript
复制
export * FromClause ;

示例:

代码语言:javascript
复制
export * from "react";

AST:

3.2.4. 示例2

语法:

代码语言:javascript
复制
export ExportClause FromClause ;

示例:

代码语言:javascript
复制
export {useState as MyUseState, useEffect} from "react";

AST:

3.2.5. 示例3

语法:

代码语言:javascript
复制
export ExportClause ;

示例:

代码语言:javascript
复制
const add = 1, sub = 3;
export {add as MyAdd, sub}

‍AST:

3.2.6. 示例4

语法:

代码语言:javascript
复制
export VariableStatement

示例:

代码语言:javascript
复制
export const x = 1;

AST:

3.2.7. 示例5

语法:

代码语言:javascript
复制
export default ClassDeclaration

‍示例:

代码语言:javascript
复制
export default function add(){};

AST:

参考资料:Modules

http://www.ecma-international.org/ecma-262/6.0/index.html#sec-modules https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md#modules


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

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

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

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

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