Skip to content

Commit 0471832

Browse files
author
Andrew Schmadel
committed
assign names to exported default functions
fixes #17
1 parent 0b8b0bf commit 0471832

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

ng-annotate-main.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,14 @@ function judgeInjectArraySuspect(path, ctx) {
765765
addInjectArrayAfterPath(node.params, opath, declaratorName);
766766

767767
} else if (isFunctionDeclarationWithArgs(node)) {
768-
// /*@ngInject*/ function foo($scope) {}
769-
addInjectArrayBeforePath(node.params,path,node.id.name);
770-
768+
if (t.isExportDefaultDeclaration(path.parent) && !node.id) {
769+
// export default function(a) {}
770+
node.id = path.scope.generateUidIdentifier('ngInjectExport');
771+
path.parentPath.insertBefore(buildInjectExpression(node.params, node.id.name));
772+
} else {
773+
// /*@ngInject*/ function foo($scope) {}
774+
addInjectArrayBeforePath(node.params,path,node.id.name);
775+
}
771776
} else if (t.isExpressionStatement(node) && t.isAssignmentExpression(node.expression) &&
772777
isFunctionExpressionWithArgs(node.expression.right) && !path.get("expression.right").$seen) {
773778
// /*@ngInject*/ foo.bar[0] = function($scope) {}

tests/es6.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ module.exports = {
5959
export default function svc(dep1){}
6060
`
6161
},
62+
{
63+
name: "exported anonymous function",
64+
explicit: true,
65+
input: `
66+
export default function ($timeout) {
67+
'ngInject';
68+
return 'foo';
69+
}
70+
`,
71+
expected: `
72+
_ngInjectExport.$inject = ['$timeout'];
73+
export default function _ngInjectExport($timeout) {
74+
'ngInject';
75+
return 'foo';
76+
}
77+
`
78+
},
6279
{
6380
name: "annotated class",
6481
explicit: true,

0 commit comments

Comments
 (0)