Skip to content

Commit 951a76f

Browse files
authored
Add overload tag (#900)
1 parent 1b095c1 commit 951a76f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+343
-385
lines changed

internal/api/encoder/encoder.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -693,14 +693,8 @@ func getChildrenPropertyMask(node *ast.Node) uint8 {
693693
case ast.KindClassDeclaration:
694694
n := node.AsClassDeclaration()
695695
return (boolToByte(n.Modifiers() != nil) << 0) | (boolToByte(n.Name() != nil) << 1) | (boolToByte(n.TypeParameters != nil) << 2) | (boolToByte(n.HeritageClauses != nil) << 3) | (boolToByte(n.Members != nil) << 4)
696-
case ast.KindJSDocPropertyTag:
697-
n := node.AsJSDocPropertyTag()
698-
if n.IsNameFirst {
699-
return (boolToByte(n.Name() != nil) << 0) | (boolToByte(n.TypeExpression != nil) << 1)
700-
}
701-
return (boolToByte(n.TypeExpression != nil) << 0) | (boolToByte(n.Name() != nil) << 1)
702-
case ast.KindJSDocParameterTag:
703-
n := node.AsJSDocParameterTag()
696+
case ast.KindJSDocParameterTag, ast.KindJSDocPropertyTag:
697+
n := node.AsJSDocParameterOrPropertyTag()
704698
if n.IsNameFirst {
705699
return (boolToByte(n.TagName != nil) << 0) | (boolToByte(n.Name() != nil) << 1) | (boolToByte(n.TypeExpression != nil) << 2) | (boolToByte(n.Comment != nil) << 3)
706700
}
@@ -745,11 +739,8 @@ func getNodeDefinedData(node *ast.Node) uint32 {
745739
case ast.KindObjectLiteralExpression:
746740
n := node.AsObjectLiteralExpression()
747741
return uint32(boolToByte(n.MultiLine)) << 24
748-
case ast.KindJSDocPropertyTag:
749-
n := node.AsJSDocPropertyTag()
750-
return uint32(boolToByte(n.IsBracketed))<<24 | uint32(boolToByte(n.IsNameFirst))<<25
751-
case ast.KindJSDocParameterTag:
752-
n := node.AsJSDocParameterTag()
742+
case ast.KindJSDocParameterTag, ast.KindJSDocPropertyTag:
743+
n := node.AsJSDocParameterOrPropertyTag()
753744
return uint32(boolToByte(n.IsBracketed))<<24 | uint32(boolToByte(n.IsNameFirst))<<25
754745
case ast.KindJsxText:
755746
n := node.AsJsxText()

internal/ast/ast.go

Lines changed: 33 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,8 @@ func (n *Node) Type() *Node {
548548
return n.AsTemplateLiteralTypeSpan().Type
549549
case KindJSDocTypeExpression:
550550
return n.AsJSDocTypeExpression().Type
551-
case KindJSDocPropertyTag:
552-
return n.AsJSDocPropertyTag().TypeExpression
551+
case KindJSDocParameterTag, KindJSDocPropertyTag:
552+
return n.AsJSDocParameterOrPropertyTag().TypeExpression
553553
case KindJSDocNullableType:
554554
return n.AsJSDocNullableType().Type
555555
case KindJSDocNonNullableType:
@@ -623,8 +623,8 @@ func (n *Node) TagName() *Node {
623623
return n.AsJSDocCallbackTag().TagName
624624
case KindJSDocOverloadTag:
625625
return n.AsJSDocOverloadTag().TagName
626-
case KindJSDocParameterTag:
627-
return n.AsJSDocParameterTag().TagName
626+
case KindJSDocParameterTag, KindJSDocPropertyTag:
627+
return n.AsJSDocParameterOrPropertyTag().TagName
628628
case KindJSDocReturnTag:
629629
return n.AsJSDocReturnTag().TagName
630630
case KindJSDocThisTag:
@@ -637,8 +637,6 @@ func (n *Node) TagName() *Node {
637637
return n.AsJSDocTypedefTag().TagName
638638
case KindJSDocSeeTag:
639639
return n.AsJSDocSeeTag().TagName
640-
case KindJSDocPropertyTag:
641-
return n.AsJSDocPropertyTag().TagName
642640
case KindJSDocSatisfiesTag:
643641
return n.AsJSDocSatisfiesTag().TagName
644642
case KindJSDocImportTag:
@@ -709,8 +707,8 @@ func (n *Node) CommentList() *NodeList {
709707
return n.AsJSDocCallbackTag().Comment
710708
case KindJSDocOverloadTag:
711709
return n.AsJSDocOverloadTag().Comment
712-
case KindJSDocParameterTag:
713-
return n.AsJSDocParameterTag().Comment
710+
case KindJSDocParameterTag, KindJSDocPropertyTag:
711+
return n.AsJSDocParameterOrPropertyTag().Comment
714712
case KindJSDocReturnTag:
715713
return n.AsJSDocReturnTag().Comment
716714
case KindJSDocThisTag:
@@ -723,8 +721,6 @@ func (n *Node) CommentList() *NodeList {
723721
return n.AsJSDocTypedefTag().Comment
724722
case KindJSDocSeeTag:
725723
return n.AsJSDocSeeTag().Comment
726-
case KindJSDocPropertyTag:
727-
return n.AsJSDocPropertyTag().Comment
728724
case KindJSDocSatisfiesTag:
729725
return n.AsJSDocSatisfiesTag().Comment
730726
case KindJSDocImportTag:
@@ -1520,12 +1516,8 @@ func (n *Node) AsJSDocTemplateTag() *JSDocTemplateTag {
15201516
return n.data.(*JSDocTemplateTag)
15211517
}
15221518

1523-
func (n *Node) AsJSDocPropertyTag() *JSDocPropertyTag {
1524-
return n.data.(*JSDocPropertyTag)
1525-
}
1526-
1527-
func (n *Node) AsJSDocParameterTag() *JSDocParameterTag {
1528-
return n.data.(*JSDocParameterTag)
1519+
func (n *Node) AsJSDocParameterOrPropertyTag() *JSDocParameterOrPropertyTag {
1520+
return n.data.(*JSDocParameterOrPropertyTag)
15291521
}
15301522

15311523
func (n *Node) AsJSDocReturnTag() *JSDocReturnTag {
@@ -1846,7 +1838,7 @@ func IsDeclarationNode(node *Node) bool {
18461838
return node.DeclarationData() != nil
18471839
}
18481840

1849-
// DeclarationBase
1841+
// ExportableBase
18501842

18511843
type ExportableBase struct {
18521844
LocalSymbol *Symbol // Local symbol declared by node (initialized by binding only for exported nodes)
@@ -9198,112 +9190,70 @@ func (node *JSDocTemplateTag) Clone(f NodeFactoryCoercible) *Node {
91989190
return cloneNode(f.AsNodeFactory().NewJSDocTemplateTag(node.TagName, node.Constraint, node.TypeParameters, node.Comment), node.AsNode(), f.AsNodeFactory().hooks)
91999191
}
92009192

9201-
// JSDocPropertyTag
9202-
type JSDocPropertyTag struct {
9193+
// JSDocParameterOrPropertyTag
9194+
type JSDocParameterOrPropertyTag struct {
92039195
JSDocTagBase
92049196
name *EntityName
92059197
IsBracketed bool
92069198
TypeExpression *TypeNode
92079199
IsNameFirst bool
92089200
}
92099201

9210-
func (f *NodeFactory) NewJSDocPropertyTag(tagName *IdentifierNode, name *EntityName, isBracketed bool, typeExpression *TypeNode, isNameFirst bool, comment *NodeList) *Node {
9211-
data := &JSDocPropertyTag{}
9202+
type (
9203+
JSDocParameterTag = JSDocParameterOrPropertyTag
9204+
JSDocPropertyTag = JSDocParameterOrPropertyTag
9205+
)
9206+
9207+
func (f *NodeFactory) newJSDocParameterOrPropertyTag(kind Kind, tagName *IdentifierNode, name *EntityName, isBracketed bool, typeExpression *TypeNode, isNameFirst bool, comment *NodeList) *Node {
9208+
data := &JSDocParameterOrPropertyTag{}
92129209
data.TagName = tagName
92139210
data.name = name
92149211
data.IsBracketed = isBracketed
92159212
data.TypeExpression = typeExpression
92169213
data.IsNameFirst = isNameFirst
92179214
data.Comment = comment
9218-
return f.newNode(KindJSDocPropertyTag, data)
9219-
}
9220-
9221-
func (f *NodeFactory) UpdateJSDocPropertyTag(node *JSDocPropertyTag, tagName *IdentifierNode, name *EntityName, isBracketed bool, typeExpression *TypeNode, isNameFirst bool, comment *NodeList) *Node {
9222-
if tagName != node.TagName || name != node.name || isBracketed != node.IsBracketed || typeExpression != node.TypeExpression || isNameFirst != node.IsNameFirst || comment != node.Comment {
9223-
return updateNode(f.NewJSDocPropertyTag(tagName, name, isBracketed, typeExpression, isNameFirst, comment), node.AsNode(), f.hooks)
9224-
}
9225-
return node.AsNode()
9226-
}
9227-
9228-
func (node *JSDocPropertyTag) ForEachChild(v Visitor) bool {
9229-
if node.IsNameFirst {
9230-
return visit(v, node.TagName) || visit(v, node.name) || visit(v, node.TypeExpression) || visitNodeList(v, node.Comment)
9231-
} else {
9232-
return visit(v, node.TagName) || visit(v, node.TypeExpression) || visit(v, node.name) || visitNodeList(v, node.Comment)
9233-
}
9234-
}
9235-
9236-
func (node *JSDocPropertyTag) VisitEachChild(v *NodeVisitor) *Node {
9237-
tagName := v.visitNode(node.TagName)
9238-
var name, typeExpression *Node
9239-
if node.IsNameFirst {
9240-
name, typeExpression = v.visitNode(node.name), v.visitNode(node.TypeExpression)
9241-
} else {
9242-
typeExpression, name = v.visitNode(node.TypeExpression), v.visitNode(node.name)
9243-
}
9244-
return v.Factory.UpdateJSDocPropertyTag(node, tagName, name, node.IsBracketed, typeExpression, node.IsNameFirst, v.visitNodes(node.Comment))
9245-
}
9246-
9247-
func (node *JSDocPropertyTag) Clone(f NodeFactoryCoercible) *Node {
9248-
return cloneNode(f.AsNodeFactory().NewJSDocPropertyTag(node.TagName, node.Name(), node.IsBracketed, node.TypeExpression, node.IsNameFirst, node.Comment), node.AsNode(), f.AsNodeFactory().hooks)
9215+
return f.newNode(kind, data)
92499216
}
92509217

9251-
func (node *JSDocPropertyTag) Name() *EntityName { return node.name }
9252-
9253-
// JSDocParameterTag
9254-
type JSDocParameterTag struct {
9255-
JSDocTagBase
9256-
name *EntityName
9257-
IsBracketed bool
9258-
TypeExpression *TypeNode
9259-
IsNameFirst bool
9218+
func (f *NodeFactory) NewJSDocParameterTag(tagName *IdentifierNode, name *EntityName, isBracketed bool, typeExpression *TypeNode, isNameFirst bool, comment *NodeList) *Node {
9219+
return f.newJSDocParameterOrPropertyTag(KindJSDocParameterTag, tagName, name, isBracketed, typeExpression, isNameFirst, comment)
92609220
}
92619221

9262-
func (f *NodeFactory) NewJSDocParameterTag(tagName *IdentifierNode, name *EntityName, isBracketed bool, typeExpression *TypeNode, isNameFirst bool, comment *NodeList) *Node {
9263-
data := &JSDocParameterTag{}
9264-
data.TagName = tagName
9265-
data.name = name
9266-
data.IsBracketed = isBracketed
9267-
data.TypeExpression = typeExpression
9268-
data.IsNameFirst = isNameFirst
9269-
data.Comment = comment
9270-
return f.newNode(KindJSDocParameterTag, data)
9222+
func (f *NodeFactory) NewJSDocPropertyTag(tagName *IdentifierNode, name *EntityName, isBracketed bool, typeExpression *TypeNode, isNameFirst bool, comment *NodeList) *Node {
9223+
return f.newJSDocParameterOrPropertyTag(KindJSDocPropertyTag, tagName, name, isBracketed, typeExpression, isNameFirst, comment)
92719224
}
92729225

9273-
func (f *NodeFactory) UpdateJSDocParameterTag(node *JSDocParameterTag, tagName *IdentifierNode, name *EntityName, isBracketed bool, typeExpression *TypeNode, isNameFirst bool, comment *NodeList) *Node {
9226+
func (f *NodeFactory) UpdateJSDocParameterOrPropertyTag(kind Kind, node *JSDocParameterOrPropertyTag, tagName *IdentifierNode, name *EntityName, isBracketed bool, typeExpression *TypeNode, isNameFirst bool, comment *NodeList) *Node {
92749227
if tagName != node.TagName || name != node.name || isBracketed != node.IsBracketed || typeExpression != node.TypeExpression || isNameFirst != node.IsNameFirst || comment != node.Comment {
9275-
return updateNode(f.NewJSDocParameterTag(tagName, name, isBracketed, typeExpression, isNameFirst, comment), node.AsNode(), f.hooks)
9228+
return updateNode(f.newJSDocParameterOrPropertyTag(kind, tagName, name, isBracketed, typeExpression, isNameFirst, comment), node.AsNode(), f.hooks)
92769229
}
92779230
return node.AsNode()
92789231
}
92799232

9280-
func (node *JSDocParameterTag) ForEachChild(v Visitor) bool {
9281-
if visit(v, node.TagName) {
9282-
return true
9283-
}
9233+
func (node *JSDocParameterOrPropertyTag) ForEachChild(v Visitor) bool {
92849234
if node.IsNameFirst {
9285-
return visit(v, node.name) || visit(v, node.TypeExpression) || visitNodeList(v, node.Comment)
9235+
return visit(v, node.TagName) || visit(v, node.name) || visit(v, node.TypeExpression) || visitNodeList(v, node.Comment)
92869236
} else {
9287-
return visit(v, node.TypeExpression) || visit(v, node.name) || visitNodeList(v, node.Comment)
9237+
return visit(v, node.TagName) || visit(v, node.TypeExpression) || visit(v, node.name) || visitNodeList(v, node.Comment)
92889238
}
92899239
}
92909240

9291-
func (node *JSDocParameterTag) VisitEachChild(v *NodeVisitor) *Node {
9241+
func (node *JSDocParameterOrPropertyTag) VisitEachChild(v *NodeVisitor) *Node {
92929242
tagName := v.visitNode(node.TagName)
92939243
var name, typeExpression *Node
92949244
if node.IsNameFirst {
92959245
name, typeExpression = v.visitNode(node.name), v.visitNode(node.TypeExpression)
92969246
} else {
92979247
typeExpression, name = v.visitNode(node.TypeExpression), v.visitNode(node.name)
92989248
}
9299-
return v.Factory.UpdateJSDocParameterTag(node, tagName, name, node.IsBracketed, typeExpression, node.IsNameFirst, v.visitNodes(node.Comment))
9249+
return v.Factory.UpdateJSDocParameterOrPropertyTag(node.Kind, node, tagName, name, node.IsBracketed, typeExpression, node.IsNameFirst, v.visitNodes(node.Comment))
93009250
}
93019251

9302-
func (node *JSDocParameterTag) Clone(f NodeFactoryCoercible) *Node {
9303-
return cloneNode(f.AsNodeFactory().NewJSDocParameterTag(node.TagName, node.Name(), node.IsBracketed, node.TypeExpression, node.IsNameFirst, node.Comment), node.AsNode(), f.AsNodeFactory().hooks)
9252+
func (node *JSDocParameterOrPropertyTag) Clone(f NodeFactoryCoercible) *Node {
9253+
return cloneNode(f.AsNodeFactory().newJSDocParameterOrPropertyTag(node.Kind, node.TagName, node.Name(), node.IsBracketed, node.TypeExpression, node.IsNameFirst, node.Comment), node.AsNode(), f.AsNodeFactory().hooks)
93049254
}
93059255

9306-
func (node *JSDocParameterTag) Name() *EntityName { return node.name }
9256+
func (node *JSDocParameterOrPropertyTag) Name() *EntityName { return node.name }
93079257

93089258
// JSDocReturnTag
93099259
type JSDocReturnTag struct {

internal/binder/binder.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,9 +2884,15 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
28842884
}
28852885
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
28862886
// This list is a work in progress. Add missing node kinds to improve their error spans
2887+
case ast.KindFunctionDeclaration, ast.KindMethodDeclaration:
2888+
if node.Flags&ast.NodeFlagsReparsed != 0 {
2889+
errorNode = node
2890+
break
2891+
}
2892+
fallthrough
28872893
case ast.KindVariableDeclaration, ast.KindBindingElement, ast.KindClassDeclaration, ast.KindClassExpression, ast.KindInterfaceDeclaration,
2888-
ast.KindModuleDeclaration, ast.KindEnumDeclaration, ast.KindEnumMember, ast.KindFunctionDeclaration, ast.KindFunctionExpression,
2889-
ast.KindMethodDeclaration, ast.KindGetAccessor, ast.KindSetAccessor, ast.KindTypeAliasDeclaration, ast.KindJSTypeAliasDeclaration, ast.KindPropertyDeclaration,
2894+
ast.KindModuleDeclaration, ast.KindEnumDeclaration, ast.KindEnumMember, ast.KindFunctionExpression,
2895+
ast.KindGetAccessor, ast.KindSetAccessor, ast.KindTypeAliasDeclaration, ast.KindJSTypeAliasDeclaration, ast.KindPropertyDeclaration,
28902896
ast.KindPropertySignature, ast.KindNamespaceImport:
28912897
errorNode = ast.GetNameOfDeclaration(node)
28922898
case ast.KindArrowFunction:
@@ -2906,6 +2912,10 @@ func GetErrorRangeForNode(sourceFile *ast.SourceFile, node *ast.Node) core.TextR
29062912
pos := scanner.SkipTrivia(sourceFile.Text(), node.AsSatisfiesExpression().Expression.End())
29072913
return scanner.GetRangeOfTokenAtPosition(sourceFile, pos)
29082914
case ast.KindConstructor:
2915+
if node.Flags&ast.NodeFlagsReparsed != 0 {
2916+
errorNode = node
2917+
break
2918+
}
29092919
scanner := scanner.GetScannerForSourceFile(sourceFile, node.Pos())
29102920
start := scanner.TokenStart()
29112921
for scanner.Token() != ast.KindConstructorKeyword && scanner.Token() != ast.KindStringLiteral && scanner.Token() != ast.KindEndOfFile {

internal/checker/checker.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,7 +3409,7 @@ func (c *Checker) checkFunctionOrConstructorSymbol(symbol *ast.Symbol) {
34093409
} else {
34103410
duplicateFunctionDeclaration = true
34113411
}
3412-
} else if previousDeclaration != nil && previousDeclaration.Parent == node.Parent && previousDeclaration.End() != node.Pos() {
3412+
} else if previousDeclaration != nil && previousDeclaration.Parent == node.Parent && previousDeclaration.End() != node.Pos() && previousDeclaration.Flags&ast.NodeFlagsReparsed == 0 {
34133413
reportImplementationExpectedError(previousDeclaration)
34143414
}
34153415
if bodyIsPresent {
@@ -17215,6 +17215,9 @@ func (c *Checker) reportImplicitAny(declaration *ast.Node, t *Type, wideningKind
1721517215
switch {
1721617216
case !c.noImplicitAny:
1721717217
diagnostic = diagnostics.X_0_implicitly_has_an_1_return_type_but_a_better_type_may_be_inferred_from_usage
17218+
case declaration.Flags&ast.NodeFlagsReparsed != 0:
17219+
c.error(declaration, diagnostics.This_overload_implicitly_returns_the_type_0_because_it_lacks_a_return_type_annotation, typeAsString)
17220+
return
1721817221
case wideningKind == WideningKindGeneratorYield:
1721917222
diagnostic = diagnostics.X_0_which_lacks_return_type_annotation_implicitly_has_an_1_yield_type
1722017223
default:
@@ -18657,7 +18660,8 @@ func (c *Checker) getSignaturesOfSymbol(symbol *ast.Symbol) []*Signature {
1865718660
// precedes the implementation node (i.e. has the same parent and ends where the implementation starts).
1865818661
if i > 0 && decl.Body() != nil {
1865918662
previous := symbol.Declarations[i-1]
18660-
if decl.Parent == previous.Parent && decl.Kind == previous.Kind && decl.Pos() == previous.End() {
18663+
if decl.Parent == previous.Parent && decl.Kind == previous.Kind &&
18664+
(decl.Pos() == previous.End() || previous.Flags&ast.NodeFlagsReparsed != 0) {
1866118665
continue
1866218666
}
1866318667
}

0 commit comments

Comments
 (0)