Skip to content

Commit

Permalink
Keep directive strings at top of original function.
Browse files Browse the repository at this point in the history
Fixes #248.
  • Loading branch information
benjamn committed Dec 1, 2016
1 parent 91db1da commit 50068cd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/regenerator-transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "regenerator-transform",
"author": "Ben Newman <bn@cs.stanford.edu>",
"description": "Explode async and generator functions into a state machine.",
"version": "0.9.6",
"version": "0.9.7",
"main": "lib/index.js",
"keywords": [
"regenerator",
Expand Down
17 changes: 16 additions & 1 deletion packages/regenerator-transform/src/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ exports.visitor = {

bodyBlockPath.get("body").forEach(function(childPath) {
let node = childPath.node;
if (node && node._blockHoist != null) {
if (t.isExpressionStatement(node) &&
t.isStringLiteral(node.expression)) {
// Babylon represents directives like "use strict" as elements
// of a bodyBlockPath.node.directives array, but they could just
// as easily be represented (by other parsers) as traditional
// string-literal-valued expression statements, so we need to
// handle that here. (#248)
outerBody.push(node);
} else if (node && node._blockHoist != null) {
outerBody.push(node);
} else {
innerBody.push(node);
Expand Down Expand Up @@ -117,6 +125,13 @@ exports.visitor = {
outerBody.push(t.returnStatement(wrapCall));
node.body = t.blockStatement(outerBody);

const oldDirectives = bodyBlockPath.node.directives;
if (oldDirectives) {
// Babylon represents directives like "use strict" as elements of
// a bodyBlockPath.node.directives array. (#248)
node.body.directives = oldDirectives;
}

let wasGeneratorFunction = node.generator;
if (wasGeneratorFunction) {
node.generator = false;
Expand Down
25 changes: 25 additions & 0 deletions test/tests.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,31 @@ describe("the arguments object", function() {
});
});

describe("directive strings", function () {
function *strict() {
"use strict";
yield ! this;
}

function *sloppy() {
yield ! this;
}

it("should be kept at top of outer function", function () {
var strictCode = String(strict);
var useStrictIndex = strictCode.indexOf("use strict");
var thisIndex = strictCode.indexOf("this");

assert.notStrictEqual(useStrictIndex, -1);
assert.ok(thisIndex > useStrictIndex);

assert.strictEqual(String(sloppy).indexOf("use strict"), -1);

check(strict(), [true]);
check(sloppy(), [false]);
});
});

describe("catch parameter shadowing", function() {
it("should leave outer variables unmodified", function() {
function *gen(x) {
Expand Down

0 comments on commit 50068cd

Please sign in to comment.