Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
fixed Sage/streamlinejs#302 - CS _this idiom
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Nov 4, 2015
1 parent f13f7b0 commit 14241cc
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,20 @@ function hoist(t, scope, nodes) {
return hoisted.concat(other);
}

function likeThis(t, node) {
return t.isThisExpression(node) || (t.isIdentifier(node) && node.name === '_this');
}

function unwrapIIFE(t, node) {
var callee = node.callee;
if (t.isFunctionExpression(callee) && callee.params.length === 0 && node.arguments.length === 0) {
// (function() { ... })() -> (await (async function() { ... })())
return callee;
} else if (t.isMemberExpression(callee) && t.isFunctionExpression(callee.object) && callee.object.params.length === 0) {
if (callee.property.name === 'call' && node.arguments.length === 1 && t.isThisExpression(node.arguments[0])) {
if (callee.property.name === 'call' && node.arguments.length === 1 && likeThis(t, node.arguments[0])) {
// (function() { ... }).call(this) -> (await (async function(_) { ... }).call(this, _)
return callee.object;
} else if (callee.property.name === 'apply' && node.arguments.length === 2 && t.isThisExpression(node.arguments[0]) && node.arguments[1].name === 'arguments') {
} else if (callee.property.name === 'apply' && node.arguments.length === 2 && likeThis(t, node.arguments[0]) && node.arguments[1].name === 'arguments') {
// (function() { ... }).apply(this, arguments) -> (await (async function() { ... }).apply(this, arguments))
return callee.object;
}
Expand Down

0 comments on commit 14241cc

Please sign in to comment.