Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ng-annotate

olov402.5kMIT1.2.2

add, remove and rebuild angularjs dependency injection annotations

angular, angularjs, di, dependency, injection, annotate, annotation, annotations, transformation

readme

DEPRECATION NOTICE - SO LONG AND THANKS FOR ALL THE BITS

ng-annotate is not maintained any longer.

Please check out its successor instead: babel-plugin-angularjs-annotate

Please don't open any issues or pull requests on ng-annotate. No new releaseses will be made.

Feel free to fork the repo and publish your modified version if you want to. More info about the deprecation can be found in Issue #245: The future of ng-annotate.

ng-annotate Build Status

ng-annotate adds and removes AngularJS dependency injection annotations.

Write your code without annotations and mark-up functions to be annotated with the "ngInject" directive prologue, just like you would "use strict". This must be at the beginning of your function.

$ cat source.js
angular.module("MyMod").controller("MyCtrl", function($scope, $timeout) {
    "ngInject";
    ...
});

Then run ng-annotate as a build-step to produce this intermediary, annotated, result (later sent to the minifier of choice):

$ ng-annotate -a source.js
angular.module("MyMod").controller("MyCtrl", ["$scope", "$timeout", function($scope, $timeout) {
    "ngInject";
    ...
}]);

Your minifier will most likely retain the "ngInject" prologues so use sed or a regexp in your build toolchain to get rid of those on the ng-annotate output. sed example: ng-annotate -a source.js | sed "s/[\"']ngInject[\"'];*//g". JavaScript regexp example: source.replace(/["']ngInject["'];*/g, "").

You can also use ng-annotate to rebuild or remove existing annotations. Rebuilding is useful if you like to check-in the annotated version of your source code. When refactoring, just change parameter names once and let ng-annotate rebuild the annotations. Removing is useful if you want to de-annotate an existing codebase that came with checked-in annotations

Installation and usage

npm install -g ng-annotate

Then run it as ng-annotate OPTIONS <file>. The errors (if any) will go to stderr, the transpiled output to stdout.

The simplest usage is ng-annotate -a infile.js > outfile.js. See OPTIONS.md for command-line documentation.

ng-annotate can be used as a library, see OPTIONS.md for its API.

Implicit matching of common code forms

ng-annotate uses static analysis to detect common AngularJS code patterns. When this works it means that you do not need to mark-up functions with "ngInject". For a lot of code bases this works very well (use ng-strict-di to simplify debugging when it doesn't) but for others it is less reliable and you may prefer to use "ngInject" instead. For more information about implicit matching see IMPLICIT.md.

Explicit annotations with ngInject

The recommended function foo($scope) { "ngInject"; ... } can be exchanged for /*@ngInject*/ function foo($scope) { ... } or ngInject(function foo($scope) { ... }). If you use the latter form then then add function ngInject(v) { return v } somewhere in your codebase or process away the ngInject function call in your build step.

Suppressing false positives with ngNoInject

The /*@ngInject*/, ngInject(..) and "ngInject" siblings have three cousins that are used for the opposite purpose, suppressing an annotation that ng-annotate added incorrectly (a "false positive"). They are called /*@ngNoInject*/, ngNoInject(..) and "ngNoInject" and do exactly what you think they do.

ES6 and TypeScript support

ng-annotate supports ES5 as input so run it with the output from Babel, Traceur, TypeScript (tsc) and the likes. Use "ngInject" on functions you want annotated. Your transpiler should preserve directive prologues, if not please file a bug on it.

<div ng-app="myApp" ng-strict-di>

Do that in your ng-annotate processed (but not minified) builds and AngularJS will let you know if there are any missing dependency injection annotations. ng-strict-di is available in AngularJS 1.3 or later.

Tools support

Changes

See CHANGES.md.

Build and test

ng-annotate is written in ES6 constlet style and uses defs.js to transpile to ES5. See BUILD.md for build and test instructions.

Issues and contributions

Please provide issues in the form of input, expected output, actual output. Include the version of ng-annotate and node that you are using. With pull requests, please include changes to the tests as well (tests/original.js, tests/with_annotations.js).

License

MIT, see LICENSE file.

ng-annotate is written by Olov Lassus with the kind help by contributors. Follow @olov on Twitter for updates about ng-annotate.

changelog

v1.2.2 2017-06-01

  • so long and thanks for all the bits

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