提案:使用语法树转换和一个简单的节点生成器替换掉现有的生成器。
In an effort to better support down-level transformations for complex emit like generators and Async Functions, and to be able to add new down-level transformations for the evolving ECMA-262 specification, we propose to replace the current emitter logic for the TypeScript compiler with an implementation that relies on transformations of the source tree.
为了更好的在底层支持复杂的生成结构,比如生成器和异步方法,并未不断发展进化的 ECMA-262标准的新规范添加转换,我们提出用一个依赖于源代码树转换的 TypeScript 编译器后端生成器替换掉当前的后端生成器逻辑。
Our current emitter is not built to handle the requirements for some of the more complex rewriting needed to support features such as generators (ES6/ES2015) and Async Functions (ES2016). In addition, as time goes on we will need to add more and more branching logic to handle down-level emit of new language features due to the yearly cadence that TC39 is adopting for the ECMAScript specification.
我们目前的后端生成器不是用来处理一些比较复杂的特性的,比如生成器(ES6 / ES2015标准)和 异步函数( ES2016)。此外,随着时间的推移,我们需要为 TC39采用的新的语言特性不断添加更多的分支逻辑。
Syntax tree transformations will give us the ability to transform our source tree in an iterative fashion, allowing us to inject new transformations at the head of a transformation chain so that few (if any) changes need to be made to existing transformations in later iterations. As a result, there would be minimal maintenance for the complex transformations needed for down-level generators, as long as new features are already transformed into a syntactically valid ES6 tree.
语法树转换器会允许我们以迭代的方式来转换我们的 TypeScript 源代码,他允许我们在转换链的头部注入一个新的转换器,这样如果有些内容需要改变的话,他会基于现有的逻辑进行转换。这样的结果是,复杂变换的生成器需要的维护成本被大大降低,只要将新的特性转变为一个语法有效的 ES6 语法树。
Generally this will also help to reduce the branching logic of our current emitter, and keep syntactic transformations isolated to an individual file for a language version or feature. As a result, the emitter itself can be simplified drastically and focus specifically on emitting the given syntax tree with almost no branching logic.
一般来说,这也可以帮助我们减少当前后端生成器的分支逻辑,并保证每个语言版本或者特性的语法转换器都是彼此鼓励隔离 在单独文件中。这使得后端生成器本身可以大大简化,并专注在特定语法树的生成中,几乎没有分支逻辑。
Requirements:
No transformation should directly modify the original source tree.
Transformations should be isolated to a specific language version or, when necessary, a language feature (i.e. TypeScript to ES6, ES6 to ES5, AMD module transformations, etc.).
Transformations should reuse existing syntax tree nodes when possible.
Transformations should employ a mechanism to quickly identify whether a node or subtree requires transformation to avoid a full walk of a source tree for each iteration.
Transformations should preserve source locations for use with source maps.
Transformations should not significantly impact the performance of the compiler, and ideally should improve compile time.
** 需求 **
- 不能有任何的转换会直接修改原始的语法树
- 转换应针对特定的语言版本或者有必要的情况下某个语言特性( 比如 TypeScript 到 ES6 ,ES6 到 ES5 , AMD 模块转换等等)
- 转换器在可以的情况下应该复用现有的语法树节点
- 转换器应该有一种机制快速确定一个节点或者子树需要被转换,避免每次迭代会对完整代码树进行遍历
- 转换器应为 sourcemap 功能保留源代码位置
- 转换器不应该明显影响编译器性能,理想情况下编译时间会改善。
Goals:
Transform a TypeScript syntax tree into a compatible JavaScript syntax tree based on compiler settings.
Source Map emit should behave as it does in the current compiler.
Comment preservation should behave as it does in the current compiler.
** 目标 **
- 将 TypeScript 语法树转换为与编译器设置兼容的 JavaScript 语法树
- SourceMap 应根据当前的编译器行为一致
- 注释应和当前编译器行为一致。
Non-Goals:
We will not be replacing our declaration emitter with transformations at this time, though we may consider this in a future proposal.
We will not support end-user extensibility of these transformations at this time, though we may consider this in a future proposal.
** 非目标 **
- 我们这次不会换掉我们的声明( Declaration File )转换器,这在我们未来计划的考虑中。
- 我们这次不会支持终端用户扩展转换器,这在我们未来计划的考虑中。
Remaining Tasks:
Update to support block-scoped declarations captured in loops.
Verify comment preservation
Investigate comment preservation performance issues
Verify/adjust source map output
Fix failing tests
Update transformations to match recent changes to the emitter.
Investigate/improve performance of tree-transforming emitter.
** 剩下的任务 **
- 支持循环中的块作用域声明捕获
- 验证注释保存
- 调查注释保存带来的性能问题
- 验证 / 适配 sourcemap 输出
- 修复失败的测试
- 在更改了后端生成器之后更新转换器
- 调查 / 改进语法转换生成器的性能。
网友评论