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

fix(forEach): fix a temporal dead zone issue in forEach. #2474

Merged
merged 1 commit into from
Apr 3, 2017

Conversation

mprobst
Copy link
Contributor

@mprobst mprobst commented Mar 17, 2017

According to the ES6 spec and the implementation in V8, accessing a lexically scoped construct like const or let before it is assigned is a ReferenceError. Unlike var, it is not implicitly undefined. Because the closure handed to subscribe is immediately invoked and accesses subscription before it is assigned, this causes a ReferenceError in compliant engines.

The error is only triggered when running in plain ES6, i.e. without transpilation.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.0003%) to 97.689% when pulling 7ea87ed on mprobst:fix-tdz-error into e2696db on ReactiveX:master.

const subscription = this.subscribe((value) => {
// Must be explicitly assigned to avoid RefernceError when accessing
// subscription below in the closure due to Temporal Dead Zone.
let subscription: Subscription = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

let subscription: Subscription;

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.

According to the ES6 spec and the implementation in V8, accessing a
lexically scoped construct like const or let its declaration has
finished is a ReferenceError. Unlike var, it is not implicitly undefined
in the entire function scope. Because the closure handed to subscribe is
immediately invoked and accesses `subscription` before it is assigned,
this causes a ReferenceError in compliant engines.

The error is only triggered when running in plain ES6, i.e. without
transpilation.
@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.689% when pulling 6ebb034 on mprobst:fix-tdz-error into e2696db on ReactiveX:master.

@kwonoj
Copy link
Member

kwonoj commented Mar 17, 2017

Curiousity question: how previous code can be executed without transpilation to hit those errorneus cases?

@mprobst
Copy link
Contributor Author

mprobst commented Mar 17, 2017

@kwonoj if you run the code in an ES6 VM, it'll fail. But you can repro this much more easily locally:

> function call(c) { return c(); }
undefined
> function undef() { const foo = call(() => foo ? 1 : 2); }
undefined
> undef()
ReferenceError: foo is not defined
    at call (repl:1:55)
    at call (repl:1:38)
    at undef (repl:1:44)
    at repl:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:537:10)
    at emitOne (events.js:101:20)
> function undef() { let foo; foo = call(() => foo ? 1 : 2); }
undefined
> undef()
undefined

@kwonoj
Copy link
Member

kwonoj commented Mar 17, 2017

Ah, question was since code is TS anyway, it should be transpiled via compiler so curious how did original could be executed. Just curiousity question though.

@mprobst
Copy link
Contributor Author

mprobst commented Mar 17, 2017 via email

@kwonoj
Copy link
Member

kwonoj commented Mar 17, 2017

Oh thought compiler does something like hoisting automatically. Thx for clarification.

@mprobst
Copy link
Contributor Author

mprobst commented Mar 20, 2017

@kwonoj can you merge? I don't have write access.

@kwonoj
Copy link
Member

kwonoj commented Mar 20, 2017

No, I intentionally left this to get second eye and release planning. /cc @benlesh

@benlesh benlesh merged commit e9e9801 into ReactiveX:master Apr 3, 2017
@benlesh
Copy link
Member

benlesh commented Apr 3, 2017

thanks @mprobst!

@lock
Copy link

lock bot commented Jun 6, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 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