这个简单的编译器可以编译我们在JavaScript中使用的Swift的尾闭包语法,将其变成正常的JavaScript语法。
关于Swift的尾闭包如果你不是很理解,可以参考Swift关于 Closures 的文档
通过这个编译器我们可以将下面的代码:
a(){}转换为:
a(() => {});或者将:
a(1, "hello"){ b, c in d() d{} d(1, "hello") d(1, "hello"){} d(1, "hello"){ e, f in g() } }转换为:
a(1, "hello", (b, c) => { d(); d(() => {}); d(1, "hello"); d(1, "hello", () => {}); d(1, "hello", (e, f) => { g() }) })项目的在线演示地址:JavaScript Trailing Closure Toy Compiler
关于项目代码部分的详细解释可以阅读这篇文章:动手写一个简单的编译器:在JavaScript中使用Swift的尾闭包语法
如果你对这个项目有什么建议和意见,欢迎提 issues 或者 Pull requests
这个项目受到 jamiebuilds 的 the-super-tiny-compiler 项目的启发,参考了里面的一些内容。
This work is licensed under a Creative Commons Attribution 4.0 International License.
