regenerator-transform
Advanced tools
Comparing version 0.12.4 to 0.13.0
324
lib/emit.js
"use strict"; | ||
var _assert = require("assert"); | ||
var _assert = _interopRequireDefault(require("assert")); | ||
var _assert2 = _interopRequireDefault(_assert); | ||
var leap = _interopRequireWildcard(require("./leap")); | ||
var _leap = require("./leap"); | ||
var meta = _interopRequireWildcard(require("./meta")); | ||
var leap = _interopRequireWildcard(_leap); | ||
var util = _interopRequireWildcard(require("./util")); | ||
var _meta = require("./meta"); | ||
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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
var meta = _interopRequireWildcard(_meta); | ||
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; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -29,37 +21,29 @@ | ||
*/ | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
function Emitter(contextId) { | ||
_assert2.default.ok(this instanceof Emitter); | ||
_assert.default.ok(this instanceof Emitter); | ||
util.getTypes().assertIdentifier(contextId); | ||
util.getTypes().assertIdentifier(contextId); // Used to generate unique temporary names. | ||
// Used to generate unique temporary names. | ||
this.nextTempId = 0; | ||
// In order to make sure the context object does not collide with | ||
this.nextTempId = 0; // In order to make sure the context object does not collide with | ||
// anything in the local scope, we might have to rename it, so we | ||
// refer to it symbolically instead of just assuming that it will be | ||
// called "context". | ||
this.contextId = contextId; | ||
// An append-only list of Statements that grows each time this.emit is | ||
this.contextId = contextId; // An append-only list of Statements that grows each time this.emit is | ||
// called. | ||
this.listing = []; | ||
// A sparse array whose keys correspond to locations in this.listing | ||
this.listing = []; // A sparse array whose keys correspond to locations in this.listing | ||
// that have been marked as branch/jump targets. | ||
this.marked = [true]; | ||
// The last location will be marked when this.getDispatchLoop is | ||
this.marked = [true]; // The last location will be marked when this.getDispatchLoop is | ||
// called. | ||
this.finalLoc = loc(); | ||
// A list of all leap.TryEntry statements emitted. | ||
this.tryEntries = []; | ||
this.finalLoc = loc(); // A list of all leap.TryEntry statements emitted. | ||
// Each time we evaluate the body of a loop, we tell this.leapManager | ||
this.tryEntries = []; // Each time we evaluate the body of a loop, we tell this.leapManager | ||
// to enter a nested loop context that determines the meaning of break | ||
// and continue statements therein. | ||
this.leapManager = new leap.LeapManager(this); | ||
@@ -69,5 +53,3 @@ } | ||
var Ep = Emitter.prototype; | ||
exports.Emitter = Emitter; | ||
// Offsets into this.listing that could be used as targets for branches or | ||
exports.Emitter = Emitter; // Offsets into this.listing that could be used as targets for branches or | ||
// jumps are represented as numeric Literal nodes. This representation has | ||
@@ -77,11 +59,13 @@ // the amazingly convenient benefit of allowing the exact value of the | ||
// refers to the location. | ||
function loc() { | ||
return util.getTypes().numericLiteral(-1); | ||
} | ||
} // Sets the exact value of the given location to the offset of the next | ||
// Statement emitted. | ||
// Sets the exact value of the given location to the offset of the next | ||
// Statement emitted. | ||
Ep.mark = function (loc) { | ||
util.getTypes().assertLiteral(loc); | ||
var index = this.listing.length; | ||
if (loc.value === -1) { | ||
@@ -92,4 +76,5 @@ loc.value = index; | ||
// once set the first time. | ||
_assert2.default.strictEqual(loc.value, index); | ||
_assert.default.strictEqual(loc.value, index); | ||
} | ||
this.marked[index] = true; | ||
@@ -108,25 +93,25 @@ return loc; | ||
this.listing.push(node); | ||
}; | ||
}; // Shorthand for emitting assignment statements. This will come in handy | ||
// for assignments to temporary variables. | ||
// Shorthand for emitting assignment statements. This will come in handy | ||
// for assignments to temporary variables. | ||
Ep.emitAssign = function (lhs, rhs) { | ||
this.emit(this.assign(lhs, rhs)); | ||
return lhs; | ||
}; | ||
}; // Shorthand for an assignment statement. | ||
// Shorthand for an assignment statement. | ||
Ep.assign = function (lhs, rhs) { | ||
var t = util.getTypes(); | ||
return t.expressionStatement(t.assignmentExpression("=", lhs, rhs)); | ||
}; | ||
}; // Convenience function for generating expressions like context.next, | ||
// context.sent, and context.rval. | ||
// Convenience function for generating expressions like context.next, | ||
// context.sent, and context.rval. | ||
Ep.contextProperty = function (name, computed) { | ||
var t = util.getTypes(); | ||
return t.memberExpression(this.contextId, computed ? t.stringLiteral(name) : t.identifier(name), !!computed); | ||
}; | ||
}; // Shorthand for setting context.rval and jumping to `context.stop()`. | ||
// Shorthand for setting context.rval and jumping to `context.stop()`. | ||
Ep.stop = function (rval) { | ||
@@ -142,3 +127,2 @@ if (rval) { | ||
util.getTypes().assertExpression(valuePath.value); | ||
this.emitAssign(this.contextProperty("rval"), this.explodeExpression(valuePath)); | ||
@@ -149,5 +133,3 @@ }; | ||
var t = util.getTypes(); | ||
t.assertLiteral(tryLoc); | ||
var catchCall = t.callExpression(this.contextProperty("catch", true), [tryLoc]); | ||
@@ -160,29 +142,26 @@ | ||
} | ||
}; | ||
}; // Emits code for an unconditional jump to the given location, even if the | ||
// exact value of the location is not yet known. | ||
// Emits code for an unconditional jump to the given location, even if the | ||
// exact value of the location is not yet known. | ||
Ep.jump = function (toLoc) { | ||
this.emitAssign(this.contextProperty("next"), toLoc); | ||
this.emit(util.getTypes().breakStatement()); | ||
}; | ||
}; // Conditional jump. | ||
// Conditional jump. | ||
Ep.jumpIf = function (test, toLoc) { | ||
var t = util.getTypes(); | ||
t.assertExpression(test); | ||
t.assertLiteral(toLoc); | ||
this.emit(t.ifStatement(test, t.blockStatement([this.assign(this.contextProperty("next"), toLoc), t.breakStatement()]))); | ||
}; | ||
}; // Conditional jump, with the condition negated. | ||
// Conditional jump, with the condition negated. | ||
Ep.jumpIfNot = function (test, toLoc) { | ||
var t = util.getTypes(); | ||
t.assertExpression(test); | ||
t.assertLiteral(toLoc); | ||
var negatedTest; | ||
var negatedTest = void 0; | ||
if (t.isUnaryExpression(test) && test.operator === "!") { | ||
@@ -196,5 +175,3 @@ // Avoid double negation. | ||
this.emit(t.ifStatement(negatedTest, t.blockStatement([this.assign(this.contextProperty("next"), toLoc), t.breakStatement()]))); | ||
}; | ||
// Returns a unique MemberExpression that can be used to store and | ||
}; // Returns a unique MemberExpression that can be used to store and | ||
// retrieve temporary values. Since the object of the member expression is | ||
@@ -204,2 +181,4 @@ // the context object, which is presumed to coexist peacefully with all | ||
// monotonically, uniqueness is assured. | ||
Ep.makeTempVar = function () { | ||
@@ -211,10 +190,8 @@ return this.contextProperty("t" + this.nextTempId++); | ||
var t = util.getTypes(); | ||
return t.functionExpression(id || null /*Anonymous*/ | ||
return t.functionExpression(id || null | ||
/*Anonymous*/ | ||
, [this.contextId], t.blockStatement([this.getDispatchLoop()]), false, // Not a generator anymore! | ||
false // Nor an expression. | ||
); | ||
}; | ||
// Turns this.listing into a loop of the form | ||
}; // Turns this.listing into a loop of the form | ||
// | ||
@@ -230,2 +207,4 @@ // while (1) switch (context.next) { | ||
// case statement. | ||
Ep.getDispatchLoop = function () { | ||
@@ -235,8 +214,6 @@ var self = this; | ||
var cases = []; | ||
var current = void 0; | ||
var current; // If we encounter a break, continue, or return statement in a switch | ||
// case, we can skip the rest of the statements until the next case. | ||
// If we encounter a break, continue, or return statement in a switch | ||
// case, we can skip the rest of the statements until the next case. | ||
var alreadyEnded = false; | ||
self.listing.forEach(function (stmt, i) { | ||
@@ -252,18 +229,11 @@ if (self.marked.hasOwnProperty(i)) { | ||
} | ||
}); | ||
}); // Now that we know how many statements there will be in this.listing, | ||
// we can finally resolve this.finalLoc.value. | ||
// Now that we know how many statements there will be in this.listing, | ||
// we can finally resolve this.finalLoc.value. | ||
this.finalLoc.value = this.listing.length; | ||
cases.push(t.switchCase(this.finalLoc, [ | ||
// Intentionally fall through to the "end" case... | ||
]), | ||
// So that the runtime can jump to the final location without having | ||
cases.push(t.switchCase(this.finalLoc, [// Intentionally fall through to the "end" case... | ||
]), // So that the runtime can jump to the final location without having | ||
// to know its offset, we provide the "end" case as a synonym. | ||
t.switchCase(t.stringLiteral("end"), [ | ||
// This will check/clear both context.thrown and context.rval. | ||
t.switchCase(t.stringLiteral("end"), [// This will check/clear both context.thrown and context.rval. | ||
t.returnStatement(t.callExpression(this.contextProperty("stop"), []))])); | ||
return t.whileStatement(t.numericLiteral(1), t.switchStatement(t.assignmentExpression("=", this.contextProperty("prev"), this.contextProperty("next")), cases)); | ||
@@ -281,13 +251,11 @@ }; | ||
var lastLocValue = 0; | ||
return t.arrayExpression(this.tryEntries.map(function (tryEntry) { | ||
var thisLocValue = tryEntry.firstLoc.value; | ||
_assert2.default.ok(thisLocValue >= lastLocValue, "try entries out of order"); | ||
_assert.default.ok(thisLocValue >= lastLocValue, "try entries out of order"); | ||
lastLocValue = thisLocValue; | ||
var ce = tryEntry.catchEntry; | ||
var fe = tryEntry.finallyEntry; | ||
var locs = [tryEntry.firstLoc, | ||
// The null here makes a hole in the array. | ||
var locs = [tryEntry.firstLoc, // The null here makes a hole in the array. | ||
ce ? ce.firstLoc : null]; | ||
@@ -302,11 +270,8 @@ | ||
})); | ||
}; | ||
// All side effects must be realized in order. | ||
}; // All side effects must be realized in order. | ||
// If any subexpression harbors a leap, all subexpressions must be | ||
// neutered of side effects. | ||
// No destructive modification of AST nodes. | ||
Ep.explode = function (path, ignoreResult) { | ||
@@ -316,9 +281,5 @@ var t = util.getTypes(); | ||
var self = this; | ||
t.assertNode(node); | ||
if (t.isDeclaration(node)) throw getDeclError(node); | ||
if (t.isStatement(node)) return self.explodeStatement(path); | ||
if (t.isExpression(node)) return self.explodeExpression(path, ignoreResult); | ||
@@ -332,5 +293,5 @@ | ||
throw getDeclError(node); | ||
// These node types should be handled by their parent nodes | ||
// (ObjectExpression, SwitchStatement, and TryStatement, respectively). | ||
case "Property": | ||
@@ -354,6 +315,3 @@ case "SwitchCase": | ||
var self = this; | ||
var before = void 0, | ||
after = void 0, | ||
head = void 0; | ||
var before, after, head; | ||
t.assertStatement(stmt); | ||
@@ -365,6 +323,6 @@ | ||
labelId = null; | ||
} | ||
} // Explode BlockStatement nodes even if they do not contain a yield, | ||
// because we don't want or need the curly braces. | ||
// Explode BlockStatement nodes even if they do not contain a yield, | ||
// because we don't want or need the curly braces. | ||
if (t.isBlockStatement(stmt)) { | ||
@@ -393,5 +351,3 @@ path.get("body").forEach(function (path) { | ||
case "LabeledStatement": | ||
after = loc(); | ||
// Did you know you can break from any labeled block statement or | ||
after = loc(); // Did you know you can break from any labeled block statement or | ||
// control structure? Well, you can! Note: when a labeled loop is | ||
@@ -416,8 +372,7 @@ // encountered, the leap.LabeledEntry created here will immediately | ||
// time, so it has no additional runtime cost. | ||
self.leapManager.withEntry(new leap.LabeledEntry(after, stmt.label), function () { | ||
self.explodeStatement(path.get("body"), stmt.label); | ||
}); | ||
self.mark(after); | ||
break; | ||
@@ -428,3 +383,2 @@ | ||
after = loc(); | ||
self.mark(before); | ||
@@ -437,3 +391,2 @@ self.jumpIfNot(self.explodeExpression(path.get("test")), after); | ||
self.mark(after); | ||
break; | ||
@@ -445,3 +398,2 @@ | ||
after = loc(); | ||
self.mark(first); | ||
@@ -454,3 +406,2 @@ self.leapManager.withEntry(new leap.LoopEntry(after, test, labelId), function () { | ||
self.mark(after); | ||
break; | ||
@@ -473,4 +424,3 @@ | ||
self.jumpIfNot(self.explodeExpression(path.get("test")), after); | ||
} else { | ||
// No test means continue unconditionally. | ||
} else {// No test means continue unconditionally. | ||
} | ||
@@ -481,3 +431,2 @@ | ||
}); | ||
self.mark(update); | ||
@@ -492,5 +441,3 @@ | ||
self.jump(head); | ||
self.mark(after); | ||
break; | ||
@@ -504,21 +451,13 @@ | ||
after = loc(); | ||
var keyIterNextFn = self.makeTempVar(); | ||
self.emitAssign(keyIterNextFn, t.callExpression(util.runtimeProperty("keys"), [self.explodeExpression(path.get("right"))])); | ||
self.mark(head); | ||
var keyInfoTmpVar = self.makeTempVar(); | ||
self.jumpIf(t.memberExpression(t.assignmentExpression("=", keyInfoTmpVar, t.callExpression(keyIterNextFn, [])), t.identifier("done"), false), after); | ||
self.emitAssign(stmt.left, t.memberExpression(keyInfoTmpVar, t.identifier("value"), false)); | ||
self.leapManager.withEntry(new leap.LoopEntry(after, head, labelId), function () { | ||
self.explodeStatement(path.get("body")); | ||
}); | ||
self.jump(head); | ||
self.mark(after); | ||
break; | ||
@@ -531,3 +470,2 @@ | ||
}); | ||
break; | ||
@@ -540,3 +478,2 @@ | ||
}); | ||
break; | ||
@@ -548,9 +485,7 @@ | ||
var disc = self.emitAssign(self.makeTempVar(), self.explodeExpression(path.get("discriminant"))); | ||
after = loc(); | ||
var defaultLoc = loc(); | ||
var condition = defaultLoc; | ||
var caseLocs = []; | ||
var caseLocs = []; // If there are no cases, .cases might be undefined. | ||
// If there are no cases, .cases might be undefined. | ||
var cases = stmt.cases || []; | ||
@@ -572,3 +507,2 @@ | ||
self.jump(self.explodeExpression(discriminant)); | ||
self.leapManager.withEntry(new leap.SwitchEntry(after), function () { | ||
@@ -578,3 +512,2 @@ path.get("cases").forEach(function (casePath) { | ||
self.mark(caseLocs[i]); | ||
casePath.get("consequent").forEach(function (path) { | ||
@@ -585,7 +518,8 @@ self.explodeStatement(path); | ||
}); | ||
self.mark(after); | ||
self.mark(after); | ||
if (defaultLoc.value === -1) { | ||
self.mark(defaultLoc); | ||
_assert2.default.strictEqual(after.value, defaultLoc.value); | ||
_assert.default.strictEqual(after.value, defaultLoc.value); | ||
} | ||
@@ -598,5 +532,3 @@ | ||
after = loc(); | ||
self.jumpIfNot(self.explodeExpression(path.get("test")), elseLoc || after); | ||
self.explodeStatement(path.get("consequent")); | ||
@@ -611,3 +543,2 @@ | ||
self.mark(after); | ||
break; | ||
@@ -620,3 +551,2 @@ | ||
}); | ||
break; | ||
@@ -629,16 +559,10 @@ | ||
after = loc(); | ||
var handler = stmt.handler; | ||
var catchLoc = handler && loc(); | ||
var catchEntry = catchLoc && new leap.CatchEntry(catchLoc, handler.param); | ||
var finallyLoc = stmt.finalizer && loc(); | ||
var finallyEntry = finallyLoc && new leap.FinallyEntry(finallyLoc, after); | ||
var tryEntry = new leap.TryEntry(self.getUnmarkedCurrentLoc(), catchEntry, finallyEntry); | ||
self.tryEntries.push(tryEntry); | ||
self.updateContextPrevLoc(tryEntry.firstLoc); | ||
self.leapManager.withEntry(tryEntry, function () { | ||
@@ -660,7 +584,5 @@ self.explodeStatement(path.get("block")); | ||
self.updateContextPrevLoc(self.mark(catchLoc)); | ||
var bodyPath = path.get("handler.body"); | ||
var safeParam = self.makeTempVar(); | ||
self.clearPendingException(tryEntry.firstLoc, safeParam); | ||
bodyPath.traverse(catchParamVisitor, { | ||
@@ -670,3 +592,2 @@ safeParam: safeParam, | ||
}); | ||
self.leapManager.withEntry(catchEntry, function () { | ||
@@ -679,13 +600,9 @@ self.explodeStatement(bodyPath); | ||
self.updateContextPrevLoc(self.mark(finallyLoc)); | ||
self.leapManager.withEntry(finallyEntry, function () { | ||
self.explodeStatement(path.get("finalizer")); | ||
}); | ||
self.emit(t.returnStatement(t.callExpression(self.contextProperty("finish"), [finallyEntry.firstLoc]))); | ||
} | ||
}); | ||
self.mark(after); | ||
break; | ||
@@ -695,3 +612,2 @@ | ||
self.emit(t.throwStatement(self.explodeExpression(path.get("argument")))); | ||
break; | ||
@@ -710,3 +626,2 @@ | ||
}, | ||
Scope: function Scope(path, state) { | ||
@@ -723,6 +638,6 @@ if (path.scope.hasOwnBinding(state.catchParamName)) { | ||
if (!isValidCompletion(record)) { | ||
_assert2.default.ok(false, "invalid completion record: " + JSON.stringify(record)); | ||
_assert.default.ok(false, "invalid completion record: " + JSON.stringify(record)); | ||
} | ||
_assert2.default.notStrictEqual(record.type, "normal", "normal completions are not abrupt"); | ||
_assert.default.notStrictEqual(record.type, "normal", "normal completions are not abrupt"); | ||
@@ -761,5 +676,3 @@ var t = util.getTypes(); | ||
return false; | ||
} | ||
// Not all offsets into emitter.listing are potential jump targets. For | ||
} // Not all offsets into emitter.listing are potential jump targets. For | ||
// example, execution typically falls into the beginning of a try block | ||
@@ -773,7 +686,7 @@ // without jumping directly there. This method returns the current offset | ||
// code shorter. | ||
Ep.getUnmarkedCurrentLoc = function () { | ||
return util.getTypes().numericLiteral(this.listing.length); | ||
}; | ||
// The context.prev property takes the value of context.next whenever we | ||
}; // The context.prev property takes the value of context.next whenever we | ||
// evaluate the switch statement discriminant, which is generally good | ||
@@ -788,2 +701,4 @@ // enough for tracking the last location we jumped to, but sometimes | ||
// be costly and verbose to set context.prev before every statement. | ||
Ep.updateContextPrevLoc = function (loc) { | ||
@@ -799,11 +714,11 @@ if (loc) { | ||
// Otherwise assert that the location matches the current offset. | ||
_assert2.default.strictEqual(loc.value, this.listing.length); | ||
_assert.default.strictEqual(loc.value, this.listing.length); | ||
} | ||
} else { | ||
loc = this.getUnmarkedCurrentLoc(); | ||
} | ||
// Make sure context.prev is up to date in case we fell into this try | ||
} // Make sure context.prev is up to date in case we fell into this try | ||
// statement without jumping to it. TODO Consider avoiding this | ||
// assignment when we know control must have jumped here. | ||
this.emitAssign(this.contextProperty("prev"), loc); | ||
@@ -815,2 +730,3 @@ }; | ||
var expr = path.node; | ||
if (expr) { | ||
@@ -823,7 +739,9 @@ t.assertExpression(expr); | ||
var self = this; | ||
var result = void 0; // Used optionally by several cases below. | ||
var after = void 0; | ||
var result; // Used optionally by several cases below. | ||
var after; | ||
function finish(expr) { | ||
t.assertExpression(expr); | ||
if (ignoreResult) { | ||
@@ -834,17 +752,15 @@ self.emit(expr); | ||
} | ||
} | ||
} // If the expression does not contain a leap, then we either emit the | ||
// expression as a standalone statement or return it whole. | ||
// If the expression does not contain a leap, then we either emit the | ||
// expression as a standalone statement or return it whole. | ||
if (!meta.containsLeap(expr)) { | ||
return finish(expr); | ||
} | ||
// If any child contains a leap (such as a yield or labeled continue or | ||
} // If any child contains a leap (such as a yield or labeled continue or | ||
// break statement), then any sibling subexpressions will almost | ||
// certainly have to be exploded in order to maintain the order of their | ||
// side effects relative to the leaping child(ren). | ||
var hasLeapingChildren = meta.containsLeap.onlyChildren(expr); | ||
// In order to save the rest of explodeExpression from a combinatorial | ||
var hasLeapingChildren = meta.containsLeap.onlyChildren(expr); // In order to save the rest of explodeExpression from a combinatorial | ||
// trainwreck of special cases, explodeViaTempVar is responsible for | ||
@@ -858,10 +774,9 @@ // deciding when a subexpression needs to be "exploded," which is my | ||
// side effects of those subexpressions. | ||
function explodeViaTempVar(tempVar, childPath, ignoreChildResult) { | ||
_assert2.default.ok(!ignoreChildResult || !tempVar, "Ignoring the result of a child expression but forcing it to " + "be assigned to a temporary variable?"); | ||
_assert.default.ok(!ignoreChildResult || !tempVar, "Ignoring the result of a child expression but forcing it to " + "be assigned to a temporary variable?"); | ||
var result = self.explodeExpression(childPath, ignoreChildResult); | ||
if (ignoreChildResult) { | ||
// Side effects already emitted above. | ||
if (ignoreChildResult) {// Side effects already emitted above. | ||
} else if (tempVar || hasLeapingChildren && !t.isLiteral(result)) { | ||
@@ -881,9 +796,9 @@ // If tempVar was provided, then the result will always be assigned | ||
} | ||
return result; | ||
} | ||
// If ignoreResult is true, then we must take full responsibility for | ||
} // If ignoreResult is true, then we must take full responsibility for | ||
// emitting the expression with all its side effects, and we should not | ||
// return a result. | ||
switch (expr.type) { | ||
@@ -896,6 +811,4 @@ case "MemberExpression": | ||
var argsPath = path.get("arguments"); | ||
var newCallee = void 0; | ||
var newCallee; | ||
var newArgs = []; | ||
var hasLeapingArgs = false; | ||
@@ -913,12 +826,7 @@ argsPath.forEach(function (argPath) { | ||
// member expression still gets bound to `this` for the call. | ||
var newObject = explodeViaTempVar( | ||
// Assign the exploded callee.object expression to a temporary | ||
var newObject = explodeViaTempVar( // Assign the exploded callee.object expression to a temporary | ||
// variable so that we can use it twice without reevaluating it. | ||
self.makeTempVar(), calleePath.get("object")); | ||
var newProperty = calleePath.node.computed ? explodeViaTempVar(null, calleePath.get("property")) : calleePath.node.property; | ||
newArgs.unshift(newObject); | ||
newCallee = t.memberExpression(t.memberExpression(newObject, newProperty, calleePath.node.computed), t.identifier("call"), false); | ||
@@ -947,3 +855,2 @@ } else { | ||
}); | ||
return finish(t.callExpression(newCallee, newArgs)); | ||
@@ -972,3 +879,2 @@ | ||
var lastIndex = expr.expressions.length - 1; | ||
path.get("expressions").forEach(function (exprPath) { | ||
@@ -981,3 +887,2 @@ if (exprPath.key === lastIndex) { | ||
}); | ||
return result; | ||
@@ -997,3 +902,4 @@ | ||
} else { | ||
_assert2.default.strictEqual(expr.operator, "||"); | ||
_assert.default.strictEqual(expr.operator, "||"); | ||
self.jumpIf(left, after); | ||
@@ -1003,5 +909,3 @@ } | ||
explodeViaTempVar(result, path.get("right"), ignoreResult); | ||
self.mark(after); | ||
return result; | ||
@@ -1013,3 +917,2 @@ | ||
var test = self.explodeExpression(path.get("test")); | ||
self.jumpIfNot(test, elseLoc); | ||
@@ -1023,13 +926,9 @@ | ||
self.jump(after); | ||
self.mark(elseLoc); | ||
explodeViaTempVar(result, path.get("alternate"), ignoreResult); | ||
self.mark(after); | ||
return result; | ||
case "UnaryExpression": | ||
return finish(t.unaryExpression(expr.operator, | ||
// Can't (and don't need to) break up the syntax of the argument. | ||
return finish(t.unaryExpression(expr.operator, // Can't (and don't need to) break up the syntax of the argument. | ||
// Think about delete a[b]. | ||
@@ -1055,7 +954,6 @@ self.explodeExpression(path.get("argument")), !!expr.prefix)); | ||
var _ret = t.returnStatement(t.callExpression(self.contextProperty("delegateYield"), [arg, t.stringLiteral(_result.property.name), after])); | ||
_ret.loc = expr.loc; | ||
self.emit(_ret); | ||
self.mark(after); | ||
return _result; | ||
@@ -1065,10 +963,8 @@ } | ||
self.emitAssign(self.contextProperty("next"), after); | ||
var ret = t.returnStatement(arg || null); // Preserve the `yield` location so that source mappings for the statements | ||
// link back to the yield properly. | ||
var ret = t.returnStatement(arg || null); | ||
// Preserve the `yield` location so that source mappings for the statements | ||
// link back to the yield properly. | ||
ret.loc = expr.loc; | ||
self.emit(ret); | ||
self.mark(after); | ||
return self.contextProperty("sent"); | ||
@@ -1075,0 +971,0 @@ |
"use strict"; | ||
var _util = require("./util"); | ||
var util = _interopRequireWildcard(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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
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; } } | ||
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. | ||
/** | ||
@@ -21,2 +13,6 @@ * Copyright (c) 2014-present, 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. | ||
@@ -26,10 +22,8 @@ exports.hoist = function (funPath) { | ||
t.assertFunction(funPath.node); | ||
var vars = {}; | ||
function varDeclToExpr(vdec, includeIdentifiers) { | ||
t.assertVariableDeclaration(vdec); | ||
// TODO assert.equal(vdec.kind, "var"); | ||
t.assertVariableDeclaration(vdec); // TODO assert.equal(vdec.kind, "var"); | ||
var exprs = []; | ||
vdec.declarations.forEach(function (dec) { | ||
@@ -46,7 +40,4 @@ // Note: We duplicate 'dec.id' here to ensure that the variable declaration IDs don't | ||
}); | ||
if (exprs.length === 0) return null; | ||
if (exprs.length === 1) return exprs[0]; | ||
return t.sequenceExpression(exprs); | ||
@@ -59,2 +50,3 @@ } | ||
var expr = varDeclToExpr(path.node, false); | ||
if (expr === null) { | ||
@@ -66,12 +58,12 @@ path.remove(); | ||
util.replaceWithOrRemove(path, t.expressionStatement(expr)); | ||
} | ||
} // Since the original node has been either removed or replaced, | ||
// avoid traversing it any further. | ||
// Since the original node has been either removed or replaced, | ||
// avoid traversing it any further. | ||
path.skip(); | ||
} | ||
}, | ||
ForStatement: function ForStatement(path) { | ||
var init = path.node.init; | ||
if (t.isVariableDeclaration(init)) { | ||
@@ -81,5 +73,5 @@ util.replaceWithOrRemove(path.get("init"), varDeclToExpr(init, false)); | ||
}, | ||
ForXStatement: function ForXStatement(path) { | ||
var left = path.get("left"); | ||
if (left.isVariableDeclaration()) { | ||
@@ -89,7 +81,5 @@ util.replaceWithOrRemove(left, varDeclToExpr(left.node, true)); | ||
}, | ||
FunctionDeclaration: function FunctionDeclaration(path) { | ||
var node = path.node; | ||
vars[node.id.name] = node.id; | ||
var assignment = t.expressionStatement(t.assignmentExpression("=", node.id, t.functionExpression(path.scope.generateUidIdentifierBasedOnNode(node), node.params, node.body, node.generator, node.expression))); | ||
@@ -100,6 +90,5 @@ | ||
// enclosing block. | ||
path.parentPath.unshiftContainer("body", assignment); | ||
path.parentPath.unshiftContainer("body", assignment); // Remove the function declaration now that we've inserted the | ||
// equivalent assignment form at the beginning of the block. | ||
// Remove the function declaration now that we've inserted the | ||
// equivalent assignment form at the beginning of the block. | ||
path.remove(); | ||
@@ -111,8 +100,7 @@ } else { | ||
util.replaceWithOrRemove(path, assignment); | ||
} | ||
} // Don't hoist variables out of inner functions. | ||
// Don't hoist variables out of inner functions. | ||
path.skip(); | ||
}, | ||
FunctionExpression: function FunctionExpression(path) { | ||
@@ -122,3 +110,2 @@ // Don't descend into nested function expressions. | ||
}, | ||
ArrowFunctionExpression: function ArrowFunctionExpression(path) { | ||
@@ -129,16 +116,13 @@ // Don't descend into nested function expressions. | ||
}); | ||
var paramNames = {}; | ||
funPath.get("params").forEach(function (paramPath) { | ||
var param = paramPath.node; | ||
if (t.isIdentifier(param)) { | ||
paramNames[param.name] = param; | ||
} else { | ||
// Variables declared by destructuring parameter patterns will be | ||
} else {// Variables declared by destructuring parameter patterns will be | ||
// harmlessly re-declared. | ||
} | ||
}); | ||
var declarations = []; | ||
Object.keys(vars).forEach(function (name) { | ||
@@ -145,0 +129,0 @@ if (!hasOwn.call(paramNames, name)) { |
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = _default; | ||
exports.default = function (context) { | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
function _default(context) { | ||
var plugin = { | ||
visitor: require("./visit").getVisitor(context) | ||
}; | ||
// Some presets manually call child presets, but fail to pass along the | ||
}; // Some presets manually call child presets, but fail to pass along the | ||
// context object. Out of an abundance of caution, we verify that it | ||
// exists first to avoid causing unnecessary breaking changes. | ||
var version = context && context.version; | ||
// The "name" property is not allowed in older versions of Babel (6.x) | ||
var version = context && context.version; // The "name" property is not allowed in older versions of Babel (6.x) | ||
// and will cause the plugin validator to throw an exception. | ||
if (version && parseInt(version, 10) >= 7) { | ||
@@ -22,2 +27,2 @@ plugin.name = "regenerator-transform"; | ||
return plugin; | ||
}; | ||
} |
"use strict"; | ||
var _assert = require("assert"); | ||
var _assert = _interopRequireDefault(require("assert")); | ||
var _assert2 = _interopRequireDefault(_assert); | ||
var _util = require("util"); | ||
@@ -13,10 +11,11 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
function Entry() { | ||
_assert2.default.ok(this instanceof Entry); | ||
} /** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
_assert.default.ok(this instanceof Entry); | ||
} | ||
@@ -34,5 +33,3 @@ function FunctionEntry(returnLoc) { | ||
Entry.call(this); | ||
var t = (0, _util2.getTypes)(); | ||
t.assertLiteral(breakLoc); | ||
@@ -66,3 +63,2 @@ t.assertLiteral(continueLoc); | ||
Entry.call(this); | ||
var t = (0, _util2.getTypes)(); | ||
@@ -72,3 +68,3 @@ t.assertLiteral(firstLoc); | ||
if (catchEntry) { | ||
_assert2.default.ok(catchEntry instanceof CatchEntry); | ||
_assert.default.ok(catchEntry instanceof CatchEntry); | ||
} else { | ||
@@ -79,10 +75,10 @@ catchEntry = null; | ||
if (finallyEntry) { | ||
_assert2.default.ok(finallyEntry instanceof FinallyEntry); | ||
_assert.default.ok(finallyEntry instanceof FinallyEntry); | ||
} else { | ||
finallyEntry = null; | ||
} | ||
} // Have to have one or the other (or both). | ||
// Have to have one or the other (or both). | ||
_assert2.default.ok(catchEntry || finallyEntry); | ||
_assert.default.ok(catchEntry || finallyEntry); | ||
this.firstLoc = firstLoc; | ||
@@ -98,8 +94,5 @@ this.catchEntry = catchEntry; | ||
Entry.call(this); | ||
var t = (0, _util2.getTypes)(); | ||
t.assertLiteral(firstLoc); | ||
t.assertIdentifier(paramId); | ||
this.firstLoc = firstLoc; | ||
@@ -126,8 +119,5 @@ this.paramId = paramId; | ||
Entry.call(this); | ||
var t = (0, _util2.getTypes)(); | ||
t.assertLiteral(breakLoc); | ||
t.assertIdentifier(label); | ||
this.breakLoc = breakLoc; | ||
@@ -141,7 +131,8 @@ this.label = label; | ||
function LeapManager(emitter) { | ||
_assert2.default.ok(this instanceof LeapManager); | ||
_assert.default.ok(this instanceof LeapManager); | ||
var Emitter = require("./emit").Emitter; | ||
_assert2.default.ok(emitter instanceof Emitter); | ||
_assert.default.ok(emitter instanceof Emitter); | ||
this.emitter = emitter; | ||
@@ -155,4 +146,6 @@ this.entryStack = [new FunctionEntry(emitter.finalLoc)]; | ||
LMp.withEntry = function (entry, callback) { | ||
_assert2.default.ok(entry instanceof Entry); | ||
_assert.default.ok(entry instanceof Entry); | ||
this.entryStack.push(entry); | ||
try { | ||
@@ -162,3 +155,4 @@ callback.call(this.emitter); | ||
var popped = this.entryStack.pop(); | ||
_assert2.default.strictEqual(popped, entry); | ||
_assert.default.strictEqual(popped, entry); | ||
} | ||
@@ -171,2 +165,3 @@ }; | ||
var loc = entry[property]; | ||
if (loc) { | ||
@@ -177,4 +172,3 @@ if (label) { | ||
} | ||
} else if (entry instanceof LabeledEntry) { | ||
// Ignore LabeledEntry entries unless we are actually breaking to | ||
} else if (entry instanceof LabeledEntry) {// Ignore LabeledEntry entries unless we are actually breaking to | ||
// a label. | ||
@@ -181,0 +175,0 @@ } else { |
"use strict"; | ||
var _assert = require("assert"); | ||
var _assert = _interopRequireDefault(require("assert")); | ||
var _assert2 = _interopRequireDefault(_assert); | ||
var _util = require("./util.js"); | ||
@@ -17,4 +15,4 @@ | ||
*/ | ||
var m = require("private").makeAccessor(); | ||
var m = require("private").makeAccessor(); | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
@@ -25,16 +23,16 @@ | ||
var t = (0, _util.getTypes)(); | ||
t.assertNode(node); | ||
t.assertNode(node); // Assume no side effects until we find out otherwise. | ||
// Assume no side effects until we find out otherwise. | ||
var result = false; | ||
function check(child) { | ||
if (result) { | ||
// Do nothing. | ||
if (result) {// Do nothing. | ||
} else if (Array.isArray(child)) { | ||
child.some(check); | ||
} else if (t.isNode(child)) { | ||
_assert2.default.strictEqual(result, false); | ||
_assert.default.strictEqual(result, false); | ||
result = predicate(child); | ||
} | ||
return result; | ||
@@ -44,2 +42,3 @@ } | ||
var keys = t.VISITOR_KEYS[node.type]; | ||
if (keys) { | ||
@@ -58,12 +57,8 @@ for (var i = 0; i < keys.length; i++) { | ||
(0, _util.getTypes)().assertNode(node); | ||
var meta = m(node); | ||
if (hasOwn.call(meta, propertyName)) return meta[propertyName]; | ||
if (hasOwn.call(meta, propertyName)) return meta[propertyName]; // Certain types are "opaque," which means they have no side | ||
// effects or leaps and we don't care about their subexpressions. | ||
// Certain types are "opaque," which means they have no side | ||
// effects or leaps and we don't care about their subexpressions. | ||
if (hasOwn.call(opaqueTypes, node.type)) return meta[propertyName] = false; | ||
if (hasOwn.call(knownTypes, node.type)) return meta[propertyName] = true; | ||
return meta[propertyName] = onlyChildren(node); | ||
@@ -73,3 +68,2 @@ } | ||
predicate.onlyChildren = onlyChildren; | ||
return predicate; | ||
@@ -81,17 +75,22 @@ } | ||
ArrowFunctionExpression: true | ||
}; | ||
}; // These types potentially have side effects regardless of what side | ||
// effects their subexpressions have. | ||
// These types potentially have side effects regardless of what side | ||
// effects their subexpressions have. | ||
var sideEffectTypes = { | ||
CallExpression: true, // Anything could happen! | ||
ForInStatement: true, // Modifies the key variable. | ||
UnaryExpression: true, // Think delete. | ||
BinaryExpression: true, // Might invoke .toString() or .valueOf(). | ||
AssignmentExpression: true, // Side-effecting by definition. | ||
UpdateExpression: true, // Updates are essentially assignments. | ||
CallExpression: true, | ||
// Anything could happen! | ||
ForInStatement: true, | ||
// Modifies the key variable. | ||
UnaryExpression: true, | ||
// Think delete. | ||
BinaryExpression: true, | ||
// Might invoke .toString() or .valueOf(). | ||
AssignmentExpression: true, | ||
// Side-effecting by definition. | ||
UpdateExpression: true, | ||
// Updates are essentially assignments. | ||
NewExpression: true // Similar to CallExpression. | ||
}; | ||
// These types are the direct cause of all leaps in control flow. | ||
}; // These types are the direct cause of all leaps in control flow. | ||
var leapTypes = { | ||
@@ -103,5 +102,4 @@ YieldExpression: true, | ||
ThrowStatement: true | ||
}; | ||
}; // All leap types are also side effect types. | ||
// All leap types are also side effect types. | ||
for (var type in leapTypes) { | ||
@@ -108,0 +106,0 @@ if (hasOwn.call(leapTypes, type)) { |
@@ -6,8 +6,12 @@ "use strict"; | ||
var _util = require("./util"); | ||
var util = _interopRequireWildcard(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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
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; } } | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
// this function converts a shorthand object generator method into a normal | ||
@@ -46,11 +50,11 @@ // (non-shorthand) object property which is a generator function expression. for | ||
throw new Error("replaceShorthandObjectMethod can only be called on Function AST node paths."); | ||
} | ||
} // this function only replaces shorthand object methods (called ObjectMethod | ||
// in Babel-speak). | ||
// this function only replaces shorthand object methods (called ObjectMethod | ||
// in Babel-speak). | ||
if (!t.isObjectMethod(path.node)) { | ||
return path; | ||
} | ||
} // this function only replaces generators. | ||
// this function only replaces generators. | ||
if (!path.node.generator) { | ||
@@ -63,3 +67,2 @@ return path; | ||
}); | ||
var functionExpression = t.functionExpression(null, // id | ||
@@ -69,3 +72,2 @@ parameters, // params | ||
path.node.generator, path.node.async); | ||
util.replaceWithOrRemove(path, t.objectProperty(t.cloneDeep(path.node.key), // key | ||
@@ -75,14 +77,8 @@ functionExpression, //value | ||
false // shorthand | ||
)); | ||
// path now refers to the ObjectProperty AST node path, but we want to return a | ||
)); // path now refers to the ObjectProperty AST node path, but we want to return a | ||
// Function AST node path for the function expression we created. we know that | ||
// the FunctionExpression we just created is the value of the ObjectProperty, | ||
// so return the "value" path off of this path. | ||
return path.get("value"); | ||
} /** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
} |
@@ -9,2 +9,3 @@ "use strict"; | ||
exports.replaceWithOrRemove = replaceWithOrRemove; | ||
/** | ||
@@ -16,3 +17,2 @@ * Copyright (c) 2014-present, Facebook, Inc. | ||
*/ | ||
var currentTypes = null; | ||
@@ -24,4 +24,5 @@ | ||
currentTypes = types; | ||
try { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
@@ -47,3 +48,5 @@ } | ||
function isReference(path) { | ||
return path.isReferenced() || path.parentPath.isAssignmentExpression({ left: path.node }); | ||
return path.isReferenced() || path.parentPath.isAssignmentExpression({ | ||
left: path.node | ||
}); | ||
} | ||
@@ -50,0 +53,0 @@ |
@@ -7,9 +7,6 @@ /** | ||
*/ | ||
"use strict"; | ||
var _assert = require("assert"); | ||
var _assert = _interopRequireDefault(require("assert")); | ||
var _assert2 = _interopRequireDefault(_assert); | ||
var _hoist = require("./hoist"); | ||
@@ -19,12 +16,8 @@ | ||
var _replaceShorthandObjectMethod = require("./replaceShorthandObjectMethod"); | ||
var _replaceShorthandObjectMethod = _interopRequireDefault(require("./replaceShorthandObjectMethod")); | ||
var _replaceShorthandObjectMethod2 = _interopRequireDefault(_replaceShorthandObjectMethod); | ||
var util = _interopRequireWildcard(require("./util")); | ||
var _util = require("./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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
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; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -53,11 +46,9 @@ | ||
return; | ||
} | ||
} // if this is an ObjectMethod, we need to convert it to an ObjectProperty | ||
// if this is an ObjectMethod, we need to convert it to an ObjectProperty | ||
path = (0, _replaceShorthandObjectMethod2.default)(path); | ||
path = (0, _replaceShorthandObjectMethod.default)(path); | ||
node = path.node; | ||
var contextId = path.scope.generateUidIdentifier("context"); | ||
var argsId = path.scope.generateUidIdentifier("args"); | ||
path.ensureBlock(); | ||
@@ -73,8 +64,7 @@ var bodyBlockPath = path.get("body"); | ||
}); | ||
var outerBody = []; | ||
var innerBody = []; | ||
bodyBlockPath.get("body").forEach(function (childPath) { | ||
var node = childPath.node; | ||
if (t.isExpressionStatement(node) && t.isStringLiteral(node.expression)) { | ||
@@ -100,18 +90,17 @@ // Babylon represents directives like "use strict" as elements | ||
var outerFnExpr = getOuterFnExpr(path); | ||
// Note that getOuterFnExpr has the side-effect of ensuring that the | ||
var outerFnExpr = getOuterFnExpr(path); // 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. | ||
t.assertIdentifier(node.id); | ||
var innerFnId = t.identifier(node.id.name + "$"); | ||
var innerFnId = t.identifier(node.id.name + "$"); // Turn all declarations into vars, and replace the original | ||
// declarations with equivalent assignment expressions. | ||
// Turn all declarations into vars, and replace the original | ||
// declarations with equivalent assignment expressions. | ||
var vars = (0, _hoist.hoist)(path); | ||
var didRenameArguments = renameArguments(path, argsId); | ||
var didRenameArguments = renameArguments(path, argsId); | ||
if (didRenameArguments) { | ||
vars = vars || t.variableDeclaration("var", []); | ||
var argumentIdentifier = t.identifier("arguments"); | ||
// we need to do this as otherwise arguments in arrow functions gets hoisted | ||
var argumentIdentifier = t.identifier("arguments"); // we need to do this as otherwise arguments in arrow functions gets hoisted | ||
argumentIdentifier._shadowedFunctionLiteral = path; | ||
@@ -128,9 +117,8 @@ vars.declarations.push(t.variableDeclarator(argsId, argumentIdentifier)); | ||
var wrapArgs = [emitter.getContextFunction(innerFnId), | ||
// Async functions that are not generators don't care about the | ||
var wrapArgs = [emitter.getContextFunction(innerFnId), // Async functions that are not generators don't care about the | ||
// outer function because they don't need it to be marked and don't | ||
// inherit from its .prototype. | ||
node.generator ? outerFnExpr : t.nullLiteral(), t.thisExpression()]; | ||
var tryLocsList = emitter.getTryLocsList(); | ||
var tryLocsList = emitter.getTryLocsList(); | ||
if (tryLocsList) { | ||
@@ -141,7 +129,6 @@ wrapArgs.push(tryLocsList); | ||
var wrapCall = t.callExpression(util.runtimeProperty(node.async ? "async" : "wrap"), wrapArgs); | ||
outerBody.push(t.returnStatement(wrapCall)); | ||
node.body = t.blockStatement(outerBody); | ||
var oldDirectives = bodyBlockPath.node.directives; | ||
var oldDirectives = bodyBlockPath.node.directives; | ||
if (oldDirectives) { | ||
@@ -154,2 +141,3 @@ // Babylon represents directives like "use strict" as elements of | ||
var wasGeneratorFunction = node.generator; | ||
if (wasGeneratorFunction) { | ||
@@ -166,7 +154,7 @@ node.generator = false; | ||
path.addComment("leading", "#__PURE__"); | ||
} | ||
// Generators are processed in 'exit' handlers so that regenerator only has to run on | ||
} // Generators are processed in 'exit' handlers so that regenerator only has to run on | ||
// an ES5 AST, but that means traversal will not pick up newly inserted references | ||
// to things like 'regeneratorRuntime'. To avoid this, we explicitly requeue. | ||
path.requeue(); | ||
@@ -176,8 +164,8 @@ }) | ||
}; | ||
}; | ||
// Given a NodePath for a Function, return an Expression node that can be | ||
}; // Given a NodePath for a Function, return an Expression node that can be | ||
// 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) { | ||
@@ -209,3 +197,2 @@ var t = util.getTypes(); | ||
t.assertIdentifier(node.id); | ||
var blockPath = funPath.findParent(function (path) { | ||
@@ -220,5 +207,7 @@ return path.isProgram() || path.isBlockStatement(); | ||
var block = blockPath.node; | ||
_assert2.default.ok(Array.isArray(block.body)); | ||
_assert.default.ok(Array.isArray(block.body)); | ||
var info = getMarkInfo(block); | ||
if (!info.decl) { | ||
@@ -230,16 +219,13 @@ info.decl = t.variableDeclaration("var", []); | ||
_assert2.default.strictEqual(info.declPath.node, info.decl); | ||
_assert.default.strictEqual(info.declPath.node, info.decl); // Get a new unique identifier for our marked variable. | ||
// Get a new unique identifier for our marked variable. | ||
var markedId = blockPath.scope.generateUidIdentifier("marked"); | ||
var markCallExp = t.callExpression(util.runtimeProperty("mark"), [node.id]); | ||
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); | ||
_assert.default.strictEqual(markCallExpPath.node, markCallExp); | ||
markCallExpPath.addComment("leading", "#__PURE__"); | ||
return markedId; | ||
@@ -253,9 +239,7 @@ } | ||
}; | ||
funcPath.traverse(argumentsVisitor, state); | ||
// If the traversal replaced any arguments references, then we need to | ||
funcPath.traverse(argumentsVisitor, state); // If the traversal replaced any arguments references, then we need to | ||
// alias the outer function's arguments binding (be it the implicit | ||
// arguments object or some other parameter or variable) to the variable | ||
// named by argsId. | ||
return state.didRenameArguments; | ||
@@ -268,3 +252,2 @@ } | ||
}, | ||
Identifier: function Identifier(path, state) { | ||
@@ -277,3 +260,2 @@ if (path.node.name === "arguments" && util.isReference(path)) { | ||
}; | ||
var functionSentVisitor = { | ||
@@ -283,3 +265,2 @@ MetaProperty: function MetaProperty(path) { | ||
if (node.meta.name === "function" && node.property.name === "sent") { | ||
@@ -291,3 +272,2 @@ var t = util.getTypes(); | ||
}; | ||
var awaitVisitor = { | ||
@@ -297,14 +277,11 @@ Function: function Function(path) { | ||
}, | ||
AwaitExpression: function AwaitExpression(path) { | ||
var t = util.getTypes(); | ||
var t = util.getTypes(); // Convert await expressions to yield expressions. | ||
// Convert await expressions to yield expressions. | ||
var argument = path.node.argument; | ||
// Transforming `await x` to `yield regeneratorRuntime.awrap(x)` | ||
var argument = path.node.argument; // Transforming `await x` to `yield regeneratorRuntime.awrap(x)` | ||
// causes the argument to be wrapped in such a way that the runtime | ||
// can distinguish between awaited and merely yielded values. | ||
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.12.4", | ||
"version": "0.13.0", | ||
"main": "lib/index.js", | ||
@@ -25,3 +25,3 @@ "keywords": [ | ||
[ | ||
"env", | ||
"@babel/preset-env", | ||
{ | ||
@@ -37,5 +37,5 @@ "loose": true | ||
"devDependencies": { | ||
"babel-cli": "^6.9.0", | ||
"babel-preset-env": "^1.2.2" | ||
"@babel/cli": "^7.0.0-beta.51", | ||
"@babel/preset-env": "^7.0.0-beta.51" | ||
} | ||
} |
@@ -43,5 +43,5 @@ # regenerator-transform | ||
```javascript | ||
require("babel-core").transform("code", { | ||
require("@babel/core").transformSync("code", { | ||
plugins: ["regenerator-transform"] | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
119838
18
3132