• Stars
    star
    2,028
  • Rank 22,829 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add, remove and rebuild AngularJS dependency injection annotations

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.

Highly recommended: enable ng-strict-di

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

More Repositories

1

jsshaper

an extensible framework for JavaScript syntax tree shaping
JavaScript
192
star
2

defs

Static scope analysis and transpilation of ES6 block scoped const and let variables to ES3 vars
JavaScript
116
star
3

jekyll-references

Jekyll Markdown references plugin
Ruby
43
star
4

ast-traverse

simple but flexible AST traversal with pre and post visitors
JavaScript
38
star
5

stringmap

Fast and robust stringmap for JavaScript
JavaScript
35
star
6

dart-range

Dart
11
star
7

node-harmony-wrapper

A node --harmony shell wrapper
Shell
10
star
8

tryor

return fn() or default value (in case of exception)
JavaScript
8
star
9

dart-scanner

Dart scanner implemented in Dart, ported from the dartc Java implementation
8
star
10

alter

Alters a string by replacing multiple range fragments in one fast pass
JavaScript
7
star
11

simple-is

Maximally minimal type-testing library for JavaScript
JavaScript
6
star
12

simple-fmt

Maximally minimal string formatting library for JavaScript
JavaScript
6
star
13

ordered-ast-traverse

Simple but flexible lexically ordered AST traversal with pre and post visitors
JavaScript
6
star
14

stringset

Fast and robust stringset for JavaScript
JavaScript
6
star
15

breakable

Break out of functions, recursive or not, in a more composable way than by using exceptions explicitly. Non-local return.
JavaScript
6
star
16

ordered-esprima-props

Lexical ordering of property names per Esprima AST type
JavaScript
3
star
17

fdelta

command-line tools for creating and applying delta files in Fossil delta format
C
2
star
18

vectorspace

Vectors are fun. Written for no particular reason other than trying out Ruby. Is likely pretty redundant, overall. Written on Ruby 1.9.1-preview 1 but should be mostly Ruby 1.8.x compatible. Again, vectors are fun.
Ruby
2
star
19

hookhttp

hook node.js require() via HTTP experiment
JavaScript
1
star
20

strto

strto is a strict string-to-number conversion library
JavaScript
1
star
21

jsoncomma

JSON with trailing commas for happy diffs
JavaScript
1
star