diff --git a/lib/callbacks/transform-esprima.js b/lib/callbacks/transform-esprima.js index 822d81d6..518351cf 100644 --- a/lib/callbacks/transform-esprima.js +++ b/lib/callbacks/transform-esprima.js @@ -41,11 +41,11 @@ if (typeof exports !== 'undefined') { var Syntax = esprima.Syntax; // ES6 forms that we don't transform yet - // ArrowFunctionExpression = Syntax.ArrowFunctionExpression, - // ClassBody = Syntax.ClassBody, - // ClassDeclaration = Syntax.ClassDeclaration, - // ClassExpression = Syntax.ClassExpression, - // MethodDefinition = Syntax.MethodDefinition, + // ArrowFunctionExpression = 'ArrowFunctionExpression', + // ClassBody = 'ClassBody', + // ClassDeclaration = 'ClassDeclaration', + // ClassExpression = 'ClassExpression', + // MethodDefinition = 'MethodDefinition', // ES5 node types that we don't use: // CatchClause: catch clause inside TryStatement @@ -93,20 +93,20 @@ if (typeof exports !== 'undefined') { } function _isFunction(node) { - return node.type === Syntax.FunctionDeclaration || node.type === Syntax.FunctionExpression; + return node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression'; } function isDot(node) { - return node.type === Syntax.MemberExpression && !node.computed; + return node.type === 'MemberExpression' && !node.computed; } function isIndex(node) { - return node.type === Syntax.MemberExpression && node.computed; + return node.type === 'MemberExpression' && node.computed; } function _identifier(name) { return { - type: Syntax.Identifier, + type: 'Identifier', name: name, }; } @@ -114,7 +114,7 @@ if (typeof exports !== 'undefined') { function _declarator(name, init) { return { _scope: init && init._scope, - type: Syntax.VariableDeclarator, + type: 'VariableDeclarator', id: _identifier(name), init: init, }; @@ -122,21 +122,21 @@ if (typeof exports !== 'undefined') { function _literal(val) { return { - type: Syntax.Literal, + type: 'Literal', value: val, }; } function _return(node) { return { - type: Syntax.ReturnStatement, + type: 'ReturnStatement', _scope: node._scope, argument: node }; } function _semicolon(node) { - var stmt = _node(node, Syntax.ExpressionStatement); + var stmt = _node(node, 'ExpressionStatement'); stmt.expression = node; return stmt; } @@ -148,14 +148,14 @@ if (typeof exports !== 'undefined') { // cosmetic stuff: template logic generates nested blocks. Flatten them. function _flatten(node) { - if (node.type == Syntax.BlockStatement || node.type == Syntax.Program) { + if (node.type == 'BlockStatement' || node.type == 'Program') { do { var found = false; var body = []; node.body.forEach(function(child) { - if (child._isFunctionReference || (child.type == Syntax.ExpressionStatement && (child.expression == null || child.expression._isFunction))) return; // eliminate empty statement and dummy function node; + if (child._isFunctionReference || (child.type == 'ExpressionStatement' && (child.expression == null || child.expression._isFunction))) return; // eliminate empty statement and dummy function node; node._async |= child._async; - if (child.type == Syntax.BlockStatement || child.type == Syntax.Program) { + if (child.type == 'BlockStatement' || child.type == 'Program') { body = body.concat(child.body); found = true; } else body.push(child); @@ -232,7 +232,7 @@ if (typeof exports !== 'undefined') { // parser the function and set the root var _root = esprima.parse("function _t(){" + str + "}").body[0].body.body; if (_root.length == 1) _root = _root[0]; - else _root = _node(_root[0], Syntax.BlockStatement, { + else _root = _node(_root[0], 'BlockStatement', { body: _root, }); // if template is an expression rather than a full statement, go one more step down @@ -247,7 +247,7 @@ if (typeof exports !== 'undefined') { var fn = null; function gen(node) { - if (node && typeof node === 'object' && node.type != Syntax.Program && node.type != Syntax.BlockStatement) node._pass = pass; + if (node && typeof node === 'object' && node.type != 'Program' && node.type != 'BlockStatement') node._pass = pass; if (_isFunction(node) && createScope) { _assert(fn == null); fn = node; @@ -262,9 +262,9 @@ if (typeof exports !== 'undefined') { return node; } node._scope = scope; - // if node is ident; statement (Syntax.ExpressionStatement) or ident expression, try to match with binding - var ident = node.type == Syntax.ExpressionStatement ? node.expression : node; - if (ident && ident.type == Syntax.Identifier && ident.name[0] === "$") { + // if node is ident; statement ('ExpressionStatement') or ident expression, try to match with binding + var ident = node.type == 'ExpressionStatement' ? node.expression : node; + if (ident && ident.type == 'Identifier' && ident.name[0] === "$") { return typeof bindings[ident.name] === 'string' ? _identifier(bindings[ident.name]) : bindings[ident.name]; } else { // recurse through sub nodes @@ -333,45 +333,45 @@ if (typeof exports !== 'undefined') { */ function _removeFast(node, options) { function _isMarker(node) { - return node.type === Syntax.Identifier && node.name === options.callback; + return node.type === 'Identifier' && node.name === options.callback; } function _isStar(node) { - return node.type === Syntax.CallExpression && _isMarker(node.callee) && node.arguments.length === 2; + return node.type === 'CallExpression' && _isMarker(node.callee) && node.arguments.length === 2; } // ~_ -> _ - if (node.type === Syntax.UnaryExpression && node.operator === '~' && _isMarker(node.argument)) { + if (node.type === 'UnaryExpression' && node.operator === '~' && _isMarker(node.argument)) { options.needsTransform = true; return node.argument; } // [_] -> _ (with multiple marker) - if (node.type === Syntax.ArrayExpression && node.elements.length === 1 && _isMarker(node.elements[0])) { + if (node.type === 'ArrayExpression' && node.elements.length === 1 && _isMarker(node.elements[0])) { options.needsTransform = true; node.elements[0]._returnArray = true; return node.elements[0]; } // _ >> x -> x - if (node.type === Syntax.BinaryExpression && node.operator === '>>' && _isMarker(node.left)) { + if (node.type === 'BinaryExpression' && node.operator === '>>' && _isMarker(node.left)) { options.needsTransform = true; return node.right; } // _ << x -> x - if (node.type === Syntax.BinaryExpression && node.operator === '<<' && _isMarker(node.left)) { + if (node.type === 'BinaryExpression' && node.operator === '<<' && _isMarker(node.left)) { options.needsTransform = true; return node.right; } // !_ -> false - if (node.type === Syntax.UnaryExpression && node.operator === '!' && _isMarker(node.argument)) { + if (node.type === 'UnaryExpression' && node.operator === '!' && _isMarker(node.argument)) { options.needsTransform = true; - node.type = Syntax.Literal; + node.type = 'Literal'; node.value = false; node.raw = "false"; delete node.argument; return node; } // void _ -> null - if (node.type === Syntax.UnaryExpression && node.operator === 'void' && _isMarker(node.argument)) { + if (node.type === 'UnaryExpression' && node.operator === 'void' && _isMarker(node.argument)) { options.needsTransform = true; - node.type = Syntax.Literal; + node.type = 'Literal'; node.value = null; node.raw = "null"; delete node.argument; @@ -412,26 +412,26 @@ if (typeof exports !== 'undefined') { function _doIt(node, parent) { switch (node.type) { - case Syntax.FunctionDeclaration: - case Syntax.FunctionExpression: + case 'FunctionDeclaration': + case 'FunctionExpression': // do not propagate into functions return node; - case Syntax.Identifier: + case 'Identifier': if (node.name == options.callback) { async = true; } else { // propagate only if async is still false _propagate(node, _doIt); } return node; - case Syntax.CallExpression: + case 'CallExpression': // special hack for coffeescript top level closure var fn = node.callee, args = node.arguments, ident; if (isDot(fn) && (ident = fn.property).name === "call" // - && (fn = fn.object).type === Syntax.FunctionExpression && fn.params.length === 0 // + && (fn = fn.object).type === 'FunctionExpression' && fn.params.length === 0 // && !fn.id && args.length === 1 // - && args[0].type === Syntax.ThisExpression) { + && args[0].type === 'ThisExpression') { _propagate(fn.body, _doIt); return node; } @@ -482,19 +482,19 @@ if (typeof exports !== 'undefined') { } var id = _genId(node), n, nn; - if (parent.type === Syntax.Identifier) return _sanitize(parent.name) + id; - if (parent.type === Syntax.AssignmentExpression) { + if (parent.type === 'Identifier') return _sanitize(parent.name) + id; + if (parent.type === 'AssignmentExpression') { n = parent.left; var s = ""; - while ((isDot(n) && (nn = n.property).type === Syntax.Identifier) || (isIndex(n) && (nn = n.property).type === Syntax.Literal)) { + while ((isDot(n) && (nn = n.property).type === 'Identifier') || (isIndex(n) && (nn = n.property).type === 'Literal')) { s = s ? (nn.name || nn.value) + "_" + s : (nn.name || nn.value); n = n.object; } - if (n.type === Syntax.Identifier) s = s ? n.name + "_" + s : n.value; + if (n.type === 'Identifier') s = s ? n.name + "_" + s : n.value; if (s) return _sanitize(s) + id; - } else if (parent.type == Syntax.Property) { + } else if (parent.type == 'Property') { n = parent.key; - if (n.type === Syntax.Identifier || n.type === Syntax.Literal) return _sanitize(n.name || n.value) + id; + if (n.type === 'Identifier' || n.type === 'Literal') return _sanitize(n.name || n.value) + id; } return id; } @@ -505,16 +505,16 @@ if (typeof exports !== 'undefined') { node._scope = scope; var async = scope.isAsync(); if (!async && !_isFunction(node)) { - if (node.type === Syntax.Identifier && node.name === options.callback && !parent._isStar) { + if (node.type === 'Identifier' && node.name === options.callback && !parent._isStar) { throw new Error(node.filename + ": Function contains async calls but does not have _ parameter: " + node.name + " at line " + (node.loc && node.loc.start.line)); } return _propagate(node, _doIt); } - if (node.type === Syntax.TryStatement) node._async = true; + if (node.type === 'TryStatement') node._async = true; switch (node.type) { - case Syntax.FunctionDeclaration: - case Syntax.FunctionExpression: + case 'FunctionDeclaration': + case 'FunctionExpression': var result = node; var cbIndex = node.params.reduce(function(index, param, i) { if (param.name != options.callback) return index; @@ -536,7 +536,7 @@ if (typeof exports !== 'undefined') { } } // if function is a statement, move it away - if (async && (parent.type === Syntax.Program || parent.type === Syntax.BlockStatement)) { + if (async && (parent.type === 'Program' || parent.type === 'BlockStatement')) { scope.functions.push(node); result = undefined; } @@ -551,20 +551,20 @@ if (typeof exports !== 'undefined') { if (cbIndex >= 0) bodyScope.functions.push(_literal("BEGIN_BODY")); // will be removed later node.body.body = bodyScope.functions.concat(node.body.body); if (bodyScope.hasThis && !node._inhibitThis) { - bodyScope.vars.push(_declarator(_safeName(options.precious, "__this"), _node(node, Syntax.ThisExpression))); + bodyScope.vars.push(_declarator(_safeName(options.precious, "__this"), _node(node, 'ThisExpression'))); } if (bodyScope.hasArguments && !node._inhibitArguments) { bodyScope.vars.push(_declarator(_safeName(options.precious, "__arguments"), _identifier("arguments"))); } if (bodyScope.vars.length > 0) { - node.body.body.splice(0, 0, _node(node, Syntax.VariableDeclaration, { + node.body.body.splice(0, 0, _node(node, 'VariableDeclaration', { kind: 'var', // will see later about preserving const, ... declarations: bodyScope.vars, })); } // do not set _async flag return result; - case Syntax.VariableDeclaration: + case 'VariableDeclaration': var declarations = node.declarations.map(function(child) { if (!scope.vars.some(function(elt) { return elt.id.name == child.id.name; @@ -576,33 +576,33 @@ if (typeof exports !== 'undefined') { $lhs: _identifier(child.id.name), $rhs: child.init }); - if (parent.type === Syntax.ForStatement) child = child.expression; + if (parent.type === 'ForStatement') child = child.expression; return child; }).filter(function(child) { return child != null; }); if (declarations.length == 0) { // leave variable if `for (var x in y)` - return parent.type === Syntax.ForInStatement // + return parent.type === 'ForInStatement' // ? node.declarations[node.declarations.length - 1].id : undefined; } var result; - if (parent.type == Syntax.BlockStatement || parent.type === Syntax.Program) { - result = _node(parent, Syntax.BlockStatement, { + if (parent.type == 'BlockStatement' || parent.type === 'Program') { + result = _node(parent, 'BlockStatement', { body: declarations, }); } else { - result = _node(parent, Syntax.SequenceExpression, { + result = _node(parent, 'SequenceExpression', { expressions: declarations, }); } result = _propagate(result, _doIt); parent._async |= result._async; return result; - case Syntax.ThisExpression: + case 'ThisExpression': scope.hasThis = true; return _identifier(_safeName(options.precious, "__this")); - case Syntax.Identifier: + case 'Identifier': if (node.name === "arguments") { scope.hasArguments = true; return _identifier(_safeName(options.precious, "__arguments")); @@ -610,23 +610,23 @@ if (typeof exports !== 'undefined') { node = _propagate(node, _doIt); node._async |= node.name === options.callback; if (node._async && !(parent.arguments) && // func(_) is ok - !(parent.type === Syntax.Property && node === parent.key) && // { _: 1 } is ok + !(parent.type === 'Property' && node === parent.key) && // { _: 1 } is ok !(isDot(parent) && node === parent.property)) throw new Error("invalid usage of '_'") parent._async |= node._async; return node; - case Syntax.NewExpression: + case 'NewExpression': var cbIndex = node.arguments.reduce(function(index, arg, i) { - if (arg.type !== Syntax.Identifier || arg.name !== options.callback) return index; + if (arg.type !== 'Identifier' || arg.name !== options.callback) return index; if (index < 0) return i; else throw new Error("duplicate _ argument"); }, -1); if (cbIndex >= 0) { - var constr = _node(node, Syntax.CallExpression, { + var constr = _node(node, 'CallExpression', { callee: _identifier(_safeName(options.precious, '__construct')), arguments: [node.callee, _literal(cbIndex)] }); - node = _node(node, Syntax.CallExpression, { + node = _node(node, 'CallExpression', { callee: constr, arguments: node.arguments }); @@ -634,16 +634,16 @@ if (typeof exports !== 'undefined') { node = _propagate(node, _doIt); parent._async |= node._async; return node; - case Syntax.CallExpression: + case 'CallExpression': _convertThen(node, options); _convertCoffeeScriptCalls(node, options); _convertApply(node, options); // fall through default: - if (node.type === Syntax.SwitchCase) { + if (node.type === 'SwitchCase') { // wrap consequent into a block, to reuse block logic in subsequent steps - if (node.consequent.length !== 1 || node.consequent[0].type !== Syntax.BlockStatement) { - node.consequent = [_node(node, Syntax.BlockStatement, { + if (node.consequent.length !== 1 || node.consequent[0].type !== 'BlockStatement') { + node.consequent = [_node(node, 'BlockStatement', { body: node.consequent, })]; } @@ -663,10 +663,10 @@ if (typeof exports !== 'undefined') { var fn = node.callee; var args = node.arguments; if (isDot(fn) && args.length === 2 // - && args[0].type === Syntax.Identifier && args[0].name === options.callback - && args[1].type === Syntax.Identifier && args[1].name === options.callback) { + && args[0].type === 'Identifier' && args[0].name === options.callback + && args[1].type === 'Identifier' && args[1].name === options.callback) { node.arguments = [fn.object, _literal(fn.property.name), args[1]]; - fn.type = Syntax.Identifier; + fn.type = 'Identifier'; fn.name = "__pthen"; } } @@ -676,7 +676,7 @@ if (typeof exports !== 'undefined') { // CoffeeScript compiler var fn = node.callee; var args = node.arguments; - if (fn.type === Syntax.FunctionExpression && fn.params.length === 0 && !fn.id && args.length == 0) { + if (fn.type === 'FunctionExpression' && fn.params.length === 0 && !fn.id && args.length == 0) { // (function() { ... })() // --> (function(_) { ... })(_) fn._noFuture = true; @@ -686,8 +686,8 @@ if (typeof exports !== 'undefined') { } else if (isDot(fn)) { var ident = fn.property; fn = fn.object; - if (fn.type === Syntax.FunctionExpression && fn.params.length === 0 && !fn.id && ident.type === Syntax.Identifier) { - if (ident.name === "call" && args.length === 1 && args[0].type === Syntax.ThisExpression) { + if (fn.type === 'FunctionExpression' && fn.params.length === 0 && !fn.id && ident.type === 'Identifier') { + if (ident.name === "call" && args.length === 1 && args[0].type === 'ThisExpression') { // (function() { ... }).call(this) // --> (function(_) { ... })(_) node.callee = fn; @@ -697,8 +697,8 @@ if (typeof exports !== 'undefined') { node.arguments = [_identifier(options.callback)]; node._scope.hasThis = true; fn._inhibitThis = true; - } else if (ident.name === "apply" && args.length === 2 && args[0].type === Syntax.ThisExpression // - && args[1].type === Syntax.Identifier && args[1].name === "arguments") { + } else if (ident.name === "apply" && args.length === 2 && args[0].type === 'ThisExpression' // + && args[1].type === 'Identifier' && args[1].name === "arguments") { // (function() { ... }).apply(this, arguments) // --> (function(_) { ... })(_) node.callee = fn; @@ -710,7 +710,7 @@ if (typeof exports !== 'undefined') { node._scope.hasArguments = true; fn._inhibitThis = true; fn._inhibitArguments = true; - } else if (ident.name === "call" && args.length === 1 && args[0].type === Syntax.Identifier && args[0].name === '_this') { + } else if (ident.name === "call" && args.length === 1 && args[0].type === 'Identifier' && args[0].name === '_this') { // (function() { ... }).call(_this) // --> (function(_) { ... }).call(_this, _) fn._noFuture = true; @@ -729,9 +729,9 @@ if (typeof exports !== 'undefined') { // Params may vary but so we only test body. if (node.body.body.length !== 1) return false; var n = node.body.body[0]; - if (n.type !== Syntax.ReturnStatement || !n.argument) return false; + if (n.type !== 'ReturnStatement' || !n.argument) return false; n = n.argument; - if (n.type !== Syntax.CallExpression) return false; + if (n.type !== 'CallExpression') return false; var args = n.arguments; var target = n.callee; if (args.length !== 2 || args[0].name !== '_this' || args[1].name !== 'arguments') return false; @@ -741,7 +741,7 @@ if (typeof exports !== 'undefined') { target = target.object; if (!isDot(target) || target.property.name !== 'prototype') return false; target = target.object; - if (target.type !== Syntax.Identifier) return false; + if (target.type !== 'Identifier') return false; // Got it. Params are useless so nuke them node.params = []; return true; @@ -768,21 +768,21 @@ if (typeof exports !== 'undefined') { for (var i = 0; i < node.body.body.length; i++) { var child = node.body. body[i]; - if (i === 0 && child.type === Syntax.VariableDeclaration) { + if (i === 0 && child.type === 'VariableDeclaration') { skip = 1; continue; } - if (child.type !== Syntax.IfStatement) return false; - if (child.test.type !== Syntax.BinaryExpression && child.test.operator != '==') return false; + if (child.type !== 'IfStatement') return false; + if (child.test.type !== 'BinaryExpression' && child.test.operator != '==') return false; var ident = child.test.left; - if (ident.type !== Syntax.Identifier) return false; - if (child.test.right.type !== Syntax.Literal || child.test.right.value !== null) return false; + if (ident.type !== 'Identifier') return false; + if (child.test.right.type !== 'Literal' || child.test.right.value !== null) return false; if (!child.consequent.body || child.consequent.body.length !== 1) return false; var assign = child.consequent.body[0]; - if (assign.type !== Syntax.ExpressionStatement) return false; + if (assign.type !== 'ExpressionStatement') return false; assign = assign.expression; - if (assign.type !== Syntax.AssignmentExpression) return false; - if (assign.left.type !== Syntax.Identifier) return false; + if (assign.type !== 'AssignmentExpression') return false; + if (assign.left.type !== 'Identifier') return false; if (assign.left.name !== ident.name) return false; // we got a candidate - let us find the param while (++paramI < node.params.length) { @@ -811,12 +811,12 @@ if (typeof exports !== 'undefined') { node.body.body = remain.splice(0, paramI); // ugly hack to insert args initializer node.body.body.splice(0, 0, _identifier("var " + args + " = Array.prototype.slice.call(arguments, 0);")); - node.body.body.push(_node(node, Syntax.ReturnStatement, { - argument: _node(node, Syntax.CallExpression, { - callee: _node(node, Syntax.MemberExpression, { - object: _node(node, Syntax.FunctionExpression, { + node.body.body.push(_node(node, 'ReturnStatement', { + argument: _node(node, 'CallExpression', { + callee: _node(node, 'MemberExpression', { + object: _node(node, 'FunctionExpression', { params: originalParams, - body: _node(node, Syntax.BlockStatement, { body: remain }), + body: _node(node, 'BlockStatement', { body: remain }), parenthesized: true, }), property: _identifier("apply"), @@ -837,8 +837,8 @@ if (typeof exports !== 'undefined') { var args = node.arguments; if (isDot(dot)) { var ident = dot.property; - if (ident.type === Syntax.Identifier && ident.name === "apply" && args.length === 2 // - && args[0].type === Syntax.ThisExpression && args[1].type === Syntax.Identifier && args[1].name === "arguments") { + if (ident.type === 'Identifier' && ident.name === "apply" && args.length === 2 // + && args[0].type === 'ThisExpression' && args[1].type === 'Identifier' && args[1].name === "arguments") { var f = dot.object; node.callee = _identifier('__apply'); node.arguments = [_identifier(options.callback), f, _identifier('__this'), _identifier('__arguments'), _literal(node._scope.cbIndex)]; @@ -853,14 +853,14 @@ if (typeof exports !== 'undefined') { function _setBreaks(node) { switch (node.type) { - case Syntax.IfStatement: + case 'IfStatement': node._breaks = node.consequent._breaks && node.alternate && node.alternate._breaks; break; - case Syntax.SwitchStatement: + case 'SwitchStatement': if (!node.hasDefault && node._async) { - node.cases.push(_node(node, Syntax.SwitchCase, { - consequent: [_node(node, Syntax.BlockStatement, { - body: [_node(node, Syntax.BreakStatement)], + node.cases.push(_node(node, 'SwitchCase', { + consequent: [_node(node, 'BlockStatement', { + body: [_node(node, 'BreakStatement')], })], })); } @@ -868,7 +868,7 @@ if (typeof exports !== 'undefined') { var stmts = node.cases[i]; if (node._async && stmts.consequent[0].body.length > 0 && !stmts._breaks) { if (i === node.cases.length - 1) { - stmts.consequent[0].body.push(_node(node, Syntax.BreakStatement)); + stmts.consequent[0].body.push(_node(node, 'BreakStatement')); stmts._breaks = true; } else { // we rewrite: @@ -897,22 +897,22 @@ if (typeof exports !== 'undefined') { } } break; - case Syntax.TryStatement: + case 'TryStatement': node._breaks = node.block._breaks && node.handlers.length && node.handlers[0].body._breaks; break; - case Syntax.BlockStatement: - case Syntax.Program: + case 'BlockStatement': + case 'Program': node.body.forEach(function(child) { node._breaks |= child._breaks; }); break; - case Syntax.SwitchCase: - if (node.consequent.length !== 1 || node.consequent[0].type !== Syntax.BlockStatement) throw new Error("internal error: SwitchCase not wrapped: " + node.consequent.length); + case 'SwitchCase': + if (node.consequent.length !== 1 || node.consequent[0].type !== 'BlockStatement') throw new Error("internal error: SwitchCase not wrapped: " + node.consequent.length); node._breaks |= node.consequent[0]._breaks; break; - case Syntax.ReturnStatement: - case Syntax.ThrowStatement: - case Syntax.BreakStatement: + case 'ReturnStatement': + case 'ThrowStatement': + case 'BreakStatement': node._breaks = true; break; } @@ -928,15 +928,15 @@ if (typeof exports !== 'undefined') { function _statementify(exp) { if (!exp) return exp; - var block = _node(exp, Syntax.BlockStatement, { + var block = _node(exp, 'BlockStatement', { body: [] }); function uncomma(node) { - if (node.type === Syntax.SequenceExpression) { + if (node.type === 'SequenceExpression') { node.expressions.forEach(uncomma); } else { - block.body.push(node.type == Syntax.ExpressionStatement ? node : _semicolon(node)); + block.body.push(node.type == 'ExpressionStatement' ? node : _semicolon(node)); } } uncomma(exp); @@ -945,9 +945,9 @@ if (typeof exports !== 'undefined') { } function _blockify(node) { - if (!node || node.type == Syntax.BlockStatement) return node; - if (node.type == Syntax.SequenceExpression) return _statementify(node); - var block = _node(node, Syntax.BlockStatement, { + if (!node || node.type == 'BlockStatement') return node; + if (node.type == 'SequenceExpression') return _statementify(node); + var block = _node(node, 'BlockStatement', { body: [node] }); block._async = node._async; @@ -1054,7 +1054,7 @@ if (typeof exports !== 'undefined') { function _doAsyncFor(node) { // extra pass to wrap async test and update - if (node.test && node.test._async && node.test.type !== Syntax.CallExpression) node.test = _flowsTemplates.CONDITION.generate(node, { + if (node.test && node.test._async && node.test.type !== 'CallExpression') node.test = _flowsTemplates.CONDITION.generate(node, { $name: "__$" + node._scope.name, $test: _doIt(node.test, node, true), }); @@ -1063,35 +1063,35 @@ if (typeof exports !== 'undefined') { $update: _statementify(node.update) }); } - if (node.type == Syntax.ForStatement && node._pass === "flows") _doAsyncFor(node); + if (node.type == 'ForStatement' && node._pass === "flows") _doAsyncFor(node); if (!scope || !scope.isAsync() || (!force && node._pass === "flows")) return _propagate(node, _doIt); switch (node.type) { - case Syntax.IfStatement: + case 'IfStatement': node.consequent = _blockify(node.consequent); node.alternate = _blockify(node.alternate); break; - case Syntax.SwitchStatement: + case 'SwitchStatement': return withTarget(node, null, false, function() { if (node._async) { var def = node.cases.filter(function(n) { return n.test == null })[0]; if (!def) { - def = _node(node, Syntax.SwitchCase, { - consequent: [_node(node, Syntax.BlockStatement, { + def = _node(node, 'SwitchCase', { + consequent: [_node(node, 'BlockStatement', { body: [], })], }); node.cases.push(def); } if (!def._breaks) { - def.consequent[0].body.push(_node(node, Syntax.BreakStatement)); + def.consequent[0].body.push(_node(node, 'BreakStatement')); } } return _propagate(node, _doIt); }); - case Syntax.WhileStatement: + case 'WhileStatement': node.body = _blockify(node.body); return withTarget(node, null, true, function() { if (node._async) { @@ -1102,7 +1102,7 @@ if (typeof exports !== 'undefined') { } return _propagate(node, _doIt); }); - case Syntax.DoWhileStatement: + case 'DoWhileStatement': node.body = _blockify(node.body); return withTarget(node, null, true, function() { if (node._async) { @@ -1114,7 +1114,7 @@ if (typeof exports !== 'undefined') { } return _propagate(node, _doIt); }); - case Syntax.ForStatement: + case 'ForStatement': node.test = node.test || _literal(1); node.body = _blockify(node.body); return withTarget(node, null, true, function() { @@ -1135,11 +1135,11 @@ if (typeof exports !== 'undefined') { } return _propagate(node, _doIt); }); - case Syntax.ForInStatement: + case 'ForInStatement': node.body = _blockify(node.body); return withTarget(node, null, true, function() { if (node._async) { - if (node.left.type != Syntax.Identifier) { + if (node.left.type != 'Identifier') { throw new Error("unsupported 'for ... in' syntax: type=" + node.left.type); } node = _flowsTemplates.FOR_IN.generate(node, { @@ -1152,7 +1152,7 @@ if (typeof exports !== 'undefined') { } return _propagate(node, _doIt); }); - case Syntax.TryStatement: + case 'TryStatement': if (node.block && node.handlers.length && node.finalizer) { node = _flowsTemplates.TRY.generate(node, { $try: node.block, @@ -1162,7 +1162,7 @@ if (typeof exports !== 'undefined') { }) } break; - case Syntax.LogicalExpression: + case 'LogicalExpression': if (node._async) { node = _flowsTemplates[node.operator === '&&' ? 'AND' : 'OR'].generate(node, { $name: "__$" + node._scope.name, @@ -1172,7 +1172,7 @@ if (typeof exports !== 'undefined') { }); } break; - case Syntax.ConditionalExpression: + case 'ConditionalExpression': if (node._async) { node = _flowsTemplates.HOOK.generate(node, { $name: "__$" + node._scope.name, @@ -1184,27 +1184,27 @@ if (typeof exports !== 'undefined') { } break; - case Syntax.SequenceExpression: + case 'SequenceExpression': if (node._async) { node = _flowsTemplates.COMMA.generate(node, { $name: "__$" + node._scope.name, - $body: _node(node, Syntax.BlockStatement, { + $body: _node(node, 'BlockStatement', { body: node.expressions.slice(0, node.expressions.length - 1).map(_semicolon), }), $result: node.expressions[node.expressions.length - 1] }); } break; - case Syntax.LabeledStatement: + case 'LabeledStatement': return withTarget(node, node.label.name, true, function() { return _propagate(node, _doIt); }); - case Syntax.BreakStatement: + case 'BreakStatement': var target = targets['break_' + (node.label ? node.label.name : '')]; if (!target) throw new Error("internal error: break target not set"); node._async = target._async; break; - case Syntax.ContinueStatement: + case 'ContinueStatement': var target = targets['continue_' + (node.label ? node.label.name : '')]; if (!target) throw new Error("internal error: continue target not set"); node._async = target._async; @@ -1225,8 +1225,8 @@ if (typeof exports !== 'undefined') { var id = _genId(node); var v = _declarator(id, exp); node[prop] = _identifier(id); - return _node(node, Syntax.BlockStatement, { - body: [_node(node, Syntax.VariableDeclaration, { + return _node(node, 'BlockStatement', { + body: [_node(node, 'VariableDeclaration', { kind: 'var', // see later declarations: [v], }), node] @@ -1237,27 +1237,27 @@ if (typeof exports !== 'undefined') { function _disassembleIt(node, parent, noResult) { if (!node._async) return _propagate(node, _scanIt); node = _propagate(node, _disassembleIt); - if (node.type === Syntax.CallExpression) { - if (node.callee.type === Syntax.Identifier && node.callee.name.indexOf('__wrap') == 0) { + if (node.type === 'CallExpression') { + if (node.callee.type === 'Identifier' && node.callee.name.indexOf('__wrap') == 0) { node._isWrapper = true; return node; } var args = node.arguments; if (args.some(function(arg) { - return (arg.type === Syntax.Identifier && arg.name === options.callback) || arg._isWrapper; + return (arg.type === 'Identifier' && arg.name === options.callback) || arg._isWrapper; })) { if (noResult) { node._scope.disassembly.push(_statementify(node)); return; } else { - if (parent.type == Syntax.Identifier && parent.name.indexOf('__') === 0) { + if (parent.type == 'Identifier' && parent.name.indexOf('__') === 0) { // don't generate another ID, use the parent one node._skipDisassembly = true; return node; } var id = _genId(node); var v = _declarator(id, node); - node = _node(node, Syntax.VariableDeclaration, { + node = _node(node, 'VariableDeclaration', { kind: 'var', // fix later declarations: [v] }); @@ -1273,38 +1273,38 @@ if (typeof exports !== 'undefined') { var scope = node._scope; if (!scope || !scope.isAsync() || !node._async) return _propagate(node, _scanIt); switch (node.type) { - case Syntax.IfStatement: + case 'IfStatement': node = _split(node, "test"); break; - case Syntax.SwitchStatement: + case 'SwitchStatement': node = _split(node, "discriminant"); break; - case Syntax.ForStatement: + case 'ForStatement': break; - case Syntax.ReturnStatement: + case 'ReturnStatement': node = _split(node, "argument"); break; - case Syntax.ThrowStatement: + case 'ThrowStatement': node = _split(node, "argument"); break; - case Syntax.VariableDeclaration: + case 'VariableDeclaration': _assert(node.declarations.length === 1); var decl = node.declarations[0]; scope.disassembly = []; decl.init = _disassembleIt(decl.init, decl); node._async = decl.init._skipDisassembly; scope.disassembly.push(node); - return _node(parent, Syntax.BlockStatement, { + return _node(parent, 'BlockStatement', { body: scope.disassembly, }); - case Syntax.ExpressionStatement: + case 'ExpressionStatement': scope.disassembly = []; node.expression = _disassembleIt(node.expression, node, true); if (node.expression) { node._async = false; scope.disassembly.push(node); } - return _node(parent, Syntax.BlockStatement, { + return _node(parent, 'BlockStatement', { body: scope.disassembly, }); } @@ -1468,16 +1468,16 @@ if (typeof exports !== 'undefined') { node = _flatten(node); if (!node._scope || !node._scope.isAsync() || node._pass === "cb") return _propagate(node, _scanIt); switch (node.type) { - case Syntax.Program: - case Syntax.BlockStatement: - if (node.type === Syntax.Program || parent.type === Syntax.FunctionExpression || parent.type === Syntax.FunctionDeclaration) { + case 'Program': + case 'BlockStatement': + if (node.type === 'Program' || parent.type === 'FunctionExpression' || parent.type === 'FunctionDeclaration') { if (parent._pass !== "cb") { // isolate the leading decls from the body because 'use strict' // do not allow hoisted functions inside try/catch var decls; for (var cut = 0; cut < node.body.length; cut++) { var child = node.body[cut]; - if (child.type === Syntax.Literal && child.value === "BEGIN_BODY") { + if (child.type === 'Literal' && child.value === "BEGIN_BODY") { decls = node.body.splice(0, cut); node.body.splice(0, 1); break; @@ -1491,13 +1491,13 @@ if (typeof exports !== 'undefined') { $fname: _literal(parent.id.name), $line: _literal(originalLine(options, node._scope.line)), $index: _literal(node._scope.cbIndex), - $decls: _node(node, Syntax.BlockStatement, { + $decls: _node(node, 'BlockStatement', { body: decls || [] }), $body: node }); } - //node.type = Syntax.Program; + //node.type = 'Program'; } // continue with block restructure for (var i = 0; i < node.body.length; i++) { @@ -1509,7 +1509,7 @@ if (typeof exports !== 'undefined') { } function _extractTail(parent, i) { - return _node(parent, Syntax.BlockStatement, { + return _node(parent, 'BlockStatement', { body: parent.body.splice(i + 1, parent.body.length - i - 1) }); } @@ -1518,20 +1518,20 @@ if (typeof exports !== 'undefined') { var node = parent.body[i]; if (node._pass === "cb") return _propagate(node, _scanIt); switch (node.type) { - case Syntax.ReturnStatement: + case 'ReturnStatement': _extractTail(parent, i); var template = node.argument ? _cbTemplates.RETURN : _cbTemplates.RETURN_UNDEFINED; node = template.generate(node, { $value: node.argument }); break; - case Syntax.ThrowStatement: + case 'ThrowStatement': _extractTail(parent, i); node = _cbTemplates.THROW.generate(node, { $exception: node.argument }); break; - case Syntax.BreakStatement: + case 'BreakStatement': if (!node._async) break; _extractTail(parent, i); if (node.label) { @@ -1542,7 +1542,7 @@ if (typeof exports !== 'undefined') { node = _cbTemplates.BREAK.generate(node, {}); } break; - case Syntax.ContinueStatement: + case 'ContinueStatement': if (!node._async) break; _extractTail(parent, i); if (node.label) { @@ -1554,7 +1554,7 @@ if (typeof exports !== 'undefined') { node = _cbTemplates.CONTINUE.generate(node, {}); } break; - case Syntax.TryStatement: + case 'TryStatement': var tail = _extractTail(parent, i); if (node.handlers.length) { node = _cbTemplates.CATCH.generate(node, { @@ -1577,18 +1577,18 @@ if (typeof exports !== 'undefined') { if (node._async) { var tail = _extractTail(parent, i); switch (node.type) { - case Syntax.IfStatement: + case 'IfStatement': node = _cbTemplates.IF.generate(node, { $name: "__$" + node._scope.name, $test: node.test, $then: node.consequent, - $else: node.alternate || _node(node, Syntax.BlockStatement, { + $else: node.alternate || _node(node, 'BlockStatement', { body: [] }), $tail: tail }); break; - case Syntax.SwitchStatement: + case 'SwitchStatement': node._pass = "cb"; // avoid infinite recursion node = _cbTemplates.SWITCH.generate(node, { $name: "__$" + node._scope.name, @@ -1596,7 +1596,7 @@ if (typeof exports !== 'undefined') { $tail: tail }); break; - case Syntax.LabeledStatement: + case 'LabeledStatement': var l = label; label = node.label.name; node = _cbTemplates.LABEL.generate(node, { @@ -1607,7 +1607,7 @@ if (typeof exports !== 'undefined') { node = _scanIt(node, parent); label = l; return node; - case Syntax.ForStatement: + case 'ForStatement': var v = _identifier(_genId(node)); var loop1 = _cbTemplates.LOOP1.generate(node, { $v: v, @@ -1635,17 +1635,17 @@ if (typeof exports !== 'undefined') { }); break; - case Syntax.VariableDeclaration: + case 'VariableDeclaration': _assert(node.declarations.length == 1); var decl = node.declarations[0]; - _assert(decl.type === Syntax.VariableDeclarator); + _assert(decl.type === 'VariableDeclarator'); var call = decl.init; decl.init = null; - _assert(call && call.type === Syntax.CallExpression); + _assert(call && call.type === 'CallExpression'); return _restructureCall(call, tail, decl.id.name); - case Syntax.ExpressionStatement: + case 'ExpressionStatement': var call = node.expression; - _assert(call.type === Syntax.CallExpression) + _assert(call.type === 'CallExpression') return _restructureCall(call, tail); default: throw new Error("internal error: bad node type: " + node.type + ": " + escodegen.generate(node)); @@ -1659,7 +1659,7 @@ if (typeof exports !== 'undefined') { function _cbIndex(args) { return args.reduce(function(index, arg, i) { - if ((arg.type == Syntax.Identifier && arg.name === options.callback) || arg._isWrapper) return i; + if ((arg.type == 'Identifier' && arg.name === options.callback) || arg._isWrapper) return i; else return index; }, -1); } @@ -1684,7 +1684,7 @@ if (typeof exports !== 'undefined') { $offset: _literal(Math.max(originalLine(options, identifier.loc && identifier.loc.start.line, col) - originalLine(options, node._scope.line), 0)), $col: _literal(originalCol(options, identifier.loc && identifier.loc.start.column, col)), $name: "__$" + node._scope.name, - $returnArray: _node(node, Syntax.Literal, { + $returnArray: _node(node, 'Literal', { value: !!returnArray, }), $result: _identifier(result), @@ -1692,7 +1692,7 @@ if (typeof exports !== 'undefined') { }); node = _propagate(node, _scanIt); - var stmt = _node(node, Syntax.ReturnStatement, { + var stmt = _node(node, 'ReturnStatement', { argument: node }); stmt._pass = "cb"; @@ -1727,7 +1727,7 @@ if (typeof exports !== 'undefined') { node._simplified = true; // eliminate extra braces on switch cases if (node.type === 'SwitchCase') { - if (node.consequent.length === 1 && node.consequent[0].type === Syntax.BlockStatement) // + if (node.consequent.length === 1 && node.consequent[0].type === 'BlockStatement') // node.consequent = node.consequent[0].body; } @@ -1755,7 +1755,7 @@ if (typeof exports !== 'undefined') { } return true; } - if (v1.type === Syntax.Identifier && v1.name[0] === "$" && typeof v2.value === "number") { + if (v1.type === 'Identifier' && v1.name[0] === "$" && typeof v2.value === "number") { //console.log("MATCHING2: " + v1.name + " with " + v2.value); result[v1.name] = v2.value; return true; @@ -1767,12 +1767,12 @@ if (typeof exports !== 'undefined') { } if (v1.type) { var exp; - if (v1.type == Syntax.BlockStatement && v1.body[0] && (exp = v1.body[0].expression) && typeof exp.name == "string" && exp.name[0] == '$') { + if (v1.type == 'BlockStatement' && v1.body[0] && (exp = v1.body[0].expression) && typeof exp.name == "string" && exp.name[0] == '$') { result[exp.name] = v2; return true; } if (v1.type != v2.type) return false; - if (v1.type == Syntax.Identifier && v1.name == '$') { + if (v1.type == 'Identifier' && v1.name == '$') { result[v1.name] = v2.name; return true; } @@ -1842,7 +1842,7 @@ if (typeof exports !== 'undefined') { doIt(o, insideBlock); }); if (!obj.type) return; - var isBlock = obj.type === Syntax.BlockStatement || obj.type === Syntax.SwitchCase; + var isBlock = obj.type === 'BlockStatement' || obj.type === 'SwitchCase'; Object.keys(obj).forEach(function(k) { var v = obj[k]; doIt(v, isBlock); @@ -1882,7 +1882,7 @@ if (typeof exports !== 'undefined') { range: true, }); node = node.body[0].body; - if (node.type !== Syntax.BlockStatement) throw new Error("source wrapper error: " + node.type); + if (node.type !== 'BlockStatement') throw new Error("source wrapper error: " + node.type); //console.log(JSON.stringify(node, null, ' ')); var strict = node.body[0] && node.body[0].expression && node.body[0].expression.value == "use strict"; strict && node.body.splice(0, 1); diff --git a/lib/fibers/transform-esprima.js b/lib/fibers/transform-esprima.js index 3fe5529a..60604367 100644 --- a/lib/fibers/transform-esprima.js +++ b/lib/fibers/transform-esprima.js @@ -47,7 +47,7 @@ function getLocals(fn, recurse) { } } var walk = Walker({ - 'Function': function(name, args, body) { + Function: function(name, args, body) { if (this.type !== 'FunctionExpression') { names[this.id.name] = this.id.name; } @@ -58,7 +58,7 @@ function getLocals(fn, recurse) { } } }, - 'VariableDeclaration': decl, + VariableDeclaration: decl, }); fn.body.body.map(walk); for (var ii = 0; ii < fn.params; ++ii) { @@ -128,7 +128,7 @@ function transform(source, options) { var lines = Object.create(null); var walk = Walker({ - 'Function': function(name, args, body) { + Function: function(name, args, body) { if (this.type !== 'FunctionExpression') { var idx = getCallback(args); (idx === -1 ? not : declared)[name] = getCallbackDefault(args, body) || idx; @@ -136,7 +136,7 @@ function transform(source, options) { } // Don't walk further }, - 'VariableDeclarator': function(name, initializer) { + VariableDeclarator: function(name, initializer) { if (!initializer) { return; } @@ -148,7 +148,7 @@ function transform(source, options) { lines[name] = this.loc.start.line; walk(initializer); }, - 'AssignmentExpression': function(left, right) { + AssignmentExpression: function(left, right) { var name = left.type === 'Identifier' && left.name; if (name) { if (right.type === 'FunctionExpression') { @@ -311,7 +311,7 @@ function transform(source, options) { } var walk = Walker({ - 'Function': function(name, args, body) { + Function: function(name, args, body) { // Open this function if (name === callback) { throw error(this.loc.start.line, 'Invalid usage of callback'); @@ -391,7 +391,7 @@ function transform(source, options) { async = oldAsync; finallies = oldFinallies; }, - 'CallExpression': function(expr, args) { + CallExpression: function(expr, args) { if (expr.type === 'Identifier' && expr.name === '_' && args.length === 2) { catchup(this.range[0]); buffer.add('fstreamline__.streamlinify('); @@ -522,7 +522,7 @@ function transform(source, options) { args.map(walk); } }, - 'Identifier': function(name) { + Identifier: function(name) { if (name === callback) { throw error(this.loc.start.line, 'Invalid usage of callback'); } else if (verboten[name]) { @@ -538,17 +538,17 @@ function transform(source, options) { catchup(this.range[1]); } }, - 'Property': function() { + Property: function() { // Dont't walk the property key, because that's an identifier and it will be clobbered, per // the below code walk(this.value); }, - 'MemberExpression': function() { + MemberExpression: function() { // See comment above for propery_init walk(this.object); if (this.computed) walk(this.property) }, - 'NewExpression': function(expr, args) { + NewExpression: function(expr, args) { var idx = getCallback(args); if (idx !== -1) { // assumes that this is a streamlined function! @@ -577,19 +577,19 @@ function transform(source, options) { args.map(walk); } }, - 'ReturnStatement': function(argument) { + ReturnStatement: function(argument) { argument && walk(argument); fixASI(this); }, - 'ThrowStatement': function(argument) { + ThrowStatement: function(argument) { argument && walk(argument); fixASI(this); }, - 'yield': function(argument) { + YieldStatement: function(argument) { argument && walk(argument); fixASI(this); }, - 'UnaryExpression': function(operator, argument) { + UnaryExpression: function(operator, argument) { if (operator === '!' || operator === 'void') { if (argument.type === 'Identifier' && argument.name === callback) { catchup(this.range[0]); @@ -602,16 +602,16 @@ function transform(source, options) { walk(argument); } }, - 'TryStatement': function(block, handlers, finalizer) { + TryStatement: function(block, handlers, finalizer) { walk(block); handlers.map(walk); finalizer && walk(finalizer); }, - 'ExpressionStatement': function(expression) { + ExpressionStatement: function(expression) { expression && walk(expression); fixASI(this); }, - 'BinaryExpression': function(operator, left, right) { + BinaryExpression: function(operator, left, right) { if (operator === '<<' || operator === '>>') { walkShift.call(this, left, right); } else { @@ -619,7 +619,7 @@ function transform(source, options) { walk(right); } }, - 'VariableDeclaration': function(declarations) { + VariableDeclaration: function(declarations) { declarations && declarations.map(walk); if (this.eligibleForASI) fixASI(this); },