@@ -32,12 +32,19 @@ function optionalMember<const T>(prop: string, type: T, value?: Value) {
32
32
} ;
33
33
}
34
34
35
- function handleTyped ( type : Node , returnType : string ) {
35
+ function string ( arg : unknown ) : string {
36
+ if ( typeof arg !== "string" ) {
37
+ throw new Error ( `Expected a string but found ${ typeof arg } ` ) ;
38
+ }
39
+ return arg ;
40
+ }
41
+
42
+ function handleTyped ( type : Node , returnType ?: Value ) {
36
43
const isTyped = type . name == "type" ;
37
- const name = isTyped ? ( type . values [ 0 ] as string ) : returnType ;
44
+ const name = string ( isTyped ? type . values [ 0 ] : returnType ) ;
38
45
const subType =
39
46
type . children . length > 0
40
- ? { type : type . children [ 0 ] . values [ 0 ] as string }
47
+ ? { type : string ( type . children [ 0 ] . values [ 0 ] ) }
41
48
: undefined ;
42
49
return {
43
50
type : name ,
@@ -61,10 +68,7 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
61
68
const mixin : Record < string , DeepPartial < Interface > > = { } ;
62
69
63
70
for ( const node of nodes ) {
64
- const name = node . values [ 0 ] ;
65
- if ( typeof name !== "string" ) {
66
- throw new Error ( `Missing name for ${ node . name } ` ) ;
67
- }
71
+ const name = string ( node . values [ 0 ] ) ;
68
72
switch ( node . name ) {
69
73
case "enum" :
70
74
enums [ name ] = handleEnum ( node ) ;
@@ -87,10 +91,7 @@ function parseKDL(kdlText: string): DeepPartial<WebIdl> {
87
91
* @param enums The record of enums to update.
88
92
*/
89
93
function handleEnum ( node : Node ) : Enum {
90
- const name = node . properties ?. name || node . values [ 0 ] ;
91
- if ( typeof name !== "string" ) {
92
- throw new Error ( "Missing enum name" ) ;
93
- }
94
+ const name = string ( node . properties ?. name || node . values [ 0 ] ) ;
94
95
const values : string [ ] = [ ] ;
95
96
96
97
for ( const child of node . children ) {
@@ -109,9 +110,6 @@ function handleEnum(node: Node): Enum {
109
110
*/
110
111
function handleMixin ( node : Node ) : DeepPartial < Interface > {
111
112
const name = node . values [ 0 ] ;
112
- if ( typeof name !== "string" ) {
113
- throw new Error ( "Missing mixin name" ) ;
114
- }
115
113
116
114
const event : Event [ ] = [ ] ;
117
115
const property : Record < string , Partial < Property > > = { } ;
@@ -123,12 +121,12 @@ function handleMixin(node: Node): DeepPartial<Interface> {
123
121
event . push ( handleEvent ( child ) ) ;
124
122
break ;
125
123
case "property" : {
126
- const propName = child . values [ 0 ] as string ;
124
+ const propName = string ( child . values [ 0 ] ) ;
127
125
property [ propName ] = handleProperty ( child ) ;
128
126
break ;
129
127
}
130
128
case "method" : {
131
- const methodName = child . values [ 0 ] as string ;
129
+ const methodName = string ( child . values [ 0 ] ) ;
132
130
method [ methodName ] = handleMethod ( child ) ;
133
131
break ;
134
132
}
@@ -152,8 +150,8 @@ function handleMixin(node: Node): DeepPartial<Interface> {
152
150
*/
153
151
function handleEvent ( child : Node ) : Event {
154
152
return {
155
- name : child . values [ 0 ] as string ,
156
- type : child . properties . type as string ,
153
+ name : string ( child . values [ 0 ] ) ,
154
+ type : string ( child . properties . type ) ,
157
155
} ;
158
156
}
159
157
@@ -163,7 +161,7 @@ function handleEvent(child: Node): Event {
163
161
*/
164
162
function handleProperty ( child : Node ) : Partial < Property > {
165
163
return {
166
- name : child . values [ 0 ] as string ,
164
+ name : string ( child . values [ 0 ] ) ,
167
165
...optionalMember ( "exposed" , "string" , child . properties ?. exposed ) ,
168
166
...optionalMember ( "optional" , "boolean" , child . properties ?. optional ) ,
169
167
...optionalMember ( "overrideType" , "string" , child . properties ?. overrideType ) ,
@@ -175,15 +173,15 @@ function handleProperty(child: Node): Partial<Property> {
175
173
* @param child The child node to handle.
176
174
*/
177
175
function handleMethod ( child : Node ) : Partial < Method > {
178
- const name = child . values [ 0 ] as string ;
176
+ const name = string ( child . values [ 0 ] ) ;
179
177
const type = child . children [ 0 ] ;
180
- const returnType = child . properties . returns as string ;
178
+ const returnType = child . properties . returns ;
181
179
182
180
const params = child . children
183
181
. filter ( ( c ) => c . properties . type )
184
182
. map ( ( c ) => ( {
185
- name : c . values [ 0 ] as string ,
186
- type : c . properties . type as string ,
183
+ name : string ( c . values [ 0 ] ) ,
184
+ type : string ( c . properties . type ) ,
187
185
} ) ) ;
188
186
189
187
const signature : Method [ "signature" ] = [
0 commit comments