Skip to content

Commit 6929ee0

Browse files
committed
allow bound function expressions in define calls
1 parent 3e38890 commit 6929ee0

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/dependencies/AMDDefineDependencyParserPlugin.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@ var ConstDependency = require("./ConstDependency");
99
var AMDDefineDependency = require("./AMDDefineDependency");
1010
var ContextDependencyHelpers = require("./ContextDependencyHelpers");
1111

12+
function isBoundFunctionExpression(expr) {
13+
if(expr.type !== "CallExpression") return false;
14+
if(expr.callee.type !== "MemberExpression") return false;
15+
if(expr.callee.computed) return false;
16+
if(expr.callee.object.type !== "FunctionExpression") return false;
17+
if(expr.callee.property.type !== "Identifier") return false;
18+
if(expr.callee.property.name !== "bind") return false;
19+
return true;
20+
}
21+
1222
module.exports = AbstractPlugin.create({
1323
"call define": function(expr) {
1424
var array, fn, obj;
1525
switch(expr.arguments.length) {
1626
case 1:
17-
if(expr.arguments[0].type == "FunctionExpression") {
27+
if(expr.arguments[0].type == "FunctionExpression" || isBoundFunctionExpression(expr.arguments[0])) {
1828
// define(f() {...})
1929
fn = expr.arguments[0];
2030
} else if(expr.arguments[0].type === "ObjectExpression") {
@@ -29,7 +39,7 @@ module.exports = AbstractPlugin.create({
2939
case 2:
3040
if(expr.arguments[0].type === "Literal") {
3141
// define("...", ...)
32-
if(expr.arguments[1].type === "FunctionExpression") {
42+
if(expr.arguments[1].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[0])) {
3343
// define("...", f() {...})
3444
fn = expr.arguments[1];
3545
} else if(expr.arguments[1].type === "ObjectExpression") {
@@ -69,6 +79,19 @@ module.exports = AbstractPlugin.create({
6979
else
7080
this.walkExpression(fn.body);
7181
}.bind(this));
82+
} else if(fn && isBoundFunctionExpression(fn)) {
83+
var inTry = this.scope.inTry;
84+
this.inScope(fn.callee.object.params.filter(function(i) {
85+
return ["require", "module", "exports"].indexOf(i.name) < 0;
86+
}), function() {
87+
this.scope.inTry = inTry;
88+
if(fn.callee.object.body.type === "BlockStatement")
89+
this.walkStatement(fn.callee.object.body);
90+
else
91+
this.walkExpression(fn.callee.object.body);
92+
}.bind(this));
93+
if(fn.arguments)
94+
this.walkExpressions(fn.arguments);
7295
} else if(fn || obj) {
7396
this.walkExpression(fn || obj);
7497
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,17 @@ it("should not fail #138", function(done) {
148148
it("should parse a bound function expression 1", function(done) {
149149
define(function(a, require, exports, module) {
150150
a.should.be.eql(123);
151-
require.should.be.type("function");
151+
(typeof require).should.be.eql("function");
152+
require("./a").should.be.eql("a");
152153
done();
153154
}.bind(null, 123));
154155
});
155156

156157
it("should parse a bound function expression 2", function(done) {
157158
define("name", function(a, require, exports, module) {
158159
a.should.be.eql(123);
159-
require.should.be.type("function");
160+
(typeof require).should.be.eql("function");
161+
require("./a").should.be.eql("a");
160162
done();
161163
}.bind(null, 123));
162164
});

0 commit comments

Comments
 (0)