From 57f4c3bc5c89d978e3fa724399ba12595077c900 Mon Sep 17 00:00:00 2001 From: Michael Duran Date: Wed, 16 Apr 2014 22:49:35 -0500 Subject: [PATCH] add new option for using single quotes arond dependency injection annotations --- .gitignore | 2 + ng-annotate-main.js | 11 +-- ng-annotate.js | 2 + run-tests.js | 3 + tests/with_annotations_single.js | 142 +++++++++++++++++++++++++++++++ 5 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 tests/with_annotations_single.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1fe1b00 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +node_modules/ diff --git a/ng-annotate-main.js b/ng-annotate-main.js index ba0177b..d31d204 100644 --- a/ng-annotate-main.js +++ b/ng-annotate-main.js @@ -84,16 +84,16 @@ function matchProp(name, props) { return null; } -function stringify(arr) { +function stringify(arr, quot) { return "[" + arr.map(function(arg) { - return '"' + arg.name + '"'; + return quot + arg.name + quot; }).join(", ") + "]"; } -function insertArray(functionExpression, fragments) { +function insertArray(functionExpression, fragments, quot) { const range = functionExpression.range; - const args = stringify(functionExpression.params); + const args = stringify(functionExpression.params, quot); fragments.push({ start: range[0], end: range[0], @@ -151,6 +151,7 @@ module.exports = function ngAnnotate(src, options) { return {src: src}; } + const quot = options.single ? '\'' : '"'; const re = (options.regexp && new RegExp(options.regexp)); const ast = esprima(src, { range: true, @@ -169,7 +170,7 @@ module.exports = function ngAnnotate(src, options) { } else if (mode === "remove" && isAnnotatedArray(target)) { removeArray(target, fragments); } else if (is.someof(mode, ["add", "rebuild"]) && isFunctionWithArgs(target)) { - insertArray(target, fragments); + insertArray(target, fragments, quot); } }}); diff --git a/ng-annotate.js b/ng-annotate.js index 6f5bc75..f33bba1 100644 --- a/ng-annotate.js +++ b/ng-annotate.js @@ -19,6 +19,8 @@ const optimist = require("optimist") .options("regexp", { describe: "detect short form myMod.controller(...) iff myMod matches regexp", }) + .options("single", { + describe: "use single quotes for dependency injection annotations"}) const argv = optimist.argv; function exit(msg) { diff --git a/run-tests.js b/run-tests.js index 67297c9..6394be3 100644 --- a/run-tests.js +++ b/run-tests.js @@ -21,6 +21,9 @@ const original = slurp("tests/original.js"); const annotated = ngAnnotate(original, {add: true}).src; test(slurp("tests/with_annotations.js"), annotated, "with_annotations.js"); +console.log("testing adding annotations using single quotes"); +const annotated2 = ngAnnotate(original, {add: true, single: true}).src; +test(slurp("tests/with_annotations_single.js"), annotated2, "with_annotations_single.js"); console.log("testing removing annotations"); const deAnnotated = ngAnnotate(annotated, {remove: true}).src; test(original, deAnnotated, "original.js"); diff --git a/tests/with_annotations_single.js b/tests/with_annotations_single.js new file mode 100644 index 0000000..8b57d85 --- /dev/null +++ b/tests/with_annotations_single.js @@ -0,0 +1,142 @@ +"use strict"; + +// long form +angular.module("MyMod").controller("MyCtrl", ['$scope', '$timeout', function($scope, $timeout) { +}]); + +// w/ dependencies +angular.module("MyMod", ["OtherMod"]).controller("MyCtrl", ['$scope', '$timeout', function($scope, $timeout) { +}]); + +// simple +myMod.controller("foo", ['$scope', '$timeout', function($scope, $timeout) { +}]); +myMod.service("foo", ['$scope', '$timeout', function($scope, $timeout) { +}]); +myMod.factory("foo", ['$scope', '$timeout', function($scope, $timeout) { +}]); +myMod.directive("foo", ['$scope', '$timeout', function($scope, $timeout) { +}]); +myMod.filter("foo", ['$scope', '$timeout', function($scope, $timeout) { +}]); +myMod.animation("foo", ['$scope', '$timeout', function($scope, $timeout) { +}]); + +// no dependencies => no need to wrap the function in an array +myMod.controller("foo", function() { +}); +myMod.service("foo", function() { +}); +myMod.factory("foo", function() { +}); +myMod.directive("foo", function() { +}); +myMod.filter("foo", function() { +}); +myMod.animation("foo", function() { +}); + +// run, config don't take names +myMod.run(['$scope', '$timeout', function($scope, $timeout) { +}]); +angular.module("MyMod").run(['$scope', function($scope) { +}]); +myMod.config(['$scope', '$timeout', function($scope, $timeout) { +}]); +angular.module("MyMod").config(function() { +}); + +// directive return object +myMod.directive("foo", ['$scope', function($scope) { + return { + controller: ['$scope', '$timeout', function($scope, $timeout) { + bar; + }] + } +}]); +myMod.directive("foo", ['$scope', function($scope) { + return { + controller: function() { + bar; + } + } +}]); + +// provider, provider $get +myMod.provider("foo", ['$scope', function($scope) { + this.$get = ['$scope', '$timeout', function($scope, $timeout) { + bar; + }]; +}]); +myMod.provider("foo", function() { + this.$get = function() { + bar; + }; +}); +myMod.provider("foo", function() { + return { + $get: ['$scope', '$timeout', function($scope, $timeout) { + bar; + }]}; +}); +myMod.provider("foo", function() { + return { + $get: function() { + bar; + }}; +}); +myMod.provider("foo", { + $get: ['$scope', '$timeout', function($scope, $timeout) { + bar; + }] +}); +myMod.provider("foo", { + $get: function() { + bar; + } +}); + +// chaining +myMod.directive("foo", ['$a', '$b', function($a, $b) { + a; +}]).factory("foo", function() { + b; + }).config(['$c', function($c) { + c; + }]).filter("foo", ['$d', '$e', function($d, $e) { + d; + }]).animation("foo", ['$f', '$g', function($f, $g) { + e; + }]); + +angular.module("MyMod").directive("foo", ['$a', '$b', function($a, $b) { + a; +}]).provider("foo", function() { + return { + $get: ['$scope', '$timeout', function($scope, $timeout) { + bar; + }]}; + }).value("foo", "bar") + .constant("foo", "bar") + .factory("foo", function() { + b; + }).config(['$c', function($c) { + c; + }]).filter("foo", ['$d', '$e', function($d, $e) { + d; + }]).animation("foo", ['$f', '$g', function($f, $g) { + e; + }]); + +// $provide +angular.module("MyMod").directive("foo", ['$a', '$b', function($a, $b) { + $provide.decorator("foo", ['$scope', '$timeout', function($scope, $timeout) { + a; + }]); + $provide.factory("bar", ['$timeout', '$scope', function($timeout, $scope) { + b; + }]); + $provide.animation("baz", ['$scope', '$timeout', function($scope, $timeout) { + c; + }]); +}]);