Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-cli): identify aliased initializer functions #54480

Closed
wants to merge 4 commits into from

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Feb 16, 2024

Fixes that initializer functions weren't being recognized if they are aliased (e.g. import {model as alias} from '@angular/core';).

To do this efficiently, I had to introduce the ImportedSymbolsTracker which scans the top-level imports of a file and allows them to be checked quickly, without having to go through the type checker. It will also be useful in the future when verifying that that initializer APIs aren't used in unexpected places.

I've also introduced tests specifically for the tryParseInitializerApiMember function so that we can test it in isolation instead of going through the various functions that call into it.

@crisbeto crisbeto added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release area: compiler Issues related to `ngc`, Angular's template compiler labels Feb 16, 2024
@ngbot ngbot bot added this to the Backlog milestone Feb 16, 2024
* Tracks which symbols are imported in specific files and under what names.
* Allows for efficient querying for references to those symbols.
*/
export class ImportedSymbolsTracker {
Copy link
Member Author

@crisbeto crisbeto Feb 16, 2024

Choose a reason for hiding this comment

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

In the future I think we'll also need methods like hasImportsFor(symbol, moduleName) and hasNamespaceImportFor(moduleName) which will allow us to skip traversing the entire file even earlier. I skipped them for now since we don't need them yet, but all the information is already there.

@crisbeto crisbeto force-pushed the aliased-init-fn branch 3 times, most recently from 0911967 to 1132572 Compare February 16, 2024 12:37
@crisbeto crisbeto marked this pull request as ready for review February 16, 2024 12:57
const transformers: ts.CustomTransformers = {
before: [
getInitializerApiJitTransform(reflectionHost, /* isCore */ false),
getInitializerApiJitTransform(reflectionHost, importTracker, /* isCore */ false),
Copy link
Member

Choose a reason for hiding this comment

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

At this point we have so many trackers 😄 local compilation, deferred etc

Fixes that initializer functions weren't being recognized if they are aliased (e.g. `import {model as alias} from '@angular/core';`).

To do this efficiently, I had to introduce the `ImportedSymbolsTracker` which scans the top-level imports of a file and allows them to be checked quickly, without having to go through the type checker. It will be useful in the future when verifying that that initializer APIs aren't used in unexpected places.

I've also introduced tests specifically for the `tryParseInitializerApiMember` function so that we can test it in isolation instead of going through the various functions that call into it.
@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 Feb 22, 2024
@dylhunn
Copy link
Contributor

dylhunn commented Feb 23, 2024

This PR was merged into the repository by commit f04ecc0.

@dylhunn dylhunn closed this in f04ecc0 Feb 23, 2024
dylhunn pushed a commit that referenced this pull request Feb 23, 2024
Fixes that initializer functions weren't being recognized if they are aliased (e.g. `import {model as alias} from '@angular/core';`).

To do this efficiently, I had to introduce the `ImportedSymbolsTracker` which scans the top-level imports of a file and allows them to be checked quickly, without having to go through the type checker. It will be useful in the future when verifying that that initializer APIs aren't used in unexpected places.

I've also introduced tests specifically for the `tryParseInitializerApiMember` function so that we can test it in isolation instead of going through the various functions that call into it.

PR Close #54480
@dylhunn dylhunn mentioned this pull request Feb 23, 2024
dylhunn added a commit to dylhunn/angular that referenced this pull request Feb 23, 2024
@dylhunn
Copy link
Contributor

dylhunn commented Feb 23, 2024

Rolled back due to breakage in g3. In summary, namedImports is an object, making it unsafe with the following key names:

constructor
hasOwnProperty
isPrototypeOf
propertyIsEnumerable
toLocaleString
toString
valueOf
__defineGetter__
__defineSetter__
__lookupGetter__
__lookupSetter__
__proto__

dylhunn added a commit that referenced this pull request Feb 26, 2024
dylhunn added a commit that referenced this pull request Feb 26, 2024
@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 Mar 25, 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: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants