Skip to content

Commit 4817b11

Browse files
author
Andrew Schmadel
committed
support object methods
fixes #16
1 parent a10e481 commit 4817b11

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

babel-ng-annotate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ module.exports = function() {
113113
ngInject.inspectFunction(path, ctx);
114114
}
115115
},
116+
ObjectMethod: {
117+
enter(path) {
118+
ngInject.inspectFunction(path, ctx);
119+
}
120+
},
116121
CallExpression: {
117122
enter(path) {
118123
ngInject.inspectCallExpression(path, ctx);

ng-annotate-main.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,10 @@ function matchProp(name, props) {
433433

434434
if ((t.isIdentifier(prop.key) && prop.key.name === name) ||
435435
(t.isLiteral(prop.key) && prop.key.value === name)) {
436-
return (propOrPath.get && propOrPath.get("value")) || prop.value; // FunctionExpression or ArrayExpression
436+
if(t.isObjectMethod(prop)){
437+
return propOrPath;
438+
}
439+
return (propOrPath.get && propOrPath.get("value")) || prop.value; // FunctionExpression or ArrayExpression
437440
}
438441
}
439442
return null;
@@ -443,7 +446,10 @@ function matchResolve(props) {
443446
const resolveObject = matchProp("resolve", props);
444447
if (resolveObject && t.isObjectExpression(resolveObject)) {
445448
return resolveObject.get("properties").map(function(prop) {
446-
return prop.get("value");
449+
if(t.isObjectMethod(prop)){
450+
return prop;
451+
}
452+
return prop.get("value");
447453
});
448454
}
449455
return [];
@@ -557,6 +563,21 @@ function judgeSuspects(ctx) {
557563
return;
558564
}
559565

566+
if(t.isObjectMethod(path) && target.params.length){
567+
// Replace object method shorthand { foo(bar){} } with long-form { foo: function(bar){} } so we can annotate it
568+
const func = t.functionExpression(null, target.params, target.body, target.generator, target.async);
569+
func.returnType = target.returnType;
570+
571+
path.replaceWith(t.objectProperty(
572+
target.key,
573+
func,
574+
target.computed
575+
));
576+
577+
path = path.get("value");
578+
target = path.node;
579+
}
580+
560581
if (isFunctionExpressionWithArgs(target) && !t.isVariableDeclarator(path.parent)) {
561582
insertArray(ctx, path);
562583
} else if (isGenericProviderName(target)) {

tests/es6.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,36 @@ module.exports = {
151151
svc.$inject = ['dep1'];
152152
svc.foo = 'bar';
153153
}
154+
}, {
155+
name: "object method",
156+
explicit: true,
157+
input: function() {
158+
var foo = {
159+
bar(baz){
160+
'ngInject';
161+
}
162+
};
163+
},
164+
expected: function() {
165+
var foo = {
166+
bar: ['baz', function(baz) {
167+
'ngInject';
168+
}]
169+
}
170+
}
171+
}, {
172+
name: "implicit object method",
173+
explicit: false,
174+
input: function() {
175+
angular.component('myComponent', {
176+
controller(a){}
177+
});
178+
},
179+
expected: function() {
180+
angular.component('myComponent', {
181+
controller: ['a', function(a){}]
182+
});
183+
}
154184
}
155185
]
156186
};

tests/ui-router.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
simpleObj: function() { a },
1212
promiseObj: function($scope, $timeout) { b },
1313
translations: "translations",
14+
objMethod(a) { a }
1415
},
1516
params: {
1617
simple: function($scope) {},
@@ -33,6 +34,11 @@ module.exports = {
3334
templateProvider: function($scope) {},
3435
controller: function($scope) {},
3536
},
37+
viewc: {
38+
dontAlterMe(arg) {},
39+
templateProvider($scope) {},
40+
controller($scope) {}
41+
},
3642
dontAlterMe: null,
3743
},
3844
controller: function($scope, simpleObj, promiseObj, translations) { c },
@@ -54,6 +60,7 @@ module.exports = {
5460
simpleObj: function() { a },
5561
promiseObj: ["$scope", "$timeout", function($scope, $timeout) { b }],
5662
translations: "translations",
63+
objMethod: ["a", function(a) { a }]
5764
},
5865
params: {
5966
simple: ["$scope", function($scope) {}],
@@ -76,6 +83,11 @@ module.exports = {
7683
templateProvider: ["$scope", function($scope) {}],
7784
controller: ["$scope", function($scope) {}],
7885
},
86+
viewc: {
87+
dontAlterMe(arg) {},
88+
templateProvider: ["$scope", function($scope) {}],
89+
controller: ["$scope", function($scope) {}]
90+
},
7991
dontAlterMe: null,
8092
},
8193
controller: ["$scope", "simpleObj", "promiseObj", "translations", function($scope, simpleObj, promiseObj, translations) { c }],
@@ -233,4 +245,4 @@ module.exports = {
233245
}
234246
}
235247
]
236-
}
248+
}

0 commit comments

Comments
 (0)