1
- import { parse , type Node } from "kdljs" ;
1
+ import { parse , type Value , type Node } from "kdljs" ;
2
2
import type { Enum , Event , Property , Interface , WebIdl } from "./types" ;
3
3
import { readdir , readFile } from "fs/promises" ;
4
4
import { merge } from "./helpers.js" ;
@@ -7,6 +7,24 @@ type DeepPartial<T> = T extends object
7
7
? { [ K in keyof T ] ?: DeepPartial < T [ K ] > }
8
8
: T ;
9
9
10
+ function optionalMember < const T > ( prop : string , type : T , value ?: Value ) {
11
+ if ( value === undefined ) {
12
+ return { } ;
13
+ }
14
+ if ( typeof value !== type ) {
15
+ throw new Error ( `Expected type ${ value } for ${ prop } ` ) ;
16
+ }
17
+ return {
18
+ [ prop ] : value as T extends "string"
19
+ ? string
20
+ : T extends "number"
21
+ ? number
22
+ : T extends "boolean"
23
+ ? boolean
24
+ : never ,
25
+ } ;
26
+ }
27
+
10
28
/**
11
29
* Converts patch files in KDL to match the [types](types.d.ts).
12
30
*/
@@ -92,15 +110,12 @@ function handleMixin(node: Node): DeepPartial<Interface> {
92
110
}
93
111
}
94
112
95
- const result = {
113
+ return {
96
114
name,
97
115
events : { event } ,
98
116
properties : { property } ,
117
+ ...optionalMember ( "extends" , "string" , node . properties ?. extends ) ,
99
118
} as DeepPartial < Interface > ;
100
- if ( node . properties . extends ) {
101
- result . extends = node . properties . extends as string ;
102
- }
103
- return result ;
104
119
}
105
120
106
121
/**
@@ -121,7 +136,9 @@ function handleEvent(child: Node): Event {
121
136
function handleProperty ( child : Node ) : Partial < Property > {
122
137
return {
123
138
name : child . values [ 0 ] as string ,
124
- exposed : child . properties ?. exposed as string ,
139
+ ...optionalMember ( "exposed" , "string" , child . properties ?. exposed ) ,
140
+ ...optionalMember ( "optional" , "boolean" , child . properties ?. optional ) ,
141
+ ...optionalMember ( "overrideType" , "string" , child . properties ?. overrideType ) ,
125
142
} ;
126
143
}
127
144
0 commit comments