Skip to content

Commit e808f33

Browse files
Feiyang1yuchenshi
authored andcommitted
fix broken typings for the non modular RTDB package (#4858)
* fix broken typings for the non modular sdk * fix lint errors * preserve comment
1 parent 9eda93c commit e808f33

File tree

7 files changed

+73
-9
lines changed

7 files changed

+73
-9
lines changed

packages/database/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import { Database } from './src/api/Database';
2828
import * as INTERNAL from './src/api/internal';
2929
import { DataSnapshot, Query, Reference } from './src/api/Reference';
3030
import * as TEST_ACCESS from './src/api/test_access';
31+
import { enableLogging } from './src/core/util/util';
3132
import { setSDKVersion } from './src/core/version';
32-
import { enableLogging, repoManagerDatabaseFromApp } from './src/exp/Database';
33+
import { repoManagerDatabaseFromApp } from './src/exp/Database';
3334

3435
const ServerValue = Database.ServerValue;
3536

packages/database/src/api/Database.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
} from '@firebase/util';
2626

2727
import {
28-
FirebaseDatabase as ExpDatabase,
2928
goOnline,
3029
useDatabaseEmulator,
3130
goOffline,
@@ -37,6 +36,14 @@ import {
3736

3837
import { Reference } from './Reference';
3938

39+
// TODO: revert to import {FirebaseDatabase as ExpDatabase} from '@firebase/database' once modular SDK goes GA
40+
/**
41+
* This is a workaround for an issue in the no-modular '@firebase/database' where its typings
42+
* reference types from `@firebase/app-exp`.
43+
*/
44+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
45+
type ExpDatabase = any;
46+
4047
/**
4148
* Class representing a firebase database.
4249
*/

packages/database/src/api/Reference.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626

2727
import {
2828
OnDisconnect as ExpOnDisconnect,
29-
DataSnapshot as ExpDataSnapshot,
3029
off,
3130
onChildAdded,
3231
onChildChanged,
@@ -54,8 +53,6 @@ import {
5453
setPriority,
5554
push,
5655
runTransaction,
57-
Query as ExpQuery,
58-
Reference as ExpReference,
5956
_QueryImpl,
6057
_ReferenceImpl,
6158
child
@@ -75,6 +72,19 @@ import { Database } from './Database';
7572
import { OnDisconnect } from './onDisconnect';
7673
import { TransactionResult } from './TransactionResult';
7774

75+
// TODO: revert to import { DataSnapshot as ExpDataSnapshot, Query as ExpQuery,
76+
// Reference as ExpReference,} from '../../exp/index'; once the modular SDK goes GA
77+
/**
78+
* This is part of a workaround for an issue in the no-modular '@firebase/database' where its typings
79+
* reference types from `@firebase/app-exp`.
80+
*/
81+
82+
/* eslint-disable @typescript-eslint/no-explicit-any */
83+
type ExpDataSnapshot = any;
84+
type ExpQuery = any;
85+
type ExpReference = any;
86+
/* eslint-enable @typescript-eslint/no-explicit-any */
87+
7888
/**
7989
* Class representing a firebase data snapshot. It wraps a SnapshotNode and
8090
* surfaces the public methods (val, forEach, etc.) we want to expose.

packages/database/src/api/onDisconnect.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717

1818
import { validateArgCount, validateCallback, Compat } from '@firebase/util';
1919

20-
import { OnDisconnect as ExpOnDisconnect } from '../../exp/index';
2120
import { Indexable } from '../core/util/misc';
2221
import { warn } from '../core/util/util';
2322

23+
// TODO: revert to import { OnDisconnect as ExpOnDisconnect } from '../../exp/index'; once the modular SDK goes GA
24+
/**
25+
* This is a workaround for an issue in the no-modular '@firebase/database' where its typings
26+
* reference types from `@firebase/app-exp`.
27+
*/
28+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
29+
type ExpOnDisconnect = any;
30+
2431
export class OnDisconnect implements Compat<ExpOnDisconnect> {
2532
constructor(readonly _delegate: ExpOnDisconnect) {}
2633

packages/database/src/core/util/util.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ import {
2626
} from '@firebase/util';
2727

2828
import { SessionStorage } from '../storage/storage';
29-
import { QueryContext } from '../view/EventRegistration';
3029

30+
// TODO: revert to import { QueryContext } from '../view/EventRegistration'; once the modular SDK goes GA
31+
/**
32+
* This is part of a workaround for an issue in the no-modular '@firebase/database' where its typings
33+
* reference types from `@firebase/app-exp`.
34+
*/
35+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
36+
type QueryContext = any;
3137
declare const window: Window;
3238
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3339
declare const Windows: any;

repo-scripts/prune-dts/extract-public-api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import * as path from 'path';
2121
import { Extractor, ExtractorConfig } from 'api-extractor-me';
2222
import * as tmp from 'tmp';
2323

24-
import { pruneDts, removeUnusedImports } from './prune-dts';
24+
import { addBlankLines, pruneDts, removeUnusedImports } from './prune-dts';
2525
import * as yargs from 'yargs';
2626

2727
/* eslint-disable no-console */
@@ -154,6 +154,8 @@ export async function generateApi(
154154
console.log('Generated rollup DTS');
155155
pruneDts(rollupDtsPath, publicDtsPath);
156156
console.log('Pruned DTS file');
157+
await addBlankLines(publicDtsPath);
158+
console.log('Added blank lines after imports');
157159
await removeUnusedImports(publicDtsPath);
158160
console.log('Removed unused imports');
159161

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,47 @@ export function pruneDts(inputLocation: string, outputLocation: string): void {
3939
const program = ts.createProgram([inputLocation], compilerOptions, host);
4040
const printer: ts.Printer = ts.createPrinter();
4141
const sourceFile = program.getSourceFile(inputLocation)!;
42+
4243
const result: ts.TransformationResult<ts.SourceFile> = ts.transform<ts.SourceFile>(
4344
sourceFile,
4445
[dropPrivateApiTransformer.bind(null, program, host)]
4546
);
4647
const transformedSourceFile: ts.SourceFile = result.transformed[0];
48+
let content = printer.printFile(transformedSourceFile);
4749

48-
const content = printer.printFile(transformedSourceFile);
4950
fs.writeFileSync(outputLocation, content);
5051
}
5152

53+
export async function addBlankLines(outputLocation: string): Promise<void> {
54+
const eslint = new ESLint({
55+
fix: true,
56+
overrideConfig: {
57+
parserOptions: {
58+
ecmaVersion: 2017,
59+
sourceType: 'module',
60+
tsconfigRootDir: __dirname,
61+
project: ['./tsconfig.eslint.json']
62+
},
63+
env: {
64+
es6: true
65+
},
66+
plugins: ['@typescript-eslint'],
67+
parser: '@typescript-eslint/parser',
68+
rules: {
69+
'unused-imports/no-unused-imports-ts': ['off'],
70+
// add blank lines after imports. Otherwise removeUnusedImports() will remove the comment
71+
// of the first item after the import block
72+
'padding-line-between-statements': [
73+
'error',
74+
{ 'blankLine': 'always', 'prev': 'import', 'next': '*' }
75+
]
76+
}
77+
}
78+
});
79+
const results = await eslint.lintFiles(outputLocation);
80+
await ESLint.outputFixes(results);
81+
}
82+
5283
export async function removeUnusedImports(
5384
outputLocation: string
5485
): Promise<void> {

0 commit comments

Comments
 (0)