Skip to content

Commit 4af28c1

Browse files
authored
Upgrade TypeScript to 4.7.4 (#6682)
1 parent 012fd76 commit 4af28c1

File tree

7 files changed

+239
-61
lines changed

7 files changed

+239
-61
lines changed

.changeset/config.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"firebase-repo-scripts-prune-dts"
2222
],
2323
"___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH": {
24-
"onlyUpdatePeerDependentsWhenOutOfRange": true,
25-
"useCalculatedVersionForSnapshots": true
24+
"onlyUpdatePeerDependentsWhenOutOfRange": true
25+
},
26+
"snapshot": {
27+
"useCalculatedVersion": true
2628
}
2729
}

.changeset/fluffy-pumpkins-train.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
'@firebase/rules-unit-testing': patch
3+
'@firebase/analytics': patch
4+
'@firebase/analytics-compat': patch
5+
'@firebase/analytics-interop-types': patch
6+
'@firebase/analytics-types': patch
7+
'@firebase/app': patch
8+
'@firebase/app-check': patch
9+
'@firebase/app-check-compat': patch
10+
'@firebase/app-check-interop-types': patch
11+
'@firebase/app-check-types': patch
12+
'@firebase/app-compat': patch
13+
'@firebase/app-types': patch
14+
'@firebase/auth': patch
15+
'@firebase/auth-compat': patch
16+
'@firebase/auth-interop-types': patch
17+
'@firebase/auth-types': patch
18+
'@firebase/component': patch
19+
'@firebase/database': patch
20+
'@firebase/database-compat': patch
21+
'@firebase/database-types': patch
22+
'firebase': minor
23+
'@firebase/firestore': patch
24+
'@firebase/firestore-compat': patch
25+
'@firebase/firestore-types': patch
26+
'@firebase/functions': patch
27+
'@firebase/functions-compat': patch
28+
'@firebase/functions-types': patch
29+
'@firebase/installations': patch
30+
'@firebase/installations-compat': patch
31+
'@firebase/installations-types': patch
32+
'@firebase/logger': patch
33+
'@firebase/messaging': patch
34+
'@firebase/messaging-compat': patch
35+
'@firebase/messaging-interop-types': patch
36+
'@firebase/messaging-types': patch
37+
'@firebase/performance': patch
38+
'@firebase/performance-compat': patch
39+
'@firebase/performance-types': patch
40+
'@firebase/remote-config': patch
41+
'@firebase/remote-config-compat': patch
42+
'@firebase/remote-config-types': patch
43+
'@firebase/storage': patch
44+
'@firebase/storage-compat': patch
45+
'@firebase/storage-types': patch
46+
'@firebase/util': patch
47+
'@firebase/webchannel-wrapper': patch
48+
---
49+
50+
Upgrade TypeScript to 4.7.4.

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
"@babel/core": "7.17.10",
6666
"@babel/plugin-transform-modules-commonjs": "7.17.9",
6767
"@babel/preset-env": "7.17.10",
68-
"@babel/register": "7.17.7",
6968
"@babel/preset-typescript": "7.16.7",
69+
"@babel/register": "7.17.7",
7070
"@changesets/changelog-github": "0.4.7",
7171
"@changesets/cli": "2.25.0",
7272
"@types/chai": "4.3.1",
@@ -75,23 +75,24 @@
7575
"@types/clone": "2.1.1",
7676
"@types/eslint": "7.29.0",
7777
"@types/inquirer": "8.2.1",
78+
"@types/js-yaml": "4.0.5",
7879
"@types/listr": "0.14.4",
7980
"@types/long": "4.0.2",
8081
"@types/mocha": "9.1.1",
8182
"@types/mz": "2.7.4",
8283
"@types/node": "12.20.50",
84+
"@types/request": "2.48.8",
8385
"@types/sinon": "9.0.11",
8486
"@types/sinon-chai": "3.2.8",
8587
"@types/tmp": "0.2.3",
8688
"@types/yargs": "17.0.13",
87-
"@types/js-yaml": "4.0.5",
88-
"@types/request": "2.48.8",
8989
"@typescript-eslint/eslint-plugin": "4.33.0",
9090
"@typescript-eslint/eslint-plugin-tslint": "4.33.0",
9191
"@typescript-eslint/parser": "4.33.0",
9292
"api-documenter-me": "0.1.1",
9393
"api-extractor-me": "0.1.2",
9494
"babel-loader": "8.2.5",
95+
"babel-plugin-transform-import-meta": "2.1.1",
9596
"chai": "4.3.6",
9697
"chai-as-promised": "7.1.1",
9798
"chalk": "4.1.2",
@@ -150,7 +151,7 @@
150151
"ts-node": "10.9.1",
151152
"tslint": "6.1.3",
152153
"typedoc": "0.16.11",
153-
"typescript": "4.2.2",
154+
"typescript": "4.7.4",
154155
"watch": "1.0.2",
155156
"webpack": "4.46.0",
156157
"yargs": "17.6.0"

repo-scripts/prune-dts/prune-dts.ts

+41-25
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ function maybeHideConstructor(
192192
function prunePrivateImports<
193193
T extends ts.InterfaceDeclaration | ts.ClassDeclaration
194194
>(
195+
factory: ts.NodeFactory,
195196
program: ts.Program,
196197
host: ts.CompilerHost,
197198
sourceFile: ts.SourceFile,
@@ -233,13 +234,13 @@ function prunePrivateImports<
233234

234235
if (exportedTypes.length > 0) {
235236
prunedHeritageClauses.push(
236-
ts.updateHeritageClause(heritageClause, exportedTypes)
237+
factory.updateHeritageClause(heritageClause, exportedTypes)
237238
);
238239
}
239240
}
240241

241242
if (ts.isClassDeclaration(node)) {
242-
return ts.updateClassDeclaration(
243+
return factory.updateClassDeclaration(
243244
node,
244245
node.decorators,
245246
node.modifiers,
@@ -252,7 +253,7 @@ function prunePrivateImports<
252253
]
253254
) as T;
254255
} else if (ts.isInterfaceDeclaration(node)) {
255-
return ts.updateInterfaceDeclaration(
256+
return factory.updateInterfaceDeclaration(
256257
node,
257258
node.decorators,
258259
node.modifiers,
@@ -339,11 +340,23 @@ function extractJSDocComment(
339340
}
340341
});
341342

342-
if (comments.length > 0) {
343+
if (comments.length > 0 && symbol.declarations) {
343344
const jsDocTags = ts.getJSDocTags(symbol.declarations[overloadCount]);
344345
const maybeNewline = jsDocTags?.length > 0 ? '\n' : '';
346+
const joinedComments = comments
347+
.map(comment => {
348+
if (comment.kind === 'linkText') {
349+
return comment.text.trim();
350+
}
351+
return comment.text;
352+
})
353+
.join('');
354+
const formattedComments = joinedComments
355+
.replace('*', '\n')
356+
.replace(' \n', '\n')
357+
.replace('\n ', '\n');
345358
return ts.factory.createJSDocComment(
346-
comments[0].text + maybeNewline,
359+
formattedComments + maybeNewline,
347360
jsDocTags
348361
);
349362
}
@@ -399,19 +412,21 @@ function extractExportedSymbol(
399412
// See if there is an exported symbol that extends this private symbol.
400413
// In this case, we can safely use the public symbol instead.
401414
for (const symbol of allExportedSymbols) {
402-
for (const declaration of symbol.declarations) {
403-
if (
404-
ts.isClassDeclaration(declaration) ||
405-
ts.isInterfaceDeclaration(declaration)
406-
) {
407-
for (const heritageClause of declaration.heritageClauses || []) {
408-
for (const type of heritageClause.types || []) {
409-
if (ts.isIdentifier(type.expression)) {
410-
const subclassName = type.expression.escapedText;
411-
if (subclassName === localSymbolName) {
412-
// TODO: We may need to change this to return a Union type if
413-
// more than one public type corresponds to the private type.
414-
return symbol;
415+
if (symbol.declarations) {
416+
for (const declaration of symbol.declarations) {
417+
if (
418+
ts.isClassDeclaration(declaration) ||
419+
ts.isInterfaceDeclaration(declaration)
420+
) {
421+
for (const heritageClause of declaration.heritageClauses || []) {
422+
for (const type of heritageClause.types || []) {
423+
if (ts.isIdentifier(type.expression)) {
424+
const subclassName = type.expression.escapedText;
425+
if (subclassName === localSymbolName) {
426+
// TODO: We may need to change this to return a Union type if
427+
// more than one public type corresponds to the private type.
428+
return symbol;
429+
}
415430
}
416431
}
417432
}
@@ -425,8 +440,8 @@ function extractExportedSymbol(
425440
// symbol. Note that this is not always safe as we might replace the local
426441
// symbol with a less restrictive type.
427442
const localSymbol = typeChecker.getSymbolAtLocation(typeName);
428-
if (localSymbol) {
429-
for (const declaration of localSymbol!.declarations) {
443+
if (localSymbol?.declarations) {
444+
for (const declaration of localSymbol.declarations) {
430445
if (
431446
ts.isClassDeclaration(declaration) ||
432447
ts.isInterfaceDeclaration(declaration)
@@ -453,6 +468,7 @@ function dropPrivateApiTransformer(
453468
context: ts.TransformationContext
454469
): ts.Transformer<ts.SourceFile> {
455470
const typeChecker = program.getTypeChecker();
471+
const { factory } = context;
456472

457473
return (sourceFile: ts.SourceFile) => {
458474
function visit(node: ts.Node): ts.Node {
@@ -469,7 +485,7 @@ function dropPrivateApiTransformer(
469485
if (
470486
!node.modifiers?.find(m => m.kind === ts.SyntaxKind.ExportKeyword)
471487
) {
472-
return ts.createToken(ts.SyntaxKind.WhitespaceTrivia);
488+
return factory.createNotEmittedStatement(node);
473489
}
474490
}
475491

@@ -482,7 +498,7 @@ function dropPrivateApiTransformer(
482498
) {
483499
// Remove any imports that reference internal APIs, while retaining
484500
// their public members.
485-
return prunePrivateImports(program, host, sourceFile, node);
501+
return prunePrivateImports(factory, program, host, sourceFile, node);
486502
} else if (
487503
ts.isPropertyDeclaration(node) ||
488504
ts.isMethodDeclaration(node) ||
@@ -491,7 +507,7 @@ function dropPrivateApiTransformer(
491507
// Remove any class and interface members that are prefixed with
492508
// underscores.
493509
if (hasPrivatePrefix(node.name as ts.Identifier)) {
494-
return ts.createToken(ts.SyntaxKind.WhitespaceTrivia);
510+
return factory.createNotEmittedStatement(node);
495511
}
496512
} else if (ts.isTypeReferenceNode(node)) {
497513
// For public types that refer internal types, find a public type that
@@ -502,9 +518,9 @@ function dropPrivateApiTransformer(
502518
node.typeName
503519
);
504520
return publicName
505-
? ts.updateTypeReferenceNode(
521+
? factory.updateTypeReferenceNode(
506522
node,
507-
ts.createIdentifier(publicName.name),
523+
factory.createIdentifier(publicName.name),
508524
node.typeArguments
509525
)
510526
: node;

scripts/build/create-overloads.ts

+31-23
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ function keepPublicFunctionsTransformer(
125125
return (sourceFile: ts.SourceFile) => {
126126
const typeChecker = program.getTypeChecker();
127127
const overloads: ts.Statement[] = [];
128+
const { factory } = context;
128129
function visit(node: ts.Node): ts.Node {
129130
if (ts.isFunctionDeclaration(node)) {
130131
// return early if the function doesn't have any parameter of the type we are looking for
@@ -137,7 +138,7 @@ function keepPublicFunctionsTransformer(
137138
return false;
138139
})
139140
) {
140-
return ts.createToken(ts.SyntaxKind.WhitespaceTrivia);
141+
return factory.createNotEmittedStatement(node);
141142
}
142143

143144
const newParameters = node.parameters.map(param => {
@@ -146,14 +147,14 @@ function keepPublicFunctionsTransformer(
146147
if (
147148
param.type.typeName.getText(sourceFile) === replaceOption.match
148149
) {
149-
return ts.updateParameter(
150+
return factory.updateParameterDeclaration(
150151
param,
151152
param.decorators,
152153
param.modifiers,
153154
param.dotDotDotToken,
154155
param.name,
155156
param.questionToken,
156-
ts.createTypeReferenceNode(
157+
factory.createTypeReferenceNode(
157158
replaceOption.replacement,
158159
param.type.typeArguments
159160
),
@@ -173,7 +174,7 @@ function keepPublicFunctionsTransformer(
173174
});
174175

175176
overloads.push(
176-
ts.updateFunctionDeclaration(
177+
factory.updateFunctionDeclaration(
177178
node,
178179
node.decorators,
179180
[],
@@ -189,7 +190,7 @@ function keepPublicFunctionsTransformer(
189190

190191
// remove all nodes other than the source file itself
191192
if (!ts.isSourceFile(node)) {
192-
return ts.createToken(ts.SyntaxKind.WhitespaceTrivia);
193+
return factory.createNotEmittedStatement(node);
193194
}
194195

195196
return node;
@@ -214,42 +215,49 @@ function keepPublicFunctionsTransformer(
214215
}
215216

216217
// hardcode adding `import { FirebaseApp as FirebaseAppCompat } from '@firebase/app-compat'`
217-
const appCompatImport = ts.createImportDeclaration(
218+
const appCompatImport = factory.createImportDeclaration(
218219
undefined,
219220
undefined,
220-
ts.createImportClause(
221+
factory.createImportClause(
222+
false,
221223
undefined,
222-
ts.createNamedImports([
223-
ts.createImportSpecifier(
224-
ts.createIdentifier('FirebaseApp'),
225-
ts.createIdentifier('FirebaseAppCompat')
224+
factory.createNamedImports([
225+
factory.createImportSpecifier(
226+
false,
227+
factory.createIdentifier('FirebaseApp'),
228+
factory.createIdentifier('FirebaseAppCompat')
226229
)
227230
])
228231
),
229-
ts.createLiteral('@firebase/app-compat')
232+
factory.createStringLiteral('@firebase/app-compat')
230233
);
231234

232-
const importStatement = ts.createImportDeclaration(
235+
const importStatement = factory.createImportDeclaration(
233236
undefined,
234237
undefined,
235-
ts.createImportClause(
238+
factory.createImportClause(
239+
false,
236240
undefined,
237-
ts.createNamedImports(
241+
factory.createNamedImports(
238242
Array.from(typesToImport).map(typeName =>
239-
ts.createImportSpecifier(undefined, ts.createIdentifier(typeName))
243+
factory.createImportSpecifier(
244+
true,
245+
undefined,
246+
factory.createIdentifier(typeName)
247+
)
240248
)
241249
)
242250
),
243-
ts.createLiteral(moduleNameToEnhance)
251+
factory.createStringLiteral(moduleNameToEnhance)
244252
);
245-
const moduleToEnhance = ts.createModuleDeclaration(
253+
const moduleToEnhance = factory.createModuleDeclaration(
246254
undefined,
247-
[ts.createModifier(ts.SyntaxKind.DeclareKeyword)],
248-
ts.createStringLiteral(moduleNameToEnhance),
249-
ts.createModuleBlock(overloads)
255+
[factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
256+
factory.createStringLiteral(moduleNameToEnhance),
257+
factory.createModuleBlock(overloads)
250258
);
251259

252-
return ts.updateSourceFileNode(transformed, [
260+
return factory.updateSourceFile(transformed, [
253261
appCompatImport,
254262
importStatement,
255263
moduleToEnhance
@@ -302,7 +310,7 @@ function findTypes(
302310
// include the type if it's not in the excludes list or a builtin type
303311
if (!typesToIgnore.includes(typeName)) {
304312
const symbol = typeCheck.getSymbolAtLocation(node.typeName);
305-
const declaration = symbol?.declarations[0];
313+
const declaration = symbol?.declarations?.[0];
306314

307315
// ignore type parameters.
308316
if (!declaration || !ts.isTypeParameterDeclaration(declaration)) {

0 commit comments

Comments
 (0)