regenerator-transform
Advanced tools
Comparing version 0.9.12 to 0.10.0
@@ -529,3 +529,3 @@ "use strict"; | ||
var discriminant = path.get("discriminant"); | ||
discriminant.replaceWith(condition); | ||
util.replaceWithOrRemove(discriminant, condition); | ||
self.jump(self.explodeExpression(discriminant)); | ||
@@ -656,3 +656,3 @@ | ||
if (path.node.name === state.catchParamName && util.isReference(path)) { | ||
path.replaceWith(state.safeParam); | ||
util.replaceWithOrRemove(path, state.safeParam); | ||
} | ||
@@ -659,0 +659,0 @@ }, |
@@ -11,2 +11,6 @@ "use strict"; | ||
var _util = require("./util"); | ||
var util = _interopRequireWildcard(_util); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
@@ -16,8 +20,2 @@ | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
// The hoist function takes a FunctionExpression or FunctionDeclaration | ||
// and replaces any Declaration nodes in its body with assignments, then | ||
// returns a VariableDeclaration containing just the names of the removed | ||
// declarations. | ||
/** | ||
@@ -33,2 +31,8 @@ * Copyright (c) 2014, Facebook, Inc. | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
// The hoist function takes a FunctionExpression or FunctionDeclaration | ||
// and replaces any Declaration nodes in its body with assignments, then | ||
// returns a VariableDeclaration containing just the names of the removed | ||
// declarations. | ||
exports.hoist = function (funPath) { | ||
@@ -72,3 +76,3 @@ t.assertFunction(funPath.node); | ||
// there can't be any new declarations inside an expression. | ||
path.replaceWith(t.expressionStatement(expr)); | ||
util.replaceWithOrRemove(path, t.expressionStatement(expr)); | ||
} | ||
@@ -85,3 +89,3 @@ | ||
if (t.isVariableDeclaration(init)) { | ||
path.get("init").replaceWith(varDeclToExpr(init, false)); | ||
util.replaceWithOrRemove(path.get("init"), varDeclToExpr(init, false)); | ||
} | ||
@@ -93,3 +97,3 @@ }, | ||
if (left.isVariableDeclaration()) { | ||
left.replaceWith(varDeclToExpr(left.node, true)); | ||
util.replaceWithOrRemove(left, varDeclToExpr(left.node, true)); | ||
} | ||
@@ -116,3 +120,3 @@ }, | ||
// without worrying about hoisting it. | ||
path.replaceWith(assignment); | ||
util.replaceWithOrRemove(path, assignment); | ||
} | ||
@@ -119,0 +123,0 @@ |
@@ -10,2 +10,6 @@ "use strict"; | ||
var _util = require("./util"); | ||
var util = _interopRequireWildcard(_util); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
@@ -66,3 +70,3 @@ | ||
path.replaceWith(t.objectProperty(t.cloneDeep(path.node.key), // key | ||
util.replaceWithOrRemove(path, t.objectProperty(t.cloneDeep(path.node.key), // key | ||
functionExpression, //value | ||
@@ -69,0 +73,0 @@ path.node.computed, // computed |
@@ -6,2 +6,3 @@ "use strict"; | ||
exports.isReference = isReference; | ||
exports.replaceWithOrRemove = replaceWithOrRemove; | ||
@@ -28,2 +29,10 @@ var _babelTypes = require("babel-types"); | ||
return path.isReferenced() || path.parentPath.isAssignmentExpression({ left: path.node }); | ||
} | ||
function replaceWithOrRemove(path, replacement) { | ||
if (replacement) { | ||
path.replaceWith(replacement); | ||
} else { | ||
path.remove(); | ||
} | ||
} |
@@ -0,1 +1,11 @@ | ||
/** | ||
* Copyright (c) 2014, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* https://raw.github.com/facebook/regenerator/master/LICENSE file. An | ||
* additional grant of patent rights can be found in the PATENTS file in | ||
* the same directory. | ||
*/ | ||
"use strict"; | ||
@@ -27,14 +37,2 @@ | ||
/** | ||
* Copyright (c) 2014, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* https://raw.github.com/facebook/regenerator/master/LICENSE file. An | ||
* additional grant of patent rights can be found in the PATENTS file in | ||
* the same directory. | ||
*/ | ||
var getMarkInfo = require("private").makeAccessor(); | ||
exports.name = "regenerator-transform"; | ||
@@ -166,3 +164,4 @@ | ||
if (wasGeneratorFunction && t.isExpression(node)) { | ||
path.replaceWith(t.callExpression(util.runtimeProperty("mark"), [node])); | ||
util.replaceWithOrRemove(path, t.callExpression(util.runtimeProperty("mark"), [node])); | ||
path.addComment("leading", "#__PURE__"); | ||
} | ||
@@ -194,25 +193,23 @@ | ||
t.isFunctionDeclaration(node)) { | ||
var pp = funPath.findParent(function (path) { | ||
return path.isProgram() || path.isBlockStatement(); | ||
}); | ||
// Return the identifier returned by runtime.mark(<node.id>). | ||
return getMarkedFunctionId(funPath); | ||
} | ||
if (!pp) { | ||
return node.id; | ||
} | ||
return node.id; | ||
} | ||
var markDecl = getRuntimeMarkDecl(pp); | ||
var markedArray = markDecl.declarations[0].id; | ||
var funDeclIdArray = markDecl.declarations[0].init.callee.object; | ||
t.assertArrayExpression(funDeclIdArray); | ||
var getMarkInfo = require("private").makeAccessor(); | ||
var index = funDeclIdArray.elements.length; | ||
funDeclIdArray.elements.push(node.id); | ||
function getMarkedFunctionId(funPath) { | ||
var node = funPath.node; | ||
t.assertIdentifier(node.id); | ||
return t.memberExpression(markedArray, t.numericLiteral(index), true); | ||
var blockPath = funPath.findParent(function (path) { | ||
return path.isProgram() || path.isBlockStatement(); | ||
}); | ||
if (!blockPath) { | ||
return node.id; | ||
} | ||
return node.id; | ||
} | ||
function getRuntimeMarkDecl(blockPath) { | ||
var block = blockPath.node; | ||
@@ -222,11 +219,23 @@ _assert2.default.ok(Array.isArray(block.body)); | ||
var info = getMarkInfo(block); | ||
if (info.decl) { | ||
return info.decl; | ||
if (!info.decl) { | ||
info.decl = t.variableDeclaration("var", []); | ||
blockPath.unshiftContainer("body", info.decl); | ||
info.declPath = blockPath.get("body.0"); | ||
} | ||
info.decl = t.variableDeclaration("var", [t.variableDeclarator(blockPath.scope.generateUidIdentifier("marked"), t.callExpression(t.memberExpression(t.arrayExpression([]), t.identifier("map"), false), [util.runtimeProperty("mark")]))]); | ||
_assert2.default.strictEqual(info.declPath.node, info.decl); | ||
blockPath.unshiftContainer("body", info.decl); | ||
// Get a new unique identifier for our marked variable. | ||
var markedId = blockPath.scope.generateUidIdentifier("marked"); | ||
var markCallExp = t.callExpression(util.runtimeProperty("mark"), [node.id]); | ||
return info.decl; | ||
var index = info.decl.declarations.push(t.variableDeclarator(markedId, markCallExp)) - 1; | ||
var markCallExpPath = info.declPath.get("declarations." + index + ".init"); | ||
_assert2.default.strictEqual(markCallExpPath.node, markCallExp); | ||
markCallExpPath.addComment("leading", "#__PURE__"); | ||
return markedId; | ||
} | ||
@@ -256,3 +265,3 @@ | ||
if (path.node.name === "arguments" && util.isReference(path)) { | ||
path.replaceWith(state.argsId); | ||
util.replaceWithOrRemove(path, state.argsId); | ||
state.didRenameArguments = true; | ||
@@ -269,3 +278,3 @@ } | ||
if (node.meta.name === "function" && node.property.name === "sent") { | ||
path.replaceWith(t.memberExpression(this.context, t.identifier("_sent"))); | ||
util.replaceWithOrRemove(path, t.memberExpression(this.context, t.identifier("_sent"))); | ||
} | ||
@@ -287,4 +296,4 @@ } | ||
// can distinguish between awaited and merely yielded values. | ||
path.replaceWith(t.yieldExpression(t.callExpression(util.runtimeProperty("awrap"), [argument]), false)); | ||
util.replaceWithOrRemove(path, t.yieldExpression(t.callExpression(util.runtimeProperty("awrap"), [argument]), false)); | ||
} | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "Explode async and generator functions into a state machine.", | ||
"version": "0.9.12", | ||
"version": "0.10.0", | ||
"main": "lib/index.js", | ||
@@ -24,3 +24,8 @@ "keywords": [ | ||
"presets": [ | ||
["env", { "loose": true }] | ||
[ | ||
"env", | ||
{ | ||
"loose": true | ||
} | ||
] | ||
], | ||
@@ -27,0 +32,0 @@ "plugins": [ |
@@ -604,3 +604,3 @@ /** | ||
let discriminant = path.get("discriminant"); | ||
discriminant.replaceWith(condition); | ||
util.replaceWithOrRemove(discriminant, condition); | ||
self.jump(self.explodeExpression(discriminant)); | ||
@@ -753,3 +753,3 @@ | ||
if (path.node.name === state.catchParamName && util.isReference(path)) { | ||
path.replaceWith(state.safeParam); | ||
util.replaceWithOrRemove(path, state.safeParam); | ||
} | ||
@@ -756,0 +756,0 @@ }, |
@@ -12,2 +12,3 @@ /** | ||
import * as t from "babel-types"; | ||
import * as util from "./util"; | ||
let hasOwn = Object.prototype.hasOwnProperty; | ||
@@ -61,3 +62,3 @@ | ||
// there can't be any new declarations inside an expression. | ||
path.replaceWith(t.expressionStatement(expr)); | ||
util.replaceWithOrRemove(path, t.expressionStatement(expr)); | ||
} | ||
@@ -74,3 +75,3 @@ | ||
if (t.isVariableDeclaration(init)) { | ||
path.get("init").replaceWith(varDeclToExpr(init, false)); | ||
util.replaceWithOrRemove(path.get("init"), varDeclToExpr(init, false)); | ||
} | ||
@@ -82,3 +83,3 @@ }, | ||
if (left.isVariableDeclaration()) { | ||
left.replaceWith(varDeclToExpr(left.node, true)); | ||
util.replaceWithOrRemove(left, varDeclToExpr(left.node, true)); | ||
} | ||
@@ -117,3 +118,3 @@ }, | ||
// without worrying about hoisting it. | ||
path.replaceWith(assignment); | ||
util.replaceWithOrRemove(path, assignment); | ||
} | ||
@@ -120,0 +121,0 @@ |
import * as t from "babel-types"; | ||
import * as util from "./util"; | ||
@@ -59,3 +60,3 @@ // this function converts a shorthand object generator method into a normal | ||
path.replaceWith( | ||
util.replaceWithOrRemove(path, | ||
t.objectProperty( | ||
@@ -62,0 +63,0 @@ t.cloneDeep(path.node.key), // key |
@@ -24,1 +24,9 @@ /** | ||
} | ||
export function replaceWithOrRemove(path, replacement) { | ||
if (replacement) { | ||
path.replaceWith(replacement) | ||
} else { | ||
path.remove(); | ||
} | ||
} |
@@ -11,2 +11,4 @@ /** | ||
"use strict"; | ||
import assert from "assert"; | ||
@@ -19,4 +21,2 @@ import * as t from "babel-types"; | ||
let getMarkInfo = require("private").makeAccessor(); | ||
exports.name = "regenerator-transform"; | ||
@@ -157,3 +157,4 @@ | ||
if (wasGeneratorFunction && t.isExpression(node)) { | ||
path.replaceWith(t.callExpression(util.runtimeProperty("mark"), [node])); | ||
util.replaceWithOrRemove(path, t.callExpression(util.runtimeProperty("mark"), [node])) | ||
path.addComment("leading", "#__PURE__"); | ||
} | ||
@@ -185,54 +186,54 @@ | ||
t.isFunctionDeclaration(node)) { | ||
let pp = funPath.findParent(function (path) { | ||
return path.isProgram() || path.isBlockStatement(); | ||
}); | ||
// Return the identifier returned by runtime.mark(<node.id>). | ||
return getMarkedFunctionId(funPath); | ||
} | ||
if (!pp) { | ||
return node.id; | ||
} | ||
return node.id; | ||
} | ||
let markDecl = getRuntimeMarkDecl(pp); | ||
let markedArray = markDecl.declarations[0].id; | ||
let funDeclIdArray = markDecl.declarations[0].init.callee.object; | ||
t.assertArrayExpression(funDeclIdArray); | ||
const getMarkInfo = require("private").makeAccessor(); | ||
let index = funDeclIdArray.elements.length; | ||
funDeclIdArray.elements.push(node.id); | ||
function getMarkedFunctionId(funPath) { | ||
const node = funPath.node; | ||
t.assertIdentifier(node.id); | ||
return t.memberExpression( | ||
markedArray, | ||
t.numericLiteral(index), | ||
true | ||
); | ||
const blockPath = funPath.findParent(function (path) { | ||
return path.isProgram() || path.isBlockStatement(); | ||
}); | ||
if (!blockPath) { | ||
return node.id; | ||
} | ||
return node.id; | ||
} | ||
function getRuntimeMarkDecl(blockPath) { | ||
let block = blockPath.node; | ||
const block = blockPath.node; | ||
assert.ok(Array.isArray(block.body)); | ||
let info = getMarkInfo(block); | ||
if (info.decl) { | ||
return info.decl; | ||
const info = getMarkInfo(block); | ||
if (!info.decl) { | ||
info.decl = t.variableDeclaration("var", []); | ||
blockPath.unshiftContainer("body", info.decl); | ||
info.declPath = blockPath.get("body.0"); | ||
} | ||
info.decl = t.variableDeclaration("var", [ | ||
t.variableDeclarator( | ||
blockPath.scope.generateUidIdentifier("marked"), | ||
t.callExpression( | ||
t.memberExpression( | ||
t.arrayExpression([]), | ||
t.identifier("map"), | ||
false | ||
), | ||
[util.runtimeProperty("mark")] | ||
) | ||
) | ||
]); | ||
assert.strictEqual(info.declPath.node, info.decl); | ||
blockPath.unshiftContainer("body", info.decl); | ||
// Get a new unique identifier for our marked variable. | ||
const markedId = blockPath.scope.generateUidIdentifier("marked"); | ||
const markCallExp = t.callExpression( | ||
util.runtimeProperty("mark"), | ||
[node.id] | ||
); | ||
return info.decl; | ||
const index = info.decl.declarations.push( | ||
t.variableDeclarator(markedId, markCallExp) | ||
) - 1; | ||
const markCallExpPath = | ||
info.declPath.get("declarations." + index + ".init"); | ||
assert.strictEqual(markCallExpPath.node, markCallExp); | ||
markCallExpPath.addComment("leading", "#__PURE__"); | ||
return markedId; | ||
} | ||
@@ -262,3 +263,3 @@ | ||
if (path.node.name === "arguments" && util.isReference(path)) { | ||
path.replaceWith(state.argsId); | ||
util.replaceWithOrRemove(path, state.argsId); | ||
state.didRenameArguments = true; | ||
@@ -274,3 +275,3 @@ } | ||
if (node.meta.name === "function" && node.property.name === "sent") { | ||
path.replaceWith(t.memberExpression(this.context, t.identifier("_sent"))); | ||
util.replaceWithOrRemove(path, t.memberExpression(this.context, t.identifier("_sent"))); | ||
} | ||
@@ -292,3 +293,3 @@ } | ||
// can distinguish between awaited and merely yielded values. | ||
path.replaceWith(t.yieldExpression( | ||
util.replaceWithOrRemove(path, t.yieldExpression( | ||
t.callExpression( | ||
@@ -295,0 +296,0 @@ util.runtimeProperty("awrap"), |
118723
3103