Skip to content

Commit 7cdc9c3

Browse files
author
Andrew Schmadel
committed
support arrow functions
1 parent cccebc4 commit 7cdc9c3

File tree

9 files changed

+876
-10
lines changed

9 files changed

+876
-10
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# babel-plugin-angularjs-annotate changelog
22

3+
## v0.3.0 2016-07-25
4+
* Add support for ES6 arrow functions
5+
36
## v0.2.0 2016-06-21
47
* Add support for ES6 class annotations
58
* Add support for exported ES6 classes and functions

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,40 @@ See [ng-annotate](https://github.com/olov/ng-annotate)'s documentation and the [
3535

3636
### ES6 Annotations
3737

38-
This plugin can annotate some ES6 classes that are not supported by ng-annotate:
38+
This plugin can annotate some ES6 classes and arrow functions that are not supported by ng-annotate:
39+
40+
#### Implicit arrow function annotation
41+
42+
Arrow functions may be annotated anywhere that a "regular" function expression may be used.
43+
44+
**NOTE:** There are places where you _shouldn't_ use arrow functions in an Angular application. Inside of an arrow function, the value of `this` is inherited from the lexical scope enclosing the function. For this reason, arrow functions should not be used to declare Angular services or providers.
45+
46+
_If you choose to ignore this warning, we'll add the annotations to your services and providers anyway, but your application probably won't work. Future releases may treat this condition as an error._
47+
48+
```js
49+
angular.module("MyMod").controller("MyCtrl", ($scope, $timeout) => {});
50+
```
51+
52+
Becomes:
53+
54+
```js
55+
angular.module("MyMod").controller("MyCtrl", ["$scope", "$timeout", ($scope, $timeout) => {}]);
56+
```
57+
58+
#### Explicit arrow function annotation
59+
60+
Arrow functions may also be explicitly marked for annotation.
61+
62+
```js
63+
var x = /* @ngInject */ ($scope) => {};
64+
```
65+
66+
Becomes:
67+
68+
```js
69+
var x = /* @ngInject */ ($scope) => {};
70+
x.$inject = ["$scope"]
71+
```
3972

4073
#### Implicit Class Annotation
4174

babel-ng-annotate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ module.exports = function() {
9797
ngInject.inspectFunction(path, ctx);
9898
}
9999
},
100+
ArrowFunctionExpression: {
101+
enter(path) {
102+
ngInject.inspectFunction(path, ctx);
103+
}
104+
},
100105
FunctionDeclaration: {
101106
enter(path) {
102107
ngInject.inspectFunction(path, ctx);

ng-annotate-main.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ function followReference(path) {
639639
// {type: "VariableDeclarator", id: {type: "Identifier", name: "foo"}, init: ..}
640640
return bound;
641641
} else if (kind === "hoisted") {
642-
assert(t.isFunctionDeclaration(bound) || t.isFunctionExpression(bound));
642+
assert(t.isFunctionDeclaration(bound) || isFunctionExpressionOrArrow(bound));
643643
// FunctionDeclaration is the common case, i.e.
644644
// function foo(a, b) {}
645645

@@ -775,7 +775,7 @@ function jumpOverIife(path) {
775775
console.warn("Not a path");
776776
}
777777

778-
if (!(t.isCallExpression(node) && t.isFunctionExpression(node.callee))) {
778+
if (!(t.isCallExpression(node) && isFunctionExpressionOrArrow(node.callee))) {
779779
return path;
780780
}
781781

@@ -799,8 +799,12 @@ function addModuleContextIndependentSuspect(target, ctx) {
799799
ctx.suspects.push(target);
800800
}
801801

802+
function isFunctionExpressionOrArrow(node) {
803+
return t.isFunctionExpression(node) || t.isArrowFunctionExpression(node);
804+
}
805+
802806
function isFunctionExpressionWithArgs(node) {
803-
return t.isFunctionExpression(node) && node.params.length >= 1;
807+
return isFunctionExpressionOrArrow(node) && node.params.length >= 1;
804808
}
805809
function isFunctionDeclarationWithArgs(node) {
806810
return t.isFunctionDeclaration(node) && node.params.length >= 1;

nginject.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function inspectObjectExpression(path, ctx) {
171171
addSuspect(path, ctx, !annotateEverything);
172172
} else {
173173
path.get("properties")
174-
.filter(prop => t.isFunctionExpression(prop.node.value))
174+
.filter(prop => isFunctionExpressionOrArrow(prop.node.value))
175175
.forEach(prop => inspectComment(prop, ctx));
176176
}
177177

@@ -203,7 +203,7 @@ function matchPrologueDirectives(prologueDirectives, path) {
203203

204204
function inspectAssignment(path, ctx){
205205
const node = path.node;
206-
if(!t.isFunctionExpression(node.right)){
206+
if(!isFunctionExpressionOrArrow(node.right)){
207207
return;
208208
}
209209

@@ -221,7 +221,7 @@ function inspectAssignment(path, ctx){
221221

222222
function inspectDeclarator(path, ctx){
223223
const node = path.node;
224-
if(!t.isFunctionExpression(node.init)){
224+
if(!isFunctionExpressionOrArrow(node.init)){
225225
return;
226226
}
227227

@@ -352,7 +352,7 @@ function nestedObjectValues(path, res) {
352352

353353
path.get("properties").forEach(function(prop) {
354354
const v = prop.get("value");
355-
if (t.isFunctionExpression(v) || t.isArrayExpression(v)) {
355+
if (isFunctionExpressionOrArrow(v) || t.isArrayExpression(v)) {
356356
res.push(v);
357357
} else if (t.isObjectExpression(v)) {
358358
nestedObjectValues(v, res);
@@ -369,7 +369,7 @@ function isAnnotatedArray(node) {
369369
const elements = node.elements;
370370

371371
// last should be a function expression
372-
if (elements.length === 0 || !t.isFunctionExpression(last(elements))) {
372+
if (elements.length === 0 || !isFunctionExpressionOrArrow(last(elements))) {
373373
return false;
374374
}
375375

@@ -383,3 +383,7 @@ function isAnnotatedArray(node) {
383383

384384
return true;
385385
}
386+
387+
function isFunctionExpressionOrArrow(node) {
388+
return t.isFunctionExpression(node) || t.isArrowFunctionExpression(node);
389+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-angularjs-annotate",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Babel plugin to add angularjs dependency injection annotations",
55
"main": "babel-ng-annotate.js",
66
"repository": {

0 commit comments

Comments
 (0)