11import { JSXElement } from "revolution/jsx-runtime" ;
22import { type Operation } from "effection" ;
33import type {
4- DocNode ,
4+ Declaration ,
55 ParamDef ,
66 TsTypeDef ,
77 TsTypeParamDef ,
88 TsTypeRefDef ,
99 VariableDef ,
1010} from "@deno/doc" ;
11+ import type { SymbolInfo } from "../../hooks/use-deno-doc.tsx" ;
1112import {
1213 Builtin ,
1314 ClassName ,
@@ -18,51 +19,53 @@ import {
1819} from "./tokens.tsx" ;
1920
2021interface TypeProps {
21- node : DocNode ;
22+ declaration : Declaration ;
23+ symbol : SymbolInfo ;
2224}
2325
2426export function * Type ( props : TypeProps ) : Operation < JSXElement > {
25- let { node } = props ;
27+ // `node` aliases the declaration; `symbol` supplies the name.
28+ let { declaration : node , symbol } = props ;
2629
2730 switch ( node . kind ) {
28- case "function" :
31+ case "function" : {
32+ let typeParams = node . def . typeParams ?? [ ] ;
2933 return (
3034 < span class = "language-ts code-highlight inline-block" >
31- { node . functionDef . isAsync
32- ? < Punctuation > { "async " } </ Punctuation >
33- : < > </ > }
35+ { node . def . isAsync ? < Punctuation > { "async " } </ Punctuation > : < > </ > }
3436 < Keyword > { node . kind } </ Keyword >
35- { node . functionDef . isGenerator ? < Punctuation > *</ Punctuation > : < > </ > }
36- { " " }
37- < span class = "token function" > { node . name } </ span >
38- { node . functionDef . typeParams . length > 0
39- ? < InterfaceTypeParams typeParams = { node . functionDef . typeParams } />
37+ { node . def . isGenerator ? < Punctuation > *</ Punctuation > : < > </ > } { " " }
38+ < span class = "token function" > { symbol . name } </ span >
39+ { typeParams . length > 0
40+ ? < InterfaceTypeParams typeParams = { typeParams } />
4041 : < > </ > }
4142 < Punctuation > (</ Punctuation >
42- < FunctionParams params = { node . functionDef . params } />
43- < Punctuation > )</ Punctuation > : { node . functionDef . returnType
44- ? < TypeDef typeDef = { node . functionDef . returnType } />
43+ < FunctionParams params = { node . def . params } />
44+ < Punctuation > )</ Punctuation > : { node . def . returnType
45+ ? < TypeDef typeDef = { node . def . returnType } />
4546 : < > </ > }
4647 </ span >
4748 ) ;
48- case "class" :
49+ }
50+ case "class" : {
51+ let impl /* implements */ = node . def . implements ?? [ ] ;
4952 return (
5053 < span class = "language-ts code-highlight inline-block" >
51- < Keyword > { node . kind } </ Keyword > < ClassName > { node . name } </ ClassName >
52- { node . classDef . extends
54+ < Keyword > { node . kind } </ Keyword > < ClassName > { symbol . name } </ ClassName >
55+ { node . def . extends
5356 ? (
5457 < >
5558 < Keyword > { " extends " } </ Keyword >
56- < ClassName > { node . classDef . extends } </ ClassName >
59+ < ClassName > { node . def . extends } </ ClassName >
5760 </ >
5861 )
5962 : < > </ > }
60- { node . classDef . implements
63+ { impl . length > 0
6164 ? (
6265 < >
6366 < Keyword > { " implements " } </ Keyword >
6467 < >
65- { node . classDef . implements
68+ { impl
6669 . flatMap ( ( typeDef ) => [ < TypeDef typeDef = { typeDef } /> , ", " ] )
6770 . slice ( 0 , - 1 ) }
6871 </ >
@@ -71,19 +74,22 @@ export function* Type(props: TypeProps): Operation<JSXElement> {
7174 : < > </ > }
7275 </ span >
7376 ) ;
74- case "interface" :
77+ }
78+ case "interface" : {
79+ let typeParams = node . def . typeParams ?? [ ] ;
80+ let ext = node . def . extends ?? [ ] ;
7581 return (
7682 < span class = "language-ts code-highlight inline-block" >
77- < Keyword > { node . kind } </ Keyword > < ClassName > { node . name } </ ClassName >
78- { node . interfaceDef . typeParams . length > 0
79- ? < InterfaceTypeParams typeParams = { node . interfaceDef . typeParams } />
83+ < Keyword > { node . kind } </ Keyword > < ClassName > { symbol . name } </ ClassName >
84+ { typeParams . length > 0
85+ ? < InterfaceTypeParams typeParams = { typeParams } />
8086 : < > </ > }
81- { node . interfaceDef . extends . length > 0
87+ { ext . length > 0
8288 ? (
8389 < >
8490 < Keyword > { " extends " } </ Keyword >
8591 < >
86- { node . interfaceDef . extends
92+ { ext
8793 . flatMap ( ( typeDef ) => [ < TypeDef typeDef = { typeDef } /> , ", " ] )
8894 . slice ( 0 , - 1 ) }
8995 </ >
@@ -92,30 +98,29 @@ export function* Type(props: TypeProps): Operation<JSXElement> {
9298 : < > </ > }
9399 </ span >
94100 ) ;
101+ }
95102 case "variable" :
96103 return (
97104 < span class = "inline-block" >
98- < TSVariableDef variableDef = { node . variableDef } name = { node . name } />
105+ < TSVariableDef variableDef = { node . def } name = { symbol . name } />
99106 </ span >
100107 ) ;
101108 case "typeAlias" :
102109 return (
103110 < span class = "inline-block" >
104111 < Keyword > { "type " } </ Keyword >
105- { node . name }
112+ { symbol . name }
106113 < Operator > { " = " } </ Operator >
107- < TypeDef typeDef = { node . typeAliasDef . tsType } />
114+ < TypeDef typeDef = { node . def . tsType } />
108115 </ span >
109116 ) ;
110117 case "enum" :
111- case "import" :
112- case "moduleDoc" :
113118 case "namespace" :
114119 default :
115120 console . log ( "<Type> unimplemented" , node . kind ) ;
116121 return (
117122 < span class = "inline-block" >
118- < Keyword > { node . kind } </ Keyword > { node . name }
123+ < Keyword > { node . kind } </ Keyword > { symbol . name }
119124 </ span >
120125 ) ;
121126 }
@@ -187,51 +192,51 @@ function TSParam({ param }: { param: ParamDef }) {
187192export function TypeDef ( { typeDef } : { typeDef : TsTypeDef } ) {
188193 switch ( typeDef . kind ) {
189194 case "literal" :
190- switch ( typeDef . literal . kind ) {
195+ switch ( typeDef . value . kind ) {
191196 case "string" :
192- return < span class = "token string" > "{ typeDef . repr } "</ span > ;
197+ return < span class = "token string" > "{ typeDef . repr ?? "" } "</ span > ;
193198 case "number" :
194- return < span class = "token number" > { typeDef . repr } </ span > ;
199+ return < span class = "token number" > { typeDef . repr ?? "" } </ span > ;
195200 case "boolean" :
196- return < span class = "token boolean" > { typeDef . repr } </ span > ;
201+ return < span class = "token boolean" > { typeDef . repr ?? "" } </ span > ;
197202 case "bigInt" :
198- return < span class = "token number" > { typeDef . repr } </ span > ;
203+ return < span class = "token number" > { typeDef . repr ?? "" } </ span > ;
199204 default :
200205 // TODO(taras): implement template
201206 return < > </ > ;
202207 }
203208 case "keyword" :
204- if ( [ "number" , "string" , "boolean" , "bigint" ] . includes ( typeDef . keyword ) ) {
205- return < Builtin > { typeDef . keyword } </ Builtin > ;
209+ if ( [ "number" , "string" , "boolean" , "bigint" ] . includes ( typeDef . value ) ) {
210+ return < Builtin > { typeDef . value } </ Builtin > ;
206211 } else {
207- return < Keyword > { typeDef . keyword } </ Keyword > ;
212+ return < Keyword > { typeDef . value } </ Keyword > ;
208213 }
209214 case "typeRef" :
210- return < TypeRef typeRef = { typeDef . typeRef } /> ;
215+ return < TypeRef typeRef = { typeDef . value } /> ;
211216 case "union" :
212- return < TypeDefUnion union = { typeDef . union } /> ;
217+ return < TypeDefUnion union = { typeDef . value } /> ;
213218 case "fnOrConstructor" :
214- if ( typeDef . fnOrConstructor . constructor ) {
215- console . log ( `<TypeDef> unimplemeneted` , typeDef . fnOrConstructor ) ;
219+ if ( typeDef . value . constructor ) {
220+ console . log ( `<TypeDef> unimplemeneted` , typeDef . value ) ;
216221 // TODO(taras): implement
217222 return < > </ > ;
218223 } else {
219224 return (
220225 < >
221226 < Punctuation > (</ Punctuation >
222- < FunctionParams params = { typeDef . fnOrConstructor . params } />
227+ < FunctionParams params = { typeDef . value . params } />
223228 < Punctuation > )</ Punctuation >
224229 < Operator > { " => " } </ Operator >
225- < TypeDef typeDef = { typeDef . fnOrConstructor . tsType } />
230+ < TypeDef typeDef = { typeDef . value . tsType } />
226231 </ >
227232 ) ;
228233 }
229234 case "indexedAccess" :
230235 return (
231236 < >
232- < TypeDef typeDef = { typeDef . indexedAccess . objType } />
237+ < TypeDef typeDef = { typeDef . value . objType } />
233238 < Punctuation > [</ Punctuation >
234- < TypeDef typeDef = { typeDef . indexedAccess . indexType } />
239+ < TypeDef typeDef = { typeDef . value . indexType } />
235240 < Punctuation > ]</ Punctuation >
236241 </ >
237242 ) ;
@@ -240,7 +245,7 @@ export function TypeDef({ typeDef }: { typeDef: TsTypeDef }) {
240245 < >
241246 < Punctuation > [</ Punctuation >
242247 < >
243- { typeDef . tuple
248+ { typeDef . value
244249 . flatMap ( ( tp ) => [ < TypeDef typeDef = { tp } /> , ", " ] )
245250 . slice ( 0 , - 1 ) }
246251 </ >
@@ -250,30 +255,30 @@ export function TypeDef({ typeDef }: { typeDef: TsTypeDef }) {
250255 case "array" :
251256 return (
252257 < >
253- < TypeDef typeDef = { typeDef . array } />
258+ < TypeDef typeDef = { typeDef . value } />
254259 < Punctuation > []</ Punctuation >
255260 </ >
256261 ) ;
257262 case "typeOperator" :
258263 return (
259264 < >
260- < Keyword > { typeDef . typeOperator . operator } </ Keyword > { " " }
261- < TypeDef typeDef = { typeDef . typeOperator . tsType } />
265+ < Keyword > { typeDef . value . operator } </ Keyword > { " " }
266+ < TypeDef typeDef = { typeDef . value . tsType } />
262267 </ >
263268 ) ;
264269 case "parenthesized" : {
265270 return (
266271 < >
267272 < Punctuation > (</ Punctuation >
268- < TypeDef typeDef = { typeDef . parenthesized } />
273+ < TypeDef typeDef = { typeDef . value } />
269274 < Punctuation > )</ Punctuation >
270275 </ >
271276 ) ;
272277 }
273278 case "intersection" : {
274279 return (
275280 < >
276- { typeDef . intersection
281+ { typeDef . value
277282 . flatMap ( ( tp ) => [
278283 < TypeDef typeDef = { tp } /> ,
279284 < Operator > { " & " } </ Operator > ,
@@ -294,37 +299,37 @@ export function TypeDef({ typeDef }: { typeDef: TsTypeDef }) {
294299 case "conditional" : {
295300 return (
296301 < >
297- < TypeDef typeDef = { typeDef . conditionalType . checkType } />
302+ < TypeDef typeDef = { typeDef . value . checkType } />
298303 < Keyword > { " extends " } </ Keyword >
299- < TypeDef typeDef = { typeDef . conditionalType . extendsType } />
304+ < TypeDef typeDef = { typeDef . value . extendsType } />
300305 < Operator > { " ? " } </ Operator >
301- < TypeDef typeDef = { typeDef . conditionalType . trueType } />
306+ < TypeDef typeDef = { typeDef . value . trueType } />
302307 < Operator > { " : " } </ Operator >
303- < TypeDef typeDef = { typeDef . conditionalType . falseType } />
308+ < TypeDef typeDef = { typeDef . value . falseType } />
304309 </ >
305310 ) ;
306311 }
307312 case "infer" : {
308313 return (
309314 < >
310315 < Keyword > { "infer " } </ Keyword >
311- { typeDef . infer . typeParam . name }
316+ { typeDef . value . typeParam . name }
312317 </ >
313318 ) ;
314319 }
315320 case "mapped" :
316321 return (
317322 < >
318323 < Punctuation > [</ Punctuation >
319- { typeDef . mappedType . typeParam . name }
324+ { typeDef . value . typeParam . name }
320325 < Keyword > { ` in ` } </ Keyword >
321- { typeDef . mappedType . typeParam . constraint
322- ? < TypeDef typeDef = { typeDef . mappedType . typeParam . constraint } />
326+ { typeDef . value . typeParam . constraint
327+ ? < TypeDef typeDef = { typeDef . value . typeParam . constraint } />
323328 : < > </ > }
324329 < Punctuation > ]</ Punctuation >
325330 < Operator > { " : " } </ Operator >
326- { typeDef . mappedType . tsType
327- ? < TypeDef typeDef = { typeDef . mappedType . tsType } />
331+ { typeDef . value . tsType
332+ ? < TypeDef typeDef = { typeDef . value . tsType } />
328333 : < > </ > }
329334 </ >
330335 ) ;
0 commit comments