Skip to content

Commit 783e251

Browse files
committed
track free vars over IIFEs
webpack#138
1 parent 6929ee0 commit 783e251

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

lib/Parser.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -549,17 +549,40 @@ Parser.prototype.walkExpression = function walkExpression(expression) {
549549
this.walkExpressions(expression.arguments);
550550
break;
551551
case "CallExpression":
552-
var callee = this.evaluateExpression(expression.callee);
553-
if(callee.isIdentifier()) {
554-
var result = this.applyPluginsBailResult("call " + callee.identifier, expression);
555-
if(result === true)
556-
break;
557-
}
552+
if(expression.callee.type === "FunctionExpression" && expression.arguments) {
553+
// (function(...) { }(...))
554+
var args = expression.arguments.map(function(arg) {
555+
var result = this.evaluateExpression(arg);
556+
if(!result.isIdentifier()) result = undefined;
557+
if(!result) {
558+
this.walkExpression(arg);
559+
return;
560+
}
561+
return result.identifier;
562+
}, this);
563+
this.inScope(expression.callee.params.filter(function(identifier, idx) {
564+
return identifier.name !== args[idx];
565+
}), function() {
566+
if(expression.callee.body.type === "BlockStatement")
567+
this.walkStatement(expression.callee.body);
568+
else
569+
this.walkExpression(expression.callee.body);
570+
}.bind(this));
558571

559-
if(expression.callee)
560-
this.walkExpression(expression.callee);
561-
if(expression.arguments)
562-
this.walkExpressions(expression.arguments);
572+
} else {
573+
574+
var callee = this.evaluateExpression(expression.callee);
575+
if(callee.isIdentifier()) {
576+
var result = this.applyPluginsBailResult("call " + callee.identifier, expression);
577+
if(result === true)
578+
break;
579+
}
580+
581+
if(expression.callee)
582+
this.walkExpression(expression.callee);
583+
if(expression.arguments)
584+
this.walkExpressions(expression.arguments);
585+
}
563586
break;
564587
case "MemberExpression":
565588
var expr = expression;

test/cases/parsing/extract-amd/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,14 @@ it("should parse a bound function expression 4", function(done) {
178178
done();
179179
}.bind(null, 123));
180180
});
181+
182+
it("should not fail issue #138 second", function() {
183+
(function(define, global) { 'use strict';
184+
define(function (require) {
185+
(typeof require).should.be.eql("function");
186+
require("./a").should.be.eql("a");
187+
return "#138 2.";
188+
});
189+
})(typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }, this);
190+
module.exports.should.be.eql("#138 2.");
191+
});

0 commit comments

Comments
 (0)