Support ngInject on export [default] function functionName() {...}

This is somewhat common, even after conversion to ES5, when ES6 modules
are still used (e.g. Webpack 2, rollup). So let's try to support it.
This commit is contained in:
Ran Benita
2017-04-28 19:21:12 +03:00
parent f855f1f79c
commit ac6cd06341
4 changed files with 23 additions and 1 deletions
+3
View File
@@ -17,6 +17,9 @@ This fork contains the following changes:
- Added a `acornOptions` option to the API, to allow overriding or passing
extra options to acorn.
- Added support for ngInject comments on `export [default] function functionName() {...}`
declarations.
- Published to npm under the name `ng-annotate-patched`.
All work is done on the `fork` branch. The `master` branch corresponds to
+6 -1
View File
@@ -795,6 +795,9 @@ function judgeInjectArraySuspect(node, ctx) {
onode = node.$parent;
declaratorName = node.id.name;
node = node.init; // var foo = ___;
} else if (is.someof(node.type, ["ExportDefaultDeclaration", "ExportNamedDeclaration"])) {
onode = node;
node = node.declaration;
} else {
onode = node;
}
@@ -1023,7 +1026,9 @@ function isFunctionExpressionWithArgs(node) {
return node.type === "FunctionExpression" && node.params.length >= 1;
}
function isFunctionDeclarationWithArgs(node) {
return node.type === "FunctionDeclaration" && node.params.length >= 1;
// For `export default function() {...}`, `id` is null, which means
// we cannot inject it. So ignore that.
return node.type === "FunctionDeclaration" && node.params.length >= 1 && node.id !== null;
}
function isGenericProviderName(node) {
return node.type === "Literal" && is.string(node.value);
+6
View File
@@ -1019,3 +1019,9 @@ myMod.service("a", MyCtrl);
import "foo";
export const bar = "";
/* @ngInject */
export default function exportDefaultFunction($scope) {}
/* @ngInject */
export function exportFunction($scope) {}
+8
View File
@@ -1062,3 +1062,11 @@ myMod.service("a", MyCtrl);
import "foo";
export const bar = "";
/* @ngInject */
export default function exportDefaultFunction($scope) {}
exportDefaultFunction.$inject = ["$scope"];
/* @ngInject */
export function exportFunction($scope) {}
exportFunction.$inject = ["$scope"];