more regenerator spring cleaning

This commit is contained in:
Sebastian McKenzie 2014-11-19 12:09:41 +11:00
parent cacee5c625
commit f43a3dec4b
5 changed files with 37 additions and 43 deletions

View File

@ -23,7 +23,7 @@ var n = types.namedTypes;
function Emitter(contextId) {
assert.ok(this instanceof Emitter);
n.Identifier.assert(contextId);
t.assertIdentifier(contextId);
// 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
@ -64,7 +64,7 @@ function loc() {
// Sets the exact value of the given location to the offset of the next
// Statement emitted.
Emitter.prototype.mark = function(loc) {
n.Literal.assert(loc);
t.assertLiteral(loc);
var index = this.listing.length;
if (loc.value === -1) {
loc.value = index;
@ -79,7 +79,7 @@ Emitter.prototype.mark = function(loc) {
Emitter.prototype.emit = function(node) {
if (t.isExpression(node)) node = t.expressionStatement(node);
n.Statement.assert(node);
t.assertStatement(node);
this.listing.push(node);
};
@ -145,7 +145,7 @@ Emitter.prototype.stop = function(rval) {
};
Emitter.prototype.setReturnValue = function(valuePath) {
n.Expression.assert(valuePath.value);
t.assertExpression(valuePath.value);
this.emitAssign(
this.contextProperty("rval"),
@ -154,7 +154,7 @@ Emitter.prototype.setReturnValue = function(valuePath) {
};
Emitter.prototype.clearPendingException = function(tryLoc, assignee) {
n.Literal.assert(tryLoc);
t.assertLiteral(tryLoc);
var catchCall = t.callExpression(
this.contextProperty("catch", true),
@ -177,8 +177,8 @@ Emitter.prototype.jump = function(toLoc) {
// Conditional jump.
Emitter.prototype.jumpIf = function(test, toLoc) {
n.Expression.assert(test);
n.Literal.assert(toLoc);
t.assertExpression(test);
t.assertLiteral(toLoc);
this.emit(t.ifStatement(
test,
@ -191,8 +191,8 @@ Emitter.prototype.jumpIf = function(test, toLoc) {
// Conditional jump, with the condition negated.
Emitter.prototype.jumpIfNot = function(test, toLoc) {
n.Expression.assert(test);
n.Literal.assert(toLoc);
t.assertExpression(test);
t.assertLiteral(toLoc);
var negatedTest;
if (t.isUnaryExpression(test) && test.operator === "!") {
@ -352,7 +352,7 @@ Emitter.prototype.explode = function(path, ignoreResult) {
var node = path.value;
var self = this;
n.Node.assert(node);
n.Node.check(node);
if (t.isStatement(node))
return self.explodeStatement(path);
@ -396,10 +396,10 @@ Emitter.prototype.explodeStatement = function(path, labelId) {
var self = this;
var after, head;
n.Statement.assert(stmt);
t.assertStatement(stmt);
if (labelId) {
n.Identifier.assert(labelId);
t.assertIdentifier(labelId);
} else {
labelId = null;
}
@ -502,7 +502,7 @@ Emitter.prototype.explodeStatement = function(path, labelId) {
break;
case "ForInStatement":
n.Identifier.assert(stmt.left);
t.assertIdentifier(stmt.left);
head = loc();
after = loc();
@ -586,7 +586,7 @@ Emitter.prototype.explodeStatement = function(path, labelId) {
for (var i = cases.length - 1; i >= 0; --i) {
var c = cases[i];
n.SwitchCase.assert(c);
t.assertSwitchCase(c);
if (c.test) {
condition = t.conditionalExpression(
@ -706,7 +706,7 @@ Emitter.prototype.explodeStatement = function(path, labelId) {
var catchScope = bodyPath.scope;
var catchParamName = handler.param.name;
n.CatchClause.assert(catchScope.node);
t.assertCatchClause(catchScope.node);
assert.strictEqual(catchScope.lookup(catchParamName), catchScope);
types.visit(bodyPath, {
@ -770,11 +770,11 @@ Emitter.prototype.emitAbruptCompletion = function(record) {
var abruptArgs = [t.literal(record.type)];
if (record.type === "break" || record.type === "continue") {
n.Literal.assert(record.target);
t.assertLiteral(record.target);
abruptArgs[1] = record.target;
} else if (record.type === "return" || record.type === "throw") {
if (record.value) {
n.Expression.assert(record.value);
t.assertExpression(record.value);
abruptArgs[1] = record.value;
}
}
@ -797,7 +797,7 @@ function isValidCompletion(record) {
}
if (type === "break" || type === "continue") {
return !_.has(record, "value") && t.isLiteral(record.target);;
return !_.has(record, "value") && t.isLiteral(record.target);
}
if (type === "return" || type === "throw") {
@ -807,7 +807,6 @@ function isValidCompletion(record) {
return false;
}
// Not all offsets into emitter.listing are potential jump targets. For
// example, execution typically falls into the beginning of a try block
// without jumping directly there. This method returns the current offset
@ -833,7 +832,7 @@ Emitter.prototype.getUnmarkedCurrentLoc = function() {
// be costly and verbose to set context.prev before every statement.
Emitter.prototype.updateContextPrevLoc = function(loc) {
if (loc) {
n.Literal.assert(loc);
t.assertLiteral(loc);
if (loc.value === -1) {
// If an uninitialized location literal was passed in, set its value
@ -859,7 +858,7 @@ Emitter.prototype.explodeExpression = function(path, ignoreResult) {
var expr = path.value;
if (expr) {
n.Expression.assert(expr);
t.assertExpression(expr);
} else {
return expr;
}
@ -868,7 +867,7 @@ Emitter.prototype.explodeExpression = function(path, ignoreResult) {
var result, after; // Used optionally by several cases below.
function finish(expr) {
n.Expression.assert(expr);
t.assertExpression(expr);
if (ignoreResult) {
self.emit(expr);
} else {

View File

@ -13,20 +13,18 @@ var types = require("ast-types");
var t = require("../../../types");
var _ = require("lodash");
var n = types.namedTypes;
// 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) {
assert.ok(funPath instanceof types.NodePath);
n.Function.assert(funPath.value);
t.assertFunction(funPath.value);
var vars = {};
function varDeclToExpr(vdec, includeIdentifiers) {
n.VariableDeclaration.assert(vdec);
t.assertVariableDeclaration(vdec);
var exprs = [];
vdec.declarations.forEach(function(dec) {

View File

@ -17,11 +17,10 @@ exports.LoopEntry = LoopEntry;
exports.TryEntry = TryEntry;
var assert = require("assert");
var types = require("ast-types");
var util = require("util");
var t = require("../../../types");
var inherits = util.inherits;
var n = types.namedTypes;
function Entry() {
assert.ok(this instanceof Entry);
@ -30,7 +29,7 @@ function Entry() {
function FunctionEntry(returnLoc) {
Entry.call(this);
n.Literal.assert(returnLoc);
t.assertLiteral(returnLoc);
this.returnLoc = returnLoc;
}
@ -40,11 +39,11 @@ inherits(FunctionEntry, Entry);
function LoopEntry(breakLoc, continueLoc, label) {
Entry.call(this);
n.Literal.assert(breakLoc);
n.Literal.assert(continueLoc);
t.assertLiteral(breakLoc);
t.assertLiteral(continueLoc);
if (label) {
n.Identifier.assert(label);
t.assertIdentifier(label);
} else {
label = null;
}
@ -59,7 +58,7 @@ inherits(LoopEntry, Entry);
function SwitchEntry(breakLoc) {
Entry.call(this);
n.Literal.assert(breakLoc);
t.assertLiteral(breakLoc);
this.breakLoc = breakLoc;
}
@ -69,7 +68,7 @@ inherits(SwitchEntry, Entry);
function TryEntry(firstLoc, catchEntry, finallyEntry) {
Entry.call(this);
n.Literal.assert(firstLoc);
t.assertLiteral(firstLoc);
if (catchEntry) {
assert.ok(catchEntry instanceof CatchEntry);
@ -96,8 +95,8 @@ inherits(TryEntry, Entry);
function CatchEntry(firstLoc, paramId) {
Entry.call(this);
n.Literal.assert(firstLoc);
n.Identifier.assert(paramId);
t.assertLiteral(firstLoc);
t.assertIdentifier(paramId);
this.firstLoc = firstLoc;
this.paramId = paramId;
@ -108,7 +107,7 @@ inherits(CatchEntry, Entry);
function FinallyEntry(firstLoc) {
Entry.call(this);
n.Literal.assert(firstLoc);
t.assertLiteral(firstLoc);
this.firstLoc = firstLoc;
}

View File

@ -19,7 +19,7 @@ var n = types.namedTypes;
function makePredicate(propertyName, knownTypes) {
function onlyChildren(node) {
n.Node.assert(node);
n.Node.check(node);
// Assume no side effects until we find out otherwise.
var result = false;
@ -44,7 +44,7 @@ function makePredicate(propertyName, knownTypes) {
}
function predicate(node) {
n.Node.assert(node);
n.Node.check(node);
var meta = m(node);
if (_.has(meta, propertyName)) return meta[propertyName];

View File

@ -14,8 +14,6 @@ var hoist = require("./hoist").hoist;
var types = require("ast-types");
var t = require("../../../types");
var n = types.namedTypes;
var runtimeAsyncMethod = runtimeProperty("async");
var runtimeWrapMethod = runtimeProperty("wrap");
var runtimeMarkMethod = runtimeProperty("mark");
@ -179,7 +177,7 @@ var visitor = types.PathVisitor.fromMethodsObject({
bodyPath.push(varDecl);
} else {
n.FunctionExpression.assert(node);
t.assertFunctionExpression(node);
return t.callExpression(runtimeMarkMethod, [node]);
}
}
@ -187,7 +185,7 @@ var visitor = types.PathVisitor.fromMethodsObject({
function shouldNotHoistAbove(stmtPath) {
var value = stmtPath.value;
n.Statement.assert(value);
t.assertStatement(value);
// If the first statement is a "use strict" declaration, make sure to
// insert hoisted declarations afterwards.