Skip to content

Directive definition in a variable #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions ng-annotate-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,30 @@ function matchDirectiveReturnObject(path) {
// only matches inside directives
// return { .. controller: function($scope, $timeout), ...}

return limit("directive",
(t.isReturnStatement(node) && node.argument && t.isObjectExpression(node.argument) && matchProp("controller", (path.get && path.get("argument.properties") || node.argument.properties))) ||
(t.isArrowFunctionExpression(node) && node.body && t.isObjectExpression(node.body) && matchProp("controller", (path.get && path.get("body.properties") || node.body.properties))));
var returnPath;
if (t.isReturnStatement(node) && node.argument) {
if (t.isObjectExpression(node.argument)) {
returnPath = matchProp("controller", (path.get && path.get("argument.properties") || node.argument.properties));
} else if (path.get && t.isIdentifier(path.get("argument"))) {
var binding = path.scope.getBinding(node.argument.name);
var bound = binding && binding.path;
if (bound && t.isVariableDeclarator(bound)) {
var init = bound.get("init");
if (init && t.isObjectExpression(init)) {
returnPath = matchProp("controller", init.get("properties"));
}
}
}
}
if (!returnPath) {
returnPath =
t.isArrowFunctionExpression(node)
&& node.body
&& t.isObjectExpression(node.body)
&& matchProp("controller", (path.get && path.get("body.properties") || node.body.properties));
}

return limit("directive", returnPath);
}

function limit(name, path) {
Expand Down
33 changes: 33 additions & 0 deletions tests/references.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,39 @@ module.exports = {
angular.module("test.feedback.pkg", [])
.component("testFeedback", testFeedback);
}
},
{
name: "directive definition as a variable",
implicit: true,
input: function () {
function testDirective() {
var directiveDefinition = {
controller: testFeedbackController
};

return directiveDefinition;

function testFeedbackController(foo) {
}
}

angular.module("MyMod").directive("testDirective", testDirective);
},
expected: function () {
function testDirective() {
testFeedbackController.$inject = ["foo"];
var directiveDefinition = {
controller: testFeedbackController
};

return directiveDefinition;

function testFeedbackController(foo) {
}
}

angular.module("MyMod").directive("testDirective", testDirective);
}
}
]
}