-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
JavaScript output has variable below return #4138
Comments
It doesn't matter where the variable is placed; it just needs to be declared so that we can use it as a temporary value. |
I realised, but it didn't look by design and I didn't see a previous issue logged. I actually found Istanbul has issues with code coverage with this, though I've logged an issue there too. |
I suspect it was because it wasn't supported before when targeting ES5, so it wasn't a "break" of previous behaviour, because there was no previous behaviour. Declaring things after the return though doesn't seem like clean idiomatic JavaScript though. |
Seems reasonable to just put the var before the return isn't it? The objections here (not very idiomatic, plays poorly with tools like code coverage and linters checking for unreachable code) seem compelling enough. Obviously a minor issue but there's not really a good reason to not fix it (eventually) right? |
This is not something that can be easily changed. the way the emitter works today is by doing transformations on the fly. transformations are done as we descend into the tree. at the time we are visiting a destructuring pattern we have emitted previous statements. moreover, destructuring can be in an expression position. so to emit the variables first we need to do a pre-walk to find destructuring assignments, somehow predict the variables needed, emit them, then as we are doing the destructuring use them. I would say leave this issue open. we value the cleanliness of the emitted code, and there is an oprtnity to make it cleaner here. however the real fix would be to split the emitter to a transformation phase followed by a writing phase. we are looking into this now. |
closing for now. marking as revisit. This should be a simple change once #5595 is in. |
Using latest commit (4c7b214) and compiling the code below results in the variable initialization below
return
.The variable is always below the return, even when the statement is assigned to a variable instead of returning.
Edit:
node node_modules/.bin/tsc test.ts --target es5
The text was updated successfully, but these errors were encountered: