Skip to content

Commit dc6d9c9

Browse files
author
Andrew Schmadel
committed
option to disable implicit matching
closes #4
1 parent 762176c commit dc6d9c9

16 files changed

Lines changed: 137 additions & 30 deletions

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ and add the plugin to your `.babelrc` file:
2929
}
3030
```
3131

32+
## Options
33+
34+
### `explicitOnly`
35+
36+
By default, this plugin will attempt to add annotations to common AngularJS code patterns. This behavior can be disabled (requiring you to mark up functions with `/* @ngInject */` or `'ngInject'`).
37+
38+
To pass this option to the plugin, [add it to your Babel configuration](https://babeljs.io/docs/plugins/#plugin-options):
39+
40+
```json
41+
{
42+
"presets": ["es2015"],
43+
"plugins": [["angularjs-annotate", { "explicitOnly" : true}]]
44+
}
45+
```
46+
3247
## Usage
3348

3449
See [ng-annotate](https://github.com/olov/ng-annotate)'s documentation and the [test sources](tests/) for details about the patterns that can be automatically detected by ng-annotate and this plugin, as well as information about how to explicitly mark functions and classes for annotation.

babel-ng-annotate.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ module.exports = function() {
5757
enter(path) {
5858
ngInject.inspectAssignment(path, ctx);
5959
},
60-
exit(path) {
61-
let targets = matchProviderGet(path);
62-
addTargets(targets);
60+
exit(path, state) {
61+
if(!state.opts.explicitOnly){
62+
let targets = matchProviderGet(path);
63+
addTargets(targets);
64+
}
6365
}
6466
},
6567
VariableDeclarator: {
@@ -81,15 +83,19 @@ module.exports = function() {
8183
enter(path) {
8284
ngInject.inspectObjectExpression(path, ctx);
8385
},
84-
exit(path) {
85-
let targets = matchProviderGet(path);
86-
addTargets(targets);
86+
exit(path, state) {
87+
if(!state.opts.explicitOnly){
88+
let targets = matchProviderGet(path);
89+
addTargets(targets);
90+
}
8791
}
8892
},
8993
ReturnStatement: {
90-
exit(path) {
91-
let targets = matchDirectiveReturnObject(path);
92-
addTargets(targets);
94+
exit(path, state) {
95+
if(!state.opts.explicitOnly){
96+
let targets = matchDirectiveReturnObject(path);
97+
addTargets(targets);
98+
}
9399
}
94100
},
95101
FunctionExpression: {
@@ -111,9 +117,9 @@ module.exports = function() {
111117
enter(path) {
112118
ngInject.inspectCallExpression(path, ctx);
113119
},
114-
exit(path) {
115-
let targets = match(path, ctx);
116-
addTargets(targets);
120+
exit(path, state) {
121+
let targets = match(path, ctx, state.opts.explicitOnly);
122+
addTargets(targets);
117123
}
118124
},
119125
ExportDeclaration: {
@@ -123,6 +129,8 @@ module.exports = function() {
123129
},
124130
Program: {
125131
enter(path, file) {
132+
file.opts.explicitOnly = file.opts.explicitOnly || false;
133+
126134
ctx.suspects = [];
127135
ctx.blocked = [];
128136
ctx.fragments = [];

ng-annotate-main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const chainedUrlRouterProvider = 2;
1616
const chainedStateProvider = 3;
1717
const chainedRegular = 4;
1818

19-
function match(path, ctx, matchPlugins) {
19+
function match(path, ctx, explicitOnly) {
2020
const node = path.node;
2121
const isMethodCall = (
2222
t.isCallExpression(node) &&
@@ -28,6 +28,10 @@ function match(path, ctx, matchPlugins) {
2828
return false;
2929
}
3030

31+
if(explicitOnly){
32+
return false;
33+
}
34+
3135
// matchInjectorInvoke must happen before matchRegular
3236
// to prevent false positive ($injector.invoke() outside module)
3337
// matchProvide must happen before matchRegular

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.3.0",
3+
"version": "0.4.0",
44
"description": "Babel plugin to add angularjs dependency injection annotations",
55
"main": "babel-ng-annotate.js",
66
"repository": {

tests/es6.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
tests: [
55
{
66
name: "simple class",
7+
implicit: true,
78
input: function(){
89
class svc {
910
constructor(dep1){
@@ -24,6 +25,7 @@ module.exports = {
2425
},
2526
{
2627
name: "exported class",
28+
implicit: true,
2729
noES5: true, // this works with the ES2015 preset, but the transformations
2830
// make it difficult to test
2931
input: `
@@ -46,6 +48,7 @@ module.exports = {
4648
},
4749
{
4850
name: "exported annotated function",
51+
explicit: true,
4952
input: `
5053
/* @ngInject */
5154
export default function svc(dep1){}
@@ -58,6 +61,7 @@ module.exports = {
5861
},
5962
{
6063
name: "annotated class",
64+
explicit: true,
6165
input: function(){
6266
/* @ngInject */
6367
class svc {
@@ -81,6 +85,7 @@ module.exports = {
8185
{
8286
name: "exported annotated class",
8387
noES5: true,
88+
explicit: true,
8489
input: `
8590
/* @ngInject */
8691
export default class svc {
@@ -103,6 +108,7 @@ module.exports = {
103108
},
104109
{
105110
name: "annotated constructor",
111+
explicit: true,
106112
input: function(){
107113
class svc {
108114
/* @ngInject */
@@ -125,6 +131,7 @@ module.exports = {
125131
},
126132
{
127133
name: "constructor with prologue directive",
134+
explicit: true,
128135
input: function(){
129136
class svc {
130137
constructor(dep1){

tests/inside_module.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
{
55
name: "Injector invoke",
66
contextDependent: true,
7+
implicit: true,
78
input: function(){
89
$injector.invoke(function($compile) {
910
$compile(myElement)(scope);
@@ -17,6 +18,7 @@ module.exports = {
1718
},
1819
{
1920
name: "httpProvider",
21+
implicit: true,
2022
contextDependent: true,
2123
input: function(){
2224
$httpProvider.interceptors.push(function($scope) { a });
@@ -29,6 +31,7 @@ module.exports = {
2931
},
3032
{
3133
name: "$routeProvider",
34+
implicit: true,
3235
contextDependent: true,
3336
input: function(){
3437
$routeProvider.when("path", {

tests/issues.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
tests: [
44
{
55
name: "regression-test for https://github.com/olov/ng-annotate/issues/221",
6+
explicit: true,
67
input: function(){
78
var FooBar = (function (_super) {
89
__extends(FooBar, _super);
@@ -87,6 +88,7 @@ module.exports = {
8788
{
8889
name: "$stateProvider.decorator should not be injected",
8990
// not a module declaration short-form, see https://github.com/olov/ng-annotate/issues/82
91+
implicit: true,
9092
input: function(){
9193
$stateProvider.decorator('parent', function (state, parentFn) {
9294
doStuff();
@@ -118,6 +120,7 @@ module.exports = {
118120
},
119121
{
120122
name: "empty var declarator",
123+
implicit: true,
121124
input: function(){
122125
var MyCtrl12;
123126
angular.module("MyMod").controller('MyCtrl', MyCtrl12);
@@ -129,6 +132,7 @@ module.exports = {
129132
},
130133
{
131134
name: "issue 115",
135+
explicit: true,
132136
input: function(){
133137
module.exports = function() {
134138
"use strict";
@@ -158,6 +162,7 @@ module.exports = {
158162
},
159163
{
160164
name: "issue 135",
165+
explicit: true,
161166
input: function(){
162167
var MyCtrl = (function() {
163168
/*@ngInject*/

tests/modals.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
tests: [
44
{
55
name: "Modal Open",
6+
implicit: true,
67
contextDependent: true,
78
input: function(){
89
$modal.open({
@@ -32,6 +33,7 @@ module.exports = {
3233
{
3334
name: "uibModal Open",
3435
contextDependent: true,
36+
implicit: true,
3537
input: function(){
3638
$uibModal.open({
3739
templateUrl: "str",
@@ -59,6 +61,7 @@ module.exports = {
5961
},
6062
{
6163
name: "Material Design Modal",
64+
implicit: true,
6265
contextDependent: true,
6366
input: function(){
6467
$mdDialog.show({
@@ -88,6 +91,7 @@ module.exports = {
8891
{
8992
name: "Material Design Bottom Sheet",
9093
contextDependent: true,
94+
implicit: true,
9195
input: function(){
9296
$mdBottomSheet.show({
9397
templateUrl: "str",
@@ -116,6 +120,7 @@ module.exports = {
116120
{
117121
name: "Material Design Toast",
118122
contextDependent: true,
123+
implicit: true,
119124
input: function(){
120125
$mdToast.show({
121126
templateUrl: "str",

tests/ngInject-arrow.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ module.exports = {
257257
var MyDirective2 = ($stateProvider) => {
258258
$stateProvider.state('astate', {
259259
resolve: {
260+
/* @ngInject */
260261
yoyo: (ma) => {
261262
},
262263
}
@@ -268,6 +269,7 @@ module.exports = {
268269
var MyDirective2 = ($stateProvider) => {
269270
$stateProvider.state('astate', {
270271
resolve: {
272+
/* @ngInject */
271273
yoyo: ['ma', (ma) => {
272274
}],
273275
}
@@ -276,5 +278,5 @@ module.exports = {
276278
MyDirective2.$inject = ['$stateProvider'];
277279
}
278280
}
279-
]
281+
].map(t => { t.explicit=true; return t; })
280282
}

tests/ngInject.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ module.exports = {
366366
function MyDirective2($stateProvider) {
367367
$stateProvider.state('astate', {
368368
resolve: {
369+
/* @ngInject */
369370
yoyo: function(ma) {
370371
},
371372
}
@@ -379,12 +380,13 @@ module.exports = {
379380
function MyDirective2($stateProvider) {
380381
$stateProvider.state('astate', {
381382
resolve: {
383+
/* @ngInject */
382384
yoyo: ['ma', function(ma) {
383385
}],
384386
}
385387
});
386388
}
387389
}
388390
}
389-
]
391+
].map(t => { t.explicit=true; return t; })
390392
}

0 commit comments

Comments
 (0)