Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/parser/jsdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (p *Parser) parseJSDocComment(parent *ast.Node, start int, end int, fullSta
// +3 for leading `/**`
p.scanner.ResetPos(start + 3)
p.setContextFlags(ast.NodeFlagsJSDoc, true)
p.parsingContexts = p.parsingContexts | ParsingContexts(PCJSDocComment)
p.parsingContexts |= 1 << PCJSDocComment

comment := p.parseJSDocCommentWorker(start, end, fullStart, initialIndent)
// move jsdoc diagnostics to jsdocDiagnostics -- for JS files only
Expand Down
4 changes: 2 additions & 2 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (p *Parser) parseSourceFileWorker() *ast.SourceFile {
if eof.Kind != ast.KindEndOfFile {
panic("Expected end of file token from scanner.")
}
if len(p.reparseList) > 0 {
if len(p.reparseList) != 0 {
statements = append(statements, p.reparseList...)
p.reparseList = nil
}
Expand Down Expand Up @@ -504,7 +504,7 @@ func (p *Parser) parseListIndex(kind ParsingContext, parseElement func(p *Parser
for i := 0; !p.isListTerminator(kind); i++ {
if p.isListElement(kind, false /*inErrorRecovery*/) {
elt := parseElement(p, len(list))
if len(p.reparseList) > 0 {
if len(p.reparseList) != 0 {
for _, e := range p.reparseList {
// Propagate @typedef type alias declarations outwards to a context that permits them.
if (ast.IsJSTypeAliasDeclaration(e) || ast.IsJSImportDeclaration(e)) && kind != PCSourceElements && kind != PCBlockStatements {
Expand Down
29 changes: 13 additions & 16 deletions internal/parser/reparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ func (p *Parser) reparseUnhosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Nod
p.finishReparsedNode(importDeclaration, tag)
p.reparseList = append(p.reparseList, importDeclaration)
case ast.KindJSDocOverloadTag:
if fun, ok := getFunctionLikeHost(parent); ok {
p.reparseList = append(p.reparseList, p.reparseJSDocSignature(tag.AsJSDocOverloadTag().TypeExpression, fun, jsDoc, tag, fun.Modifiers()))
// Create overload signatures only for function, method, and constructor declarations outside object literals
if (ast.IsFunctionDeclaration(parent) || ast.IsMethodDeclaration(parent) || ast.IsConstructorDeclaration(parent)) && p.parsingContexts&(1<<PCObjectLiteralMembers) == 0 {
p.reparseList = append(p.reparseList, p.reparseJSDocSignature(tag.AsJSDocOverloadTag().TypeExpression, parent, jsDoc, tag, parent.Modifiers()))
}
}
}
Expand All @@ -121,14 +122,10 @@ func (p *Parser) reparseJSDocSignature(jsSignature *ast.Node, fun *ast.Node, jsD
var signature *ast.Node
clonedModifiers := p.factory.DeepCloneReparseModifiers(modifiers)
switch fun.Kind {
case ast.KindFunctionDeclaration, ast.KindFunctionExpression, ast.KindArrowFunction:
case ast.KindFunctionDeclaration:
signature = p.factory.NewFunctionDeclaration(clonedModifiers, nil, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil)
case ast.KindMethodDeclaration, ast.KindMethodSignature:
case ast.KindMethodDeclaration:
signature = p.factory.NewMethodDeclaration(clonedModifiers, nil, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil, nil)
case ast.KindGetAccessor:
signature = p.factory.NewGetAccessorDeclaration(clonedModifiers, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil)
case ast.KindSetAccessor:
signature = p.factory.NewSetAccessorDeclaration(clonedModifiers, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil)
case ast.KindConstructor:
signature = p.factory.NewConstructorDeclaration(clonedModifiers, nil, nil, nil, nil, nil)
case ast.KindJSDocCallbackTag:
Expand Down Expand Up @@ -323,7 +320,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
return
}
}
if fun, ok := getFunctionLikeHost(parent); ok {
if fun := getFunctionLikeHost(parent); fun != nil {
noTypedParams := core.Every(fun.Parameters(), func(param *ast.Node) bool { return param.Type() == nil })
if fun.TypeParameterList() == nil && fun.Type() == nil && noTypedParams && tag.TypeExpression() != nil {
fun.FunctionLikeData().FullSignature = p.factory.DeepCloneReparse(tag.TypeExpression().Type())
Expand Down Expand Up @@ -386,7 +383,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
}
case ast.KindJSDocTemplateTag:
if fun, ok := getFunctionLikeHost(parent); ok {
if fun := getFunctionLikeHost(parent); fun != nil {
if fun.TypeParameters() == nil && fun.FunctionLikeData().FullSignature == nil {
fun.FunctionLikeData().TypeParameters = p.gatherTypeParameters(jsDoc, nil /*tagWithTypeParameters*/)
p.finishMutatedNode(fun)
Expand All @@ -405,7 +402,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
}
case ast.KindJSDocParameterTag:
if fun, ok := getFunctionLikeHost(parent); ok && fun.FunctionLikeData().FullSignature == nil {
if fun := getFunctionLikeHost(parent); fun != nil && fun.FunctionLikeData().FullSignature == nil {
parameterTag := tag.AsJSDocParameterOrPropertyTag()
if param, ok := findMatchingParameter(fun, parameterTag, jsDoc); ok {
if param.Type == nil && parameterTag.TypeExpression != nil {
Expand All @@ -420,7 +417,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
}
case ast.KindJSDocThisTag:
if fun, ok := getFunctionLikeHost(parent); ok {
if fun := getFunctionLikeHost(parent); fun != nil {
params := fun.Parameters()
if len(params) == 0 || params[0].Name().Kind != ast.KindThisKeyword {
thisParam := p.factory.NewParameterDeclaration(
Expand All @@ -447,7 +444,7 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
}
}
case ast.KindJSDocReturnTag:
if fun, ok := getFunctionLikeHost(parent); ok && fun.FunctionLikeData().FullSignature == nil {
if fun := getFunctionLikeHost(parent); fun != nil && fun.FunctionLikeData().FullSignature == nil {
if fun.Type() == nil && tag.TypeExpression() != nil {
fun.FunctionLikeData().Type = p.factory.DeepCloneReparse(tag.TypeExpression().Type())
p.finishMutatedNode(fun)
Expand Down Expand Up @@ -570,7 +567,7 @@ func findMatchingParameter(fun *ast.Node, parameterTag *ast.JSDocParameterTag, j
return nil, false
}

func getFunctionLikeHost(host *ast.Node) (*ast.Node, bool) {
func getFunctionLikeHost(host *ast.Node) *ast.Node {
fun := host
if host.Kind == ast.KindVariableStatement && host.AsVariableStatement().DeclarationList != nil {
for _, declaration := range host.AsVariableStatement().DeclarationList.AsVariableDeclarationList().Declarations.Nodes {
Expand All @@ -593,9 +590,9 @@ func getFunctionLikeHost(host *ast.Node) (*ast.Node, bool) {
}
}
if ast.IsFunctionLike(fun) {
return fun, true
return fun
}
return nil, false
return nil
}

func (p *Parser) makeNewCast(t *ast.TypeNode, e *ast.Node, isAssertion bool) *ast.Node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ const example3 = {


//// [jsFileAlternativeUseOfOverloadTag.d.ts]
declare function Example1(value: any): any;
declare const example1: {
/**
* @overload Example1(value)
Expand All @@ -115,8 +114,6 @@ declare const example1: {
*/
constructor: (value: any, options: any) => void;
};
declare function Example2(value: any, secretAccessKey: any, sessionToken: any): any;
declare function Example2(): any;
declare const example2: {
/**
* Example 2
Expand All @@ -138,7 +135,6 @@ declare const example2: {
*/
constructor: () => void;
};
declare function evaluate(): any;
type callback = (error: any, result: any) => any;
declare const example3: {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

//// [jsFileAlternativeUseOfOverloadTag.d.ts]
-declare namespace example1 {
+declare function Example1(value: any): any;
+declare const example1: {
/**
* @overload Example1(value)
Expand Down Expand Up @@ -58,8 +57,6 @@
-declare namespace example3 {
+ constructor: (value: any, options: any) => void;
+};
+declare function Example2(value: any, secretAccessKey: any, sessionToken: any): any;
+declare function Example2(): any;
+declare const example2: {
+ /**
+ * Example 2
Expand All @@ -81,13 +78,12 @@
+ */
+ constructor: () => void;
+};
+declare function evaluate(): any;
+type callback = (error: any, result: any) => any;
+declare const example3: {
/**
* @overload evaluate(options = {}, [callback])
* Evaluate something
@@= skipped -63, +48 lines =@@
@@= skipped -63, +44 lines =@@
* @param result [String]
* @see callback
*/
Expand Down