Skip to content

Commit d222dd8

Browse files
authored
support @type on parameters (#1427)
1 parent 96c0688 commit d222dd8

File tree

7 files changed

+37
-67
lines changed

7 files changed

+37
-67
lines changed

internal/parser/reparser.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node)
292292
parent.AsVariableDeclaration().Type = p.factory.DeepCloneReparse(tag.AsJSDocTypeTag().TypeExpression.Type())
293293
p.finishMutatedNode(parent)
294294
}
295+
case ast.KindParameter:
296+
param := parent.AsParameterDeclaration()
297+
if param.Type == nil && tag.AsJSDocTypeTag().TypeExpression != nil {
298+
param.Type = p.reparseJSDocTypeLiteral(tag.AsJSDocTypeTag().TypeExpression.Type())
299+
}
300+
p.finishMutatedNode(parent)
295301
case ast.KindCommonJSExport:
296302
export := parent.AsCommonJSExport()
297303
if export.Type == nil && tag.AsJSDocTypeTag().TypeExpression != nil {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ declare const TabbedShowLayout: {
216216
export default TabbedShowLayout;
217217
//// [jsDeclarationsReactComponents4.d.ts]
218218
declare const TabbedShowLayout: {
219-
(prop: any): JSX.Element;
219+
(prop: {
220+
className: string;
221+
}): JSX.Element;
220222
defaultProps: {
221223
tabs: string;
222224
};
@@ -236,7 +238,7 @@ out/jsDeclarationsReactComponents1.d.ts(1,23): error TS2307: Cannot find module
236238
out/jsDeclarationsReactComponents1.d.ts(3,15): error TS2503: Cannot find namespace 'JSX'.
237239
out/jsDeclarationsReactComponents2.d.ts(1,19): error TS2307: Cannot find module 'react' or its corresponding type declarations.
238240
out/jsDeclarationsReactComponents3.d.ts(10,7): error TS2503: Cannot find namespace 'JSX'.
239-
out/jsDeclarationsReactComponents4.d.ts(2,18): error TS2503: Cannot find namespace 'JSX'.
241+
out/jsDeclarationsReactComponents4.d.ts(4,9): error TS2503: Cannot find namespace 'JSX'.
240242
out/jsDeclarationsReactComponents5.d.ts(3,5): error TS2503: Cannot find namespace 'JSX'.
241243

242244

@@ -284,8 +286,10 @@ out/jsDeclarationsReactComponents5.d.ts(3,5): error TS2503: Cannot find namespac
284286

285287
==== out/jsDeclarationsReactComponents4.d.ts (1 errors) ====
286288
declare const TabbedShowLayout: {
287-
(prop: any): JSX.Element;
288-
~~~
289+
(prop: {
290+
className: string;
291+
}): JSX.Element;
292+
~~~
289293
!!! error TS2503: Cannot find namespace 'JSX'.
290294
defaultProps: {
291295
tabs: string;

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@
114114
+export default TabbedShowLayout;
115115
//// [jsDeclarationsReactComponents4.d.ts]
116116
+declare const TabbedShowLayout: {
117-
+ (prop: any): JSX.Element;
117+
+ (prop: {
118+
+ className: string;
119+
+ }): JSX.Element;
118120
+ defaultProps: {
119121
+ tabs: string;
120122
+ };
@@ -154,7 +156,7 @@
154156
+out/jsDeclarationsReactComponents1.d.ts(3,15): error TS2503: Cannot find namespace 'JSX'.
155157
+out/jsDeclarationsReactComponents2.d.ts(1,19): error TS2307: Cannot find module 'react' or its corresponding type declarations.
156158
+out/jsDeclarationsReactComponents3.d.ts(10,7): error TS2503: Cannot find namespace 'JSX'.
157-
+out/jsDeclarationsReactComponents4.d.ts(2,18): error TS2503: Cannot find namespace 'JSX'.
159+
+out/jsDeclarationsReactComponents4.d.ts(4,9): error TS2503: Cannot find namespace 'JSX'.
158160
+out/jsDeclarationsReactComponents5.d.ts(3,5): error TS2503: Cannot find namespace 'JSX'.
159161
+
160162
+
@@ -202,8 +204,10 @@
202204
+
203205
+==== out/jsDeclarationsReactComponents4.d.ts (1 errors) ====
204206
+ declare const TabbedShowLayout: {
205-
+ (prop: any): JSX.Element;
206-
+ ~~~
207+
+ (prop: {
208+
+ className: string;
209+
+ }): JSX.Element;
210+
+ ~~~
207211
+!!! error TS2503: Cannot find namespace 'JSX'.
208212
+ defaultProps: {
209213
+ tabs: string;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ const TabbedShowLayout = (/** @type {{className: string}}*/prop) => {
130130
<div className={prop.className} key="">
131131
>div : Symbol(div, Decl(react16.d.ts, 2546, 114))
132132
>className : Symbol(className, Decl(jsDeclarationsReactComponents4.jsx, 3, 12))
133+
>prop.className : Symbol(className, Decl(jsDeclarationsReactComponents4.jsx, 1, 38))
133134
>prop : Symbol(prop, Decl(jsDeclarationsReactComponents4.jsx, 1, 26))
135+
>className : Symbol(className, Decl(jsDeclarationsReactComponents4.jsx, 1, 38))
134136
>key : Symbol(key, Decl(jsDeclarationsReactComponents4.jsx, 3, 39))
135137

136138
ok

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@
144144
->div : Symbol(JSX.IntrinsicElements.div, Decl(react16.d.ts, 2546, 114))
145145
+>div : Symbol(div, Decl(react16.d.ts, 2546, 114))
146146
>className : Symbol(className, Decl(jsDeclarationsReactComponents4.jsx, 3, 12))
147-
->prop.className : Symbol(className, Decl(jsDeclarationsReactComponents4.jsx, 1, 38))
147+
>prop.className : Symbol(className, Decl(jsDeclarationsReactComponents4.jsx, 1, 38))
148148
>prop : Symbol(prop, Decl(jsDeclarationsReactComponents4.jsx, 1, 26))
149-
->className : Symbol(className, Decl(jsDeclarationsReactComponents4.jsx, 1, 38))
150-
>key : Symbol(key, Decl(jsDeclarationsReactComponents4.jsx, 3, 39))
149+
@@= skipped -21, +21 lines =@@
151150

152151
ok
153152
</div>
@@ -167,7 +166,7 @@
167166

168167
tabs: "default value"
169168
>tabs : Symbol(tabs, Decl(jsDeclarationsReactComponents4.jsx, 9, 33))
170-
@@= skipped -37, +35 lines =@@
169+
@@= skipped -16, +16 lines =@@
171170
};
172171

173172
export default TabbedShowLayout;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,20 @@ import React from "react";
145145
>React : typeof React
146146

147147
const TabbedShowLayout = (/** @type {{className: string}}*/prop) => {
148-
>TabbedShowLayout : { (prop: any): JSX.Element; defaultProps: { tabs: string; }; }
149-
>(/** @type {{className: string}}*/prop) => { return ( <div className={prop.className} key=""> ok </div> );} : { (prop: any): JSX.Element; defaultProps: { tabs: string; }; }
150-
>prop : any
148+
>TabbedShowLayout : { (prop: { className: string; }): JSX.Element; defaultProps: { tabs: string; }; }
149+
>(/** @type {{className: string}}*/prop) => { return ( <div className={prop.className} key=""> ok </div> );} : { (prop: { className: string; }): JSX.Element; defaultProps: { tabs: string; }; }
150+
>prop : { className: string; }
151151

152152
return (
153153
>( <div className={prop.className} key=""> ok </div> ) : JSX.Element
154154

155155
<div className={prop.className} key="">
156156
><div className={prop.className} key=""> ok </div> : JSX.Element
157157
>div : any
158-
>className : any
159-
>prop.className : any
160-
>prop : any
161-
>className : any
158+
>className : string
159+
>prop.className : string
160+
>prop : { className: string; }
161+
>className : string
162162
>key : string
163163

164164
ok
@@ -171,7 +171,7 @@ const TabbedShowLayout = (/** @type {{className: string}}*/prop) => {
171171
TabbedShowLayout.defaultProps = {
172172
>TabbedShowLayout.defaultProps = { tabs: "default value"} : { tabs: string; }
173173
>TabbedShowLayout.defaultProps : { tabs: string; }
174-
>TabbedShowLayout : { (prop: any): JSX.Element; defaultProps: { tabs: string; }; }
174+
>TabbedShowLayout : { (prop: { className: string; }): JSX.Element; defaultProps: { tabs: string; }; }
175175
>defaultProps : { tabs: string; }
176176
>{ tabs: "default value"} : { tabs: string; }
177177

@@ -182,7 +182,7 @@ TabbedShowLayout.defaultProps = {
182182
};
183183

184184
export default TabbedShowLayout;
185-
>TabbedShowLayout : { (prop: any): JSX.Element; defaultProps: { tabs: string; }; }
185+
>TabbedShowLayout : { (prop: { className: string; }): JSX.Element; defaultProps: { tabs: string; }; }
186186

187187
=== jsDeclarationsReactComponents5.jsx ===
188188
import React from 'react';

testdata/baselines/reference/submoduleAccepted/conformance/jsDeclarationsReactComponents.types.diff

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,52 +36,7 @@
3636

3737
=== jsDeclarationsReactComponents4.jsx ===
3838
import React from "react";
39-
>React : typeof React
40-
41-
const TabbedShowLayout = (/** @type {{className: string}}*/prop) => {
42-
->TabbedShowLayout : { (prop: { className: string; }): JSX.Element; defaultProps: { tabs: string; }; }
43-
->(/** @type {{className: string}}*/prop) => { return ( <div className={prop.className} key=""> ok </div> );} : { (prop: { className: string; }): JSX.Element; defaultProps: { tabs: string; }; }
44-
->prop : { className: string; }
45-
+>TabbedShowLayout : { (prop: any): JSX.Element; defaultProps: { tabs: string; }; }
46-
+>(/** @type {{className: string}}*/prop) => { return ( <div className={prop.className} key=""> ok </div> );} : { (prop: any): JSX.Element; defaultProps: { tabs: string; }; }
47-
+>prop : any
48-
49-
return (
50-
>( <div className={prop.className} key=""> ok </div> ) : JSX.Element
51-
@@= skipped -17, +17 lines =@@
52-
<div className={prop.className} key="">
53-
><div className={prop.className} key=""> ok </div> : JSX.Element
54-
>div : any
55-
->className : string
56-
->prop.className : string
57-
->prop : { className: string; }
58-
->className : string
59-
+>className : any
60-
+>prop.className : any
61-
+>prop : any
62-
+>className : any
63-
>key : string
64-
65-
ok
66-
@@= skipped -16, +16 lines =@@
67-
TabbedShowLayout.defaultProps = {
68-
>TabbedShowLayout.defaultProps = { tabs: "default value"} : { tabs: string; }
69-
>TabbedShowLayout.defaultProps : { tabs: string; }
70-
->TabbedShowLayout : { (prop: { className: string; }): JSX.Element; defaultProps: { tabs: string; }; }
71-
+>TabbedShowLayout : { (prop: any): JSX.Element; defaultProps: { tabs: string; }; }
72-
>defaultProps : { tabs: string; }
73-
>{ tabs: "default value"} : { tabs: string; }
74-
75-
@@= skipped -11, +11 lines =@@
76-
};
77-
78-
export default TabbedShowLayout;
79-
->TabbedShowLayout : { (prop: { className: string; }): JSX.Element; defaultProps: { tabs: string; }; }
80-
+>TabbedShowLayout : { (prop: any): JSX.Element; defaultProps: { tabs: string; }; }
81-
82-
=== jsDeclarationsReactComponents5.jsx ===
83-
import React from 'react';
84-
@@= skipped -10, +10 lines =@@
39+
@@= skipped -54, +54 lines =@@
8540
>PropTypes : typeof PropTypes
8641

8742
function Tree({ allowDropOnRoot }) {

0 commit comments

Comments
 (0)