Proposal: Replace emitter with syntax tree transformations and a simplified node emitter

提案:使用語(yǔ)法樹(shù)轉(zhuǎn)換和一個(gè)簡(jiǎn)單的節(jié)點(diǎn)生成器替換掉現(xiàn)有的生成器。

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.

為了更好的在底層支持復(fù)雜的生成結(jié)構(gòu),比如生成器和異步方法,并未不斷發(fā)展進(jìn)化的 ECMA-262標(biāo)準(zhǔn)的新規(guī)范添加轉(zhuǎn)換,我們提出用一個(gè)依賴(lài)于源代碼樹(shù)轉(zhuǎn)換的 TypeScript 編譯器后端生成器替換掉當(dāng)前的后端生成器邏輯。

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.

我們目前的后端生成器不是用來(lái)處理一些比較復(fù)雜的特性的,比如生成器(ES6 / ES2015標(biāo)準(zhǔn))和 異步函數(shù)( ES2016)。此外,隨著時(shí)間的推移,我們需要為 TC39采用的新的語(yǔ)言特性不斷添加更多的分支邏輯。

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.

語(yǔ)法樹(shù)轉(zhuǎn)換器會(huì)允許我們以迭代的方式來(lái)轉(zhuǎn)換我們的 TypeScript 源代碼,他允許我們?cè)谵D(zhuǎn)換鏈的頭部注入一個(gè)新的轉(zhuǎn)換器,這樣如果有些內(nèi)容需要改變的話,他會(huì)基于現(xiàn)有的邏輯進(jìn)行轉(zhuǎn)換。這樣的結(jié)果是,復(fù)雜變換的生成器需要的維護(hù)成本被大大降低,只要將新的特性轉(zhuǎn)變?yōu)橐粋€(gè)語(yǔ)法有效的 ES6 語(yǔ)法樹(shù)。

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.

一般來(lái)說(shuō),這也可以幫助我們減少當(dāng)前后端生成器的分支邏輯,并保證每個(gè)語(yǔ)言版本或者特性的語(yǔ)法轉(zhuǎn)換器都是彼此鼓勵(lì)隔離 在單獨(dú)文件中。這使得后端生成器本身可以大大簡(jiǎn)化,并專(zhuān)注在特定語(yǔ)法樹(shù)的生成中,幾乎沒(méi)有分支邏輯。

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.

** 需求 **

  • 不能有任何的轉(zhuǎn)換會(huì)直接修改原始的語(yǔ)法樹(shù)
  • 轉(zhuǎn)換應(yīng)針對(duì)特定的語(yǔ)言版本或者有必要的情況下某個(gè)語(yǔ)言特性( 比如 TypeScript 到 ES6 ,ES6 到 ES5 , AMD 模塊轉(zhuǎn)換等等)
  • 轉(zhuǎn)換器在可以的情況下應(yīng)該復(fù)用現(xiàn)有的語(yǔ)法樹(shù)節(jié)點(diǎn)
  • 轉(zhuǎn)換器應(yīng)該有一種機(jī)制快速確定一個(gè)節(jié)點(diǎn)或者子樹(shù)需要被轉(zhuǎn)換,避免每次迭代會(huì)對(duì)完整代碼樹(shù)進(jìn)行遍歷
  • 轉(zhuǎn)換器應(yīng)為 sourcemap 功能保留源代碼位置
  • 轉(zhuǎn)換器不應(yīng)該明顯影響編譯器性能,理想情況下編譯時(shí)間會(huì)改善。

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.

** 目標(biāo) **

  • 將 TypeScript 語(yǔ)法樹(shù)轉(zhuǎn)換為與編譯器設(shè)置兼容的 JavaScript 語(yǔ)法樹(shù)
  • SourceMap 應(yīng)根據(jù)當(dāng)前的編譯器行為一致
  • 注釋?xiě)?yīng)和當(dāng)前編譯器行為一致。

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.

** 非目標(biāo) **

  • 我們這次不會(huì)換掉我們的聲明( Declaration File )轉(zhuǎn)換器,這在我們未來(lái)計(jì)劃的考慮中。
  • 我們這次不會(huì)支持終端用戶(hù)擴(kuò)展轉(zhuǎn)換器,這在我們未來(lái)計(jì)劃的考慮中。

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.

** 剩下的任務(wù) **

  • 支持循環(huán)中的塊作用域聲明捕獲
  • 驗(yàn)證注釋保存
  • 調(diào)查注釋保存帶來(lái)的性能問(wèn)題
  • 驗(yàn)證 / 適配 sourcemap 輸出
  • 修復(fù)失敗的測(cè)試
  • 在更改了后端生成器之后更新轉(zhuǎn)換器
  • 調(diào)查 / 改進(jìn)語(yǔ)法轉(zhuǎn)換生成器的性能。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容