Skip to content

Commit 007b14e

Browse files
committed
Handle amd define with arrow function
1 parent dd47437 commit 007b14e

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/dependencies/AMDDefineDependencyParserPlugin.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ function isBoundFunctionExpression(expr) {
2323
return true;
2424
}
2525

26+
function isUnboundFunctionExpression(expr) {
27+
if(expr.type === "FunctionExpression") return true;
28+
if(expr.type === "ArrowFunctionExpression") return true;
29+
return false;
30+
}
31+
32+
function isCallable(expr) {
33+
if(isUnboundFunctionExpression(expr)) return true;
34+
if(isBoundFunctionExpression(expr)) return true;
35+
return false;
36+
}
37+
2638
class AMDDefineDependencyParserPlugin {
2739
constructor(options) {
2840
this.options = options;
@@ -38,7 +50,7 @@ class AMDDefineDependencyParserPlugin {
3850
let array, fn, obj, namedModule;
3951
switch(expr.arguments.length) {
4052
case 1:
41-
if(expr.arguments[0].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[0])) {
53+
if(isCallable(expr.arguments[0])) {
4254
// define(f() {...})
4355
fn = expr.arguments[0];
4456
} else if(expr.arguments[0].type === "ObjectExpression") {
@@ -54,7 +66,7 @@ class AMDDefineDependencyParserPlugin {
5466
if(expr.arguments[0].type === "Literal") {
5567
namedModule = expr.arguments[0].value;
5668
// define("...", ...)
57-
if(expr.arguments[1].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[1])) {
69+
if(isCallable(expr.arguments[1])) {
5870
// define("...", f() {...})
5971
fn = expr.arguments[1];
6072
} else if(expr.arguments[1].type === "ObjectExpression") {
@@ -67,7 +79,7 @@ class AMDDefineDependencyParserPlugin {
6779
}
6880
} else {
6981
array = expr.arguments[0];
70-
if(expr.arguments[1].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[1])) {
82+
if(isCallable(expr.arguments[1])) {
7183
// define([...], f() {})
7284
fn = expr.arguments[1];
7385
} else if(expr.arguments[1].type === "ObjectExpression") {
@@ -84,7 +96,7 @@ class AMDDefineDependencyParserPlugin {
8496
// define("...", [...], f() {...})
8597
namedModule = expr.arguments[0].value;
8698
array = expr.arguments[1];
87-
if(expr.arguments[2].type === "FunctionExpression" || isBoundFunctionExpression(expr.arguments[2])) {
99+
if(isCallable(expr.arguments[2])) {
88100
// define("...", [...], f() {})
89101
fn = expr.arguments[2];
90102
} else if(expr.arguments[2].type === "ObjectExpression") {
@@ -102,7 +114,7 @@ class AMDDefineDependencyParserPlugin {
102114
let fnParams = null;
103115
let fnParamsOffset = 0;
104116
if(fn) {
105-
if(fn.type === "FunctionExpression") fnParams = fn.params;
117+
if(isUnboundFunctionExpression(fn)) fnParams = fn.params;
106118
else if(isBoundFunctionExpression(fn)) {
107119
fnParams = fn.callee.object.params;
108120
fnParamsOffset = fn.arguments.length - 1;
@@ -134,7 +146,7 @@ class AMDDefineDependencyParserPlugin {
134146
});
135147
}
136148
let inTry;
137-
if(fn && fn.type === "FunctionExpression") {
149+
if(fn && isUnboundFunctionExpression(fn)) {
138150
inTry = parser.scope.inTry;
139151
parser.inScope(fnParams, () => {
140152
parser.scope.renames = fnRenames;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ it("should not fail #138", function(done) {
168168
}(function () { done() }));
169169
});
170170

171+
it("should parse a simplified commonjs wrapping", function(done) {
172+
define(function(require) {
173+
require("./a").should.be.eql("a");
174+
done();
175+
});
176+
});
177+
178+
it("should parse a simplified commonjs wrapping arrow function", function(done) {
179+
define((require) => {
180+
require("./a").should.be.eql("a");
181+
done();
182+
});
183+
});
184+
171185
it("should parse a bound function expression 1", function(done) {
172186
define(function(a, require, exports, module) {
173187
a.should.be.eql(123);

0 commit comments

Comments
 (0)