Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "importRuntime" option to "transform-runtime" #356

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
378 changes: 188 additions & 190 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@
"test": "test/run.sh"
},
"dependencies": {
"@babel/core": "^7.0.0-beta.51",
"@babel/runtime": "^7.0.0-beta.51",
"@babel/types": "^7.0.0-beta.51",
"@babel/core": "^7.0.0",
"@babel/runtime": "^7.0.0",
"@babel/types": "^7.0.0",
"commoner": "^0.10.8",
"private": "^0.1.6",
"recast": "^0.15.3",
"regenerator-preset": "^0.12.0",
"regenerator-runtime": "^0.12.1",
"regenerator-transform": "^0.13.3",
"regenerator-preset": "file:packages/regenerator-preset",
"regenerator-runtime": "file:packages/regenerator-runtime",
"regenerator-transform": "file:packages/regenerator-transform",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes were made by lerna I think.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we revert the lerna changes?

"through": "^2.3.8"
},
"devDependencies": {
"@babel/parser": "^7.0.0-beta.51",
"@babel/plugin-transform-parameters": "^7.0.0-beta.51",
"@babel/plugin-transform-spread": "^7.0.0-beta.51",
"@babel/parser": "^7.0.0",
"@babel/plugin-transform-parameters": "^7.0.0",
"@babel/plugin-transform-spread": "^7.0.0",
"babel-check-duplicated-nodes": "^1.0.0",
"browserify": "^16.2.2",
"mocha": "^5.2.0",
Expand Down
12 changes: 6 additions & 6 deletions packages/regenerator-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
},
"license": "MIT",
"dependencies": {
"@babel/plugin-proposal-function-sent": "^7.0.0-beta.51",
"@babel/plugin-syntax-async-generators": "^7.0.0-beta.51",
"@babel/plugin-transform-arrow-functions": "^7.0.0-beta.51",
"@babel/plugin-transform-block-scoping": "^7.0.0-beta.51",
"@babel/plugin-transform-classes": "^7.0.0-beta.51",
"@babel/plugin-transform-for-of": "^7.0.0-beta.51",
"@babel/plugin-proposal-function-sent": "^7.0.0",
"@babel/plugin-syntax-async-generators": "^7.0.0",
"@babel/plugin-transform-arrow-functions": "^7.0.0",
"@babel/plugin-transform-block-scoping": "^7.0.0",
"@babel/plugin-transform-classes": "^7.0.0",
"@babel/plugin-transform-for-of": "^7.0.0",
"regenerator-transform": "^0.13.0"
}
}
4 changes: 3 additions & 1 deletion packages/regenerator-transform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ $ npm install regenerator-transform
["regenerator-transform", {
asyncGenerators: false, // true by default
generators: false, // true by default
async: false // true by default
async: false, // true by default

importRuntime: true // false by default
}]
]
}
Expand Down
6 changes: 4 additions & 2 deletions packages/regenerator-transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
]
},
"dependencies": {
"@babel/helper-module-imports": "^7.0.0",
"private": "^0.1.6"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.51",
"@babel/preset-env": "^7.0.0-beta.51"
"@babel/core": "^7.1.0",
"@babel/cli": "^7.0.0",
"@babel/preset-env": "^7.0.0"
}
}
7 changes: 5 additions & 2 deletions packages/regenerator-transform/src/emit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as util from "./util";

let hasOwn = Object.prototype.hasOwnProperty;

function Emitter(contextId) {
function Emitter(contextId, opts) {
assert.ok(this instanceof Emitter);

util.getTypes().assertIdentifier(contextId);
Expand Down Expand Up @@ -47,6 +47,9 @@ function Emitter(contextId) {
// to enter a nested loop context that determines the meaning of break
// and continue statements therein.
this.leapManager = new leap.LeapManager(this);

// The options passed to the plugin
this.opts = opts;
}

let Ep = Emitter.prototype;
Expand Down Expand Up @@ -537,7 +540,7 @@ Ep.explodeStatement = function(path, labelId) {
self.emitAssign(
keyIterNextFn,
t.callExpression(
util.runtimeProperty("keys"),
util.runtimeProperty("keys", path.scope, this.opts),
[self.explodeExpression(path.get("right"))]
)
);
Expand Down
26 changes: 24 additions & 2 deletions packages/regenerator-transform/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import { addDefault } from "@babel/helper-module-imports";

let currentTypes = null;

export function wrapWithTypes(types, fn) {
Expand All @@ -23,10 +25,30 @@ export function getTypes() {
return currentTypes;
}

export function runtimeProperty(name) {
const runtimeNames = new WeakMap();

export function runtimeProperty(name, scope, opts) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that runtimeProperty takes options and does more than just construct a member expression, I think we should move it out of util.js and make it a method of the Emitter class. That way we won't need to pass the options explicitly, since they're stored as a property of the Emitter object now. From what I can tell, everywhere runtimeProperty was previously called, we have access to an Emitter instance, so this change should be pretty straightforward. What do you think @nicolo-ribaudo?

const t = getTypes();

let runtimeId;
if (!opts.importRuntime) {
runtimeId = t.identifier("regeneratorRuntime");
} else {
const programPath = scope.getProgramParent().path;
if (runtimeNames.has(programPath.node)) {
runtimeId = t.identifier(runtimeNames.get(programPath.node));
} else {
runtimeId = addDefault(programPath, "regenerator-runtime", {
nameHint: "regeneratorRuntime",
importedInterop: "uncompiled",
blockHoist: 3
});
runtimeNames.set(programPath.node, runtimeId.name);
}
}

return t.memberExpression(
t.identifier("regeneratorRuntime"),
runtimeId,
t.identifier(name),
false
);
Expand Down
25 changes: 14 additions & 11 deletions packages/regenerator-transform/src/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ exports.getVisitor = ({ types: t }) => ({
let bodyBlockPath = path.get("body");

if (node.async) {
bodyBlockPath.traverse(awaitVisitor);
bodyBlockPath.traverse(awaitVisitor, state);
}

bodyBlockPath.traverse(functionSentVisitor, {
Expand Down Expand Up @@ -78,7 +78,7 @@ exports.getVisitor = ({ types: t }) => ({
bodyBlockPath.node.body = innerBody;
}

let outerFnExpr = getOuterFnExpr(path);
let outerFnExpr = getOuterFnExpr(path, state.opts);
// Note that getOuterFnExpr has the side-effect of ensuring that the
// function has a name (so node.id will always be an Identifier), even
// if a temporary name has to be synthesized.
Expand All @@ -100,7 +100,7 @@ exports.getVisitor = ({ types: t }) => ({
));
}

let emitter = new Emitter(contextId);
let emitter = new Emitter(contextId, state.opts);
emitter.explode(path.get("body"));

if (vars && vars.declarations.length > 0) {
Expand All @@ -122,7 +122,7 @@ exports.getVisitor = ({ types: t }) => ({
}

let wrapCall = t.callExpression(
util.runtimeProperty(node.async ? "async" : "wrap"),
util.runtimeProperty(node.async ? "async" : "wrap", path.scope, state.opts),
wrapArgs
);

Expand All @@ -146,7 +146,10 @@ exports.getVisitor = ({ types: t }) => ({
}

if (wasGeneratorFunction && t.isExpression(node)) {
util.replaceWithOrRemove(path, t.callExpression(util.runtimeProperty("mark"), [node]))
util.replaceWithOrRemove(path, t.callExpression(
util.runtimeProperty("mark", path.scope, state.opts),
[node]
));
path.addComment("leading", "#__PURE__");
}

Expand Down Expand Up @@ -174,7 +177,7 @@ exports.getVisitor = ({ types: t }) => ({
// used to refer reliably to the function object from inside the function.
// This expression is essentially a replacement for arguments.callee, with
// the key advantage that it works in strict mode.
function getOuterFnExpr(funPath) {
function getOuterFnExpr(funPath, opts) {
const t = util.getTypes();
let node = funPath.node;
t.assertFunction(node);
Expand All @@ -188,15 +191,15 @@ function getOuterFnExpr(funPath) {
if (node.generator && // Non-generator functions don't need to be marked.
t.isFunctionDeclaration(node)) {
// Return the identifier returned by runtime.mark(<node.id>).
return getMarkedFunctionId(funPath);
return getMarkedFunctionId(funPath, opts);
}

return t.clone(node.id);
}

const getMarkInfo = require("private").makeAccessor();

function getMarkedFunctionId(funPath) {
function getMarkedFunctionId(funPath, opts) {
const t = util.getTypes();
const node = funPath.node;
t.assertIdentifier(node.id);
Expand Down Expand Up @@ -224,7 +227,7 @@ function getMarkedFunctionId(funPath) {
// Get a new unique identifier for our marked variable.
const markedId = blockPath.scope.generateUidIdentifier("marked");
const markCallExp = t.callExpression(
util.runtimeProperty("mark"),
util.runtimeProperty("mark", funPath.scope, opts),
[t.clone(node.id)]
);

Expand Down Expand Up @@ -293,7 +296,7 @@ let awaitVisitor = {
path.skip(); // Don't descend into nested function scopes.
},

AwaitExpression: function(path) {
AwaitExpression: function(path, state) {
const t = util.getTypes();

// Convert await expressions to yield expressions.
Expand All @@ -304,7 +307,7 @@ let awaitVisitor = {
// can distinguish between awaited and merely yielded values.
util.replaceWithOrRemove(path, t.yieldExpression(
t.callExpression(
util.runtimeProperty("awrap"),
util.runtimeProperty("awrap", path.scope, state.opts),
[argument]
),
false
Expand Down
39 changes: 39 additions & 0 deletions test/tests.transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*/

var assert = require("assert");
var babel = require("@babel/core");
var recast = require("recast");
var types = recast.types;
var n = types.namedTypes;
var transform = require("..").transform;
var compile = require("..").compile;
var babelPlugin = require("../packages/regenerator-transform");

var UglifyJS = require("uglify-js");

Expand Down Expand Up @@ -238,3 +240,40 @@ context("functions", function() {
});
});
});

context("regenerator-transform", function () {
function transform(code, pluginOptions, babelOptions) {
return babel.transformSync(code, Object.assign({
plugins: [[babelPlugin, pluginOptions]],
configFile: false
}, babelOptions)).code;
}

describe("importRuntime option", function () {
it("es modules", function() {
var out = transform(
"function* f() {}",
{ importRuntime: true },
{ sourceType: "module" }
);

var importRE = /import (\w+) from "regenerator-runtime"/;
assert.ok(importRE.test(out));
var runtimeName = importRE.exec(out)[1];
assert.ok(out.includes(runtimeName + ".wrap"));
});

it("cjs modules", function() {
var out = transform(
"function* f() {}",
{ importRuntime: true },
{ sourceType: "script" }
);

var importRE = /var (\w+) = require\("regenerator-runtime"\)/;
assert.ok(importRE.test(out));
var runtimeName = importRE.exec(out)[1];
assert.ok(out.includes(runtimeName + ".wrap"));
});
});
});