10000 support block scoped vars captured in closures inside loops by vladima · Pull Request #5208 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

support block scoped vars captured in closures inside loops #5208

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 5 commits into from
Oct 26, 2015

Conversation

vladima
Copy link
Contributor
@vladima vladima commented Oct 11, 2015

Fixes #3915

@DanielRosenwasser
Copy link
Member

I noticed that there isn't a flag to enable this. We should consider having a flag to warn when block-scoped variables are captured, or create a TSLint rule for it that we can use ourselves.

@vladima
Copy link
Contributor Author
vladima commented Oct 11, 2015

Don't think we should hide it under the flag:

  • this is a missing bit in our ES6 compliance story that we deliberately decided to punt before.
  • this is not a breaking change, code that used to compile before will continue to compile.

Adding TSLint rule for it sounds reasonable

@vladima
Copy link
Contributor Author
vladima commented Oct 16, 2015

pinging @mhegazy: can you have a look at this PR when you have time?


interface ConvertedLoopState {
// set of labels that occured inside the converted loop
// used to determine if labeled jump can be emitted as is or it should be dispatched to calling code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use JSDoc for these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@DanielRosenwasser
Copy link
Member

I don't see an example of arguments being used both inside and outside of the loop. Can you point it out to me or add one?

convertedLoopState = {};
if (convertedOuterLoopState) { // convertedOuterLoopState !== undefined means that this converted loop is nested in another converted loop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this line should have a period at the end of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@DanielRosenwasser
Copy link
Member

Can you add a test for this capturing?

class C {
    constructor(private N: number) { }
    foo() {
        for (let i = 0; i < 100; i++) {
            let f = () => this.N * i;
        }
    }
}

@vladima
Copy link
Contributor Author
vladima commented Oct 19, 2015

@DanielRosenwasser ok

@vladima
Copy link
Contributor Author
vladima commented Oct 19, 2015

@DanielRosenwasser any other comments?

emit(node.label);
write(": ");
}

function emitLabelledStatement(node: LabeledStatement) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, thanks

@mhegazy
Copy link
Contributor
mhegazy commented Oct 26, 2015

👍

vladima added a commit that referenced this pull request Oct 26, 2015
support block scoped vars captured in closures inside loops
@vladima vladima merged commit 4dbd04c into master Oct 26, 2015
@vladima vladima deleted the capturedBlockScopedVars branch October 26, 2015 23:58
@nycdotnet
Copy link

This is awesome!!!

@Nemikolh
Copy link

With version typescript@1.8.0-dev.20151121 this example:

class C {
  constructor(private N: number) {}
  foo() {
    for (let i of [0]) {
      let f = () => i;
      this.N = f();
    }
  }
}

is not correctly transformed. (The _this binding is not created)

@DanielRosenwasser
Copy link
Member

Thanks for the heads up @Nemikolh. I filed #5746 on your behalf.

@Nemikolh
Copy link

Glad to help! :)

@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.

7 participants
0