@@ -2,7 +2,7 @@ import * as webidl2 from 'webidl2'
2
2
import * as ts from 'typescript'
3
3
4
4
export function convertIDL ( rootTypes : webidl2 . IDLRootType [ ] ) {
5
- const file = ts . createSourceFile ( 'ammo.d.ts' , '' , ts . ScriptTarget . Latest , /* setParentNodes */ false , ts . ScriptKind . TS )
5
+ const file = ts . createSourceFile ( 'ammo.d.ts' , '' , ts . ScriptTarget . Latest , false , ts . ScriptKind . TS )
6
6
7
7
const printer = ts . createPrinter ( {
8
8
newLine : ts . NewLineKind . LineFeed ,
@@ -19,13 +19,37 @@ export function convertIDL(rootTypes: webidl2.IDLRootType[]) {
19
19
}
20
20
}
21
21
22
+ // declare function Ammo(): Promise<void>;
23
+ const ammoPromise = ts . createFunctionDeclaration (
24
+ /* decorators */ [ ] ,
25
+ /* modifiers */ [ ts . createModifier ( ts . SyntaxKind . DeclareKeyword ) ] ,
26
+ /* asteriskToken */ undefined ,
27
+ /* name */ 'Ammo' ,
28
+ /* typeParameters */ [ ] ,
29
+ /* parameters */ [ ] ,
30
+ /* type */ ts . createTypeReferenceNode ( 'Promise' , [ ts . createKeywordTypeNode ( ts . SyntaxKind . VoidKeyword ) ] ) ,
31
+ /* body */ undefined
32
+ )
33
+ // function destroy(obj: any): void;
34
+ const ammoDestroy = ts . createFunctionDeclaration (
35
+ /* decorators */ [ ] ,
36
+ /* modifiers */ [ ] ,
37
+ /* asteriskToken */ undefined ,
38
+ /* name */ 'destroy' ,
39
+ /* typeParameters */ [ ] ,
40
+ /* parameters */ [ ts . createParameter ( [ ] , [ ] , undefined , 'obj' , undefined , ts . createKeywordTypeNode ( ts . SyntaxKind . AnyKeyword ) ) ] ,
41
+ /* type */ ts . createKeywordTypeNode ( ts . SyntaxKind . VoidKeyword ) ,
42
+ /* body */ undefined
43
+ )
44
+ // declare module Ammo { ... }
22
45
const ammo = ts . createModuleDeclaration (
23
- [ ] ,
24
- [ ts . createModifier ( ts . SyntaxKind . DeclareKeyword ) ] ,
25
- ts . createIdentifier ( 'Ammo' ) ,
26
- ts . createModuleBlock ( nodes )
46
+ /* decorators */ [ ] ,
47
+ /* modifiers */ [ ts . createModifier ( ts . SyntaxKind . DeclareKeyword ) ] ,
48
+ /* name */ ts . createIdentifier ( 'Ammo' ) ,
49
+ /* body */ ts . createModuleBlock ( [ ammoDestroy , ... nodes ] )
27
50
)
28
- return printer . printNode ( ts . EmitHint . Unspecified , ammo , file )
51
+
52
+ return [ printer . printNode ( ts . EmitHint . Unspecified , ammoPromise , file ) , printer . printNode ( ts . EmitHint . Unspecified , ammo , file ) ] . join ( '\n' )
29
53
}
30
54
31
55
function convertInterface ( idl : webidl2 . InterfaceType ) {
@@ -113,7 +137,7 @@ function convertType(idl: webidl2.IDLTypeDescription): ts.TypeNode {
113
137
return ts . createArrayTypeNode ( subtype )
114
138
}
115
139
if ( idl . union ) {
116
- return ts . createUnionTypeNode ( ( idl . idlType ) . map ( convertType ) )
140
+ return ts . createUnionTypeNode ( idl . idlType . map ( convertType ) )
117
141
}
118
142
throw new Error ( 'Unsupported IDL type' )
119
143
}
0 commit comments