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): avoid conflicts with built-in global variables in for loop blocks #53319

Closed
wants to merge 1 commit into from

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Dec 2, 2023

Currently we generate the following TCB for a @for loop:

// @for (item of items; track item) {...}
for (const item of this.items) {
  var _t1 = item;
  // Do things with `_t1`
}

This is problematic if the item name is the same as a global variable (e.g. document), because when the TCB has references to that variable (e.g. document.createElement), it'll find the loop initializer instead of the global variable.

These changes fix the issue by generating the following instead:

for (const _t1 of this.items) {
  // Do things with `_t1`
}

Fixes #53293.

…for loop blocks

Currently we generate the following TCB for a `@for` loop:

```ts
// @for (item of items; track item) {...}

for (const item of this.items) {
  var _t1 = item;
  // Do things with `_t1`
}
```

This is problematic if the item name is the same as a global variable (e.g. `document`), because when the TCB has references to that variable (e.g. `document.createElement`), it'll find the loop initializer instead of the global variable.

These changes fix the issue by generating the following instead:

```ts
for (const _t1 of this.items) {
  // Do things with `_t1`
}
```

Fixes angular#53293.
@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 Dec 2, 2023
@ngbot ngbot bot modified the milestone: Backlog Dec 2, 2023
@crisbeto crisbeto requested a review from atscott December 2, 2023 08:53
@crisbeto crisbeto marked this pull request as ready for review December 2, 2023 08:53
@crisbeto crisbeto removed the request for review from atscott December 4, 2023 04:16
@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 Dec 4, 2023
@dylhunn
Copy link
Contributor

dylhunn commented Dec 5, 2023

This PR was merged into the repository by commit d7a83f9.

@dylhunn dylhunn closed this in d7a83f9 Dec 5, 2023
dylhunn pushed a commit that referenced this pull request Dec 5, 2023
…for loop blocks (#53319)

Currently we generate the following TCB for a `@for` loop:

```ts
// @for (item of items; track item) {...}

for (const item of this.items) {
  var _t1 = item;
  // Do things with `_t1`
}
```

This is problematic if the item name is the same as a global variable (e.g. `document`), because when the TCB has references to that variable (e.g. `document.createElement`), it'll find the loop initializer instead of the global variable.

These changes fix the issue by generating the following instead:

```ts
for (const _t1 of this.items) {
  // Do things with `_t1`
}
```

Fixes #53293.

PR Close #53319
tbondwilkinson pushed a commit to tbondwilkinson/angular that referenced this pull request Dec 6, 2023
…for loop blocks (angular#53319)

Currently we generate the following TCB for a `@for` loop:

```ts
// @for (item of items; track item) {...}

for (const item of this.items) {
  var _t1 = item;
  // Do things with `_t1`
}
```

This is problematic if the item name is the same as a global variable (e.g. `document`), because when the TCB has references to that variable (e.g. `document.createElement`), it'll find the loop initializer instead of the global variable.

These changes fix the issue by generating the following instead:

```ts
for (const _t1 of this.items) {
  // Do things with `_t1`
}
```

Fixes angular#53293.

PR Close angular#53319
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.

Control flow schematic: @for (document of <collection>; track document) results in template not compiling
3 participants