Skip to content

fix(compiler): correctly intercept index in loop tracking function#53604

Closed
crisbeto wants to merge 1 commit intoangular:mainfrom
crisbeto:53600/nested-track-by-index
Closed

fix(compiler): correctly intercept index in loop tracking function#53604
crisbeto wants to merge 1 commit intoangular:mainfrom
crisbeto:53600/nested-track-by-index

Conversation

@crisbeto
Copy link
Copy Markdown
Member

The for loop tracking function doesn't allow references to local template variables, aside from $index and the item which are passed in as parameters. We enforce this by rewriting all variable references to the components scope.

The problem is that the logic that rewrites the references first walks the view tree and then checks if the variable is $index or the item. This is problematic in nested for loops, because it'll find the $index of the parent.

These changes resolve the issue by checking for $index and the item first.

Fixes #53600.

The for loop tracking function doesn't allow references to local template variables, aside from `$index` and the item which are passed in as parameters. We enforce this by rewriting all variable references to the components scope.

The problem is that the logic that rewrites the references first walks the view tree and then checks if the variable is `$index` or the item. This is problematic in nested for loops, because it'll find the `$index` of the parent.

These changes resolve the issue by checking for `$index` and the item first.

Fixes angular#53600.
@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 16, 2023
@ngbot ngbot bot added this to the Backlog milestone Dec 16, 2023
@JoostK JoostK 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 16, 2023
@jessicajaniuk
Copy link
Copy Markdown
Contributor

This PR was merged into the repository by commit 3a689c2.

jessicajaniuk pushed a commit that referenced this pull request Dec 18, 2023
…53604)

The for loop tracking function doesn't allow references to local template variables, aside from `$index` and the item which are passed in as parameters. We enforce this by rewriting all variable references to the components scope.

The problem is that the logic that rewrites the references first walks the view tree and then checks if the variable is `$index` or the item. This is problematic in nested for loops, because it'll find the `$index` of the parent.

These changes resolve the issue by checking for `$index` and the item first.

Fixes #53600.

PR Close #53604
@peiran-sun
Copy link
Copy Markdown

/**
* Evaluate a piece of JIT generated code.
* @param sourceUrl The URL of this generated code.
* @param ctx A context object that contains an AST of the code to be evaluated.
* @param vars A map containing the names and values of variables that the evaluated code might
* reference.
* @param createSourceMap If true then create a source-map for the generated code and include it
* inline as a source-map comment.
* @returns The result of evaluating the code.
*/
evaluateCode(sourceUrl, ctx, vars, createSourceMap) {
let fnBody = "use strict";${ctx.toSource()}\n//# sourceURL=${sourceUrl};
const fnArgNames = [];
const fnArgValues = [];
for (const argName in vars) {
fnArgValues.push(vars[argName]);
fnArgNames.push(argName);
}
if (createSourceMap) {
// using new Function(...) generates a header, 1 line of no arguments, 2 lines otherwise
// E.g. // function anonymous(a,b,c // /**/) { ... }
// We don't want to hard code this fact, so we auto detect it via an empty function first.
const emptyFn = newTrustedFunctionForJIT(...fnArgNames.concat('return null;')).toString();
const headerLines = emptyFn.slice(0, emptyFn.indexOf('return null;')).split('\n').length - 1;
fnBody += \n${ctx.toSourceMapGenerator(sourceUrl, headerLines).toJsComment()};
}
const fn = newTrustedFunctionForJIT(...fnArgNames.concat(fnBody));
return this.executeFunction(fn, fnArgValues);
}

let fnBody = `"use strict";${ctx.toSource()}\n//# sourceURL=${sourceUrl}`;

The translation of your request from Chinese to English is: "Due to the presence of '\n', using native-federation fails. Please assist in fixing this bug."

source
dist

ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
…ngular#53604)

The for loop tracking function doesn't allow references to local template variables, aside from `$index` and the item which are passed in as parameters. We enforce this by rewriting all variable references to the components scope.

The problem is that the logic that rewrites the references first walks the view tree and then checks if the variable is `$index` or the item. This is problematic in nested for loops, because it'll find the `$index` of the parent.

These changes resolve the issue by checking for `$index` and the item first.

Fixes angular#53600.

PR Close angular#53604
rlmestre pushed a commit to rlmestre/angular that referenced this pull request Jan 26, 2024
…ngular#53604)

The for loop tracking function doesn't allow references to local template variables, aside from `$index` and the item which are passed in as parameters. We enforce this by rewriting all variable references to the components scope.

The problem is that the logic that rewrites the references first walks the view tree and then checks if the variable is `$index` or the item. This is problematic in nested for loops, because it'll find the `$index` of the parent.

These changes resolve the issue by checking for `$index` and the item first.

Fixes angular#53600.

PR Close angular#53604
amilamen pushed a commit to amilamen/angular that referenced this pull request Jan 26, 2024
…ngular#53604)

The for loop tracking function doesn't allow references to local template variables, aside from `$index` and the item which are passed in as parameters. We enforce this by rewriting all variable references to the components scope.

The problem is that the logic that rewrites the references first walks the view tree and then checks if the variable is `$index` or the item. This is problematic in nested for loops, because it'll find the `$index` of the parent.

These changes resolve the issue by checking for `$index` and the item first.

Fixes angular#53600.

PR Close angular#53604
@angular-automatic-lock-bot
Copy link
Copy Markdown

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 Feb 4, 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.

@ngfor throw error

4 participants