Skip to content

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

Merged
merged 6 commits into from
Oct 31, 2015

Conversation

DanielRosenwasser
Copy link
Member

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.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 30, 2015

@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.
Copy link
Contributor

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.

@rbuckton
Copy link
Contributor

Other than the const comment, 👍

let rhsReference = createSynthesizedNode(SyntaxKind.Identifier) as Identifier;
rhsReference.text = node.expression.kind === SyntaxKind.Identifier ?
makeUniqueName((<Identifier>node.expression).text) :
makeTempVariableName(TempFlags.Auto);
Copy link
Contributor

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?

Copy link
Member Author

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

@yuit
Copy link
Contributor

yuit commented Oct 30, 2015

otherwise lgtm

DanielRosenwasser added a commit that referenced this pull request Oct 31, 2015
Always create a temporary for iterated expressions in a for-of loop
@DanielRosenwasser DanielRosenwasser merged commit 5cbcafa into master Oct 31, 2015
@DanielRosenwasser DanielRosenwasser deleted the mutatedArrayInForOf branch October 31, 2015 00:42
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants