Package detail

babel-plugin-angularjs-annotate

schmod394.7kMIT0.10.0

Babel plugin to add angularjs dependency injection annotations

angular, angularjs, di, dependency

readme

babel-plugin-angularjs-annotate

Circle CI npm version

Fork of ng-annotate for Babel users, with a focus on speed and ES6 support.

Adds Angular 1.x DI annotations to ES5/ES6 code being processed by Babel, with support for explicit annotations (/* @ngInject */), and automatic (implicit) annotation of typical Angular code patterns.

Fully compatible with ES5, transpiled ES6, and raw ES6 sources. Offers significantly reduced build times for projects already using Babel, compared to the standalone ng-annotate tool.

This plugin currently supports matching and transforming all of the patterns currently recognized by ng-annotate (explicit and implicit), and passes the relevant portions of ng-annotate's test suite.

Installation

Use like any other Babel plugin.

Most users will want to run

$ npm install babel-plugin-angularjs-annotate --save-dev

and add the plugin to your .babelrc file:

{
  "presets": ["@babel/preset-env"],
  "plugins": ["angularjs-annotate"]
}

Options

explicitOnly

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').

To pass this option to the plugin, add it to your Babel configuration:

{
  "presets": ["@babel/preset-env"],
  "plugins": [["angularjs-annotate", { "explicitOnly" : true}]]
}

Usage

See ng-annotate's documentation and the test sources 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.

Try it out in your browser.

ES6 Annotations

This plugin can annotate some ES6 classes and arrow functions that are not supported by ng-annotate:

Implicit arrow function annotation

Arrow functions may be annotated anywhere that a "regular" function expression may be used.

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.

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.

angular.module("MyMod").controller("MyCtrl", ($scope, $timeout) => {});

Becomes:

angular.module("MyMod").controller("MyCtrl", ["$scope", "$timeout", ($scope, $timeout) => {}]);

Explicit arrow function annotation

Arrow functions may also be explicitly marked for annotation.

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

Becomes:

var x = /* @ngInject */ ($scope) => {};
x.$inject = ["$scope"]

Implicit Class Annotation

If a class is declared as an Angular service or factory in the same file as it is declared, it will be annotated automatically:

class svc {
    constructor(dep1){
        this.dep1 = dep1;
    }
}
angular.module('MyMod').service('MySvc', svc);

Becomes:

class svc {
    constructor(dep1){
        this.dep1 = dep1;
    }
}
svc.$inject = ['dep1'];
angular.module('MyMod').service('MySvc', svc);

Explicit Class Annotation

If a class is exported and used in another file/module, it must be explicitly marked for injection:

/* @ngInject */
class svc {
  constructor(dep1){
      this.dep1 = dep1;
  }
}

Prologue directives may also be used here:

class svc {
  constructor(dep1){
      "ngInject";
      this.dep1 = dep1;
  }
}

Object Method Shorthand

Object methods can be written with the new shorthand syntax:

let foo = {
  bar($http){
    'ngInject';
  }
};
$stateProvider.state('myState', {
  controller($scope) {}
});

Exports

Exported functions and classes may be annotated. Exported functions must have names:

/* @ngInject */
export default function svc(dep1){}

Notes & Philosophy

This project/experiment does not seek to replace ng-annotate. However, it does seek to provide similar functionality for Angular 1.x developers who are already using Babel and/or writing code in ES6.

Because of some of the limitations presented by Babel's transformation process, this project does not aim to achieve feature parity, or provide identical output to ng-annotate. Notably, Babel does not preserve formatting and indentations like ng-annotate does, and this project does not seek to replicate the features of ng-annotate that remove or transform existing annotations.

Initially, I had hoped to make very few modifications to the upstream sources, in the hopes of eventually merging babel support directly into ng-annotate. Unfortunately, Babylon appears to have diverged too far from Acorn to make that goal realistic. (I would love to be wrong here, and would welcome contributions that close the gap between the two projects!)

To run tests:

npm test

License

MIT, see LICENSE file.

This project is a fork of ng-annotate, which was written by Olov Lassus with the kind help by contributors. Follow @olov on Twitter for updates about ng-annotate.

changelog

babel-plugin-angularjs-annotate changelog

0.10.0 2018-12-13

  • Upgrade to Babel 7 (thanks @pioug!)

0.9.0 2018-05-20

  • New Feature: Support directive definition in variable (#36, Thanks @noppa!)
  • New Feature: Support @ngInject in a multiline comment (#32 & #37, Thanks @sbrunner and @kamilbrk!)

0.8.2 2017-09-26

  • bugfix: Fix issue w/ $inject hoisting and nested injected blocks (#29)

0.8.1 2017-09-26

  • bugfix: Annotate functions inside of functions w/ default arguments (#30)

0.8.0 2017-07-12

  • New Feature: Support exported anonymous functions (#17)
  • New Feature: Support exported anonymous classes (#24)
  • New Feature: Allow multiple asterisks, ie. (/** @ngInject **/) (#23)

v0.7.0 2017-01-09

  • New Feature: Support for ES6 Object Method shorthand (#16)

v0.6.0 2016-10-14

  • New Feature: print a warning if we ask to re-annotate an DI injection array if the new parameters do not match, and throw an exception if the number of parameters is wrong.

v0.5.3 2016-10-14

  • bugfix: fix error when re-annotating existing array

v0.5.2 2016-09-28

  • bugfix: fix crash on illegal component declaration (#11)

v0.5.0 2016-08-04

  • bugfix: follow references in component definition objects (#7)
  • bugfix: remove superfluous dependency on babel
  • New Feature: Add annotations to injectable template and templateUrl component properties
  • New Feature: Adds a very simple REPL (see the gh-pages branch).

v0.4.0 2016-07-25

  • New option: Disable implicit matching (only annotate functions marked up with /* @ngInject */ or 'ngInject')

v0.3.0 2016-07-25

  • Add support for ES6 arrow functions

v0.2.0 2016-06-21

  • Add support for ES6 class annotations
  • Add support for exported ES6 classes and functions
  • Run all tests against transpiled and non-transpiled code.

v0.1.0 2016-04-15

  • Initial release

ng-annotate changelog

v1.2.1 2016-01-10

  • bugfix case where $inject arrays were incorrectly hoisted
  • angular-dashboard-framework optional: match apply

v1.2.0 2015-12-24

  • bugfix $inject array positioning with TypeScript inheritance (__extends)

v1.1.1 2015-12-22

  • bugfix parsing in strict mode even if source is not

v1.1.0 2015-12-19

  • bugfix $inject arrays for function definitions not being hoisted
  • match angular.module("MyMod").component("foo", {controller: function ..})
  • match angular.module("MyMod").decorator("foo", function ..)
  • match $controllerProvider.register
  • match $uibModal.open from angular-ui (recently renamed from $modal.open)
  • ui-router declarations improvements

v1.0.2 2015-07-17

  • bugfix reference-following crash

v1.0.1 2015-06-25

  • don't include .gitignore in npm package

v1.0.0 2015-05-27

  • optional matchers: --list and --enable
  • angular-dashboard-framework optional
  • bugfix documentation of sourcemap API
  • improved incoming sourcemap support
  • match flux-angular myMod.store("MyCtrl", function ..)
  • bugfix duplicated fn.$inject arrays in some IIFE situations
  • emit LF/CRLF newlines depending on input newlines
  • minor newline fixes

v0.15.4 2015-01-29

  • improved Traceur compatibility ("ngInject" prologue => fn.$inject = [..] arrays)

v0.15.3 2015-01-28

  • bugfix "ngInject" directive prologue (removing and rebuilding)
  • bugfix extra newlines when rebuilding existing fn.$inject = [..] arrays

v0.15.2 2015-01-26

  • bugfix crash on ES6 input (but ng-annotate does not yet understand ES6)

v0.15.1 2015-01-15

  • bugfix release for compatibility with io.js

v0.15.0 2015-01-15

  • "ngInject" directive prologue (usage like "use strict")
  • / @ngNoInject /, ngNoInject(..) and "ngNoInject" for suppressing false positives
  • Acorn is now the default and only parser
  • removed the experimental --es6 option and made it the default

v0.14.1 2014-12-04

  • bugfix / @ngInject / not working as expected in case of other matches

v0.14.0 2014-11-27

  • support sourcemap combination and better map granularity

v0.13.0 2014-11-18

  • match $mdDialog.show, $mdToast.show and $mdBottomSheet.show
  • improved $provide matching (.decorator, .service, .factory and .provider)

v0.12.1 2014-11-13

  • bugfix crash when reference-following to an empty variable declarator

v0.12.0 2014-11-10

  • improved TypeScript compatibility due to improved matching through IIFE's
  • match $injector.invoke
  • $modal.open is no longer experimental
  • reference-following is no longer experimental

v0.11.0 2014-11-03

  • bugfix reference-following such as var Ctrl = function(dep1, dep2) {}

v0.10.3 2014-11-03

  • match properties {name: ..}, {"name": ..} and {'name': ..} alike

v0.10.2 2014-10-09

  • --es6 option for ES6 support via the Acorn parser (experimental)

v0.10.1 2014-09-19

  • support stateHelperProvider.setNestedState nested children

v0.10.0 2014-09-15

  • support stateHelperProvider.setNestedState
  • optional renaming of declarations and references (experimental)
  • further improved detection of existing fn.$inject = [..] arrays
  • improved insertion of $inject arrays in case of early return
  • improved angular module detection (reference-following)
  • restrict matching based on method context (directive, provider)

v0.9.11 2014-08-09

  • improved detection of existing fn.$inject = [..] arrays

v0.9.10 2014-08-07

  • reference-following (experimental)
  • ngInject(..) as an alternative to / @ngInject / ..
  • more flexible / @ngInject / placement (object literals)

v0.9.9 2014-08-02

  • --sourcemap option for generating inline source maps

v0.9.8 2014-07-28

  • match implicit config function: angular.module("MyMod", function(dep) {})
  • match through IIFE's

v0.9.7 2014-07-11

  • more capable / @ngInject / (support function expression assignment)

v0.9.6 2014-06-12

  • match myMod.invoke
  • more capable --regexp option (match any method callee, identifier or not)

v0.9.5 2014-05-23

  • added ability to read from stdin and write to file
  • bugfix name of generated fn.$inject = [..] arrays (was: fn.$injects)

v0.9.4 2014-05-19

  • stricter match: only match code inside of angular modules (except explicit)
  • ui-router declarations improvements
  • bugfix duplicated annotations arrays in case of redundant / @ngInject /
  • indent generated fn.$inject = [..] arrays nicely

v0.9.3 2014-05-16

  • / @ngInject / object literal support
  • bugfix ES5 strict mode oops
  • added more tools that support ng-annotate to README

v0.9.2 2014-05-15

  • match $modal.open from angular-ui/bootstrap (experimental)
  • --stats option for runtime statistics (experimental)

v0.9.1 2014-05-14

  • revert match .controller(name, ..) that was added in 0.9.0 because it triggered false positives

v0.9.0 2014-05-13

  • explicit annotations using / @ngInject /
  • --plugin option to load user plugins (experimental, 0.9.x may change API)
  • match $httpProvider.interceptors.push(function($scope) {})
  • match $httpProvider.responseInterceptors.push(function($scope) {})
  • match self and that as aliases to this for this.$get = function($scope){}
  • match .controller(name, ..) in addition to .controller("name", ..)
  • bugfix ui-router declarations
  • bugfix angular.module("MyMod").bootstrap(e, [], {}) disrupting chaining
  • even faster (~6% faster annotating angular.js)
  • add error array to API return object

v0.8.0 2014-05-09

  • ngRoute support: $routeProvider.when("path", { .. })
  • even faster (~11% faster annotating angular.js)

v0.7.3 2014-05-07

  • support obj.myMod.controller(..) in addition to myMod.controller(..)

v0.7.2 2014-05-01

  • ui-router declarations improvements

v0.7.1 2014-04-30

  • ui-router declarations improvements

v0.7.0 2014-04-30

  • ui-router declarations support

v0.6.0 2014-04-20

  • --single_quotes option to output '$scope' instead of "$scope"

v0.5.0 2014-04-11

  • tweaked output: ["foo", "bar", ..] instead of ["foo","bar", ..]

v0.4.0 2013-10-31

  • match angular.module("MyMod").animation(".class", function ..)

v0.3.3 2013-10-03

  • bugfix .provider("foo", function($scope) ..) annotation. fixes #2

v0.3.2 2013-09-30

  • bugfix angular.module("MyMod").constant("foo", "bar") disrupting chaining
  • match $provide.decorator (in addition to other $provide methods)

v0.3.1 2013-09-30

  • bugfix angular.module("MyMod").value("foo", "bar") disrupting chaining

v0.3.0 2013-09-30

  • ES5 build via defs
  • Grunt-support via grunt-ng-annotate

v0.2.0 2013-09-06

  • better matching

v0.1.2 2013-09-03

  • better README

v0.1.1 2013-09-03

  • cross-platform shell script wrapper