termi / es6-transpiler

Tomorrow's JavaScript syntax today
Other
216 stars 18 forks source link

Possibility of es6 output #19

Open termi opened 10 years ago

termi commented 10 years ago

Since all modern browsers have already implemented some ES6 feature, it's a good idea to allow not to transpile some of es6 features. For example, node.js(V8) with --harmony flag will support "block binding" (let / const) so the transpiler can keep this untouched.

At the same time, it may not be so easy due for some cases need to write a new logic. FireFox 27+, for example, has support of block binding and destructuring (some basic patterns) but has no spread/rest support.


Output examples

EcmaScript 6

let [a, b, ...c] = [1, 2, 3, 4, 5, 6]
console.log(a === 1, b === 2, c.join("|") === "3|4|5|6")

{
    let [a, b] = [1, [[[4], 3], 2]], c, d, e;
    [b, [[[a], d],c]] = [a, b];
    console.log(b === 1, c === 2, d === 3, a === 4);
}

For FireFox 27+ (only spread transpiling)

var SLICE$0 = Array.prototype.slice;let [a, b] = (c = [1, 2, 3, 4, 5, 6]), c = SLICE$0.call(c, 2);
console.log(a === 1, b === 2, c.join("|") === "3|4|5|6")

{
    let [a, b] = [1, [[[4], 3], 2]], c, d, e;
    [b, [[[a], d],c]] = [a, b];
    console.log(b === 1, c === 2, d === 3, a === 4);
}

For node.js --harmony (destructuring and spread transpiling)

var SLICE$0 = Array.prototype.slice;var $D$0;let a = (c = [1, 2, 3, 4, 5, 6])[0], b = c[1], c = SLICE$0.call(c, 2)
console.log(a === 1, b === 2, c.join("|") === "3|4|5|6")

{
    let a = (b = [1, [[[4], 3], 2]])[0], b = b[1], c, d, e;
    b = ($D$0 = [a, b])[0], a = (d = (c = $D$0[1])[0])[0][0], d = d[1], c = c[1], $D$0;
    console.log(b === 1, c === 2, d === 3, a === 4);
}

For all other engines

var SLICE$0 = Array.prototype.slice;var $D$0;var a = (c = [1, 2, 3, 4, 5, 6])[0], b = c[1], c = SLICE$0.call(c, 2)
console.log(a === 1, b === 2, c.join("|") === "3|4|5|6")

{
    var a$0 = (b$0 = [1, [[[4], 3], 2]])[0], b$0 = b$0[1], c$0, d, e;
    b$0 = ($D$0 = [a$0, b$0])[0], a$0 = (d = (c$0 = $D$0[1])[0])[0][0], d = d[1], c$0 = c$0[1], $D$0;
    console.log(b$0 === 1, c$0 === 2, d === 3, a$0 === 4);
}