-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Always create a temporary for iterated expressions in a for-of loop #5477
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
Conversation
@rbuckton can you take a look. |
@@ -3614,10 +3614,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi | |||
// | |||
// for (let v of arr) { } | |||
// | |||
// we don't want to emit a temporary variable for the RHS, just use it directly. | |||
let rhsIsIdentifier = node.expression.kind === SyntaxKind.Identifier; | |||
// we can't reuse 'arr' because it might be modified within the body of the loop. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One possibility might be to check if the rhs is declared as const
, and then we can emit it inline.
Other than the |
let rhsReference = createSynthesizedNode(SyntaxKind.Identifier) as Identifier; | ||
rhsReference.text = node.expression.kind === SyntaxKind.Identifier ? | ||
makeUniqueName((<Identifier>node.expression).text) : | ||
makeTempVariableName(TempFlags.Auto); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add tests for this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have tests for that case
otherwise lgtm |
Always create a temporary for iterated expressions in a for-of loop
Fixes #5475. The only special handling we use now is that we create a name based on the original identifier if we had an identifier.