Skip to content

Commit 092b34f

Browse files
authored
Align and simplify getFunctionLikeHost and GetNextJSDocCommentLocation (#3790)
1 parent e1f8f97 commit 092b34f

6 files changed

Lines changed: 82 additions & 39 deletions

File tree

internal/ast/utilities.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4021,25 +4021,17 @@ func GetHostSignatureFromJSDoc(node *Node) *Node {
40214021
// Finds the declaration that owns the JSDoc for a function-like node.
40224022
// Keep these hosts aligned with JSDoc parameter reparsing so unmatched @param diagnostics use the same attachment rules.
40234023
func GetNextJSDocCommentLocation(node *Node) *Node {
4024-
parent := node.Parent
4025-
if parent == nil {
4026-
return nil
4027-
}
4028-
switch parent.Kind {
4029-
case KindPropertyAssignment, KindExportAssignment, KindPropertyDeclaration, KindReturnStatement:
4030-
return parent
4031-
case KindExpressionStatement:
4032-
if node.Kind == KindPropertyAccessExpression {
4033-
return parent
4034-
}
4035-
case KindVariableStatement:
4036-
if HasSyntacticModifier(parent, ModifierFlagsExport) {
4024+
if parent := node.Parent; parent != nil {
4025+
switch parent.Kind {
4026+
case KindPropertyAssignment, KindExportAssignment, KindPropertyDeclaration, KindVariableDeclaration,
4027+
KindSatisfiesExpression, KindReturnStatement, KindVariableStatement, KindExpressionStatement:
40374028
return parent
4029+
case KindVariableDeclarationList:
4030+
if parent.AsVariableDeclarationList().Declarations.Nodes[0] == node {
4031+
return parent
4032+
}
40384033
}
40394034
}
4040-
if IsVariableLike(parent) && parent.Initializer() == node {
4041-
return parent
4042-
}
40434035
return nil
40444036
}
40454037

internal/checker/jsdoc.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ func (c *Checker) checkUnmatchedJSDocParameters(node *ast.Node) {
8282
}
8383

8484
func getAllJSDocTags(node *ast.Node) []*ast.Node {
85-
for current := node; current != nil; current = ast.GetNextJSDocCommentLocation(current) {
86-
jsdocs := current.JSDoc(nil)
87-
if len(jsdocs) == 0 {
88-
continue
89-
}
90-
lastJSDoc := jsdocs[len(jsdocs)-1].AsJSDoc()
91-
if lastJSDoc.Tags != nil {
92-
return lastJSDoc.Tags.Nodes
85+
if node.Flags&ast.NodeFlagsJSDoc == 0 {
86+
for current := node; current != nil; current = ast.GetNextJSDocCommentLocation(current) {
87+
jsdocs := current.JSDoc(nil)
88+
if len(jsdocs) == 0 {
89+
continue
90+
}
91+
lastJSDoc := jsdocs[len(jsdocs)-1].AsJSDoc()
92+
if lastJSDoc.Tags != nil {
93+
return lastJSDoc.Tags.Nodes
94+
}
9395
}
9496
}
9597
return nil

internal/parser/reparser.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -573,25 +573,19 @@ func skipSatisfiesExpressions(node *ast.Node) *ast.Node {
573573

574574
func getFunctionLikeHost(host *ast.Node) *ast.Node {
575575
fun := host
576-
if host.Kind == ast.KindVariableStatement && host.AsVariableStatement().DeclarationList != nil {
577-
for _, declaration := range host.AsVariableStatement().DeclarationList.AsVariableDeclarationList().Declarations.Nodes {
578-
initializer := skipSatisfiesExpressions(declaration.Initializer())
579-
if ast.IsFunctionLike(initializer) {
580-
fun = initializer
581-
break
582-
}
576+
switch host.Kind {
577+
case ast.KindVariableStatement:
578+
if nodes := host.AsVariableStatement().DeclarationList.AsVariableDeclarationList().Declarations.Nodes; len(nodes) != 0 {
579+
fun = nodes[0].Initializer()
583580
}
584-
} else if host.Kind == ast.KindPropertyAssignment {
585-
fun = skipSatisfiesExpressions(host.Initializer())
586-
} else if host.Kind == ast.KindPropertyDeclaration {
587-
fun = skipSatisfiesExpressions(host.Initializer())
588-
} else if host.Kind == ast.KindExportAssignment {
589-
fun = host.Expression()
590-
} else if host.Kind == ast.KindReturnStatement {
581+
case ast.KindPropertyAssignment, ast.KindPropertyDeclaration:
582+
fun = host.Initializer()
583+
case ast.KindExportAssignment, ast.KindReturnStatement:
591584
fun = host.Expression()
592-
} else if host.Kind == ast.KindExpressionStatement {
585+
case ast.KindExpressionStatement:
593586
fun = ast.GetRightMostAssignedExpression(host.Expression())
594587
}
588+
fun = skipSatisfiesExpressions(fun)
595589
if ast.IsFunctionLike(fun) {
596590
return fun
597591
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/shorthandPropertyAssignmentNoCrash.ts] ////
2+
3+
=== shorthandPropertyAssignmentNoCrash.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/3789
5+
6+
function ff(f: any) {
7+
>ff : Symbol(ff, Decl(shorthandPropertyAssignmentNoCrash.ts, 0, 0))
8+
>f : Symbol(f, Decl(shorthandPropertyAssignmentNoCrash.ts, 2, 12))
9+
10+
let g;
11+
>g : Symbol(g, Decl(shorthandPropertyAssignmentNoCrash.ts, 3, 7))
12+
13+
({ g = (x: any, y: any) => x + y } = f);
14+
>g : Symbol(g, Decl(shorthandPropertyAssignmentNoCrash.ts, 4, 6))
15+
>x : Symbol(x, Decl(shorthandPropertyAssignmentNoCrash.ts, 4, 12))
16+
>y : Symbol(y, Decl(shorthandPropertyAssignmentNoCrash.ts, 4, 19))
17+
>x : Symbol(x, Decl(shorthandPropertyAssignmentNoCrash.ts, 4, 12))
18+
>y : Symbol(y, Decl(shorthandPropertyAssignmentNoCrash.ts, 4, 19))
19+
>f : Symbol(f, Decl(shorthandPropertyAssignmentNoCrash.ts, 2, 12))
20+
}
21+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [tests/cases/compiler/shorthandPropertyAssignmentNoCrash.ts] ////
2+
3+
=== shorthandPropertyAssignmentNoCrash.ts ===
4+
// https://github.com/microsoft/typescript-go/issues/3789
5+
6+
function ff(f: any) {
7+
>ff : (f: any) => void
8+
>f : any
9+
10+
let g;
11+
>g : any
12+
13+
({ g = (x: any, y: any) => x + y } = f);
14+
>({ g = (x: any, y: any) => x + y } = f) : any
15+
>{ g = (x: any, y: any) => x + y } = f : any
16+
>{ g = (x: any, y: any) => x + y } : { g?: any; }
17+
>g : any
18+
>(x: any, y: any) => x + y : (x: any, y: any) => any
19+
>x : any
20+
>y : any
21+
>x + y : any
22+
>x : any
23+
>y : any
24+
>f : any
25+
}
26+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @noEmit: true
2+
3+
// https://github.com/microsoft/typescript-go/issues/3789
4+
5+
function ff(f: any) {
6+
let g;
7+
({ g = (x: any, y: any) => x + y } = f);
8+
}

0 commit comments

Comments
 (0)