关于@babel/parser,@babel/generator,@babel/traverse, @babel/types使用
最近需要写一个脚本对项目代码进行修改故将研究进度记录在这里。
这三个关于babel的库都从属于@babel/core,所以@babel/core就可以使用这三个库。
参考文档:babel中文文档 (这三个库在babel文档中工具分类中)。
以下全部基于@babel/core version:7.13.8
@babel/parser
Babel Parser(以前为Babylon)是Babel中使用的JavaScript解析器
- 默认启用最新的ECMAScript版本(ES2020)
- 支持JSX, Flow, Typescript
babelParser.parse(code, [options])
使用方式如下:
import * as babelParser from '@babel/parser';
const code = 'const a = 1;';
babelParser.parse(code);
参数解释,通过@babel/parse的声明文件来解释
export function parse(
input: string,//需要解析代码字符串
options?: ParserOptions //可选参数,在解析过程中一些配置项 关于配置项内容在下面会讲解
): import("@babel/types").File; //返回是@babel/types 里面的File类型这个在下面介绍@babel/types时候会讲到
//关于ParserOptions 参数声明如下
export interface ParserOptions {
/**
* By default, import and export declarations can only appear at a program's top level.
* 默认情况下,导入和导出声明只能出现在程序的顶层
* Setting this option to true allows them anywhere where a statement is allowed.
* 将此选项设置为true允许在任何允许语句的地方使用。
*/
allowImportExportEverywhere?: boolean;
/**
* By default, await use is not allowed outside of an async function.
* 默认情况下,不允许在异步函数之外使用await。
* Set this to true to accept such code.
* 将此设置为true以接受这样的代码。
*/
allowAwaitOutsideFunction?: boolean;
/**
* By default, a return statement at the top level raises an error.
* 默认情况下,顶层的return语句会引发错误。
* Set this to true to accept such code.
* 将此设置为true以接受这样的代码。
*/
allowReturnOutsideFunction?: boolean;
/**
* By default, super use is not allowed outside of class and object methods.
* 默认情况下,不允许在类和对象方法之外super
* Set this to true to accept such code.
* 将此设置为true以接受这样的代码。
*/
allowSuperOutsideMethod?: boolean;
/**
* By default, exported identifiers must refer to a declared variable.
* 默认情况下,导出的标识符必须引用声明的变量。
* Set this to true to allow export statements to reference undeclared variables.
* 将此设置为true以允许导出语句引用未声明的变量。
*/
allowUndeclaredExports?: boolean;
/**
* By default, Babel always throws an error when it finds some invalid code.
* 默认情况下,Babel在发现无效代码时总是抛出错误。
* When this option is set to true, it will store the parsing error and
* try to continue parsing the invalid input file.
* 当该选项设置为true时,它将存储解析错误和尝试继续解析无效的输入文件
*/
errorRecovery?: boolean;
/**
* Indicate the mode the code should be parsed in.
* 指出代码应该被解析的模式。
* Can be one of "script", "module", or "unambiguous". Defaults to "script".
* 可以是“script”、“module”或“unambiguous”之一。默认为“script”。
* "unambiguous" will make @babel/parser attempt to guess, based on the presence
* of ES6 import or export statements.
* “unambiguous”将使@babel/parser尝试猜测,基于存在ES6导入或导出语句的
* Files with ES6 imports and exports are considered "module" and are otherwise "script".
* 带有ES6导入和导出的文件被认为是“module”,否则就是“script”。
*/
sourceType?: "script" | "module" | "unambiguous";
/**
* Correlate output AST nodes with their source filename.
* 将输出AST节点与其源文件名关联起来。
* Useful when generating code and source maps from the ASTs of multiple input files.
* 在从多个输入文件的ast生成代码和源代码映射时很有用。
*/
sourceFilename?: string;
/**
* By default, the first line of code parsed is treated as line 1.
* 默认情况下,第一行代码被解析为第1行。
* You can provide a line number to alternatively start with.
* 您可以提供一个行号作为开头。
* Useful for integration with other source tools.
* 对于与其他源工具的集成很有用。
*/
startLine?: number;
/**
* Array containing the plugins that you want to enable.
* 包含要启用的插件的数组
*/
plugins?: ParserPlugin[];
/**
* Should the parser work in strict mode.
* 解析器是否在严格模式下工作
* Defaults to true if sourceType === 'module'. Otherwise, false.
* 如果sourceType === 'module'默认为true。否则,假的。
*/
strictMode?: boolean;
/**
* Adds a ranges property to each node: [node.start, node.end]
* 是否向每个节点添加一个ranges属性:[node.start, node.end]
ranges?: boolean;
/**
* Adds all parsed tokens to a tokens property on the File node.
* 将所有已解析的标记添加到File节点上的tokens属性。
*/
tokens?: boolean;
/**
* By default, the parser adds information about parentheses by setting
* 默认情况下,解析器通过设置来添加关于圆括号的信息
* `extra.parenthesized` to `true` as needed.
* When this option is `true` the parser creates `ParenthesizedExpression`
* 当该选项为“true”时,解析器会创建`ParenthesizedExpression`
* AST nodes instead of using the `extra` property.
*/
createParenthesizedExpressions?: boolean;
}
关于options
首先解释一下AST :Abstract syntax tree 的缩写翻译过来就是抽象语法树 至于抽象语法书的结构是如何的会在@babel/types,babel有提供一个在线查看网站