@@ -3,15 +3,31 @@ import * as assert from "assert";
3
3
import * as fs from "fs" ;
4
4
import * as path from "path" ;
5
5
import * as ts from "typescript" ;
6
- import { Type , TypeParam , Comment , Field , Class , Struct , TypeDef , Function , Variable } from "./types" ;
6
+ import {
7
+ Type ,
8
+ TypeParam ,
9
+ Comment ,
10
+ Field ,
11
+ Class ,
12
+ Struct ,
13
+ TypeDef ,
14
+ Function ,
15
+ Variable ,
16
+ } from "./types" ;
7
17
8
18
// Get files to build overrides from
9
19
const publicOverridesDir = path . join ( __dirname , ".." , "overrides" ) ;
10
- const filePaths = fs . readdirSync ( publicOverridesDir ) . map ( ( fileName ) => path . join ( publicOverridesDir , fileName ) ) ;
20
+ const filePaths = fs
21
+ . readdirSync ( publicOverridesDir )
22
+ . map ( ( fileName ) => path . join ( publicOverridesDir , fileName ) ) ;
11
23
// Additional internal overrides
12
24
const additionalOverridesDir = process . argv [ 2 ] && path . resolve ( process . argv [ 2 ] ) ;
13
- if ( additionalOverridesDir ) {
14
- filePaths . push ( ...fs . readdirSync ( additionalOverridesDir ) . map ( ( fileName ) => path . join ( additionalOverridesDir , fileName ) ) )
25
+ if ( additionalOverridesDir ) {
26
+ filePaths . push (
27
+ ...fs
28
+ . readdirSync ( additionalOverridesDir )
29
+ . map ( ( fileName ) => path . join ( additionalOverridesDir , fileName ) )
30
+ ) ;
15
31
}
16
32
17
33
// Parse file into AST
@@ -81,7 +97,10 @@ for (const filePath of filePaths) {
81
97
name : "typeof" ,
82
98
args : [ { name : type . exprName . getText ( sourceFile ) } ] ,
83
99
} ;
84
- } else if ( ts . isTypeOperatorNode ( type ) && type . operator === ts . SyntaxKind . KeyOfKeyword ) {
100
+ } else if (
101
+ ts . isTypeOperatorNode ( type ) &&
102
+ type . operator === ts . SyntaxKind . KeyOfKeyword
103
+ ) {
85
104
return {
86
105
name : "keyof" ,
87
106
args : [ { name : type . type . getText ( sourceFile ) } ] ,
@@ -97,7 +116,9 @@ for (const filePath of filePaths) {
97
116
} else if ( ts . isParenthesizedTypeNode ( type ) ) {
98
117
return convertType ( type . type ) ;
99
118
}
100
- throw new TypeError ( `unrecognised type at ${ getPos ( type . pos ) } : ${ type . getText ( sourceFile ) } ` ) ;
119
+ throw new TypeError (
120
+ `unrecognised type at ${ getPos ( type . pos ) } : ${ type . getText ( sourceFile ) } `
121
+ ) ;
101
122
}
102
123
103
124
function convertTupleMember ( node : ts . TypeNode | ts . NamedTupleMember ) : Field {
@@ -111,15 +132,19 @@ for (const filePath of filePaths) {
111
132
}
112
133
}
113
134
114
- function convertTypeParams ( nodes ?: ts . NodeArray < ts . TypeParameterDeclaration > ) : TypeParam [ ] | undefined {
135
+ function convertTypeParams (
136
+ nodes ?: ts . NodeArray < ts . TypeParameterDeclaration >
137
+ ) : TypeParam [ ] | undefined {
115
138
return nodes ?. map ( ( node ) => ( {
116
139
name : node . name . getText ( sourceFile ) ,
117
140
constraint : node . constraint && convertType ( node . constraint ) ,
118
141
default : node . default && convertType ( node . default ) ,
119
142
} ) ) ;
120
143
}
121
144
122
- function convertCommentText ( text ?: string | ts . NodeArray < ts . JSDocText | ts . JSDocLink > ) : string | undefined {
145
+ function convertCommentText (
146
+ text ?: string | ts . NodeArray < ts . JSDocText | ts . JSDocLink >
147
+ ) : string | undefined {
123
148
if ( text === undefined || typeof text === "string" ) {
124
149
return text ?. toString ( ) ;
125
150
} else {
@@ -186,7 +211,11 @@ for (const filePath of filePaths) {
186
211
}
187
212
188
213
function convertMethod (
189
- node : ts . MethodSignature | ts . MethodDeclaration | ts . FunctionDeclaration | ts . ConstructSignatureDeclaration
214
+ node :
215
+ | ts . MethodSignature
216
+ | ts . MethodDeclaration
217
+ | ts . FunctionDeclaration
218
+ | ts . ConstructSignatureDeclaration
190
219
) : Field {
191
220
const defaultName = ts . isConstructSignatureDeclaration ( node ) ? "new" : "" ;
192
221
const name = node . name ?. getText ( sourceFile ) ?? defaultName ;
@@ -199,7 +228,9 @@ for (const filePath of filePaths) {
199
228
return { name, type, ...meta , typeparams } ;
200
229
}
201
230
202
- function convertProperty ( node : ts . PropertySignature | ts . PropertyDeclaration ) : Field {
231
+ function convertProperty (
232
+ node : ts . PropertySignature | ts . PropertyDeclaration
233
+ ) : Field {
203
234
const name = node . name . getText ( sourceFile ) ;
204
235
const type = convertType ( node . type ) ;
205
236
type . optional = node . questionToken && true ;
@@ -218,7 +249,11 @@ for (const filePath of filePaths) {
218
249
if ( ts . isConstructorDeclaration ( node ) ) {
219
250
return convertConstructor ( node ) ;
220
251
}
221
- if ( ts . isMethodSignature ( node ) || ts . isMethodDeclaration ( node ) || ts . isConstructSignatureDeclaration ( node ) ) {
252
+ if (
253
+ ts . isMethodSignature ( node ) ||
254
+ ts . isMethodDeclaration ( node ) ||
255
+ ts . isConstructSignatureDeclaration ( node )
256
+ ) {
222
257
return convertMethod ( node ) ;
223
258
}
224
259
if ( ts . isPropertySignature ( node ) || ts . isPropertyDeclaration ( node ) ) {
@@ -227,7 +262,9 @@ for (const filePath of filePaths) {
227
262
if ( ts . isIndexSignatureDeclaration ( node ) ) {
228
263
return convertIndexSignature ( node ) ;
229
264
}
230
- throw new TypeError ( `unrecognised member at ${ getPos ( node . pos ) } : ${ node . getText ( sourceFile ) } ` ) ;
265
+ throw new TypeError (
266
+ `unrecognised member at ${ getPos ( node . pos ) } : ${ node . getText ( sourceFile ) } `
267
+ ) ;
231
268
}
232
269
233
270
function convertHeritageClause ( node : ts . HeritageClause ) : Type [ ] {
@@ -237,7 +274,9 @@ for (const filePath of filePaths) {
237
274
} ) ) ;
238
275
}
239
276
240
- function convertHeritageClauses ( nodes ?: ts . NodeArray < ts . HeritageClause > ) : Pick < Class , "extends" | "implements" > {
277
+ function convertHeritageClauses (
278
+ nodes ?: ts . NodeArray < ts . HeritageClause >
279
+ ) : Pick < Class , "extends" | "implements" > {
241
280
const heritage : Pick < Class , "extends" | "implements" > = { } ;
242
281
for ( const node of nodes ?? [ ] ) {
243
282
if ( node . token === ts . SyntaxKind . ExtendsKeyword ) {
@@ -337,7 +376,11 @@ for (const filePath of filePaths) {
337
376
} else if ( ts . isVariableStatement ( node ) ) {
338
377
return convertVariable ( node ) ;
339
378
}
340
- throw new TypeError ( `unrecognised statement at ${ getPos ( node . pos ) } : ${ node . getText ( sourceFile ) } ` ) ;
379
+ throw new TypeError (
380
+ `unrecognised statement at ${ getPos ( node . pos ) } : ${ node . getText (
381
+ sourceFile
382
+ ) } `
383
+ ) ;
341
384
}
342
385
343
386
/**
@@ -355,20 +398,28 @@ for (const filePath of filePaths) {
355
398
assert . strictEqual ( node1 . name , node2 . name ) ;
356
399
357
400
if ( node1 . kind !== node2 . kind ) {
358
- throw new TypeError ( `Conflicting types for ${ node1 . name } : ${ node1 . kind } vs ${ node2 . kind } ` ) ;
401
+ throw new TypeError (
402
+ `Conflicting types for ${ node1 . name } : ${ node1 . kind } vs ${ node2 . kind } `
403
+ ) ;
359
404
}
360
405
switch ( node1 . kind ) {
361
406
case "typedef" :
362
407
case "function" :
363
408
case "variable" :
364
- throw new TypeError ( `Two ${ node1 . kind } s with the same name ${ node1 . name } ` ) ;
409
+ throw new TypeError (
410
+ `Two ${ node1 . kind } s with the same name ${ node1 . name } `
411
+ ) ;
365
412
case "struct" :
366
413
case "class" :
367
414
if ( node1 . extends !== node2 . extends ) {
368
- throw new TypeError ( `Conflicting extends values for multiple overrides of ${ node1 . name } ` ) ;
415
+ throw new TypeError (
416
+ `Conflicting extends values for multiple overrides of ${ node1 . name } `
417
+ ) ;
369
418
}
370
419
if ( node1 . implements !== node2 . implements ) {
371
- throw new TypeError ( `Conflicting extends values for multiple overrides of ${ node1 . name } ` ) ;
420
+ throw new TypeError (
421
+ `Conflicting extends values for multiple overrides of ${ node1 . name } `
422
+ ) ;
372
423
}
373
424
for ( const memberToMerge of node2 . members ) {
374
425
node1 . members . push ( memberToMerge ) ;
@@ -398,4 +449,8 @@ for (const filePath of filePaths) {
398
449
}
399
450
}
400
451
401
- fs . writeFileSync ( "overrides.json" , JSON . stringify ( declarations , null , 2 ) , "utf8" ) ;
452
+ fs . writeFileSync (
453
+ "overrides.json" ,
454
+ JSON . stringify ( declarations , null , 2 ) ,
455
+ "utf8"
456
+ ) ;
0 commit comments