Skip to content

Commit a568e20

Browse files
authored
Process @overload tags only in limited contexts (#2263)
1 parent 1d138ea commit a568e20

File tree

5 files changed

+17
-28
lines changed

5 files changed

+17
-28
lines changed

internal/parser/jsdoc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (p *Parser) parseJSDocComment(parent *ast.Node, start int, end int, fullSta
133133
// +3 for leading `/**`
134134
p.scanner.ResetPos(start + 3)
135135
p.setContextFlags(ast.NodeFlagsJSDoc, true)
136-
p.parsingContexts = p.parsingContexts | ParsingContexts(PCJSDocComment)
136+
p.parsingContexts |= 1 << PCJSDocComment
137137

138138
comment := p.parseJSDocCommentWorker(start, end, fullStart, initialIndent)
139139
// move jsdoc diagnostics to jsdocDiagnostics -- for JS files only

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 {

testdata/baselines/reference/submodule/compiler/jsFileAlternativeUseOfOverloadTag.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ const example3 = {
106106

107107

108108
//// [jsFileAlternativeUseOfOverloadTag.d.ts]
109-
declare function Example1(value: any): any;
110109
declare const example1: {
111110
/**
112111
* @overload Example1(value)
@@ -115,8 +114,6 @@ declare const example1: {
115114
*/
116115
constructor: (value: any, options: any) => void;
117116
};
118-
declare function Example2(value: any, secretAccessKey: any, sessionToken: any): any;
119-
declare function Example2(): any;
120117
declare const example2: {
121118
/**
122119
* Example 2
@@ -138,7 +135,6 @@ declare const example2: {
138135
*/
139136
constructor: () => void;
140137
};
141-
declare function evaluate(): any;
142138
type callback = (error: any, result: any) => any;
143139
declare const example3: {
144140
/**

testdata/baselines/reference/submodule/compiler/jsFileAlternativeUseOfOverloadTag.js.diff

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
//// [jsFileAlternativeUseOfOverloadTag.d.ts]
77
-declare namespace example1 {
8-
+declare function Example1(value: any): any;
98
+declare const example1: {
109
/**
1110
* @overload Example1(value)
@@ -58,8 +57,6 @@
5857
-declare namespace example3 {
5958
+ constructor: (value: any, options: any) => void;
6059
+};
61-
+declare function Example2(value: any, secretAccessKey: any, sessionToken: any): any;
62-
+declare function Example2(): any;
6360
+declare const example2: {
6461
+ /**
6562
+ * Example 2
@@ -81,13 +78,12 @@
8178
+ */
8279
+ constructor: () => void;
8380
+};
84-
+declare function evaluate(): any;
8581
+type callback = (error: any, result: any) => any;
8682
+declare const example3: {
8783
/**
8884
* @overload evaluate(options = {}, [callback])
8985
* Evaluate something
90-
@@= skipped -63, +48 lines =@@
86+
@@= skipped -63, +44 lines =@@
9187
* @param result [String]
9288
* @see callback
9389
*/

0 commit comments

Comments
 (0)