You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to the spec, the value of this inside a ModuleEnvironment is supposed to be undefined.
However, currently the value of this module depends on how the module loader evaluates the module code. For example, this refers to module.exports in Node.js (which includes 6to5-node).
A possible solution would be to wrap every module in an IIFE:
(function(){"use strict";// module code}());
That said, it's not 100% clear to me when a source should be evaluated as "module" and when as "script". But based on the spec it seems that any source containing an import or export declaration has to be considered as a module.
And I guess logically, CJS modules should be considered "modules" as well.
6to5 assumes that all code given to it is a module since it tranpiles export and import statements. See my previous comment for an alternative solution that would be safer for developers:
An alternate solution would be to just throw an error whenever we see any top level this expressions. It'd negate the need for a closure and would be more safe as under no circumstance would you reference this while it equaling undefined.
@fkling The this within the arrow function would still be defined as "top level". I'd traverse the AST and skip all function declarations and function expressions and throw on any instance of ThisExpression. So basically the following transformer verbatim:
exports.FunctionDeclaration=exports.FunctionExpression=function(node,parent,scope,context){context.skip();};exports.ThisExpression=function(node,parent,scope,context,file){throwfile.errorWithNode(node,"Top level `this` is not allowed");};
I'm trying to transpile some browser code, where top-level this maps to window. 6to5 is now throwing for this case.
Specifically, the use of top-level this is being automatically introduced in my code by the vulcanize --csp tool.
Its possible they should not be using top-level this, but its worked thus far and works in browsers, so I'm inclined to think 6to5 should support this.
I was running 6to5 after vulcanize since vulcanize inlines an unknown subset of my bower_components, and I would prefer not to just blindly transpile all of those first.
Some more info, not sure if it will help past the original explanation:
Error:
ReferenceError: /Users/mmocny/dev/v2apps/sensortag/dist/index.js: Line 3193: Top level `this` is not allowed
3191 | parse: parse
3192 | };
> 3193 | })(this);
| ^
3194 |
3195 | // Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3196 | // This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
at File.errorWithNode (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/file.js:297:13)
at Object.exports.ThisExpression [as enter] (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/transformation/transformers/other/use-strict.js:20:14)
at transformVisitor.enter (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/transformation/transformer-pass.js:50:16)
at TraversalContext.enterNode (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:65:16)
at TraversalContext.visitNode (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:117:15)
at TraversalContext.visit (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:150:26)
at traverseNode (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:171:17)
at TraversalContext.visitNode (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:130:5)
at TraversalContext.visit (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:142:17)
at traverseNode (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:171:17)
at TraversalContext.visitNode (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:130:5)
at TraversalContext.visit (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:150:26)
at traverseNode (/Users/mmocny/dev/v2apps/sensortag/node_modules/gulp-6to5/node_modules/6to5-core/lib/6to5/traverse/index.js:171:17)
Unless a more compelling argument can be put forth I'm not willing to change it, sorry. This change has already paid off as it's already caught quite a few people running their vendor code through 6to5 as well as some developers expecting this to be window or exports. I'll possibly move this into another transformer so you can more easily blacklist it (currently it's in the useStrict transformer).
@sebmck in this case, 6to5 is producing a compile-time error which won't always result in a runtime error. What if we were transpiling a snippet of ES6 and running it within another context? Are we assuming that 6to5 is always transpiling modules?
I'm not sure it's worth the tradeoff in protecting developers when it's causing valid ES5 code to throw at compile-time, that's kind of surprising and not nice for beginners either.
Are we assuming that 6to5 is always transpiling modules?
Yes, that's where the implicit strict mode comes from and if you were to want to do this then you'd blacklist the useStrict transformer as it'd no longer be valid. Otherwise it's a script in which case there are some even weirder strict mode rules.
I'm not sure it's worth the tradeoff in protecting developers when it's
causing valid ES5 code to throw at compile-time, that's kind of surprising
and not nice for beginners either.
Reply to this email directly or view it on GitHub #562 (comment).
@mmocny Just needed some time to think about it and weigh the pros and cons. I'm happy to listen to any criticism with any of my decisions as it effects the users more than it does me.
This issue has been a real blocker when deps do not compile. And the idea that deps should never go through compile is also problematic: babel/babel-loader#28.
Hi guys! I found this problem in my code and I wish babel warns me or have an option to fail like it did if detects that this is being transformed to undefined.
There is no way for me to notice it was transpiled to undefined until I call it and fails.
Activity
sebmck commentedon Jan 21, 2015
6to5 assumes that all code given to it is a module since it tranpiles
exportandimportstatements. See my previous comment for an alternative solution that would be safer for developers:fkling commentedon Jan 21, 2015
Which however would not help with the case mentioned in the other issue:
sebmck commentedon Jan 21, 2015
@fkling The
thiswithin the arrow function would still be defined as "top level". I'd traverse the AST and skip all function declarations and function expressions and throw on any instance ofThisExpression. So basically the following transformer verbatim:fkling commentedon Jan 21, 2015
Thanks for the clarification 👍
(though, technically this would still be a deviation from the spec ;) )
add illegal top level this transformer - fixes #562
sebmck commentedon Jan 21, 2015
Added in the 3.0.0 branch.
sebmck commentedon Jan 28, 2015
Fixed as of 3.0.0
mmocny commentedon Jan 29, 2015
I'm trying to transpile some browser code, where top-level
thismaps towindow. 6to5 is now throwing for this case.Specifically, the use of top-level
thisis being automatically introduced in my code by thevulcanize --csptool.Its possible they should not be using top-level
this, but its worked thus far and works in browsers, so I'm inclined to think 6to5 should support this.wdyt?
eventualbuddha commentedon Jan 29, 2015
Can you give us a more concrete example? It may be that you should simply be running
vulcanizeafter transpiling with 6to5.mmocny commentedon Jan 29, 2015
Thats a good workaround, I'll try it.
I was running 6to5 after vulcanize since vulcanize inlines an unknown subset of my bower_components, and I would prefer not to just blindly transpile all of those first.
mmocny commentedon Jan 29, 2015
Some more info, not sure if it will help past the original explanation:
Error:
Original code after vulcanization:
15 remaining items
sebmck commentedon Feb 1, 2015
Unless a more compelling argument can be put forth I'm not willing to change it, sorry. This change has already paid off as it's already caught quite a few people running their vendor code through 6to5 as well as some developers expecting
thisto bewindoworexports. I'll possibly move this into another transformer so you can more easily blacklist it (currently it's in theuseStricttransformer).hughsk commentedon Feb 1, 2015
@sebmck in this case, 6to5 is producing a compile-time error which won't always result in a runtime error. What if we were transpiling a snippet of ES6 and running it within another context? Are we assuming that 6to5 is always transpiling modules?
I'm not sure it's worth the tradeoff in protecting developers when it's causing valid ES5 code to throw at compile-time, that's kind of surprising and not nice for beginners either.
sebmck commentedon Feb 1, 2015
@hughsk
Yes, that's where the implicit strict mode comes from and if you were to want to do this then you'd blacklist the
useStricttransformer as it'd no longer be valid. Otherwise it's a script in which case there are some even weirder strict mode rules.Update 6to5.
sebmck commentedon Feb 1, 2015
To expand on this, transforming to
undefinedin the case you bought upisn't a solution then since
thiscan be whatever.On Sunday, 1 February 2015, Hugh Kennedy [email protected] wrote:
Sebastian McKenzie
remap top level this to undefined - #562
sebmck commentedon Feb 1, 2015
After some deliberation and discussion on gitter I've decided to make top level
thismap toundefinedas of 3.3.3.mmocny commentedon Feb 2, 2015
Woot. Glad about the change, but curious what convinced you?
On Sun, 1 Feb 2015 18:53 Sebastian McKenzie [email protected]
wrote:
sebmck commentedon Feb 2, 2015
@mmocny Just needed some time to think about it and weigh the pros and cons. I'm happy to listen to any criticism with any of my decisions as it effects the users more than it does me.
mmocny commentedon Feb 2, 2015
This is a great quality to have!
On Sun, 1 Feb 2015 21:15 Sebastian McKenzie [email protected]
wrote:
jasonkuhrt commentedon Feb 4, 2015
This issue has been a real blocker when deps do not compile. And the idea that deps should never go through compile is also problematic: babel/babel-loader#28.
sebmck commentedon Feb 4, 2015
@jasonkuhrt You should only be passing ES6 code to 6to5.
chipironcin commentedon Dec 30, 2016
Hi guys! I found this problem in my code and I wish babel warns me or have an option to fail like it did if detects that
thisis being transformed toundefined.There is no way for me to notice it was transpiled to
undefineduntil I call it and fails.