Skip to content

Commit 19ec922

Browse files
authored
Fix getAsyncAndAwaitOccurrences function handling (#1837)
1 parent 6b117d8 commit 19ec922

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

internal/ast/utilities.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3865,3 +3865,7 @@ func IsJSDocNameReferenceContext(node *Node) bool {
38653865
func IsImportOrImportEqualsDeclaration(node *Node) bool {
38663866
return IsImportDeclaration(node) || IsImportEqualsDeclaration(node)
38673867
}
3868+
3869+
func GetContainingFunction(node *Node) *Node {
3870+
return FindAncestor(node.Parent, IsFunctionLike)
3871+
}

internal/checker/checker.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,7 @@ func (c *Checker) checkParameter(node *ast.Node) {
24642464
// or if its FunctionBody is strict code(11.1.5).
24652465
c.checkGrammarModifiers(node)
24662466
c.checkVariableLikeDeclaration(node)
2467-
fn := getContainingFunction(node)
2467+
fn := ast.GetContainingFunction(node)
24682468
var paramName string
24692469
if node.Name() != nil && ast.IsIdentifier(node.Name()) {
24702470
paramName = node.Name().Text()
@@ -5522,7 +5522,7 @@ func (c *Checker) checkVariableLikeDeclaration(node *ast.Node) {
55225522
}
55235523
if ast.IsBindingElement(node) {
55245524
propName := node.PropertyName()
5525-
if propName != nil && ast.IsIdentifier(node.Name()) && ast.IsPartOfParameterDeclaration(node) && ast.NodeIsMissing(getContainingFunction(node).Body()) {
5525+
if propName != nil && ast.IsIdentifier(node.Name()) && ast.IsPartOfParameterDeclaration(node) && ast.NodeIsMissing(ast.GetContainingFunction(node).Body()) {
55265526
// type F = ({a: string}) => void;
55275527
// ^^^^^^
55285528
// variable renaming in function type notation is confusing,
@@ -5557,7 +5557,7 @@ func (c *Checker) checkVariableLikeDeclaration(node *ast.Node) {
55575557
c.checkSourceElements(name.AsBindingPattern().Elements.Nodes)
55585558
}
55595559
// For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body
5560-
if initializer != nil && ast.IsPartOfParameterDeclaration(node) && ast.NodeIsMissing(getContainingFunction(node).Body()) {
5560+
if initializer != nil && ast.IsPartOfParameterDeclaration(node) && ast.NodeIsMissing(ast.GetContainingFunction(node).Body()) {
55615561
c.error(node, diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation)
55625562
return
55635563
}
@@ -10508,7 +10508,7 @@ func (c *Checker) checkSpreadExpression(node *ast.Node, checkMode CheckMode) *Ty
1050810508

1050910509
func (c *Checker) checkYieldExpression(node *ast.Node) *Type {
1051010510
c.checkGrammarYieldExpression(node)
10511-
fn := getContainingFunction(node)
10511+
fn := ast.GetContainingFunction(node)
1051210512
if fn == nil {
1051310513
return c.anyType
1051410514
}
@@ -22814,7 +22814,7 @@ func (c *Checker) getTypeAliasInstantiation(symbol *ast.Symbol, typeArguments []
2281422814

2281522815
func isLocalTypeAlias(symbol *ast.Symbol) bool {
2281622816
declaration := core.Find(symbol.Declarations, isTypeAlias)
22817-
return declaration != nil && getContainingFunction(declaration) != nil
22817+
return declaration != nil && ast.GetContainingFunction(declaration) != nil
2281822818
}
2281922819

2282022820
func (c *Checker) getDeclaredTypeOfSymbol(symbol *ast.Symbol) *Type {
@@ -28318,7 +28318,7 @@ func (c *Checker) getContextualTypeForStaticPropertyDeclaration(declaration *ast
2831828318
}
2831928319

2832028320
func (c *Checker) getContextualTypeForReturnExpression(node *ast.Node, contextFlags ContextFlags) *Type {
28321-
fn := getContainingFunction(node)
28321+
fn := ast.GetContainingFunction(node)
2832228322
if fn != nil {
2832328323
contextualReturnType := c.getContextualReturnType(fn, contextFlags)
2832428324
if contextualReturnType != nil {
@@ -28413,7 +28413,7 @@ func (c *Checker) getContextualSignatureForFunctionLikeDeclaration(node *ast.Nod
2841328413
}
2841428414

2841528415
func (c *Checker) getContextualTypeForYieldOperand(node *ast.Node, contextFlags ContextFlags) *Type {
28416-
fn := getContainingFunction(node)
28416+
fn := ast.GetContainingFunction(node)
2841728417
if fn != nil {
2841828418
functionFlags := getFunctionFlags(fn)
2841928419
contextualReturnType := c.getContextualReturnType(fn, contextFlags)

internal/checker/grammarchecks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ func (c *Checker) checkGrammarForInOrForOfStatement(forInOrOfStatement *ast.ForI
12631263
// use of 'for-await-of' in non-async function
12641264
if !c.hasParseDiagnostics(sourceFile) {
12651265
diagnostic := createDiagnosticForNode(forInOrOfStatement.AwaitModifier, diagnostics.X_for_await_loops_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules)
1266-
containingFunc := getContainingFunction(forInOrOfStatement.AsNode())
1266+
containingFunc := ast.GetContainingFunction(forInOrOfStatement.AsNode())
12671267
if containingFunc != nil && containingFunc.Kind != ast.KindConstructor {
12681268
debug.Assert((getFunctionFlags(containingFunc)&FunctionFlagsAsync) == 0, "Enclosing function should never be an async function.")
12691269
if hasAsyncModifier(containingFunc) {

internal/checker/utilities.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,10 +911,6 @@ func (s *orderedSet[T]) add(value T) {
911911
s.values = append(s.values, value)
912912
}
913913

914-
func getContainingFunction(node *ast.Node) *ast.Node {
915-
return ast.FindAncestor(node.Parent, ast.IsFunctionLike)
916-
}
917-
918914
func getContainingFunctionOrClassStaticBlock(node *ast.Node) *ast.Node {
919915
return ast.FindAncestor(node.Parent, ast.IsFunctionLikeOrClassStaticBlockDeclaration)
920916
}

internal/ls/documenthighlights.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,14 @@ func getLoopBreakContinueOccurrences(node *ast.Node, sourceFile *ast.SourceFile)
550550
}
551551

552552
func getAsyncAndAwaitOccurrences(node *ast.Node, sourceFile *ast.SourceFile) []*ast.Node {
553-
parent := ast.FindAncestor(node.Parent, ast.IsFunctionLike)
554-
if parent == nil {
553+
fun := ast.GetContainingFunction(node)
554+
if fun == nil {
555555
return nil
556556
}
557-
parentFunc := parent.AsFunctionDeclaration()
557+
558558
var keywords []*ast.Node
559559

560-
modifiers := parentFunc.Modifiers()
560+
modifiers := fun.Modifiers()
561561
if modifiers != nil {
562562
for _, modifier := range modifiers.Nodes {
563563
if modifier.Kind == ast.KindAsyncKeyword {
@@ -566,7 +566,7 @@ func getAsyncAndAwaitOccurrences(node *ast.Node, sourceFile *ast.SourceFile) []*
566566
}
567567
}
568568

569-
parentFunc.ForEachChild(func(child *ast.Node) bool {
569+
fun.ForEachChild(func(child *ast.Node) bool {
570570
traverseWithoutCrossingFunction(child, sourceFile, func(child *ast.Node) {
571571
if ast.IsAwaitExpression(child) {
572572
token := lsutil.GetFirstToken(child, sourceFile)

0 commit comments

Comments
 (0)