-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix using transform variable shadowing in for-of loops #61934
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
base: main
Are you sure you want to change the base?
Changes from all commits
34e17b3
237579e
6fead46
2738f69
8f3df02
0b3bf33
957cf79
88c9bfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,67 +1,67 @@ | ||||||||||||
import { | ||||||||||||
addEmitHelpers, | ||||||||||||
addRange, | ||||||||||||
append, | ||||||||||||
arrayFrom, | ||||||||||||
BindingElement, | ||||||||||||
Block, | ||||||||||||
Bundle, | ||||||||||||
CaseOrDefaultClause, | ||||||||||||
chainBundle, | ||||||||||||
ClassDeclaration, | ||||||||||||
Debug, | ||||||||||||
EmitFlags, | ||||||||||||
ExportAssignment, | ||||||||||||
ExportSpecifier, | ||||||||||||
Expression, | ||||||||||||
firstOrUndefined, | ||||||||||||
ForOfStatement, | ||||||||||||
ForStatement, | ||||||||||||
GeneratedIdentifierFlags, | ||||||||||||
getEmitFlags, | ||||||||||||
hasSyntacticModifier, | ||||||||||||
Identifier, | ||||||||||||
IdentifierNameMap, | ||||||||||||
isArray, | ||||||||||||
isBindingPattern, | ||||||||||||
isBlock, | ||||||||||||
isCaseClause, | ||||||||||||
isCustomPrologue, | ||||||||||||
isExpression, | ||||||||||||
isGeneratedIdentifier, | ||||||||||||
isIdentifier, | ||||||||||||
isLocalName, | ||||||||||||
isNamedEvaluation, | ||||||||||||
isOmittedExpression, | ||||||||||||
isPrologueDirective, | ||||||||||||
isSourceFile, | ||||||||||||
isStatement, | ||||||||||||
isVariableDeclarationList, | ||||||||||||
isVariableStatement, | ||||||||||||
ModifierFlags, | ||||||||||||
Node, | ||||||||||||
NodeFlags, | ||||||||||||
setCommentRange, | ||||||||||||
setEmitFlags, | ||||||||||||
setOriginalNode, | ||||||||||||
setSourceMapRange, | ||||||||||||
setTextRange, | ||||||||||||
skipOuterExpressions, | ||||||||||||
SourceFile, | ||||||||||||
Statement, | ||||||||||||
SwitchStatement, | ||||||||||||
SyntaxKind, | ||||||||||||
TransformationContext, | ||||||||||||
TransformFlags, | ||||||||||||
transformNamedEvaluation, | ||||||||||||
VariableDeclaration, | ||||||||||||
VariableDeclarationList, | ||||||||||||
VariableStatement, | ||||||||||||
visitArray, | ||||||||||||
visitEachChild, | ||||||||||||
visitNode, | ||||||||||||
visitNodes, | ||||||||||||
VisitResult, | ||||||||||||
import { | ||||||||||||
addEmitHelpers, | ||||||||||||
addRange, | ||||||||||||
append, | ||||||||||||
arrayFrom, | ||||||||||||
BindingElement, | ||||||||||||
Block, | ||||||||||||
Bundle, | ||||||||||||
CaseOrDefaultClause, | ||||||||||||
chainBundle, | ||||||||||||
ClassDeclaration, | ||||||||||||
Debug, | ||||||||||||
EmitFlags, | ||||||||||||
ExportAssignment, | ||||||||||||
ExportSpecifier, | ||||||||||||
Expression, | ||||||||||||
firstOrUndefined, | ||||||||||||
ForOfStatement, | ||||||||||||
ForStatement, | ||||||||||||
GeneratedIdentifierFlags, | ||||||||||||
getEmitFlags, | ||||||||||||
hasSyntacticModifier, | ||||||||||||
Identifier, | ||||||||||||
IdentifierNameMap, | ||||||||||||
isArray, | ||||||||||||
isBindingPattern, | ||||||||||||
isBlock, | ||||||||||||
isCaseClause, | ||||||||||||
isCustomPrologue, | ||||||||||||
isExpression, | ||||||||||||
isGeneratedIdentifier, | ||||||||||||
isIdentifier, | ||||||||||||
isLocalName, | ||||||||||||
isNamedEvaluation, | ||||||||||||
isOmittedExpression, | ||||||||||||
isPrologueDirective, | ||||||||||||
isSourceFile, | ||||||||||||
isStatement, | ||||||||||||
isVariableDeclarationList, | ||||||||||||
isVariableStatement, | ||||||||||||
ModifierFlags, | ||||||||||||
Node, | ||||||||||||
NodeFlags, | ||||||||||||
setCommentRange, | ||||||||||||
setEmitFlags, | ||||||||||||
setOriginalNode, | ||||||||||||
setSourceMapRange, | ||||||||||||
setTextRange, | ||||||||||||
skipOuterExpressions, | ||||||||||||
SourceFile, | ||||||||||||
Statement, | ||||||||||||
SwitchStatement, | ||||||||||||
SyntaxKind, | ||||||||||||
TransformationContext, | ||||||||||||
TransformFlags, | ||||||||||||
transformNamedEvaluation, | ||||||||||||
VariableDeclaration, | ||||||||||||
VariableDeclarationList, | ||||||||||||
VariableStatement, | ||||||||||||
visitArray, | ||||||||||||
visitEachChild, | ||||||||||||
visitNode, | ||||||||||||
visitNodes, | ||||||||||||
VisitResult, | ||||||||||||
} from "../_namespaces/ts.js"; | ||||||||||||
|
||||||||||||
const enum UsingKind { | ||||||||||||
|
@@ -289,54 +289,64 @@ export function transformESNext(context: TransformationContext): (x: SourceFile | |||||||||||
); | ||||||||||||
} | ||||||||||||
|
||||||||||||
return visitEachChild(node, visitor, context); | ||||||||||||
} | ||||||||||||
|
||||||||||||
function visitForOfStatement(node: ForOfStatement) { | ||||||||||||
if (isUsingVariableDeclarationList(node.initializer)) { | ||||||||||||
// given: | ||||||||||||
// | ||||||||||||
// for (using x of y) { ... } | ||||||||||||
// | ||||||||||||
// produces a shallow transformation to: | ||||||||||||
// | ||||||||||||
// for (const x_1 of y) { | ||||||||||||
// using x = x; | ||||||||||||
// ... | ||||||||||||
// } | ||||||||||||
// | ||||||||||||
// before handing the shallow transformation back to the visitor for an in-depth transformation. | ||||||||||||
const forInitializer = node.initializer; | ||||||||||||
const forDecl = firstOrUndefined(forInitializer.declarations) || factory.createVariableDeclaration(factory.createTempVariable(/*recordTempVariable*/ undefined)); | ||||||||||||
|
||||||||||||
const isAwaitUsing = getUsingKindOfVariableDeclarationList(forInitializer) === UsingKind.Async; | ||||||||||||
const temp = factory.getGeneratedNameForNode(forDecl.name); | ||||||||||||
const usingVar = factory.updateVariableDeclaration(forDecl, forDecl.name, /*exclamationToken*/ undefined, /*type*/ undefined, temp); | ||||||||||||
const usingVarList = factory.createVariableDeclarationList([usingVar], isAwaitUsing ? NodeFlags.AwaitUsing : NodeFlags.Using); | ||||||||||||
const usingVarStatement = factory.createVariableStatement(/*modifiers*/ undefined, usingVarList); | ||||||||||||
return visitNode( | ||||||||||||
factory.updateForOfStatement( | ||||||||||||
node, | ||||||||||||
node.awaitModifier, | ||||||||||||
factory.createVariableDeclarationList([ | ||||||||||||
factory.createVariableDeclaration(temp), | ||||||||||||
], NodeFlags.Const), | ||||||||||||
node.expression, | ||||||||||||
isBlock(node.statement) ? | ||||||||||||
factory.updateBlock(node.statement, [ | ||||||||||||
usingVarStatement, | ||||||||||||
...node.statement.statements, | ||||||||||||
]) : | ||||||||||||
factory.createBlock([ | ||||||||||||
usingVarStatement, | ||||||||||||
node.statement, | ||||||||||||
], /*multiLine*/ true), | ||||||||||||
), | ||||||||||||
visitor, | ||||||||||||
isStatement, | ||||||||||||
); | ||||||||||||
} | ||||||||||||
return visitEachChild(node, visitor, context); | ||||||||||||
return visitEachChild(node, visitor, context); | ||||||||||||
} | ||||||||||||
|
||||||||||||
function visitForOfStatement(node: ForOfStatement) { | ||||||||||||
if (isUsingVariableDeclarationList(node.initializer)) { | ||||||||||||
// given: | ||||||||||||
// | ||||||||||||
// for (using x of y) { ... } | ||||||||||||
// | ||||||||||||
// produces a shallow transformation to: | ||||||||||||
// | ||||||||||||
// for (const x_1 of y) { | ||||||||||||
// using x = x_1; | ||||||||||||
// { ... } | ||||||||||||
// } | ||||||||||||
// | ||||||||||||
// where the original loop body is wrapped in an additional block scope | ||||||||||||
// to handle shadowing variables naturally through block scoping. | ||||||||||||
const forInitializer = node.initializer; | ||||||||||||
const forDecl = firstOrUndefined(forInitializer.declarations) || factory.createVariableDeclaration(factory.createTempVariable(/*recordTempVariable*/ undefined)); | ||||||||||||
|
||||||||||||
const isAwaitUsing = getUsingKindOfVariableDeclarationList(forInitializer) === UsingKind.Async; | ||||||||||||
const temp = factory.getGeneratedNameForNode(forDecl.name); | ||||||||||||
const usingVar = factory.updateVariableDeclaration(forDecl, forDecl.name, /*exclamationToken*/ undefined, /*type*/ undefined, temp); | ||||||||||||
const usingVarList = factory.createVariableDeclarationList([usingVar], isAwaitUsing ? NodeFlags.AwaitUsing : NodeFlags.Using); | ||||||||||||
const usingVarStatement = factory.createVariableStatement(/*modifiers*/ undefined, usingVarList); | ||||||||||||
|
||||||||||||
// Wrap the original loop body in an additional block scope to handle shadowing | ||||||||||||
// Don't create an extra block if the original statement is empty or contains only empty statements | ||||||||||||
const isEmptyBlock = isBlock(node.statement) && ( | ||||||||||||
node.statement.statements.length === 0 || | ||||||||||||
node.statement.statements.every(stmt => stmt.kind === SyntaxKind.EmptyStatement) | ||||||||||||
); | ||||||||||||
const shouldWrapInBlock = !isEmptyBlock; | ||||||||||||
|
||||||||||||
const statements: Statement[] = [usingVarStatement]; | ||||||||||||
if (shouldWrapInBlock) { | ||||||||||||
const wrappedStatement = isBlock(node.statement) ? | ||||||||||||
node.statement : | ||||||||||||
factory.createBlock([node.statement], /*multiLine*/ true); | ||||||||||||
statements.push(wrappedStatement); | ||||||||||||
} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
return visitNode( | ||||||||||||
factory.updateForOfStatement( | ||||||||||||
node, | ||||||||||||
node.awaitModifier, | ||||||||||||
factory.createVariableDeclarationList([ | ||||||||||||
factory.createVariableDeclaration(temp), | ||||||||||||
], NodeFlags.Const), | ||||||||||||
node.expression, | ||||||||||||
factory.createBlock(statements, /*multiLine*/ true), | ||||||||||||
), | ||||||||||||
visitor, | ||||||||||||
isStatement, | ||||||||||||
); | ||||||||||||
} | ||||||||||||
return visitEachChild(node, visitor, context); | ||||||||||||
} | ||||||||||||
|
||||||||||||
function visitCaseOrDefaultClause(node: CaseOrDefaultClause, envBinding: Identifier) { | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,7 +118,9 @@ try { | |
var env_1 = { stack: [], error: void 0, hasError: false }; | ||
try { | ||
var _e = __addDisposableResource(env_1, _e_1, true); | ||
; | ||
{ | ||
; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should consider a block with just an empty statement the same as an empty block and avoid emitting the new block. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in commit 957cf79. The implementation now considers blocks containing only empty statements as effectively empty and avoids generating the extra wrapping block in those cases. |
||
} | ||
} | ||
catch (e_2) { | ||
env_1.error = e_2; | ||
|
@@ -159,7 +161,9 @@ export function test() { | |
case 3: | ||
_f.trys.push([3, 4, 5, 8]); | ||
_b = __addDisposableResource(env_2, _b_1, true); | ||
; | ||
{ | ||
; | ||
} | ||
return [3 /*break*/, 8]; | ||
case 4: | ||
e_3 = _f.sent(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
main.ts(12,7): error TS2481: Cannot initialize outer scoped variable 'baz' in the same scope as block scoped declaration 'baz'. | ||
|
||
|
||
==== main.ts (1 errors) ==== | ||
class Foo {} | ||
|
||
for (using foo of []) { | ||
const foo = new Foo(); | ||
} | ||
|
||
for (using bar of []) { | ||
let bar = "test"; | ||
} | ||
|
||
for (using baz of []) { | ||
var baz = 42; | ||
~~~ | ||
!!! error TS2481: Cannot initialize outer scoped variable 'baz' in the same scope as block scoped declaration 'baz'. | ||
} | ||
|
||
==== tslib.d.ts (0 errors) ==== | ||
export declare function __addDisposableResource<T>(env: any, value: T, async: boolean): T; | ||
export declare function __disposeResources(env: any): void; | ||
|
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.
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.
Applied the suggested change to check for empty statements before empty blocks. The condition now includes
node.statement.kind === SyntaxKind.EmptyStatement ||
as suggested. Commit 688e38f44.