Skip to content

Commit 5cabdf2

Browse files
authored
Fix AMD define detection for arrow functions (#44)
1 parent 0b39580 commit 5cabdf2

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ function isModuleIdentifier(obj) {
9090
return obj.type && obj.type === 'Identifier' && obj.name === 'module';
9191
}
9292

93+
function isFunctionLike(node) {
94+
if (!node) return false;
95+
96+
return node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
97+
}
98+
9399
// module.exports.foo
94100
function isModuleExportsAttach(node) {
95101
if (!node.object || !node.object.object || !node.object.property) return false;
@@ -139,7 +145,7 @@ module.exports.isNamedForm = function(node) {
139145
return args && args.length === 3 &&
140146
(args[0].type === 'Literal' || args[0].type === 'StringLiteral') &&
141147
args[1].type === 'ArrayExpression' &&
142-
args[2].type === 'FunctionExpression';
148+
isFunctionLike(args[2]);
143149
};
144150

145151
// define([deps], func)
@@ -150,7 +156,7 @@ module.exports.isDependencyForm = function(node) {
150156

151157
return args && args.length === 2 &&
152158
args[0].type === 'ArrayExpression' &&
153-
args[1].type === 'FunctionExpression';
159+
isFunctionLike(args[1]);
154160
};
155161

156162
// define(func(require))
@@ -162,7 +168,7 @@ module.exports.isFactoryForm = function(node) {
162168

163169
// Node should have a function whose first param is 'require'
164170
return args && args.length === 1 &&
165-
args[0].type === 'FunctionExpression' &&
171+
isFunctionLike(args[0]) &&
166172
firstParamNode && firstParamNode.type === 'Identifier' &&
167173
firstParamNode.name === 'require';
168174
};
@@ -183,7 +189,7 @@ module.exports.isREMForm = function(node) {
183189
const args = node.arguments;
184190
const params = args.length > 0 ? args[0].params : null;
185191

186-
if (!args || args.length === 0 || args[0].type !== 'FunctionExpression' || params.length !== 3) {
192+
if (!args || args.length === 0 || !isFunctionLike(args[0]) || params.length !== 3) {
187193
return false;
188194
}
189195

test/is-define-amd.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,15 @@ testSuite('detects a named form AMD define function call', () => {
2525
assert.not.ok(check('define();', types.isDefineAMD));
2626
});
2727

28+
testSuite('detects AMD define with arrow callback returning class', () => {
29+
assert.ok(check('define(["jquery"] , ($) => { class A {} return A;});', types.isDefineAMD));
30+
});
31+
32+
testSuite('detects AMD forms with arrow callbacks', () => {
33+
assert.ok(check('define("name", ["jquery"], ($) => $);', types.isDefineAMD));
34+
assert.ok(check('define(["jquery"], ($) => $);', types.isDefineAMD));
35+
assert.ok(check('define((require) => require("jquery"));', types.isDefineAMD));
36+
assert.ok(check('define((require, exports, module) => { exports.x = true; });', types.isDefineAMD));
37+
});
38+
2839
testSuite.run();

0 commit comments

Comments
 (0)