ngxtension / ngxtension-platform

Utilities for Angular
https://ngxtension.netlify.app/
MIT License
544 stars 80 forks source link

Invalid output that can't compile for convert-di-to-inject with local and "public" dependencies #367

Closed timdeschryver closed 2 months ago

timdeschryver commented 2 months ago

👋 thanks for this great library. Last week I've used the schematic to migrate our codebase to move away from constructors to inject. I stumbled across this edge case, resulting in a project that couldn't be compiled anymore after running the convert-di-to-inject migration.

If a dependency is used within the constructor AND outside of it, the this. keyword isn't prefixed within the constructor. If the dependency is only used within the constructor this isn't a problem as the dependency will be created as a variable within the constructor.

The following output already shows this behavior (myService.doSomething(); needs to be this.myService.doSomething();)

https://github.com/ngxtension/ngxtension-platform/blob/16b66e514dcff6fd440f12d799d44a6e098f9ee6/libs/plugin/src/generators/convert-di-to-inject/__snapshots__/generator.spec.ts.snap#L48C1-L81C6

This can be fixed by moving the commented code to the else block if the dependency is used outside the ctor.

https://github.com/ngxtension/ngxtension-platform/blob/16b66e514dcff6fd440f12d799d44a6e098f9ee6/libs/plugin/src/generators/convert-di-to-inject/generator.ts#L211C1-L230C13

If you want I can create a PR for this.