Skip to content

Commit b5398d4

Browse files
authored
Merge pull request #36 from noppa/directive-definition-in-variable
Directive definition in a variable
2 parents 2b767cb + efe4d1e commit b5398d4

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

ng-annotate-main.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,30 @@ function matchDirectiveReturnObject(path) {
7373
// only matches inside directives
7474
// return { .. controller: function($scope, $timeout), ...}
7575

76-
return limit("directive",
77-
(t.isReturnStatement(node) && node.argument && t.isObjectExpression(node.argument) && matchProp("controller", (path.get && path.get("argument.properties") || node.argument.properties))) ||
78-
(t.isArrowFunctionExpression(node) && node.body && t.isObjectExpression(node.body) && matchProp("controller", (path.get && path.get("body.properties") || node.body.properties))));
76+
var returnPath;
77+
if (t.isReturnStatement(node) && node.argument) {
78+
if (t.isObjectExpression(node.argument)) {
79+
returnPath = matchProp("controller", (path.get && path.get("argument.properties") || node.argument.properties));
80+
} else if (path.get && t.isIdentifier(path.get("argument"))) {
81+
var binding = path.scope.getBinding(node.argument.name);
82+
var bound = binding && binding.path;
83+
if (bound && t.isVariableDeclarator(bound)) {
84+
var init = bound.get("init");
85+
if (init && t.isObjectExpression(init)) {
86+
returnPath = matchProp("controller", init.get("properties"));
87+
}
88+
}
89+
}
90+
}
91+
if (!returnPath) {
92+
returnPath =
93+
t.isArrowFunctionExpression(node)
94+
&& node.body
95+
&& t.isObjectExpression(node.body)
96+
&& matchProp("controller", (path.get && path.get("body.properties") || node.body.properties));
97+
}
98+
99+
return limit("directive", returnPath);
79100
}
80101

81102
function limit(name, path) {

tests/references.js

+33
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,39 @@ module.exports = {
209209
angular.module("test.feedback.pkg", [])
210210
.component("testFeedback", testFeedback);
211211
}
212+
},
213+
{
214+
name: "directive definition as a variable",
215+
implicit: true,
216+
input: function () {
217+
function testDirective() {
218+
var directiveDefinition = {
219+
controller: testFeedbackController
220+
};
221+
222+
return directiveDefinition;
223+
224+
function testFeedbackController(foo) {
225+
}
226+
}
227+
228+
angular.module("MyMod").directive("testDirective", testDirective);
229+
},
230+
expected: function () {
231+
function testDirective() {
232+
testFeedbackController.$inject = ["foo"];
233+
var directiveDefinition = {
234+
controller: testFeedbackController
235+
};
236+
237+
return directiveDefinition;
238+
239+
function testFeedbackController(foo) {
240+
}
241+
}
242+
243+
angular.module("MyMod").directive("testDirective", testDirective);
244+
}
212245
}
213246
]
214247
}

0 commit comments

Comments
 (0)