Skip to content

Commit a51449f

Browse files
committed
Process @overload tags only in limited contexts
1 parent 8beb55c commit a51449f

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

internal/parser/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (p *Parser) parseSourceFileWorker() *ast.SourceFile {
345345
if eof.Kind != ast.KindEndOfFile {
346346
panic("Expected end of file token from scanner.")
347347
}
348-
if len(p.reparseList) > 0 {
348+
if len(p.reparseList) != 0 {
349349
statements = append(statements, p.reparseList...)
350350
p.reparseList = nil
351351
}
@@ -504,7 +504,7 @@ func (p *Parser) parseListIndex(kind ParsingContext, parseElement func(p *Parser
504504
for i := 0; !p.isListTerminator(kind); i++ {
505505
if p.isListElement(kind, false /*inErrorRecovery*/) {
506506
elt := parseElement(p, len(list))
507-
if len(p.reparseList) > 0 {
507+
if len(p.reparseList) != 0 {
508508
for _, e := range p.reparseList {
509509
// Propagate @typedef type alias declarations outwards to a context that permits them.
510510
if (ast.IsJSTypeAliasDeclaration(e) || ast.IsJSImportDeclaration(e)) && kind != PCSourceElements && kind != PCBlockStatements {

internal/parser/reparser.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ func (p *Parser) reparseUnhosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Nod
111111
p.finishReparsedNode(importDeclaration, tag)
112112
p.reparseList = append(p.reparseList, importDeclaration)
113113
case ast.KindJSDocOverloadTag:
114-
if fun, ok := getFunctionLikeHost(parent); ok {
115-
p.reparseList = append(p.reparseList, p.reparseJSDocSignature(tag.AsJSDocOverloadTag().TypeExpression, fun, jsDoc, tag, fun.Modifiers()))
114+
// Create overload signatures only for function, method, and constructor declarations outside object literals
115+
if (ast.IsFunctionDeclaration(parent) || ast.IsMethodDeclaration(parent) || ast.IsConstructorDeclaration(parent)) && p.parsingContexts&(1<<PCObjectLiteralMembers) == 0 {
116+
p.reparseList = append(p.reparseList, p.reparseJSDocSignature(tag.AsJSDocOverloadTag().TypeExpression, parent, jsDoc, tag, parent.Modifiers()))
116117
}
117118
}
118119
}
@@ -121,14 +122,10 @@ func (p *Parser) reparseJSDocSignature(jsSignature *ast.Node, fun *ast.Node, jsD
121122
var signature *ast.Node
122123
clonedModifiers := p.factory.DeepCloneReparseModifiers(modifiers)
123124
switch fun.Kind {
124-
case ast.KindFunctionDeclaration, ast.KindFunctionExpression, ast.KindArrowFunction:
125+
case ast.KindFunctionDeclaration:
125126
signature = p.factory.NewFunctionDeclaration(clonedModifiers, nil, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil)
126-
case ast.KindMethodDeclaration, ast.KindMethodSignature:
127+
case ast.KindMethodDeclaration:
127128
signature = p.factory.NewMethodDeclaration(clonedModifiers, nil, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil, nil)
128-
case ast.KindGetAccessor:
129-
signature = p.factory.NewGetAccessorDeclaration(clonedModifiers, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil)
130-
case ast.KindSetAccessor:
131-
signature = p.factory.NewSetAccessorDeclaration(clonedModifiers, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil)
132129
case ast.KindConstructor:
133130
signature = p.factory.NewConstructorDeclaration(clonedModifiers, nil, nil, nil, nil, nil)
134131
case ast.KindJSDocCallbackTag:
@@ -323,7 +320,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
323320
return
324321
}
325322
}
326-
if fun, ok := getFunctionLikeHost(parent); ok {
323+
if fun := getFunctionLikeHost(parent); fun != nil {
327324
noTypedParams := core.Every(fun.Parameters(), func(param *ast.Node) bool { return param.Type() == nil })
328325
if fun.TypeParameterList() == nil && fun.Type() == nil && noTypedParams && tag.TypeExpression() != nil {
329326
fun.FunctionLikeData().FullSignature = p.factory.DeepCloneReparse(tag.TypeExpression().Type())
@@ -386,7 +383,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
386383
}
387384
}
388385
case ast.KindJSDocTemplateTag:
389-
if fun, ok := getFunctionLikeHost(parent); ok {
386+
if fun := getFunctionLikeHost(parent); fun != nil {
390387
if fun.TypeParameters() == nil && fun.FunctionLikeData().FullSignature == nil {
391388
fun.FunctionLikeData().TypeParameters = p.gatherTypeParameters(jsDoc, nil /*tagWithTypeParameters*/)
392389
p.finishMutatedNode(fun)
@@ -405,7 +402,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
405402
}
406403
}
407404
case ast.KindJSDocParameterTag:
408-
if fun, ok := getFunctionLikeHost(parent); ok && fun.FunctionLikeData().FullSignature == nil {
405+
if fun := getFunctionLikeHost(parent); fun != nil && fun.FunctionLikeData().FullSignature == nil {
409406
parameterTag := tag.AsJSDocParameterOrPropertyTag()
410407
if param, ok := findMatchingParameter(fun, parameterTag, jsDoc); ok {
411408
if param.Type == nil && parameterTag.TypeExpression != nil {
@@ -420,7 +417,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
420417
}
421418
}
422419
case ast.KindJSDocThisTag:
423-
if fun, ok := getFunctionLikeHost(parent); ok {
420+
if fun := getFunctionLikeHost(parent); fun != nil {
424421
params := fun.Parameters()
425422
if len(params) == 0 || params[0].Name().Kind != ast.KindThisKeyword {
426423
thisParam := p.factory.NewParameterDeclaration(
@@ -447,7 +444,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
447444
}
448445
}
449446
case ast.KindJSDocReturnTag:
450-
if fun, ok := getFunctionLikeHost(parent); ok && fun.FunctionLikeData().FullSignature == nil {
447+
if fun := getFunctionLikeHost(parent); fun != nil && fun.FunctionLikeData().FullSignature == nil {
451448
if fun.Type() == nil && tag.TypeExpression() != nil {
452449
fun.FunctionLikeData().Type = p.factory.DeepCloneReparse(tag.TypeExpression().Type())
453450
p.finishMutatedNode(fun)
@@ -570,7 +567,7 @@ func findMatchingParameter(fun *ast.Node, parameterTag *ast.JSDocParameterTag, j
570567
return nil, false
571568
}
572569

573-
func getFunctionLikeHost(host *ast.Node) (*ast.Node, bool) {
570+
func getFunctionLikeHost(host *ast.Node) *ast.Node {
574571
fun := host
575572
if host.Kind == ast.KindVariableStatement && host.AsVariableStatement().DeclarationList != nil {
576573
for _, declaration := range host.AsVariableStatement().DeclarationList.AsVariableDeclarationList().Declarations.Nodes {
@@ -593,9 +590,9 @@ func getFunctionLikeHost(host *ast.Node) (*ast.Node, bool) {
593590
}
594591
}
595592
if ast.IsFunctionLike(fun) {
596-
return fun, true
593+
return fun
597594
}
598-
return nil, false
595+
return nil
599596
}
600597

601598
func (p *Parser) makeNewCast(t *ast.TypeNode, e *ast.Node, isAssertion bool) *ast.Node {

0 commit comments

Comments
 (0)