Skip to content

Improve DX for identifying missing or unused module imports #41028

Description

@Airblader

🚀 feature request

Relevant Package

This feature request is for @angular/core or @angular/compiler (?)

Description

Angular will currently detect (and complain) if a directive with an input binding exists, but the directive is not known (due to its defining module not being imported):

<app-child [myAwesomeDirective]="42"></app-child>

However, this doesn't work for directives without an input binding, i.e.

<app-child myAwesomeDirective></app-child>

The reason for this is – I believe – that myAwesomeDirective is treated as an HTML attribute (which makes sense), and HTML attributes are, well, legal HTML. And custom attributes are specifically frequently used as content projection selectors.

However, this causes a couple of issues in the developer experience, specifically

  1. No error being shown if you forget to import the module.
  2. Making it unsafe to try and identify unused module imports through trial & error of removing them (and there being a lack of alternatives to safely do so).

Describe the solution you'd like

I don't have a specific solution in mind which I think can be considered good enough. Initially I thought about a new strictness setting which fails on unknown HTML attributes, but especially due to the content projection case this would be problematic and a setting I'd only activate for this specific purpose (which feels awkward); in terms of HTML, there's nothing "illegal" about unknown attributes.

I will point out, however, that IDEs such as IntelliJ can and do warn about unknown HTML attributes, even taking content projection into consideration for this. This is somewhat suitable for case (1), though only for code as you write it, not when e.g. moving a component to a different module, and it doesn't cover (2) which is actually my main concern as there's currently no good way of "housekeeping" module imports in a safe, reliable manner. To clarify, this isn't about tree-shaking, unused modules can of course be tree-shaken, but rather about general maintenance and maybe ultimately even allowing to uninstall unused dependencies.

I'm not sure what, if anything, can be reasonably done here, but would like to at least open this for visibility.


To give an example, UI libraries such as Angular Material or ng-zorro-antd require you to import components individually. So you end up importing your buttons, checkboxes, tabs and what have you. But the list only ever grows and can be tough to reduce it down to what is actually needed later on.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimearea: language-serviceIssues related to Angular's VS Code language servicecore: directive matchingfeatureLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under consideration

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions