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

for-of with const emits wrong JS code #10220

Closed
jrieken opened this issue Aug 9, 2016 · 4 comments
Closed

for-of with const emits wrong JS code #10220

jrieken opened this issue Aug 9, 2016 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@jrieken
Copy link
Member

jrieken commented Aug 9, 2016

TypeScript Version: Playground

http://www.typescriptlang.org/play/#src=for%20(const%20a%20of%20%5B1%2C%202%2C%203%2C%204%5D)%20%7B%20console.log(a)%3B%20%7D

When using a for-of and a const loop variable the generated code still assigns the variable (a) which is counter const and different from nodejs running such code.

Code

// A *self-contained* demonstration of the problem follows...
for (const a of [1, 2, 3, 4]) { console.log(a); }

Expected behavior:

for (var _i = 0, _a = [1, 2, 3, 4]; _i < _a.length; _i++) {
    var a = _a[0];
    console.log(a);
}

Actual behavior:

for (var _i = 0, _a = [1, 2, 3, 4]; _i < _a.length; _i++) {
    var a = _a[_i];
    console.log(a);
}
@jrieken jrieken changed the title for-of with const emit wrong JS code for-of with const emits wrong JS code Aug 9, 2016
@jrieken
Copy link
Member Author

jrieken commented Aug 9, 2016

for reference run: node -e "for (const a of [1, 2, 3, 4]) { console.log(a); }" which prints

$ node -e "for (const a of [1, 2, 3, 4]) { console.log(a); }"
1
1
1
1

jrieken added a commit to microsoft/vscode that referenced this issue Aug 9, 2016
@weswigham
Copy link
Member

@jrieken That would be an error in the js runtime. AFAIK, a const binding in a ForOf declaration is supposed to receive a new binding each iteration.

Run that same code in the latest chrome (and thus recent v8):
image
And you get the expected results. We generate the correct code here.

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 9, 2016
@vladima
Copy link
Contributor

vladima commented Aug 9, 2016

for reference: for-of with const variable

@jrieken
Copy link
Member Author

jrieken commented Aug 10, 2016

wow - good to know

@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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants