Skip to content

Add diagnostic to flag unsupported usages of initializer APIs#54993

Closed
crisbeto wants to merge 5 commits intoangular:mainfrom
crisbeto:invalid-initializer-fn
Closed

Add diagnostic to flag unsupported usages of initializer APIs#54993
crisbeto wants to merge 5 commits intoangular:mainfrom
crisbeto:invalid-initializer-fn

Conversation

@crisbeto
Copy link
Member

@crisbeto crisbeto commented Mar 22, 2024

Adds a diagnostic that will flag any usages of initializer APIs outside of initializers. Includes the following changes:

refactor(compiler-cli): add API to check if imports exist

Adds a couple of APIs to the ImportedSymbolsTracker that allow us to quickly check if a specific symbol is imported in a file.

refactor(compiler-cli): introduce infrastructure for running additional checks against TypeScript files

Adds the new SourceFileValidator that will be used to check for file-level issues that may prevent Angular from working, like invoking the input() function outside of an initializer. Currently only one check is planned, but this setup will allow us to easily add more in the future.

fix(compiler-cli): add diagnostic if initializer API is used outside of an initializer

Adds a rule that will produce a diagnostic when an initializer-based API is used outside of an initializer.

fix(compiler-cli): report cases where initializer APIs are used in a non-directive class

Expands the check for initializer APIs to also flag when the function is called on a class that isn't a component or directive.

refactor(compiler-cli): allow source file validator rule to produce a single diagnostic

Allows for SourceFileValidatorRule.checkNode to produce a single diagnostic. The most common case should be one diagnostic per node so this allows us to save some array allocations.

Fixes #54381.

@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer target: major This PR is targeted for the next major release area: compiler Issues related to `ngc`, Angular's template compiler labels Mar 22, 2024
@ngbot ngbot bot modified the milestone: Backlog Mar 22, 2024
Copy link
Member

@devversion devversion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🎉

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-for: public-api

@pullapprove pullapprove bot requested a review from dylhunn March 23, 2024 20:49
Copy link
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-for: public-api

Copy link
Member

@pkozlowski-opensource pkozlowski-opensource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Reviewed-for: public-api

Adds a couple of APIs to the `ImportedSymbolsTracker` that allow us to quickly check if a specific symbol is imported in a file.
…al checks against TypeScript files

Adds the new `SourceFileValidator` that will be used to check for file-level issues that may prevent Angular from working, like invoking the `input()` function outside of an initializer. Currently only one check is planned, but this setup will allow us to easily add more in the future.
…of an initializer

Adds a rule that will produce a diagnostic when an initializer-based API is used outside of an initializer.

Fixes angular#54381.
…non-directive class

Expands the check for initializer APIs to also flag when the function is called on a class that isn't a component or directive.
… single diagnostic

Allows for `SourceFileValidatorRule.checkNode` to produce a single diagnostic. The most common case should be one diagnostic per node so this allows us to save some array allocations.
@crisbeto crisbeto force-pushed the invalid-initializer-fn branch from 1920539 to de4d76b Compare March 28, 2024 09:15
@crisbeto
Copy link
Member Author

This change should be ready to go now. I've rebased, addressed the feedback and expanded the check to also cover classes that aren't components/directives.

@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Mar 28, 2024
@dylhunn
Copy link
Contributor

dylhunn commented Mar 28, 2024

This PR was merged into the repository by commit 336916c.

@dylhunn dylhunn closed this in 3d9f01c Mar 28, 2024
dylhunn pushed a commit that referenced this pull request Mar 28, 2024
…al checks against TypeScript files (#54993)

Adds the new `SourceFileValidator` that will be used to check for file-level issues that may prevent Angular from working, like invoking the `input()` function outside of an initializer. Currently only one check is planned, but this setup will allow us to easily add more in the future.

PR Close #54993
dylhunn pushed a commit that referenced this pull request Mar 28, 2024
…of an initializer (#54993)

Adds a rule that will produce a diagnostic when an initializer-based API is used outside of an initializer.

Fixes #54381.

PR Close #54993
dylhunn pushed a commit that referenced this pull request Mar 28, 2024
…non-directive class (#54993)

Expands the check for initializer APIs to also flag when the function is called on a class that isn't a component or directive.

PR Close #54993
dylhunn pushed a commit that referenced this pull request Mar 28, 2024
… single diagnostic (#54993)

Allows for `SourceFileValidatorRule.checkNode` to produce a single diagnostic. The most common case should be one diagnostic per node so this allows us to save some array allocations.

PR Close #54993
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling field initializers (input, output, queries, etc) should be errors when not used to declare a field in a directive

6 participants