mgechev / ngast

Parser for Angular projects.

Home Page:https://mgechev.github.io/ngast/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[ngtsc] Link dependencies injected with @Inject decorator with the provider

GrandSchtroumpf opened this issue · comments

The problem

With this example :

const TOKEN = new InjectionToken();
@Component({
  ...
  providers: [{ provide: TOKEN, useValue: true }]
})
export class MainComponent {
  constructor(@Inject(TOKEN) isTrue) {}
}

@NgModule({
  declarations: [MainComponent],
  providers: [{ provide: TOKEN, useValue: false }]
})

When running :

const [component] = workspace.getAllComponents();
const [token] = component.getDependencies();

The value token should reference the provider inside the component (with the value true).
Current behavior: returns undefined.


How to solve :

  1. Do more research on the compiler to found out if this information is hold somewhere
  2. If not create a ProviderRegistry that keeps track of all the provider per class declaration or at the root if providedIn: 'root'
  3. For components & directives we can use the scope to look for the closest implementation of the provider.
  4. For injectables & Pipes there is no scope available, we should scan every modules & root field of the ProviderRegistry.

Hopefully there is a built-in solution in the angular compiler so we can stop at step 1 🤞