Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --rename option to rename declarations and references #47

Merged
merged 10 commits into from
Sep 15, 2014

Conversation

frapontillo
Copy link
Contributor

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 #44

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
@@ -218,7 +218,7 @@ function matchRegular(node, ctx) {
const args = node.arguments;
const target = (is.someof(method.name, ["config", "run"]) ?
args.length === 1 && args[0] :
args.length === 2 && args[0].type === "Literal" && is.string(args[0].value) && args[1]);
args.length === 2 && args[0].type === "Literal" && is.string(args[0].value) && [args[0], args[1]]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main difference is here, where I return another target, besides the defined function, that is the given provider name.

@olov
Copy link
Owner

olov commented Jul 16, 2014

Thanks for the PR! I'll review it and provide feedback as soon as I can (may take a day or two).

@frapontillo
Copy link
Contributor Author

Thanks! This was mainly needed to solve incompatibilities between angular-ui-bootstrap ang angular-strap, as they both declare their own $modal and $tooltip.

@frapontillo
Copy link
Contributor Author

While trying to build I realized that nginject-comments.js was having issues due to my modifications of the stringify function. Also, the compiler complained about the use of var, so I replaced the occurrences with let.

@@ -126,6 +131,15 @@ function runAnnotate(err, src) {
});
}

if (config.rename) {
let flattenRename = config.rename.split(" ");
let renameMap = {};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of added lets should be replaced with consts. Even this one (const is shallow constant, you can still mess with keys).

@mgcrea
Copy link

mgcrea commented Jul 27, 2014

👍

@olov
Copy link
Owner

olov commented Jul 27, 2014

Overall I think this is fine and I'm willing to merge it as an experimental feature. Because renaming is not really a core feature of ng-annotate, it may be removed in the future, in which case I'll try to make it possible to implement the same thing with a plugin. Also see #42. Make sure that you're confident with this.

Before merging, it needs tests. Create a tests/rename_original.js and tests/rename_renamed.js and the necessary code in the runner. Also please change all your let to const, then only make things let when the variable binding changes (v8 will tell you if you're unsure, so will defs in build.sh).

@frapontillo
Copy link
Contributor Author

Achieving the same thing with a plugin would be awesome.
I will try to complete the PR in the next couple of days (I hope), as I'll be AFK the first half of August.

@frapontillo
Copy link
Contributor Author

Tests written and passed.
I have integrated e57d008 by @smrq by cherry picking.

@h2non
Copy link

h2non commented Jul 29, 2014

Waiting for the merge!

@olov
Copy link
Owner

olov commented Aug 2, 2014

Ok, can you fix the following:

  • rebase against HEAD
  • the rename-map should be a https://www.npmjs.org/package/stringmap, not a JS object
  • the ng-annotate API should take a rename-map in the form of [{from: string, to: string}, ..], then convert it to stringmap
  • i don't want to manually keep the test input and output files in sync with the main original.js file so for that reason you can slim down the two files into something that tests renaming with less examples.

Thanks!

@frapontillo
Copy link
Contributor Author

Sure, I'm on vacation right now, I will be able to get back to it after
14th August.
Il 02/ago/2014 13:46 "Olov Lassus" [email protected] ha scritto:

Ok, can you fix the following:

  • rebase against HEAD
  • the rename-map should be a https://www.npmjs.org/package/stringmap,
    not a JS object
  • the ng-annotate API should take a rename-map in the form of [{from:
    string, to: string}, ..], then convert it to stringmap
  • i don't want to manually keep the test input and output files in
    sync with the main original.js file so for that reason you can slim down
    the two files into something that tests renaming with less examples.

Thanks!


Reply to this email directly or view it on GitHub
#47 (comment).

Conflicts:
	ng-annotate-main.js
	ng-annotate.js
	nginject-comments.js
	run-tests.js
@frapontillo
Copy link
Contributor Author

About the rename API, could you please confirm that ng-annotate.js should take a JSON representation in the form of "[{\"from\":\"string\", \"to\":\"newString\"}]", then pass it to ng-annotate-main.js as a stringmap?

CLI example:

$ node --harmony ng-annotate.js -a --rename "[{\"from\":\"$a\", \"to\":\"$aRenamed\"}]" tests/rename.js

@olov
Copy link
Owner

olov commented Aug 18, 2014

No, the CLI can function as you already made it, the API however: require("ng-annotate")(src, {add: true, rename: [{from: "hello", to: "kitty"}, {from: "oi", to: "yo"}]})

@frapontillo
Copy link
Contributor Author

So every from-to association should be transformed into a stringmap?

@olov
Copy link
Owner

olov commented Aug 18, 2014

const renamemap = stringmap();
rename.forEach(function(pair) { renamemap.set(pair.from, pair.to) })

The rename CLI option remains the same, but it is passed to `ng-annotate-main.js`
as a from-to object collection; it is then transformed and used as a stringmap.
Rename tests have been separated from the original full test and simplified to test
against the main cases.
@frapontillo
Copy link
Contributor Author

Changes have been pushed, thank you for your support in this :)

@mgcrea
Copy link

mgcrea commented Sep 15, 2014

@olov Would be great if you could merge this, it's blocking our release of a compat build for angular-strap. Thanks!

@olov olov merged commit a77e91c into olov:master Sep 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Way to change a service name/reference
6 participants