Skip to content

Support for arrow function #240

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

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions ng-annotate-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ function followReference(node) {
// {type: "VariableDeclarator", id: {type: "Identifier", name: "foo"}, init: ..}
return parent;
} else if (kind === "fun") {
assert(ptype === "FunctionDeclaration" || ptype === "FunctionExpression")
assert(ptype === "FunctionDeclaration" || ptype === "FunctionExpression" || ptype === "ArrowFunctionExpression")
// FunctionDeclaration is the common case, i.e.
// function foo(a, b) {}

Expand Down Expand Up @@ -958,11 +958,10 @@ function judgeInjectArraySuspect(node, ctx) {
}

function jumpOverIife(node) {
let outerfn;
if (!(node.type === "CallExpression" && (outerfn = node.callee).type === "FunctionExpression")) {
const outerfn = node.callee
if (!(node.type === "CallExpression" && (outerfn.type === "FunctionExpression" || outerfn.type === "ArrowFunctionExpression"))) {
return node;
}

const outerbody = outerfn.body.body;
for (let i = 0; i < outerbody.length; i++) {
const statement = outerbody[i];
Expand All @@ -988,9 +987,10 @@ function isAnnotatedArray(node) {
return false;
}
const elements = node.elements;
const lastElement = last(elements);

// last should be a function expression
if (elements.length === 0 || last(elements).type !== "FunctionExpression") {
if (elements.length === 0 || (lastElement.type !== "FunctionExpression" && lastElement.type !== "ArrowFunctionExpression")) {
return false;
}

Expand All @@ -1005,7 +1005,7 @@ function isAnnotatedArray(node) {
return true;
}
function isFunctionExpressionWithArgs(node) {
return node.type === "FunctionExpression" && node.params.length >= 1;
return (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && node.params.length >= 1;
}
function isFunctionDeclarationWithArgs(node) {
return node.type === "FunctionDeclaration" && node.params.length >= 1;
Expand Down
9 changes: 7 additions & 2 deletions nginject.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
function inspectNode(node, ctx) {
if (node.type === "CallExpression") {
inspectCallExpression(node, ctx);
} else if (node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
} else if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration") {
inspectFunction(node, ctx);
}
}
Expand Down Expand Up @@ -81,6 +81,11 @@ function inspectFunction(node, ctx) {
function matchPrologueDirectives(prologueDirectives, node) {
const body = node.body.body;

// There is no body when not using curly brace, such as x => x
if (!body) {
return;
}

let found = null;
for (let i = 0; i < body.length; i++) {
if (body[i].type !== "ExpressionStatement") {
Expand Down Expand Up @@ -195,7 +200,7 @@ function nestedObjectValues(node, res) {

node.properties.forEach(function(prop) {
const v = prop.value;
if (is.someof(v.type, ["FunctionExpression", "ArrayExpression"])) {
if (is.someof(v.type, ["FunctionExpression", "ArrowFunctionExpression", "ArrayExpression"])) {
res.push(v);
} else if (v.type === "ObjectExpression") {
nestedObjectValues(v, res);
Expand Down
9 changes: 9 additions & 0 deletions tests/original.js
Original file line number Diff line number Diff line change
Expand Up @@ -1016,3 +1016,12 @@ var MyCtrl = (function() {
})();

myMod.service("a", MyCtrl);

var ArrowFnCtrl = ($scope) => {
"ngInject"
return $scope
}

var ArrowFnCtrl = /*@ngInject*/($scope) => { $scope }

var ArrowFnCtrl = /*@ngInject*/($filter) => $filter
10 changes: 10 additions & 0 deletions tests/with_annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,3 +1059,13 @@ var MyCtrl = (function() {
})();

myMod.service("a", MyCtrl);

var ArrowFnCtrl = ($scope) => {
"ngInject"
return $scope
}
ArrowFnCtrl.$inject = ["$scope"];

var ArrowFnCtrl = /*@ngInject*/["$scope", ($scope) => { $scope }]

var ArrowFnCtrl = /*@ngInject*/["$filter", ($filter) => $filter]