@@ -11,14 +11,14 @@ const {
11
11
/**
12
12
* Convert interface name `IFoo` or `Foo` to `FooEncodable`.
13
13
*
14
- * @param interface Interface name (`IFoo` or `Foo`).
14
+ * @param iface Interface name (`IFoo` or `Foo`).
15
15
* @returns Converted interface name (`FooEncodable`).
16
16
*/
17
- function getEncodableInterfaceName ( interface ) {
17
+ function getEncodableInterfaceName ( iface ) {
18
18
const base =
19
- interface . escapedText . match ( / ^ I [ A - Z ] / u) !== null
20
- ? interface . escapedText . slice ( 1 )
21
- : interface . escapedText ;
19
+ iface . escapedText . match ( / ^ I [ A - Z ] / u) !== null
20
+ ? iface . escapedText . slice ( 1 )
21
+ : iface . escapedText ;
22
22
return `${ base } Encodable` ;
23
23
}
24
24
@@ -28,9 +28,9 @@ function getEncodableInterfaceName(interface) {
28
28
* - Make all array properties `readonly`
29
29
* - Tag the interface with `tag.ProtobufMessage`
30
30
*/
31
- function updateMessageInterface ( interface ) {
31
+ function updateMessageInterface ( iface ) {
32
32
// Make all array properties `readonly`
33
- for ( const member of interface . members ) {
33
+ for ( const member of iface . members ) {
34
34
if (
35
35
member . type . kind !== ts . SyntaxKind . ParenthesizedType ||
36
36
member . type . type . kind !== ts . SyntaxKind . UnionType
@@ -43,16 +43,16 @@ function updateMessageInterface(interface) {
43
43
if ( type . kind !== ts . SyntaxKind . ArrayType ) {
44
44
continue ;
45
45
}
46
- union . types [ i ] = ts . createTypeOperatorNode ( ts . SyntaxKind . ReadonlyKeyword , type ) ;
46
+ union . types [ i ] = ts . factory . createTypeOperatorNode ( ts . SyntaxKind . ReadonlyKeyword , type ) ;
47
47
}
48
48
}
49
49
50
50
// Tag the interface with `tag.ProtobufMessage`
51
- return createNewtypeNode ( getEncodableInterfaceName ( interface . name ) , interface , [
52
- ts . createTypeReferenceNode (
53
- ts . createQualifiedName (
54
- ts . createIdentifier ( 'tag' ) ,
55
- ts . createIdentifier ( 'ProtobufMessage' ) ,
51
+ return createNewtypeNode ( getEncodableInterfaceName ( iface . name ) , iface , [
52
+ ts . factory . createTypeReferenceNode (
53
+ ts . factory . createQualifiedName (
54
+ ts . factory . createIdentifier ( 'tag' ) ,
55
+ ts . factory . createIdentifier ( 'ProtobufMessage' ) ,
56
56
) ,
57
57
undefined ,
58
58
) ,
@@ -74,7 +74,10 @@ function updateMessageClass(class_) {
74
74
member . kind === ts . SyntaxKind . PropertyDeclaration &&
75
75
member . type . kind === ts . SyntaxKind . ArrayType
76
76
) {
77
- member . type = ts . createTypeOperatorNode ( ts . SyntaxKind . ReadonlyKeyword , member . type ) ;
77
+ member . type = ts . factory . createTypeOperatorNode (
78
+ ts . SyntaxKind . ReadonlyKeyword ,
79
+ member . type ,
80
+ ) ;
78
81
}
79
82
80
83
// Update `encode` method interface reference
@@ -86,7 +89,7 @@ function updateMessageClass(class_) {
86
89
if ( parameter . name . escapedText !== 'message' ) {
87
90
continue ;
88
91
}
89
- parameter . type . typeName . right = ts . createIdentifier (
92
+ parameter . type . typeName . right = ts . factory . createIdentifier (
90
93
getEncodableInterfaceName ( parameter . type . typeName . right ) ,
91
94
) ;
92
95
}
@@ -96,17 +99,20 @@ function updateMessageClass(class_) {
96
99
97
100
/**
98
101
* Create tag import (for `ProtobufMessage`).
102
+ *
103
+ * Output:
104
+ *
105
+ * import type * as tag from "../tag";
99
106
*/
100
107
function createTagImportNode ( ) {
101
- return ts . createImportDeclaration (
102
- undefined ,
108
+ return ts . factory . createImportDeclaration (
103
109
undefined ,
104
- ts . createImportClause (
105
- undefined ,
106
- ts . createNamespaceImport ( ts . createIdentifier ( 'tag' ) ) ,
110
+ ts . factory . createImportClause (
107
111
true ,
112
+ ts . factory . createNamespaceImport ( ts . factory . createIdentifier ( 'tag' ) ) ,
113
+ undefined ,
108
114
) ,
109
- ts . createStringLiteral ( '../tag' ) ,
115
+ ts . factory . createStringLiteral ( '../tag' ) ,
110
116
) ;
111
117
}
112
118
@@ -153,7 +159,8 @@ function updateMessageNamespaces(root, matchers) {
153
159
154
160
function main ( ) {
155
161
// Parse source from stdin
156
- const source = createSource ( 'index.d.ts' , fs . readFileSync ( 0 , 'utf8' ) ) ;
162
+ const sourceFromStdin = fs . readFileSync ( 0 , 'utf8' ) ;
163
+ const source = createSource ( 'index.d.ts' , sourceFromStdin ) ;
157
164
158
165
// Insert tag import
159
166
source . statements . unshift ( createTagImportNode ( ) ) ;
@@ -164,11 +171,10 @@ function main() {
164
171
// Insert 'Long' import
165
172
// TODO(WEBMD-48): To be removed once we make use of BigInt
166
173
source . statements . unshift (
167
- ts . createImportDeclaration (
168
- undefined ,
174
+ ts . factory . createImportDeclaration (
169
175
undefined ,
170
- ts . createImportClause ( ts . createIdentifier ( 'Long' ) , undefined , true ) ,
171
- ts . createStringLiteral ( 'long' ) ,
176
+ ts . factory . createImportClause ( true , ts . factory . createIdentifier ( 'Long' ) , undefined ) ,
177
+ ts . factory . createStringLiteral ( 'long' ) ,
172
178
) ,
173
179
) ;
174
180
0 commit comments