Skip to content

Ensure that class bodies have 'use strict' if the parent scope wasn't already #7349

@loganfsmyth

Description

@loganfsmyth

ES6 class scopes are automatically strict-mode. In Babel 6 the module transform pretty much just always made modules strict, but now that people are more likely to be using sourceType: "unambiguous", we'll want to make sure the class transform injects use strict if the file isn't a module and doesn't have a use strict directive in a parent scope.

Input Code

class Foo {
  method(){

  }
}

Expected Behavior

var Foo = function () {
  "use strict";
  function Foo() {
    _classCallCheck(this, Foo);
  }

  _createClass(Foo, [{
    key: "method",
    value: function method() {}
  }]);

  return Foo;
}();

Possible Solution

We likely want to walk up the parent paths, and any time a BlockStatement that is a function body is passed, we can check if path.node.directives has a use strict or not. If we get all the way to the top and didn't find one, and the root Program node doesn't have sourceType === "module", we should add a Directive node to the classes wrapper function expression.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuehelp wantedoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions