对Babel的研究1
@babel/parser
Babel解析器(以前是Babylon)是Babel中使用的JavaScript解析器。
- 默认情况下启用了最新的ECMAScript版本(ES2017)。
- 评论附件。
- 支持JSX,Flow,Typescript。
- 支持实验语言建议(至少在阶段0接受任何PR )。
学分
由于@RReverser和@marijnh的出色工作,因此很大程度上基于acorn和acorn- jsx。
API
babelParser.parse(code, [options])
babelParser.parseExpression(code, [options])
parse()将提供的内容解析code为整个ECMAScript程序,同时 parseExpression()尝试解析单个Expression并考虑性能。如有疑问,请使用.parse()。
选件
- allowImportExportEverywhere:默认情况下, - import并- export声明只能出现在一个节目的顶尖水平。将此选项设置为- true允许它们在允许语句的任何位置。
- allowAwaitOutsideFunction:默认情况下, - await仅允许在异步函数内部使用,或者在- topLevelAwait启用插件后在模块的顶级范围内使用。将其设置为- true在脚本的顶级范围内也接受它。
- allowReturnOutsideFunction:默认情况下,顶层的return语句会引发错误。设置 - true为接受这样的代码。
- allowSuperOutsideMethod:默认情况下, - super不允许在类和对象方法之外使用。设置- true为接受这样的代码。
- allowUndeclaredExports:默认情况下,导出在当前模块作用域中未声明的标识符将引发错误。尽管ECMAScript模块规范要求此行为,但是Babel的解析器无法在以后可能会插入适当声明的插件管道中预期转换,因此有时设置此选项 - true以防止解析器过早地抱怨未声明的导出会很重要。稍后添加。
- createParenthesizedExpressions:默认情况下,解析器 - extra.parenthesized在表达式节点上设置。当此选项设置为- true,- ParenthesizedExpression都转而创建AST节点。
- errorRecovery:默认情况下,Babel在发现一些无效代码时总是抛出错误。当此选项设置 - true为时,它将存储解析错误并尝试继续解析无效的输入文件。生成的AST将具有一个- errors表示所有解析错误数组的属性。请注意,即使启用此选项,- @babel/parser也可能引发不可恢复的错误。
- plugins:包含要启用的插件的数组。 
- sourceType的:指明模式的代码应解析可以是一个。 - "script",- "module"或- "unambiguous"。默认为- "script"。- "unambiguous"将根据ES6 或语句的存在使@ babel / parser尝试猜测。带有ES6 和的文件将被考虑,否则为。- import- export- import- export- "module"- "script"
- sourceFilename:将输出的AST节点与其源文件名相关联。从多个输入文件的AST生成代码和源映射时很有用。 
- startLine:默认情况下,解析的第一行代码被视为第1行。您可以提供一个行号来替代。与其他源工具集成时很有用。 
- strictMode:默认情况下,仅当存在 - "use strict";指令或所解析的文件为ECMAScript模块时,ECMAScript代码才会被解析为严格 。将此选项设置- true为始终以严格模式解析文件。
- range: - ranges向每个节点添加一个属性:- [node.start, node.end]
- 令牌:将所有已解析的令牌添加到节点 - tokens上的属性中- File
输出量
Babel解析器根据Babel AST格式生成AST 。它基于ESTree规范,但存在以下差异:
现在有一个
estree可以恢复这些偏差的插件
- 文字标记被StringLiteral,NumericLiteral,BooleanLiteral,NullLiteral,RegExpLiteral取代
- 属性令牌被ObjectProperty和ObjectMethod取代
- MethodDefinition替换为ClassMethod
- 程序和BlockStatement包含Directive和DirectiveLiteral的其他directives字段
- 类方法,OBJECTPROPERTY和ObjectMethod值属性的属性中FunctionExpression被强制/带入的主要方法的节点。
AST for JSX代码基于Facebook JSX AST。
森弗
在大多数情况下,Babel Parser会遵循semver的规定。唯一需要注意的是,某些符合规范的错误修复程序可能会在补丁程序版本下发布。
例如:我们将问题修复到#107之类的早期错误-每个文件有多个默认导出。即使会导致构建失败,也可以将其视为错误修复。
require("@babel/parser").parse("code", { // parse in strict mode and allow module declarations sourceType: "module", plugins: [ // enable jsx and flow syntax "jsx", "flow" ] });
外挂程式
杂
| 名称 | 代码示例 | 
|---|---|
| estree(回购) | 不适用 | 
语言扩展
| 名称 | 代码示例 | 
|---|---|
| flow(回购) | var a: string = ""; | 
| flowComments(docs) | /*:: type Foo = {...}; */ | 
| jsx(回购) | <a attr="b">{s}</a> | 
| typescript(回购) | var a: string = ""; | 
| v8intrinsic | %DebugPrint(foo); | 
ECMAScript 建议
| 名称 | 代码示例 | 
|---|---|
| asyncGenerators(提案) | async function*() {},for await (let a of b) {} | 
| bigInt(提案) | 100n | 
| classProperties(提案) | class A { b = 1; } | 
| classPrivateProperties(提案) | class A { #b = 1; } | 
| classPrivateMethods(提案) | class A { #c() {} } | 
| decorators(提案)decorators-legacy | @a class A {} | 
| doExpressions(提案) | var a = do { if (true) { ‘hi‘; } }; | 
| dynamicImport(提案) | import(‘./guy‘).then(a) | 
| exportDefaultFrom(提案) | export v from "mod" | 
| exportNamespaceFrom(提案) | export * as ns from "mod" | 
| functionBind(提案) | a::b,::console.log | 
| functionSent | function.sent | 
| importMeta(提案) | import.meta.url | 
| logicalAssignment(提案) | a &&= b | 
| nullishCoalescingOperator(提案) | a ?? b | 
| numericSeparator(提案) | 1_000_000 | 
| objectRestSpread(提案) | var a = { b, ...c }; | 
| optionalCatchBinding(提案) | try {throw 0;} catch{do();} | 
| optionalChaining(提案) | a?.b | 
| partialApplication(提案) | f(?, a) | 
| pipelineOperator(提案) | a |> b | 
| throwExpressions(提案) | () => throw new Error("") | 
| topLevelAwait(提案) | await promise在模块中 | 
