add new option for using single quotes arond dependency injection annotations

This commit is contained in:
Michael Duran
2014-04-16 22:49:35 -05:00
parent f7ebab3bb0
commit 57f4c3bc5c
5 changed files with 155 additions and 5 deletions
+2
View File
@@ -0,0 +1,2 @@
.idea/
node_modules/
+6 -5
View File
@@ -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);
}
}});
+2
View File
@@ -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) {
+3
View File
@@ -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");
+142
View File
@@ -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;
}]);
}]);