Skip to content

Commit 150b6f2

Browse files
Protobuf postprocessing: Use NodeFactory to create nodes
Most `ts.create*` and `ts.update*` functions are deprecated as of TypeScript 4.0. See this pull request for more details: microsoft/TypeScript#35282 Additionally, some API calls (especially `createImportClause`) needed to be updated because apparently the API changed.
1 parent b189826 commit 150b6f2

File tree

2 files changed

+54
-43
lines changed

2 files changed

+54
-43
lines changed

tools/generate-protobuf-postprocess.cjs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
100107
function 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

154160
function 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

tools/generator-utils.cjs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@ function traverse(root, matchers, transform) {
3333

3434
/**
3535
* Create types import (for `WeakOpaque`).
36+
*
37+
* Output:
38+
*
39+
* import type * as types from "~/common/types";
3640
*/
3741
function createTypesImportNode() {
38-
return ts.createImportDeclaration(
39-
undefined,
42+
return ts.factory.createImportDeclaration(
4043
undefined,
41-
ts.createImportClause(
42-
undefined,
43-
ts.createNamespaceImport(ts.createIdentifier('types')),
44+
ts.factory.createImportClause(
4445
true,
46+
ts.factory.createNamespaceImport(ts.factory.createIdentifier('types')),
47+
undefined,
4548
),
46-
ts.createStringLiteral('~/common/types'),
49+
ts.factory.createStringLiteral('~/common/types'),
4750
);
4851
}
4952

@@ -56,24 +59,26 @@ function createTypesImportNode() {
5659
* @returns a new-type node.
5760
*/
5861
function createNewtypeNode(name, target, tags) {
59-
return ts.createTypeAliasDeclaration(
60-
undefined,
62+
return ts.factory.createTypeAliasDeclaration(
6163
undefined,
6264
name,
6365
undefined,
64-
ts.createTypeReferenceNode(
65-
ts.createQualifiedName(ts.createIdentifier('types'), ts.createIdentifier('WeakOpaque')),
66+
ts.factory.createTypeReferenceNode(
67+
ts.factory.createQualifiedName(
68+
ts.factory.createIdentifier('types'),
69+
ts.factory.createIdentifier('WeakOpaque'),
70+
),
6671
[
67-
ts.createTypeReferenceNode(target.name, undefined),
68-
ts.createIntersectionTypeNode([
69-
ts.createTypeLiteralNode([
70-
ts.createPropertySignature(
71-
[ts.createModifier(ts.SyntaxKind.ReadonlyKeyword)],
72+
ts.factory.createTypeReferenceNode(target.name, undefined),
73+
ts.factory.createIntersectionTypeNode([
74+
ts.factory.createTypeLiteralNode([
75+
ts.factory.createPropertySignature(
76+
[ts.factory.createModifier(ts.SyntaxKind.ReadonlyKeyword)],
7277
name,
7378
undefined,
74-
ts.createTypeOperatorNode(
79+
ts.factory.createTypeOperatorNode(
7580
ts.SyntaxKind.UniqueKeyword,
76-
ts.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword),
81+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword),
7782
),
7883
undefined,
7984
),

0 commit comments

Comments
 (0)