Skip to content

Commit d9d70f9

Browse files
committed
add --rename option to rename declarations and references
The `--rename` option takes a string as input, treating every couple, separated by a space, as a find-replace option. For example, `--rename "$theService $myService "theFactory myFactory"`will replace `$theService` with `$myService` and `theFactory` with `myFactory` in both the declaration and in any annotation that will be added by ng-annotate. The actual use of the provider won't be changed. Closes olov#44
1 parent 12deaf2 commit d9d70f9

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Use the `--single_quotes` option to output `'$scope'` instead of `"$scope"`.
5151
Use the `--regexp` option to restrict matching further or to expand matching.
5252
See description further down.
5353

54+
Use the `--rename` option to rename providers (services, factories, controllers, etc.) with
55+
a new name when declared and referenced through annotation.
56+
5457
Use the `--plugin` option to load a user plugin with the provided path (*experimental*,
5558
0.9.x may change API). See [plugin-example.js](plugin-example.js) for more info.
5659

ng-annotate-main.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ function matchRegular(node, ctx) {
218218
const args = node.arguments;
219219
const target = (is.someof(method.name, ["config", "run"]) ?
220220
args.length === 1 && args[0] :
221-
args.length === 2 && args[0].type === "Literal" && is.string(args[0].value) && args[1]);
221+
args.length === 2 && args[0].type === "Literal" && is.string(args[0].value) && [args[0], args[1]]);
222222

223223
if (target) {
224224
target.$always = true;
@@ -266,16 +266,20 @@ function matchResolve(props) {
266266
return [];
267267
};
268268

269-
function stringify(arr, quot) {
269+
function getReplaceString(ctx, originalString) {
270+
return (ctx.rename[originalString] || originalString);
271+
}
272+
273+
function stringify(ctx, arr, quot) {
270274
return "[" + arr.map(function(arg) {
271-
return quot + arg.name + quot;
275+
return quot + getReplaceString(ctx, arg.name) + quot;
272276
}).join(", ") + "]";
273277
}
274278

275-
function insertArray(functionExpression, fragments, quot) {
279+
function insertArray(ctx, functionExpression, fragments, quot) {
276280
const range = functionExpression.range;
277281

278-
const args = stringify(functionExpression.params, quot);
282+
const args = stringify(ctx, functionExpression.params, quot);
279283
fragments.push({
280284
start: range[0],
281285
end: range[0],
@@ -288,13 +292,14 @@ function insertArray(functionExpression, fragments, quot) {
288292
});
289293
}
290294

291-
function replaceArray(array, fragments, quot) {
295+
function replaceArray(ctx, array, fragments, quot) {
292296
const functionExpression = last(array.elements);
293297

294298
if (functionExpression.params.length === 0) {
295299
return removeArray(array, fragments);
296300
}
297-
const args = stringify(functionExpression.params, quot);
301+
302+
const args = stringify(ctx, functionExpression.params, quot);
298303
fragments.push({
299304
start: array.range[0],
300305
end: functionExpression.range[0],
@@ -317,6 +322,15 @@ function removeArray(array, fragments) {
317322
});
318323
}
319324

325+
function replaceString(ctx, string, fragments, quot) {
326+
var customReplace = getReplaceString(ctx, string.value);
327+
fragments.push({
328+
start: string.range[0],
329+
end: string.range[1],
330+
str: quot + customReplace + quot
331+
});
332+
}
333+
320334
function judgeSuspects(ctx) {
321335
const suspects = ctx.suspects;
322336
const mode = ctx.mode;
@@ -341,11 +355,13 @@ function judgeSuspects(ctx) {
341355
}
342356

343357
if (mode === "rebuild" && isAnnotatedArray(target)) {
344-
replaceArray(target, fragments, quot);
358+
replaceArray(ctx, target, fragments, quot);
345359
} else if (mode === "remove" && isAnnotatedArray(target)) {
346360
removeArray(target, fragments);
347361
} else if (is.someof(mode, ["add", "rebuild"]) && isFunctionExpressionWithArgs(target)) {
348-
insertArray(target, fragments, quot);
362+
insertArray(ctx, target, fragments, quot);
363+
} else if (isGenericProviderName(target)) {
364+
replaceString(ctx, target, fragments, quot);
349365
}
350366
}
351367
}
@@ -368,6 +384,9 @@ function isFunctionExpressionWithArgs(node) {
368384
function isFunctionDeclarationWithArgs(node) {
369385
return node.type === "FunctionDeclaration" && node.params.length >= 1;
370386
}
387+
function isGenericProviderName(node) {
388+
return node.type === "Literal" && is.string(node.value);
389+
}
371390

372391
module.exports = function ngAnnotate(src, options) {
373392
const mode = (options.add && options.remove ? "rebuild" :
@@ -380,6 +399,7 @@ module.exports = function ngAnnotate(src, options) {
380399

381400
const quot = options.single_quotes ? "'" : '"';
382401
const re = (options.regexp ? new RegExp(options.regexp) : /^[a-zA-Z0-9_\$\.\s]+$/);
402+
const rename = options.rename || {};
383403
let ast;
384404
const stats = {};
385405
try {
@@ -437,6 +457,7 @@ module.exports = function ngAnnotate(src, options) {
437457
return src.slice(range[0], range[1]);
438458
},
439459
re: re,
460+
rename: rename,
440461
comments: comments,
441462
fragments: fragments,
442463
triggers: triggers,

ng-annotate.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const optimist = require("optimist")
3434
.options("regexp", {
3535
describe: "detect short form myMod.controller(...) iff myMod matches regexp",
3636
})
37+
.options("rename", {
38+
describe: "rename declarations and annotated refernces\n" +
39+
"originalName newName anotherOriginalName anotherNewName ...",
40+
default: ""
41+
})
3742
.options("plugin", {
3843
describe: "use plugin with path (experimental)",
3944
})
@@ -101,7 +106,7 @@ function runAnnotate(err, src) {
101106
}, {});
102107

103108

104-
["add", "remove", "o", "regexp", "single_quotes", "plugin", "stats"].forEach(function(opt) {
109+
["add", "remove", "o", "regexp", "rename", "single_quotes", "plugin", "stats"].forEach(function(opt) {
105110
if (opt in argv) {
106111
config[opt] = argv[opt];
107112
}
@@ -126,6 +131,15 @@ function runAnnotate(err, src) {
126131
});
127132
}
128133

134+
if (config.rename) {
135+
var flattenRename = config.rename.split(" ");
136+
var renameMap = {};
137+
for (var i = 0; i < flattenRename.length; i = i + 2) {
138+
renameMap[flattenRename[i]]= flattenRename[i+1];
139+
}
140+
config.rename = renameMap;
141+
}
142+
129143
const run_t0 = Date.now();
130144
const ret = ngAnnotate(src, config);
131145
const run_t1 = Date.now();

0 commit comments

Comments
 (0)