Skip to content

support block scoped vars captured in closures inside loops#5208

Merged
vladima merged 5 commits into
masterfrom
capturedBlockScopedVars
Oct 26, 2015
Merged

support block scoped vars captured in closures inside loops#5208
vladima merged 5 commits into
masterfrom
capturedBlockScopedVars

Conversation

@vladima

@vladima vladima commented Oct 11, 2015

Copy link
Copy Markdown
Contributor

Fixes #3915

@DanielRosenwasser

Copy link
Copy Markdown
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

vladima commented Oct 11, 2015

Copy link
Copy Markdown
Contributor Author

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

vladima commented Oct 16, 2015

Copy link
Copy Markdown
Contributor Author

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

Comment thread src/compiler/emitter.ts Outdated

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

Comment thread src/compiler/emitter.ts Outdated

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

vladima commented Oct 19, 2015

Copy link
Copy Markdown
Contributor Author

@DanielRosenwasser ok

@vladima

vladima commented Oct 19, 2015

Copy link
Copy Markdown
Contributor Author

@DanielRosenwasser any other comments?

Comment thread src/compiler/emitter.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Spelling

Copy link
Copy Markdown
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

mhegazy commented Oct 26, 2015

Copy link
Copy Markdown
Contributor

👍

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
Copy Markdown

This is awesome!!!

@Nemikolh

Copy link
Copy Markdown

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
Copy Markdown
Member

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

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