@@ -11,14 +11,14 @@ const {
1111/**
1212 * Convert interface name `IFoo` or `Foo` to `FooEncodable`.
1313 *
14- * @param interface Interface name (`IFoo` or `Foo`).
14+ * @param iface Interface name (`IFoo` or `Foo`).
1515 * @returns Converted interface name (`FooEncodable`).
1616 */
17- function getEncodableInterfaceName ( interface ) {
17+ function getEncodableInterfaceName ( iface ) {
1818 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 ;
2222 return `${ base } Encodable` ;
2323}
2424
@@ -28,9 +28,9 @@ function getEncodableInterfaceName(interface) {
2828 * - Make all array properties `readonly`
2929 * - Tag the interface with `tag.ProtobufMessage`
3030 */
31- function updateMessageInterface ( interface ) {
31+ function updateMessageInterface ( iface ) {
3232 // Make all array properties `readonly`
33- for ( const member of interface . members ) {
33+ for ( const member of iface . members ) {
3434 if (
3535 member . type . kind !== ts . SyntaxKind . ParenthesizedType ||
3636 member . type . type . kind !== ts . SyntaxKind . UnionType
@@ -43,16 +43,16 @@ function updateMessageInterface(interface) {
4343 if ( type . kind !== ts . SyntaxKind . ArrayType ) {
4444 continue ;
4545 }
46- union . types [ i ] = ts . createTypeOperatorNode ( ts . SyntaxKind . ReadonlyKeyword , type ) ;
46+ union . types [ i ] = ts . factory . createTypeOperatorNode ( ts . SyntaxKind . ReadonlyKeyword , type ) ;
4747 }
4848 }
4949
5050 // 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' ) ,
5656 ) ,
5757 undefined ,
5858 ) ,
@@ -74,7 +74,10 @@ function updateMessageClass(class_) {
7474 member . kind === ts . SyntaxKind . PropertyDeclaration &&
7575 member . type . kind === ts . SyntaxKind . ArrayType
7676 ) {
77- member . type = ts . createTypeOperatorNode ( ts . SyntaxKind . ReadonlyKeyword , member . type ) ;
77+ member . type = ts . factory . createTypeOperatorNode (
78+ ts . SyntaxKind . ReadonlyKeyword ,
79+ member . type ,
80+ ) ;
7881 }
7982
8083 // Update `encode` method interface reference
@@ -86,7 +89,7 @@ function updateMessageClass(class_) {
8689 if ( parameter . name . escapedText !== 'message' ) {
8790 continue ;
8891 }
89- parameter . type . typeName . right = ts . createIdentifier (
92+ parameter . type . typeName . right = ts . factory . createIdentifier (
9093 getEncodableInterfaceName ( parameter . type . typeName . right ) ,
9194 ) ;
9295 }
@@ -96,17 +99,20 @@ function updateMessageClass(class_) {
9699
97100/**
98101 * Create tag import (for `ProtobufMessage`).
102+ *
103+ * Output:
104+ *
105+ * import type * as tag from "../tag";
99106 */
100107function createTagImportNode ( ) {
101- return ts . createImportDeclaration (
102- undefined ,
108+ return ts . factory . createImportDeclaration (
103109 undefined ,
104- ts . createImportClause (
105- undefined ,
106- ts . createNamespaceImport ( ts . createIdentifier ( 'tag' ) ) ,
110+ ts . factory . createImportClause (
107111 true ,
112+ ts . factory . createNamespaceImport ( ts . factory . createIdentifier ( 'tag' ) ) ,
113+ undefined ,
108114 ) ,
109- ts . createStringLiteral ( '../tag' ) ,
115+ ts . factory . createStringLiteral ( '../tag' ) ,
110116 ) ;
111117}
112118
@@ -153,7 +159,8 @@ function updateMessageNamespaces(root, matchers) {
153159
154160function main ( ) {
155161 // 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 ) ;
157164
158165 // Insert tag import
159166 source . statements . unshift ( createTagImportNode ( ) ) ;
@@ -164,11 +171,10 @@ function main() {
164171 // Insert 'Long' import
165172 // TODO(WEBMD-48): To be removed once we make use of BigInt
166173 source . statements . unshift (
167- ts . createImportDeclaration (
168- undefined ,
174+ ts . factory . createImportDeclaration (
169175 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' ) ,
172178 ) ,
173179 ) ;
174180
0 commit comments