alekseykulikov/babel-preset-es2015-node5

Include babel-plugin-transform-es2015-block-scoping

Closed this issue · 5 comments

The V8 in node 5 does not support ES2015 compatible block scoping for let and const.

For example, in node 5.2, this input:

const a = {a: 'b', c: 'd', e: 'f'}
for (const key in a) { console.log(key, a[key]); }

produces this output:

a b
a b
a b

Whereas according to the spec it should output:

a b
c d
e f

This is fixed by including babel-plugin-transform-es2015-block-scoping in the preset.

This looks like really edge case, similar to #3:

const a = {a: 'b', c: 'd', e: 'f'}
for (let key in a) { console.log(key, a[key]); }

Produces right result.

Personally I always use let for loops and have never experiences this issue. I need to add section to Readme to warn about minor V8 incompatibilities like this, which should be fixed by using https://www.npmjs.com/package/babel-preset-es2015 if you rely 100% on spec.

I also tend to use let in this scenario but const is technically more appropriate - the only time you need to use let here is when you're doing a real for loop. for in and for of support const.

Perhaps it's a good idea to leave babel-plugin-transform-es2015-block-scoping enabled anyway, because performance of var is quite a lot better than let and const in current V8s.

Yes, performance is the argument. But if we're going to think in same direction, we have to use default babel-preset-es2015 since, I guess, performance of function is better than =>, the same for classes and so on. I ran simple test on my medium size project and haven't found significant difference in usage of es2015-node5 and es2015 on node@5.3.
I still think, the note in Readme could work.

Today, I've updated eslint and got warning from prefer-const rule forcing me to use const instead of let in a loop:

for (let dir of dirs) {

Probably, If I didn't know about problem with V8, I would change it and could have hard time debugging my app. So, I agree, we need to include babel-plugin-transform-es2015-block-scoping, but it looks like major breakage, since consumers of this module expect native let/const.

This issue is not the case, when function is running in strict mode.
I added the test and it passes in node@5. I also added travis.ci integration, so we can test potential compilation bugs.