You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unused imports diagnostics for compound arrays without NgModules #59754
Recently the new flag "unusedStandaloneImports" was added that detects unused standalone imports in the given standalone components.
It works just fine, however there're quite a lot of use cases where modules are applicable even along with standalone components, e.g. complex UI packages like @angular/material/tabs, thus having the diagnostics to check whether such modules are used is also highly valuable.
I understand that resolving all dependencies of a module is much more computationally heavy operation in comparison with standalone components, but I can still see it being useful as an optional flag that can be enabled only on CI or to cleanup such imports manually from time to time.
As @crisbeto mentions, NgModules play a role in DI more so than standalone components. We could look for standalone components that are present in @NgModule.imports to determine if they are used, but this would have to consider any of the declarations of the NgModule (making it a bit more complicated) and we're generally steering in the direction of standalone components, so there's no plans at this time to introduce additional checks for NgModule setups.
and we're generally steering in the direction of standalone components
Understandable, given that are there any recommendations from the team how do you see imports of the large features without modules (e.g. the @angular/material/tabs that I've mentioned earlier) ? Just a destructed array of components won't really work because some of them may not be used in a certain scenario and importing every bit manually is sometimes too verbose
Thinking about this more, I realize there's quite a few variations surrounding this topic.
Standalone components with an NgModule in its imports array.
Standalone components with a reference to an array of components in its imports array.
NgModule components with NgModule.imports that isn't used.
My initial interpretation of this request was concerning 3), but I see now it's more about 1). In that case it's indeed DI that gets in the way, as the compiler doesn't know whether an NgModule or its transitive imports registers any providers.
For 2) it is already possible to declare an array of component references and refer to that in Component.imports. However, this situation is currently exempt from the detection as it is assumed that the array exists for sharing purposes (iff the array is exported). This assumption is fair, but it misses the detection of a reference to such array where none of its contents are being used. I think that is something that could be achieved, with the constraint that the array could still not contain NgModules (because of DI).
So I do think there is a possibility to extend the unused directive checks, but interestingly that would still exclude NgModules.
Yes @JoostK that's exactly what I meant, probably my original description wasn't fully clear. If there would be a possibility to import an array of standalone components as a const and verify if at least one of them is used within a given standalone component, that would really help much.
Currently we're using a lot modules with { imports: [...components], exports: [...components] } in the metadata, so I guess those can easily be changed to the direct import of components constant.
I have, but with a different scope. Only scenario 2 (from my earlier comment) can be detected, the scenario this was originally requested to be covered cannot (at least not fully safely)
I'm wondering if such a diagnostic really makes sense.
The common use case I see is a library exposing the array in addition to each individiual directive/component.
Users who reach out to this array probably don't care about adding each individual directive but this will still be flagged by the diagnostic.
Users who reach out to this array probably don't care about adding each individual directive but this will still be flagged by the diagnostic.
This isn't the intent behind this request, at least not what I described as 2) in #59754 (comment). The rule ought to flag references to arrays where none of the array's elements are used. Note that #66620 doesn't actually implement this rule, it is different in semantics and I haven't determined yet if its suggestion could be considered meaninful as well.
Activity
crisbeto commentedon Jan 28, 2025
The problem with modules is that they can bring in providers, in addition to declarations.
JoostK commentedon Jan 29, 2025
As @crisbeto mentions, NgModules play a role in DI more so than standalone components. We could look for standalone components that are present in
@NgModule.importsto determine if they are used, but this would have to consider any of the declarations of the NgModule (making it a bit more complicated) and we're generally steering in the direction of standalone components, so there's no plans at this time to introduce additional checks for NgModule setups.dmitry-stepanenko commentedon Jan 30, 2025
@JoostK
Understandable, given that are there any recommendations from the team how do you see imports of the large features without modules (e.g. the
@angular/material/tabsthat I've mentioned earlier) ? Just a destructed array of components won't really work because some of them may not be used in a certain scenario and importing every bit manually is sometimes too verboseJoostK commentedon Jan 30, 2025
Thinking about this more, I realize there's quite a few variations surrounding this topic.
importsarray.importsarray.NgModule.importsthat isn't used.My initial interpretation of this request was concerning 3), but I see now it's more about 1). In that case it's indeed DI that gets in the way, as the compiler doesn't know whether an NgModule or its transitive imports registers any providers.
For 2) it is already possible to declare an array of component references and refer to that in
Component.imports. However, this situation is currently exempt from the detection as it is assumed that the array exists for sharing purposes (iff the array is exported). This assumption is fair, but it misses the detection of a reference to such array where none of its contents are being used. I think that is something that could be achieved, with the constraint that the array could still not contain NgModules (because of DI).So I do think there is a possibility to extend the unused directive checks, but interestingly that would still exclude NgModules.
dmitry-stepanenko commentedon Jan 30, 2025
Yes @JoostK that's exactly what I meant, probably my original description wasn't fully clear. If there would be a possibility to import an array of standalone components as a const and verify if at least one of them is used within a given standalone component, that would really help much.
Currently we're using a lot modules with
{ imports: [...components], exports: [...components] }in the metadata, so I guess those can easily be changed to the direct import ofcomponentsconstant.dmitry-stepanenko commentedon Jan 31, 2025
@JoostK taking into account our discussion above, would you like to reopen this ticket?
[-]Unused imports diagnostics for modules[/-][+]Unused imports diagnostics for compound arrays without NgModules[/+]JoostK commentedon Jan 31, 2025
I have, but with a different scope. Only scenario 2 (from my earlier comment) can be detected, the scenario this was originally requested to be covered cannot (at least not fully safely)
refactor(compiler-cli): detect unused standalone imports in local arrays
JeanMeche commentedon Jan 18, 2026
I'm wondering if such a diagnostic really makes sense.
The common use case I see is a library exposing the array in addition to each individiual directive/component.
Users who reach out to this array probably don't care about adding each individual directive but this will still be flagged by the diagnostic.
JoostK commentedon Jan 19, 2026
This isn't the intent behind this request, at least not what I described as 2) in #59754 (comment). The rule ought to flag references to arrays where none of the array's elements are used. Note that #66620 doesn't actually implement this rule, it is different in semantics and I haven't determined yet if its suggestion could be considered meaninful as well.
JeanMeche commentedon Jan 19, 2026
Oh okay, I was indeed mislead by #66620. In that case, yes what's we're discussing here makes sense.
refactor(compiler-cli): detect unused standalone imports in local arrays
refactor(compiler-cli): detect unused standalone imports in local arrays