Skip to content

Commit 46ccafb

Browse files
authored
Add modifiers on reparsed signature from @overload (#1438)
1 parent 79d0450 commit 46ccafb

File tree

9 files changed

+117
-177
lines changed

9 files changed

+117
-177
lines changed

internal/parser/reparser.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (p *Parser) reparseUnhosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Nod
104104
export.Loc = tag.Loc
105105
export.Flags = p.contextFlags | ast.NodeFlagsReparsed
106106
modifiers := p.newModifierList(export.Loc, p.nodeSlicePool.NewSlice1(export))
107-
functionType := p.reparseJSDocSignature(callbackTag.TypeExpression, tag, jsDoc, tag)
107+
functionType := p.reparseJSDocSignature(callbackTag.TypeExpression, tag, jsDoc, tag, nil)
108108

109109
typeAlias := p.factory.NewJSTypeAliasDeclaration(modifiers, p.factory.DeepCloneReparse(callbackTag.FullName), nil, functionType)
110110
typeAlias.AsTypeAliasDeclaration().TypeParameters = p.gatherTypeParameters(jsDoc, tag)
@@ -127,20 +127,21 @@ func (p *Parser) reparseUnhosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Nod
127127
p.reparseList = append(p.reparseList, importDeclaration)
128128
case ast.KindJSDocOverloadTag:
129129
if fun, ok := getFunctionLikeHost(parent); ok {
130-
p.reparseList = append(p.reparseList, p.reparseJSDocSignature(tag.AsJSDocOverloadTag().TypeExpression, fun, jsDoc, tag))
130+
p.reparseList = append(p.reparseList, p.reparseJSDocSignature(tag.AsJSDocOverloadTag().TypeExpression, fun, jsDoc, tag, fun.Modifiers()))
131131
}
132132
}
133133
}
134134

135-
func (p *Parser) reparseJSDocSignature(jsSignature *ast.Node, fun *ast.Node, jsDoc *ast.Node, tag *ast.Node) *ast.Node {
135+
func (p *Parser) reparseJSDocSignature(jsSignature *ast.Node, fun *ast.Node, jsDoc *ast.Node, tag *ast.Node, modifiers *ast.ModifierList) *ast.Node {
136136
var signature *ast.Node
137+
clonedModifiers := p.factory.DeepCloneReparseModifiers(modifiers)
137138
switch fun.Kind {
138139
case ast.KindFunctionDeclaration, ast.KindFunctionExpression, ast.KindArrowFunction:
139-
signature = p.factory.NewFunctionDeclaration(nil, nil, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil)
140+
signature = p.factory.NewFunctionDeclaration(clonedModifiers, nil, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil)
140141
case ast.KindMethodDeclaration, ast.KindMethodSignature:
141-
signature = p.factory.NewMethodDeclaration(nil, nil, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil)
142+
signature = p.factory.NewMethodDeclaration(clonedModifiers, nil, p.factory.DeepCloneReparse(fun.Name()), nil, nil, nil, nil, nil)
142143
case ast.KindConstructor:
143-
signature = p.factory.NewConstructorDeclaration(nil, nil, nil, nil, nil)
144+
signature = p.factory.NewConstructorDeclaration(clonedModifiers, nil, nil, nil, nil)
144145
case ast.KindJSDocCallbackTag:
145146
signature = p.factory.NewFunctionTypeNode(nil, nil, nil)
146147
default:

testdata/baselines/reference/submodule/conformance/overloadTag1.errors.txt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
overloadTag1.js(26,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'.
1+
overloadTag1.js(7,5): error TS2394: This overload signature is not compatible with its implementation signature.
2+
overloadTag1.js(25,29): error TS2769: No overload matches this call.
3+
The last overload gave the following error.
4+
Argument of type 'string' is not assignable to parameter of type 'boolean'.
5+
overloadTag1.js(43,29): error TS2769: No overload matches this call.
6+
The last overload gave the following error.
7+
Argument of type 'string' is not assignable to parameter of type 'boolean'.
28

39

4-
==== overloadTag1.js (1 errors) ====
10+
==== overloadTag1.js (3 errors) ====
511
/**
612
* @overload
713
* @param {number} a
814
* @param {number} b
915
* @returns {number}
1016
*
1117
* @overload
18+
~~~~~~~~
19+
!!! error TS2394: This overload signature is not compatible with its implementation signature.
20+
!!! related TS2750 overloadTag1.js:16:17: The implementation signature is declared here.
1221
* @param {string} a
1322
* @param {boolean} b
1423
* @returns {string}
@@ -27,9 +36,13 @@ overloadTag1.js(26,25): error TS2345: Argument of type 'boolean' is not assignab
2736
}
2837
var o1 = overloaded(1,2)
2938
var o2 = overloaded("zero", "one")
39+
~~~~~
40+
!!! error TS2769: No overload matches this call.
41+
!!! error TS2769: The last overload gave the following error.
42+
!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'boolean'.
43+
!!! related TS2771 overloadTag1.js:7:5: The last overload is declared here.
44+
!!! related TS2793 overloadTag1.js:16:17: The call would have succeeded against this implementation, but implementation signatures of overloads are not externally visible.
3045
var o3 = overloaded("a",false)
31-
~~~~~
32-
!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'.
3346

3447
/**
3548
* @overload
@@ -47,4 +60,10 @@ overloadTag1.js(26,25): error TS2345: Argument of type 'boolean' is not assignab
4760
}
4861
uncheckedInternally(1,2)
4962
uncheckedInternally("zero", "one")
63+
~~~~~
64+
!!! error TS2769: No overload matches this call.
65+
!!! error TS2769: The last overload gave the following error.
66+
!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'boolean'.
67+
!!! related TS2771 overloadTag1.js:34:5: The last overload is declared here.
68+
!!! related TS2793 overloadTag1.js:39:17: The call would have succeeded against this implementation, but implementation signatures of overloads are not externally visible.
5069

testdata/baselines/reference/submodule/conformance/overloadTag1.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -97,31 +97,7 @@ uncheckedInternally("zero", "one");
9797

9898

9999
//// [overloadTag1.d.ts]
100-
/**
101-
* @overload
102-
* @param {number} a
103-
* @param {number} b
104-
* @returns {number}
105-
*
106-
* @overload
107-
* @param {string} a
108-
* @param {boolean} b
109-
* @returns {string}
110-
*
111-
* @param {string | number} a
112-
* @param {string | number} b
113-
* @returns {string | number}
114-
*/
115-
export declare function overloaded(a: string | number, b: string | number): string | number;
116-
/**
117-
* @overload
118-
* @param {number} a
119-
* @param {number} b
120-
* @returns {number}
121-
*
122-
* @overload
123-
* @param {string} a
124-
* @param {boolean} b
125-
* @returns {string}
126-
*/
127-
export declare function uncheckedInternally(a: any, b: any): any;
100+
export declare function overloaded(a: number, b: number): number;
101+
export declare function overloaded(a: string, b: boolean): string;
102+
export declare function uncheckedInternally(a: number, b: number): number;
103+
export declare function uncheckedInternally(a: string, b: boolean): string;

testdata/baselines/reference/submodule/conformance/overloadTag1.js.diff

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
--- old.overloadTag1.js
22
+++ new.overloadTag1.js
3-
@@= skipped -111, +111 lines =@@
4-
* @param {string | number} b
5-
* @returns {string | number}
6-
*/
3+
@@= skipped -96, +96 lines =@@
4+
5+
6+
//// [overloadTag1.d.ts]
7+
-/**
8+
- * @overload
9+
- * @param {number} a
10+
- * @param {number} b
11+
- * @returns {number}
12+
- *
13+
- * @overload
14+
- * @param {string} a
15+
- * @param {boolean} b
16+
- * @returns {string}
17+
- *
18+
- * @param {string | number} a
19+
- * @param {string | number} b
20+
- * @returns {string | number}
21+
- */
722
-export function overloaded(a: number, b: number): number;
823
-/**
924
- * @overload
@@ -45,16 +60,7 @@
4560
- * @returns {string}
4661
- */
4762
-export function uncheckedInternally(a: string, b: boolean): string;
48-
+export declare function overloaded(a: string | number, b: string | number): string | number;
49-
+/**
50-
+ * @overload
51-
+ * @param {number} a
52-
+ * @param {number} b
53-
+ * @returns {number}
54-
+ *
55-
+ * @overload
56-
+ * @param {string} a
57-
+ * @param {boolean} b
58-
+ * @returns {string}
59-
+ */
60-
+export declare function uncheckedInternally(a: any, b: any): any;
63+
+export declare function overloaded(a: number, b: number): number;
64+
+export declare function overloaded(a: string, b: boolean): string;
65+
+export declare function uncheckedInternally(a: number, b: number): number;
66+
+export declare function uncheckedInternally(a: string, b: boolean): string;

testdata/baselines/reference/submodule/conformance/overloadTag1.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @returns {string | number}
1818
*/
1919
export function overloaded(a,b) {
20-
>overloaded : Symbol(overloaded, Decl(overloadTag1.js, 0, 0))
20+
>overloaded : Symbol(overloaded, Decl(overloadTag1.js, 1, 4), Decl(overloadTag1.js, 6, 4), Decl(overloadTag1.js, 0, 0))
2121
>a : Symbol(a, Decl(overloadTag1.js, 15, 27))
2222
>b : Symbol(b, Decl(overloadTag1.js, 15, 29))
2323

@@ -64,7 +64,7 @@ var o3 = overloaded("a",false)
6464
* @returns {string}
6565
*/
6666
export function uncheckedInternally(a, b) {
67-
>uncheckedInternally : Symbol(uncheckedInternally, Decl(overloadTag1.js, 25, 30))
67+
>uncheckedInternally : Symbol(uncheckedInternally, Decl(overloadTag1.js, 28, 4), Decl(overloadTag1.js, 33, 4), Decl(overloadTag1.js, 25, 30))
6868
>a : Symbol(a, Decl(overloadTag1.js, 38, 36))
6969
>b : Symbol(b, Decl(overloadTag1.js, 38, 38))
7070

testdata/baselines/reference/submodule/conformance/overloadTag1.symbols.diff

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
--- old.overloadTag1.symbols
22
+++ new.overloadTag1.symbols
3-
@@= skipped -41, +41 lines =@@
3+
@@= skipped -16, +16 lines =@@
4+
* @returns {string | number}
5+
*/
6+
export function overloaded(a,b) {
7+
->overloaded : Symbol(overloaded, Decl(overloadTag1.js, 0, 0))
8+
+>overloaded : Symbol(overloaded, Decl(overloadTag1.js, 1, 4), Decl(overloadTag1.js, 6, 4), Decl(overloadTag1.js, 0, 0))
9+
>a : Symbol(a, Decl(overloadTag1.js, 15, 27))
10+
>b : Symbol(b, Decl(overloadTag1.js, 15, 29))
11+
12+
@@= skipped -25, +25 lines =@@
413
}
514
var o1 = overloaded(1,2)
615
>o1 : Symbol(o1, Decl(overloadTag1.js, 23, 3))
@@ -19,7 +28,16 @@
1928

2029
/**
2130
* @overload
22-
@@= skipped -31, +31 lines =@@
31+
@@= skipped -22, +22 lines =@@
32+
* @returns {string}
33+
*/
34+
export function uncheckedInternally(a, b) {
35+
->uncheckedInternally : Symbol(uncheckedInternally, Decl(overloadTag1.js, 25, 30))
36+
+>uncheckedInternally : Symbol(uncheckedInternally, Decl(overloadTag1.js, 28, 4), Decl(overloadTag1.js, 33, 4), Decl(overloadTag1.js, 25, 30))
37+
>a : Symbol(a, Decl(overloadTag1.js, 38, 36))
38+
>b : Symbol(b, Decl(overloadTag1.js, 38, 38))
39+
40+
@@= skipped -9, +9 lines =@@
2341
>b : Symbol(b, Decl(overloadTag1.js, 38, 38))
2442
}
2543
uncheckedInternally(1,2)

testdata/baselines/reference/submodule/conformance/overloadTag1.types

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @returns {string | number}
1818
*/
1919
export function overloaded(a,b) {
20-
>overloaded : (a: string | number, b: string | number) => string | number
20+
>overloaded : { (a: number, b: number): number; (a: string, b: boolean): string; }
2121
>a : string | number
2222
>b : string | number
2323

@@ -59,23 +59,23 @@ export function overloaded(a,b) {
5959
>"Invalid arguments" : "Invalid arguments"
6060
}
6161
var o1 = overloaded(1,2)
62-
>o1 : string | number
63-
>overloaded(1,2) : string | number
64-
>overloaded : (a: string | number, b: string | number) => string | number
62+
>o1 : number
63+
>overloaded(1,2) : number
64+
>overloaded : { (a: number, b: number): number; (a: string, b: boolean): string; }
6565
>1 : 1
6666
>2 : 2
6767

6868
var o2 = overloaded("zero", "one")
69-
>o2 : string | number
70-
>overloaded("zero", "one") : string | number
71-
>overloaded : (a: string | number, b: string | number) => string | number
69+
>o2 : never
70+
>overloaded("zero", "one") : never
71+
>overloaded : { (a: number, b: number): number; (a: string, b: boolean): string; }
7272
>"zero" : "zero"
7373
>"one" : "one"
7474

7575
var o3 = overloaded("a",false)
76-
>o3 : string | number
77-
>overloaded("a",false) : string | number
78-
>overloaded : (a: string | number, b: string | number) => string | number
76+
>o3 : string
77+
>overloaded("a",false) : string
78+
>overloaded : { (a: number, b: number): number; (a: string, b: boolean): string; }
7979
>"a" : "a"
8080
>false : false
8181

@@ -91,7 +91,7 @@ var o3 = overloaded("a",false)
9191
* @returns {string}
9292
*/
9393
export function uncheckedInternally(a, b) {
94-
>uncheckedInternally : (a: any, b: any) => any
94+
>uncheckedInternally : { (a: number, b: number): number; (a: string, b: boolean): string; }
9595
>a : any
9696
>b : any
9797

@@ -101,14 +101,14 @@ export function uncheckedInternally(a, b) {
101101
>b : any
102102
}
103103
uncheckedInternally(1,2)
104-
>uncheckedInternally(1,2) : any
105-
>uncheckedInternally : (a: any, b: any) => any
104+
>uncheckedInternally(1,2) : number
105+
>uncheckedInternally : { (a: number, b: number): number; (a: string, b: boolean): string; }
106106
>1 : 1
107107
>2 : 2
108108

109109
uncheckedInternally("zero", "one")
110-
>uncheckedInternally("zero", "one") : any
111-
>uncheckedInternally : (a: any, b: any) => any
110+
>uncheckedInternally("zero", "one") : never
111+
>uncheckedInternally : { (a: number, b: number): number; (a: string, b: boolean): string; }
112112
>"zero" : "zero"
113113
>"one" : "one"
114114

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,52 @@
11
--- old.overloadTag1.errors.txt
22
+++ new.overloadTag1.errors.txt
33
@@= skipped -0, +0 lines =@@
4-
-overloadTag1.js(7,5): error TS2394: This overload signature is not compatible with its implementation signature.
4+
overloadTag1.js(7,5): error TS2394: This overload signature is not compatible with its implementation signature.
55
-overloadTag1.js(25,10): error TS2769: No overload matches this call.
66
- Overload 1 of 2, '(a: number, b: number): number', gave the following error.
77
- Argument of type 'string' is not assignable to parameter of type 'number'.
88
- Overload 2 of 2, '(a: string, b: boolean): string', gave the following error.
9-
- Argument of type 'string' is not assignable to parameter of type 'boolean'.
9+
+overloadTag1.js(25,29): error TS2769: No overload matches this call.
10+
+ The last overload gave the following error.
11+
Argument of type 'string' is not assignable to parameter of type 'boolean'.
1012
-overloadTag1.js(43,1): error TS2769: No overload matches this call.
1113
- Overload 1 of 2, '(a: number, b: number): number', gave the following error.
1214
- Argument of type 'string' is not assignable to parameter of type 'number'.
1315
- Overload 2 of 2, '(a: string, b: boolean): string', gave the following error.
14-
- Argument of type 'string' is not assignable to parameter of type 'boolean'.
15-
-
16-
-
17-
-==== overloadTag1.js (3 errors) ====
18-
+overloadTag1.js(26,25): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'.
19-
+
20-
+
21-
+==== overloadTag1.js (1 errors) ====
22-
/**
23-
* @overload
24-
* @param {number} a
25-
@@= skipped -18, +8 lines =@@
26-
* @returns {number}
27-
*
28-
* @overload
29-
- ~~~~~~~~
30-
-!!! error TS2394: This overload signature is not compatible with its implementation signature.
31-
-!!! related TS2750 overloadTag1.js:16:17: The implementation signature is declared here.
32-
* @param {string} a
33-
* @param {boolean} b
34-
* @returns {string}
35-
@@= skipped -21, +18 lines =@@
16+
+overloadTag1.js(43,29): error TS2769: No overload matches this call.
17+
+ The last overload gave the following error.
18+
Argument of type 'string' is not assignable to parameter of type 'boolean'.
19+
20+
21+
@@= skipped -39, +35 lines =@@
3622
}
3723
var o1 = overloaded(1,2)
3824
var o2 = overloaded("zero", "one")
3925
- ~~~~~~~~~~
40-
-!!! error TS2769: No overload matches this call.
26+
+ ~~~~~
27+
!!! error TS2769: No overload matches this call.
4128
-!!! error TS2769: Overload 1 of 2, '(a: number, b: number): number', gave the following error.
4229
-!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'number'.
4330
-!!! error TS2769: Overload 2 of 2, '(a: string, b: boolean): string', gave the following error.
44-
-!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'boolean'.
31+
+!!! error TS2769: The last overload gave the following error.
32+
!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'boolean'.
33+
+!!! related TS2771 overloadTag1.js:7:5: The last overload is declared here.
34+
+!!! related TS2793 overloadTag1.js:16:17: The call would have succeeded against this implementation, but implementation signatures of overloads are not externally visible.
4535
var o3 = overloaded("a",false)
46-
+ ~~~~~
47-
+!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'.
4836

4937
/**
50-
* @overload
51-
@@= skipped -24, +20 lines =@@
38+
@@= skipped -24, +24 lines =@@
5239
}
5340
uncheckedInternally(1,2)
5441
uncheckedInternally("zero", "one")
5542
- ~~~~~~~~~~~~~~~~~~~
56-
-!!! error TS2769: No overload matches this call.
43+
+ ~~~~~
44+
!!! error TS2769: No overload matches this call.
5745
-!!! error TS2769: Overload 1 of 2, '(a: number, b: number): number', gave the following error.
5846
-!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'number'.
5947
-!!! error TS2769: Overload 2 of 2, '(a: string, b: boolean): string', gave the following error.
60-
-!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'boolean'.
48+
+!!! error TS2769: The last overload gave the following error.
49+
!!! error TS2769: Argument of type 'string' is not assignable to parameter of type 'boolean'.
50+
+!!! related TS2771 overloadTag1.js:34:5: The last overload is declared here.
51+
+!!! related TS2793 overloadTag1.js:39:17: The call would have succeeded against this implementation, but implementation signatures of overloads are not externally visible.
6152

0 commit comments

Comments
 (0)