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

Use implicit mode to add ngInject annotations #10

Open
schmod opened this issue Aug 4, 2016 · 7 comments
Open

Use implicit mode to add ngInject annotations #10

schmod opened this issue Aug 4, 2016 · 7 comments

Comments

@schmod
Copy link
Owner

schmod commented Aug 4, 2016

Create a mode for developers to "retrofit" an existing code-base to use explicit annotations.

This mode should use our "implicit" matching to find all of the locations where we think that annotations belong. Instead of adding the $inject annotations in these places, we should then add the 'ngInject' directives that would be necessary to use this plugin in "explicit-only" mode.

In practice, this transformation would be run against an entire codebase (with no other transformations), manually reviewed by a human, and the results would then be checked into source control.

Ideally, this should also convert /* @ngInject */ to 'ngInject'; wherever possible.


As a follow-up, also add a "verify" mode that scans existing code for missing 'ngInject' directives, and prints a warning or error.

Along with strictDi, this brings us most of the benefits of disabling "implicit" matching, while also providing a layer of safety to warn developers when they forget to add 'ngInject' to their code.

@ghost
Copy link

ghost commented Sep 21, 2016

What do you we think would be needed to make this happen? I'd like to see this land, so I'd be willing to open a PR.

@timofei-iatsenko
Copy link

This is a really good proposal. I currently have a hybrid application with AngularJs + Angular7, and ngAnnotate is one of the things which block me to use AngularCLI.

I would be really happy if I can find all places where implicit annotation applied, and replace it to manual annotation, and remove ngAnnotate at all.

Your proposal can help with that because I can manually one by one check all places with 'ngInject', and fix them and then push to GIT.

By the way, if it's hard to implement, may be you can point me where I can put a console.log to see all files with implicit annotations?

@schmod
Copy link
Owner Author

schmod commented Oct 23, 2018

The project isn't really set up to do codemods at the moment (and I'd kind of discourage folks from doing this, because it provides little safety in the event of a forgotten annotation), but it's certainly something I can look at once I have time to dust this off and get the other bugs fixed.

@schmod
Copy link
Owner Author

schmod commented Oct 23, 2018

Actually, re-reading @thekip's comment, it sounds like you just want to run ngAnnotate once and check the results in as your new source?

@timofei-iatsenko
Copy link

I'm actually want to run ngAnnotate in a kind of "dry run" mode, just to point me places where I don't have explicit annotations.

I have explicit markers via 'ngInject' (which I can find just by string in IDE) and also I have places where code annotated implictly by "euristic" detection. I want to find all this 'euristic' places, mark them explicitly and then one by one convert to manual annotations / decorators / port to new angular / whatever.

For me even list in console with filename and line numbers will be enough. So the question where can I hack this ngAnnotate and add console.log statement to display this files.

@schmod
Copy link
Owner Author

schmod commented Oct 23, 2018

In ng-annotate-main.js, look for anything that calls insertBefore or insertAfter

I'm planning to eventually add something like a "dry-run" mode with the explicitOnly mode -- we'll only annotate functions that you've explicitly marked for injection, but warn or fail with an error if it looks like you forgot one.

(In my experience, it isn't really safe to run this once and forget it if you're actively developing a codebase -- the manual injection syntax is ridiculously error-prone when humans are writing it)

@timofei-iatsenko
Copy link

Finally I got that. In my case almost 99% occurrences was in block.unshiftContainer("body", [expr]); in addInjectArrayBeforePath() function.

I also had to add file name information in visitor to context, to be able to console.log it further.

      Program: {
        enter(path, file) {
          file.opts.explicitOnly = file.opts.explicitOnly || false;

          ctx.suspects = [];
          ctx.blocked = [];
          ctx.fragments = [];
+         ctx.filename = file.filename;
          ctx.srcForRange = function(node) {
            return file.file.code.slice(node.start, node.end);
          };
        },
        exit() {
          judgeSuspects(ctx);
        }
      }

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

No branches or pull requests

2 participants