Skip to content

Way to change a service name/reference #44

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

Closed
frapontillo opened this issue Jul 16, 2014 · 6 comments · Fixed by #47
Closed

Way to change a service name/reference #44

frapontillo opened this issue Jul 16, 2014 · 6 comments · Fixed by #47

Comments

@frapontillo
Copy link
Contributor

Is there a way to change a given service name/reference through the examined source? Since AngularJS doesn't support namespacing when injecting instances and functions, I need to pre-process some libraries that conflict with each other.

Can plugins be used to achieve this?

@olov
Copy link
Owner

olov commented Jul 16, 2014

It's out of scope for what ng-annotate is intended to do, frankly. Plugins are not really designed for that use-case either - you need to do your processing either before or after ng-annotate runs.

I would probably have checked-in modified versions of the libraries in my repo or created a separate tool that matches [..., "foobar", ..., function(..., foobar, ...) {}] (i.e. the output of ng-annotate) and alters it accordingly. The risks of false positives would be very low. You could use https://www.npmjs.org/package/ordered-ast-traverse and https://www.npmjs.org/package/alter for simple traversal and batched text manipulations (what ng-annotate uses).

@olov olov closed this as completed Jul 16, 2014
@olov
Copy link
Owner

olov commented Jul 16, 2014

On a second thought, perhaps this use-case should be possible to implement using plugins after all. There are some similarities to #42.

ng-annotate should mark all nodes corresponding to a parameter-injected function. Plugins should be able to register a function that runs between judgeSuspects and alter (see ng-annotate-main.js). Your plugin could then update references there.

Re-opening.

@olov olov reopened this Jul 16, 2014
@frapontillo
Copy link
Contributor Author

So can this be done in the following way?

  1. Between judgeSuspects(ctx) and alter(src, fragments), look for fragments with a str that contains the string to replace.

    a. Replace the old string with the new one (quite naive, we should check for proper variable names here)

  2. Find all of the provider definitions (controller, service, etc.)

  3. Generate a defFragments array

    a. Filter the definitions with the one to replace
    b. Add the new definition fragment to the array

  4. Merge defFragments with fragments

  5. Proceed to alter

@olov
Copy link
Owner

olov commented Jul 16, 2014

Not currently, because the fragments array is not necessarily populated. Instead I'll change ng-annotate to mark all relevant nodes instead.

Similarly, I may be able to mark all definition nodes.

@frapontillo
Copy link
Contributor Author

The function of interest is, in this case, matchRegular(node, ctx) where you check for regular definitions and return the target function. How can we save the definition fragment as well?

If we return the full node and delegate the target evaluation when building fragments, we can discriminate there against definitions and references, thus making one single pass.

EDIT: I am making some progress, I may be able to push a PR in a few, or maybe tomorrow.

frapontillo added a commit to frapontillo/ng-annotate that referenced this issue Jul 16, 2014
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.

Also, a couple of JS grammar fixes here and there.

Closes olov#44
frapontillo added a commit to frapontillo/ng-annotate that referenced this issue Jul 16, 2014
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
frapontillo added a commit to frapontillo/ng-annotate that referenced this issue Jul 16, 2014
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
@olov olov closed this as completed in #47 Sep 15, 2014
@olov
Copy link
Owner

olov commented Sep 15, 2014

landed in 0.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants