-
Notifications
You must be signed in to change notification settings - Fork 13k
emit temporary vars at the top of the scope #23956
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
Conversation
is that my fault? |
src/compiler/transformers/esnext.ts
Outdated
@@ -677,8 +677,8 @@ namespace ts { | |||
const trailingStatements = endLexicalEnvironment(); | |||
if (statementOffset > 0 || some(statements) || some(trailingStatements)) { | |||
const block = convertToFunctionBody(body, /*multiLine*/ true); | |||
addRange(statements, block.statements.slice(statementOffset)); | |||
addRange(statements, trailingStatements); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the change in position, leadingStatements
would be a more appropriate term now.
why travis-ci fault again😂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kingwl this is a good change, but upon further review it seems like its not comprehensive. There are a number of other places where we are still adding hoisted declarations at the end of the scope rather than at the beginning.
If you search for all references to endLexicalEnvironment
you'll see a number of additional cases in:
- src/compiler/transformers/es2015.ts
- src/compiler/transformers/es2017.ts
- src/compiler/transformers/esnext.ts
- src/compiler/transformers/generators.ts
- src/compiler/transformers/module.ts
- src/compiler/transformers/system.ts
- src/compiler/transformers/ts.ts
Most (but not all) of these cases look something like this:
addRange(statements, endLexicalEnvironment());
To fully resolve these (and consider the linked issue closed), I would recommend adding the following function to src/compiler/core.ts (after the existing addRange
function):
export function prependRange<T>(to: T[], from: ReadonlyArray<T> | undefined) {
if (some(from)) to.unshift(...from);
}
Then I would replace these specific addRange
calls with prependRange
.
87903a9
to
b6d06ef
Compare
what |
Thank you for the contribution! |
Fixes #23732
looks a easy fix
but something need to consider: some temporary variable has swapped and name has changed