Skip to content

Add failing test for directive returning when the return is implicit. #18

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
Jul 12, 2017
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
6 changes: 6 additions & 0 deletions babel-ng-annotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ module.exports = function() {
ArrowFunctionExpression: {
enter(path) {
ngInject.inspectFunction(path, ctx);
},
exit(path, state) {
if(!state.opts.explicitOnly){
let targets = matchDirectiveReturnObject(path);
addTargets(targets);
}
}
},
FunctionDeclaration: {
Expand Down
6 changes: 3 additions & 3 deletions ng-annotate-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ 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)));
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))));
}

function limit(name, path) {
Expand Down
10 changes: 10 additions & 0 deletions tests/simple-arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ module.exports = {
}
}
});
myMod.directive("foo", ($scope) => ({
controller: ($scope, $timeout) => {
bar;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, this is not an implicit return, because bar is inside of a block-body ({ }).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait. Nevermind. We're looking at two different things. I do indeed see the implicit return on the line above 🤦‍♂️

}
}));
},
expected: function(){
// directive return object
Expand All @@ -269,6 +274,11 @@ module.exports = {
}
}
}]);
myMod.directive("foo", ["$scope", ($scope) => ({
controller: ["$scope", "$timeout", ($scope, $timeout) => {
bar;
}]
})]);
}
},
{
Expand Down