-
Notifications
You must be signed in to change notification settings - Fork 640
Add {}/class-expression expandos #885
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
Open
sandersn
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
sandersn:add-more-expandos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ const ( | |
ContainerFlagsHasLocals ContainerFlags = 1 << 5 | ||
ContainerFlagsIsInterface ContainerFlags = 1 << 6 | ||
ContainerFlagsIsObjectLiteralOrClassExpressionMethodOrAccessor ContainerFlags = 1 << 7 | ||
ContainerFlagsIsThisContainer ContainerFlags = 1 << 8 | ||
) | ||
|
||
type Binder struct { | ||
|
@@ -48,7 +49,7 @@ type Binder struct { | |
|
||
parent *ast.Node | ||
container *ast.Node | ||
thisParentContainer *ast.Node | ||
thisContainer *ast.Node | ||
blockScopeContainer *ast.Node | ||
lastContainer *ast.Node | ||
currentFlow *ast.FlowNode | ||
|
@@ -626,7 +627,7 @@ func (b *Binder) bind(node *ast.Node) bool { | |
case ast.KindBinaryExpression: | ||
switch ast.GetAssignmentDeclarationKind(node.AsBinaryExpression()) { | ||
case ast.JSDeclarationKindProperty: | ||
b.bindFunctionPropertyAssignment(node) | ||
b.bindExpandoPropertyAssignment(node) | ||
case ast.JSDeclarationKindThisProperty: | ||
b.bindThisPropertyAssignment(node) | ||
} | ||
|
@@ -1012,74 +1013,104 @@ func addLateBoundAssignmentDeclarationToSymbol(node *ast.Node, symbol *ast.Symbo | |
symbol.AssignmentDeclarationMembers.Add(node) | ||
} | ||
|
||
func (b *Binder) bindFunctionPropertyAssignment(node *ast.Node) { | ||
func (b *Binder) bindExpandoPropertyAssignment(node *ast.Node) { | ||
expr := node.AsBinaryExpression() | ||
parentName := expr.Left.Expression().Text() | ||
symbol := b.lookupName(parentName, b.blockScopeContainer) | ||
parent := expr.Left.Expression() | ||
symbol := b.lookupEntity(parent, b.blockScopeContainer) | ||
if symbol == nil { | ||
symbol = b.lookupName(parentName, b.container) | ||
symbol = b.lookupEntity(parent, b.container) | ||
} | ||
if symbol != nil && symbol.ValueDeclaration != nil { | ||
// For an assignment 'fn.xxx = ...', where 'fn' is a previously declared function or a previously | ||
// declared const variable initialized with a function expression or arrow function, we add expando | ||
// property declarations to the function's symbol. | ||
var funcSymbol *ast.Symbol | ||
switch { | ||
case ast.IsFunctionDeclaration(symbol.ValueDeclaration): | ||
funcSymbol = symbol | ||
case ast.IsVariableDeclaration(symbol.ValueDeclaration) && symbol.ValueDeclaration.Parent.Flags&ast.NodeFlagsConst != 0: | ||
initializer := symbol.ValueDeclaration.Initializer() | ||
if initializer != nil && ast.IsFunctionExpressionOrArrowFunction(initializer) { | ||
funcSymbol = initializer.Symbol() | ||
} | ||
if symbol = getInitializerSymbol(symbol); symbol != nil { | ||
// Fix up parent pointers since we're going to use these nodes before we bind into them | ||
setParent(expr.Left, node) | ||
setParent(expr.Right, node) | ||
if ast.HasDynamicName(node) { | ||
b.bindAnonymousDeclaration(node, ast.SymbolFlagsProperty|ast.SymbolFlagsAssignment, ast.InternalSymbolNameComputed) | ||
addLateBoundAssignmentDeclarationToSymbol(node, symbol) | ||
} else { | ||
b.declareSymbol(ast.GetExports(symbol), symbol, node, ast.SymbolFlagsProperty|ast.SymbolFlagsAssignment, ast.SymbolFlagsPropertyExcludes) | ||
} | ||
if funcSymbol != nil { | ||
// Fix up parent pointers since we're going to use these nodes before we bind into them | ||
setParent(expr.Left, node) | ||
setParent(expr.Right, node) | ||
if ast.HasDynamicName(node) { | ||
b.bindAnonymousDeclaration(node, ast.SymbolFlagsProperty|ast.SymbolFlagsAssignment, ast.InternalSymbolNameComputed) | ||
addLateBoundAssignmentDeclarationToSymbol(node, funcSymbol) | ||
} else { | ||
b.declareSymbol(ast.GetExports(funcSymbol), funcSymbol, node, ast.SymbolFlagsProperty|ast.SymbolFlagsAssignment, ast.SymbolFlagsPropertyExcludes) | ||
} | ||
} | ||
} | ||
|
||
func getInitializerSymbol(symbol *ast.Symbol) *ast.Symbol { | ||
if symbol == nil || symbol.ValueDeclaration == nil { | ||
return nil | ||
} | ||
declaration := symbol.ValueDeclaration | ||
// For an assignment 'fn.xxx = ...', where 'fn' is a previously declared function or a previously | ||
// declared const variable initialized with a function expression or arrow function, we add expando | ||
// property declarations to the function's symbol. | ||
// This also applies to class expressions and empty object literals. | ||
switch { | ||
case ast.IsFunctionDeclaration(declaration) || ast.IsInJSFile(declaration) && ast.IsClassDeclaration(declaration): | ||
return symbol | ||
case ast.IsVariableDeclaration(declaration) && | ||
(declaration.Parent.Flags&ast.NodeFlagsConst != 0 || ast.IsInJSFile(declaration)): | ||
initializer := declaration.Initializer() | ||
if isExpandoInitializer(initializer) { | ||
return initializer.Symbol() | ||
} | ||
case ast.IsBinaryExpression(declaration) && ast.IsInJSFile(declaration): | ||
initializer := declaration.AsBinaryExpression().Right | ||
if isExpandoInitializer(initializer) { | ||
return initializer.Symbol() | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
sandersn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func isExpandoInitializer(initializer *ast.Node) bool { | ||
if initializer == nil { | ||
return false | ||
} | ||
if ast.IsFunctionExpressionOrArrowFunction(initializer) { | ||
return true | ||
} else if ast.IsInJSFile(initializer) { | ||
return ast.IsClassExpression(initializer) || (ast.IsObjectLiteralExpression(initializer) && len(initializer.AsObjectLiteralExpression().Properties.Nodes) == 0) | ||
} | ||
return false | ||
} | ||
|
||
func (b *Binder) bindThisPropertyAssignment(node *ast.Node) { | ||
if !ast.IsInJSFile(node) { | ||
return | ||
} | ||
bin := node.AsBinaryExpression() | ||
if ast.IsPropertyAccessExpression(bin.Left) && ast.IsPrivateIdentifier(bin.Left.AsPropertyAccessExpression().Name()) { | ||
if ast.IsPropertyAccessExpression(bin.Left) && ast.IsPrivateIdentifier(bin.Left.AsPropertyAccessExpression().Name()) || | ||
b.thisContainer == nil { | ||
return | ||
} | ||
thisContainer := ast.GetThisContainer(node /*includeArrowFunctions*/, false /*includeClassComputedPropertyName*/, false) | ||
switch thisContainer.Kind { | ||
if classSymbol, symbolTable := b.getThisClassAndSymbolTable(); symbolTable != nil { | ||
if ast.HasDynamicName(node) { | ||
b.declareSymbolEx(symbolTable, classSymbol, node, ast.SymbolFlagsProperty, ast.SymbolFlagsNone, true /*isReplaceableByMethod*/, true /*isComputedName*/) | ||
addLateBoundAssignmentDeclarationToSymbol(node, classSymbol) | ||
} else { | ||
b.declareSymbolEx(symbolTable, classSymbol, node, ast.SymbolFlagsProperty|ast.SymbolFlagsAssignment, ast.SymbolFlagsNone, true /*isReplaceableByMethod*/, false /*isComputedName*/) | ||
} | ||
} else if b.thisContainer.Kind != ast.KindFunctionDeclaration && b.thisContainer.Kind != ast.KindFunctionExpression { | ||
// !!! constructor functions | ||
panic("Unhandled case in bindThisPropertyAssignment: " + b.thisContainer.Kind.String()) | ||
} | ||
} | ||
|
||
func (b *Binder) getThisClassAndSymbolTable() (classSymbol *ast.Symbol, symbolTable ast.SymbolTable) { | ||
if b.thisContainer == nil { | ||
return nil, nil | ||
} | ||
switch b.thisContainer.Kind { | ||
case ast.KindFunctionDeclaration, ast.KindFunctionExpression: | ||
// !!! constructor functions | ||
case ast.KindConstructor, ast.KindPropertyDeclaration, ast.KindMethodDeclaration, ast.KindGetAccessor, ast.KindSetAccessor, ast.KindClassStaticBlockDeclaration: | ||
// this.property assignment in class member -- bind to the containing class | ||
containingClass := thisContainer.Parent | ||
classSymbol := containingClass.Symbol() | ||
var symbolTable ast.SymbolTable | ||
if ast.IsStatic(thisContainer) { | ||
symbolTable = ast.GetExports(containingClass.Symbol()) | ||
classSymbol = b.thisContainer.Parent.Symbol() | ||
if ast.IsStatic(b.thisContainer) { | ||
symbolTable = ast.GetExports(classSymbol) | ||
} else { | ||
symbolTable = ast.GetMembers(containingClass.Symbol()) | ||
symbolTable = ast.GetMembers(classSymbol) | ||
} | ||
if ast.HasDynamicName(node) { | ||
b.declareSymbolEx(symbolTable, containingClass.Symbol(), node, ast.SymbolFlagsProperty, ast.SymbolFlagsNone, true /*isReplaceableByMethod*/, true /*isComputedName*/) | ||
addLateBoundAssignmentDeclarationToSymbol(node, classSymbol) | ||
} else { | ||
b.declareSymbolEx(symbolTable, containingClass.Symbol(), node, ast.SymbolFlagsProperty|ast.SymbolFlagsAssignment, ast.SymbolFlagsNone, true /*isReplaceableByMethod*/, false /*isComputedName*/) | ||
} | ||
case ast.KindSourceFile, ast.KindModuleDeclaration: | ||
// top-level this.property as assignment to globals is no longer supported | ||
default: | ||
panic("Unhandled case in bindThisPropertyAssignment: " + thisContainer.Kind.String()) | ||
} | ||
return classSymbol, symbolTable | ||
} | ||
|
||
func (b *Binder) bindEnumDeclaration(node *ast.Node) { | ||
|
@@ -1211,22 +1242,39 @@ func (b *Binder) bindTypeParameter(node *ast.Node) { | |
} | ||
} | ||
|
||
func (b *Binder) lookupEntity(node *ast.Node, container *ast.Node) *ast.Symbol { | ||
if ast.IsIdentifier(node) { | ||
return b.lookupName(node.AsIdentifier().Text, container) | ||
} | ||
if ast.IsPropertyAccessExpression(node) && node.AsPropertyAccessExpression().Expression.Kind == ast.KindThisKeyword || | ||
ast.IsElementAccessExpression(node) && node.AsElementAccessExpression().Expression.Kind == ast.KindThisKeyword { | ||
if _, symbolTable := b.getThisClassAndSymbolTable(); symbolTable != nil { | ||
if name := ast.GetElementOrPropertyAccessName(node); name != nil { | ||
return symbolTable[name.Text()] | ||
} | ||
} | ||
return nil | ||
} | ||
if symbol := getInitializerSymbol(b.lookupEntity(node.Expression(), container)); symbol != nil && symbol.Exports != nil { | ||
if name := ast.GetElementOrPropertyAccessName(node); name != nil { | ||
return symbol.Exports[name.Text()] | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (b *Binder) lookupName(name string, container *ast.Node) *ast.Symbol { | ||
localsContainer := container.LocalsContainerData() | ||
if localsContainer != nil { | ||
local := localsContainer.Locals[name] | ||
if local != nil { | ||
if localsContainer := container.LocalsContainerData(); localsContainer != nil { | ||
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. Just a style improvement--at least I hope it's an improvement. |
||
if local := localsContainer.Locals[name]; local != nil { | ||
return core.OrElse(local.ExportSymbol, local) | ||
} | ||
} | ||
if ast.IsSourceFile(container) { | ||
local := container.AsSourceFile().JSGlobalAugmentations[name] | ||
if local != nil { | ||
if local := container.AsSourceFile().JSGlobalAugmentations[name]; local != nil { | ||
return local | ||
} | ||
} | ||
declaration := container.DeclarationData() | ||
if declaration != nil && declaration.Symbol != nil { | ||
if declaration := container.DeclarationData(); declaration != nil && declaration.Symbol != nil { | ||
return declaration.Symbol.Exports[name] | ||
} | ||
return nil | ||
|
@@ -1438,7 +1486,7 @@ func (b *Binder) bindContainer(node *ast.Node, containerFlags ContainerFlags) { | |
// and block-container. Then after we pop out of processing the children, we restore | ||
// these saved values. | ||
saveContainer := b.container | ||
saveThisParentContainer := b.thisParentContainer | ||
saveThisContainer := b.thisContainer | ||
savedBlockScopeContainer := b.blockScopeContainer | ||
// Depending on what kind of node this is, we may have to adjust the current container | ||
// and block-container. If the current node is a container, then it is automatically | ||
|
@@ -1458,9 +1506,6 @@ func (b *Binder) bindContainer(node *ast.Node, containerFlags ContainerFlags) { | |
// for it. We must clear this so we don't accidentally move any stale data forward from | ||
// a previous compilation. | ||
if containerFlags&ContainerFlagsIsContainer != 0 { | ||
if node.Kind != ast.KindArrowFunction { | ||
b.thisParentContainer = b.container | ||
} | ||
b.container = node | ||
b.blockScopeContainer = node | ||
if containerFlags&ContainerFlagsHasLocals != 0 { | ||
|
@@ -1472,6 +1517,9 @@ func (b *Binder) bindContainer(node *ast.Node, containerFlags ContainerFlags) { | |
b.blockScopeContainer = node | ||
b.addToContainerChain(node) | ||
} | ||
if containerFlags&ContainerFlagsIsThisContainer != 0 { | ||
b.thisContainer = node | ||
} | ||
if containerFlags&ContainerFlagsIsControlFlowContainer != 0 { | ||
saveCurrentFlow := b.currentFlow | ||
saveBreakTarget := b.currentBreakTarget | ||
|
@@ -1552,7 +1600,7 @@ func (b *Binder) bindContainer(node *ast.Node, containerFlags ContainerFlags) { | |
b.bindChildren(node) | ||
} | ||
b.container = saveContainer | ||
b.thisParentContainer = saveThisParentContainer | ||
b.thisContainer = saveThisContainer | ||
b.blockScopeContainer = savedBlockScopeContainer | ||
} | ||
|
||
|
@@ -2572,19 +2620,24 @@ func GetContainerFlags(node *ast.Node) ContainerFlags { | |
return ContainerFlagsIsContainer | ContainerFlagsIsControlFlowContainer | ContainerFlagsHasLocals | ||
case ast.KindGetAccessor, ast.KindSetAccessor, ast.KindMethodDeclaration: | ||
if ast.IsObjectLiteralOrClassExpressionMethodOrAccessor(node) { | ||
return ContainerFlagsIsContainer | ContainerFlagsIsControlFlowContainer | ContainerFlagsHasLocals | ContainerFlagsIsFunctionLike | ContainerFlagsIsObjectLiteralOrClassExpressionMethodOrAccessor | ||
return ContainerFlagsIsContainer | ContainerFlagsIsControlFlowContainer | ContainerFlagsHasLocals | ContainerFlagsIsFunctionLike | ContainerFlagsIsObjectLiteralOrClassExpressionMethodOrAccessor | ContainerFlagsIsThisContainer | ||
} | ||
fallthrough | ||
case ast.KindConstructor, ast.KindFunctionDeclaration, ast.KindMethodSignature, ast.KindCallSignature, ast.KindJSDocSignature, | ||
ast.KindFunctionType, ast.KindConstructSignature, ast.KindConstructorType, ast.KindClassStaticBlockDeclaration: | ||
case ast.KindConstructor, ast.KindClassStaticBlockDeclaration: | ||
return ContainerFlagsIsContainer | ContainerFlagsIsControlFlowContainer | ContainerFlagsHasLocals | ContainerFlagsIsFunctionLike | ContainerFlagsIsThisContainer | ||
case ast.KindMethodSignature, ast.KindCallSignature, ast.KindJSDocSignature, ast.KindFunctionType, ast.KindConstructSignature, ast.KindConstructorType: | ||
return ContainerFlagsIsContainer | ContainerFlagsIsControlFlowContainer | ContainerFlagsHasLocals | ContainerFlagsIsFunctionLike | ||
case ast.KindFunctionExpression, ast.KindArrowFunction: | ||
case ast.KindFunctionDeclaration: | ||
return ContainerFlagsIsContainer | ContainerFlagsIsControlFlowContainer | ContainerFlagsHasLocals | ContainerFlagsIsFunctionLike | ContainerFlagsIsThisContainer | ||
case ast.KindFunctionExpression: | ||
return ContainerFlagsIsContainer | ContainerFlagsIsControlFlowContainer | ContainerFlagsHasLocals | ContainerFlagsIsFunctionLike | ContainerFlagsIsFunctionExpression | ContainerFlagsIsThisContainer | ||
case ast.KindArrowFunction: | ||
return ContainerFlagsIsContainer | ContainerFlagsIsControlFlowContainer | ContainerFlagsHasLocals | ContainerFlagsIsFunctionLike | ContainerFlagsIsFunctionExpression | ||
case ast.KindModuleBlock: | ||
return ContainerFlagsIsControlFlowContainer | ||
case ast.KindPropertyDeclaration: | ||
if node.AsPropertyDeclaration().Initializer != nil { | ||
return ContainerFlagsIsControlFlowContainer | ||
return ContainerFlagsIsControlFlowContainer | ContainerFlagsIsThisContainer | ||
} else { | ||
return ContainerFlagsNone | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.