Skip to content

Commit 39ffd0e

Browse files
author
Andrew Schmadel
committed
play nicely with default function arguments
1 parent 99c600c commit 39ffd0e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

ng-annotate-main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ function judgeInjectArraySuspect(path, ctx) {
771771
if (t.isExportDefaultDeclaration(path.parent) && !node.id) {
772772
// export default function(a) {}
773773
node.id = path.scope.generateUidIdentifier('ngInjectExport');
774+
path.parentPath.scope.crawl();
774775
path.parentPath.insertBefore(buildInjectExpression(node.params, node.id.name));
775776
} else {
776777
// /*@ngInject*/ function foo($scope) {}
@@ -780,6 +781,7 @@ function judgeInjectArraySuspect(path, ctx) {
780781
isFunctionExpressionWithArgs(node.expression.right) && !path.get("expression.right").$seen) {
781782
// /*@ngInject*/ foo.bar[0] = function($scope) {}
782783
let inject = buildInjectExpression(node.expression.right.params, t.cloneDeep(node.expression.left));
784+
path.parentPath.scope.crawl();
783785
path.insertAfter(inject);
784786

785787
} else if (path = followReference(path)) {
@@ -792,7 +794,7 @@ function judgeInjectArraySuspect(path, ctx) {
792794

793795
function buildInjectExpression(params, name){
794796
let left = t.isNode(name) ? name : t.identifier(name);
795-
let paramStrings = params.map(param => t.stringLiteral(param.name));
797+
let paramStrings = params.map(param => t.stringLiteral(getNamedParam(param)));
796798
let arr = t.arrayExpression(paramStrings); // ["$scope"]
797799
let member = t.memberExpression(left, t.identifier("$inject")); // foo.$inject =
798800
return t.expressionStatement(t.assignmentExpression("=", member , arr));
@@ -810,6 +812,7 @@ function judgeInjectArraySuspect(path, ctx) {
810812
}
811813
block.unshiftContainer("body", [expr]);
812814
} else {
815+
path.parentPath.scope.crawl();
813816
path.insertBefore(buildInjectExpression(params, name));
814817
}
815818
}
@@ -820,6 +823,7 @@ function judgeInjectArraySuspect(path, ctx) {
820823
trailingComments = path.node.trailingComments;
821824
path.node.trailingComments = [];
822825
}
826+
path.parentPath.scope.crawl();
823827
let newNode = path.insertAfter(buildInjectExpression(params, name));
824828
newNode.trailingComments = trailingComments;
825829
}
@@ -870,6 +874,12 @@ function isGenericProviderName(node) {
870874
return t.isLiteral(node) && is.string(node.value);
871875
}
872876

877+
function getNamedParam(p) {
878+
let param = p;
879+
if (t.isAssignmentPattern(p)) param = p.left;
880+
return param.name;
881+
}
882+
873883
function getConstructor(node){
874884
var body = node.body.body;
875885
for(var i=0; i< body.length; i++){

tests/issues.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ module.exports = {
199199
"ngInject"
200200
}])
201201
}
202+
},
203+
{
204+
name: "default outer function parameters",
205+
input: function(){
206+
var outside = function (arg = {}){
207+
var inside = function ($q) {
208+
'ngInject'
209+
};
210+
return inside;
211+
};
212+
}, expected: function() {
213+
var outside = function(arg = {}) {
214+
var inside = function ($q) {
215+
'ngInject';
216+
};
217+
inside.$inject = ['$q'];
218+
return inside;
219+
};
220+
},
221+
explicit: true
202222
}
203223
]
204224
}

0 commit comments

Comments
 (0)