Skip to content

this value in module code #562

Closed
Closed

Description

@fkling

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.

Activity

  1. sebmck commented on Jan 21, 2015

    @sebmck
    Contributor

    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.

  2. fkling commented on Jan 21, 2015

    @fkling
    ContributorAuthor

    An alternate solution would be to just throw an error whenever we see any top level this expressions.

    Which however would not help with the case mentioned in the other issue:

    module.exports = {
        x: 'X',
        getX: () => {
          return this.x;
        }
    };
  3. sebmck commented on Jan 21, 2015

    @sebmck
    Contributor

    @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) {
      throw file.errorWithNode(node, "Top level `this` is not allowed");
    };
  4. fkling commented on Jan 21, 2015

    @fkling
    ContributorAuthor

    Thanks for the clarification 👍

    (though, technically this would still be a deviation from the spec ;) )

  5. added a commit that references this issue on Jan 21, 2015
    da6c2b8
  6. sebmck commented on Jan 21, 2015

    @sebmck
    Contributor

    Added in the 3.0.0 branch.

  7. added this to the 3.0.0 milestone on Jan 21, 2015
  8. sebmck commented on Jan 28, 2015

    @sebmck
    Contributor

    Fixed as of 3.0.0

  9. mmocny commented on Jan 29, 2015

    @mmocny

    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.

    wdyt?

  10. eventualbuddha commented on Jan 29, 2015

    @eventualbuddha
    Member

    Can you give us a more concrete example? It may be that you should simply be running vulcanize after transpiling with 6to5.

  11. mmocny commented on Jan 29, 2015

    @mmocny

    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.

  12. mmocny commented on Jan 29, 2015

    @mmocny

    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)
    

    Original code after vulcanization:

    (function (global) {
        'use strict';
    
       // OMITTED lots of code
    })(this);
    
  13. 15 remaining items

  14. sebmck commented on Feb 1, 2015

    @sebmck
    Contributor

    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).

  15. hughsk commented on Feb 1, 2015

    @hughsk
    Contributor

    @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.

  16. sebmck commented on Feb 1, 2015

    @sebmck
    Contributor

    @hughsk

    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.

  17. added a commit that references this issue on Feb 1, 2015
  18. sebmck commented on Feb 1, 2015

    @sebmck
    Contributor

    To expand on this, transforming to undefined in the case you bought up
    isn't a solution then since this can be whatever.

    On Sunday, 1 February 2015, Hugh Kennedy [email protected] wrote:

    @sebmck https://github.com/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.

    Reply to this email directly or view it on GitHub
    #562 (comment).

    Sebastian McKenzie

  19. sebmck commented on Feb 1, 2015

    @sebmck
    Contributor

    After some deliberation and discussion on gitter I've decided to make top level this map to undefined as of 3.3.3.

  20. mmocny commented on Feb 2, 2015

    @mmocny

    Woot. Glad about the change, but curious what convinced you?

    On Sun, 1 Feb 2015 18:53 Sebastian McKenzie [email protected]
    wrote:

    After some deliberation and discussion on gitter I've decided to make top
    level this map to undefined as of 3.3.3.

    —
    Reply to this email directly or view it on GitHub
    #562 (comment).

  21. sebmck commented on Feb 2, 2015

    @sebmck
    Contributor

    @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.

  22. mmocny commented on Feb 2, 2015

    @mmocny

    This is a great quality to have!

    On Sun, 1 Feb 2015 21:15 Sebastian McKenzie [email protected]
    wrote:

    @mmocny https://github.com/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.

    —
    Reply to this email directly or view it on GitHub
    #562 (comment).

  23. jasonkuhrt commented on Feb 4, 2015

    @jasonkuhrt

    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.

  24. sebmck commented on Feb 4, 2015

    @sebmck
    Contributor

    @jasonkuhrt You should only be passing ES6 code to 6to5.

  25. chipironcin commented on Dec 30, 2016

    @chipironcin

    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.

  26. added
    outdated
    on May 5, 2018
  27. locked as resolved and limited conversation to collaborators on May 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

outdated

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions