Skip to content

Commit 6668b59

Browse files
authored
Apply strictNullChecks to all packages except database (#1805)
* TS changes before splitting database * Roll back database changes * [AUTOMATED]: Prettier Code Styling * Put back database nullcheck exception * Finish fixing strictNullCheck errors * Fix logger test. * Fix tests * Update primitive array types to type[] * [AUTOMATED]: Prettier Code Styling * Put test exclusion back in rxfire * Address PR comments * [AUTOMATED]: Prettier Code Styling * Addressing PR comments * Few more comments to address * change assertThrows to assume it never returns null * [AUTOMATED]: Prettier Code Styling * Fix additional warnings after merge * Fix storage test * [AUTOMATED]: Prettier Code Styling * Address comments in PR
1 parent 54ee383 commit 6668b59

40 files changed

+182
-159
lines changed

config/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"compilerOptions": {
44
"declaration": true,
55
"importHelpers": true,
6+
"strictNullChecks": true,
67
"lib": [
78
"es2015",
89
"dom"

integration/typescript/test/namespace.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import * as firebase from 'firebase';
1919
import * as namespaceDefinition from '../../shared/namespaceDefinition.json';
20-
import * as validateNamespace from '../../shared/validator';
20+
import validateNamespace from '../../shared/validator';
2121

2222
firebase.initializeApp({
2323
apiKey: 'test-api-key',

integration/typescript/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"noImplicitAny": true,
1010
"outDir": "dist",
1111
"target": "ES5",
12-
"sourceMap": true
12+
"sourceMap": true,
13+
"esModuleInterop": true
1314
},
1415
"exclude": [
1516
"node_modules",

packages/app/src/firebaseApp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ export class FirebaseAppImpl implements FirebaseApp {
114114
}
115115

116116
return Promise.all(
117-
services.map(service => {
118-
return service.INTERNAL.delete();
119-
})
117+
services
118+
.filter(service => 'INTERNAL' in service)
119+
.map(service => service.INTERNAL!.delete())
120120
);
121121
})
122122
.then(

packages/app/src/lite/firebaseAppLite.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ export class FirebaseAppLiteImpl implements FirebaseApp {
9696
}
9797

9898
return Promise.all(
99-
services.map(service => {
100-
return service.INTERNAL.delete();
101-
})
99+
services
100+
.filter(service => 'INTERNAL' in service)
101+
.map(service => service.INTERNAL!.delete())
102102
);
103103
})
104104
.then(

packages/app/test/firebaseApp.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ function executeFirebaseTests() {
161161
const [app, , instanceIdentifier] = args;
162162
return new TestService(app, instanceIdentifier);
163163
},
164-
null,
165-
null,
164+
undefined,
165+
undefined,
166166
true
167167
);
168168
firebase.initializeApp({});
@@ -200,8 +200,8 @@ function executeFirebaseTests() {
200200
const [app, , instanceIdentifier] = args;
201201
return new TestService(app, instanceIdentifier);
202202
},
203-
null,
204-
null,
203+
undefined,
204+
undefined,
205205
false // <-- multi instance flag
206206
);
207207
firebase.initializeApp({});
@@ -268,11 +268,13 @@ function executeFirebaseTests() {
268268
// service's value.
269269
return (app as _FirebaseApp).INTERNAL.getToken()
270270
.then(token => {
271-
assert.equal('tokenFor0', token.accessToken);
271+
assert.isNotNull(token);
272+
assert.equal('tokenFor0', token!.accessToken);
272273
return (app2 as _FirebaseApp).INTERNAL.getToken();
273274
})
274275
.then(token => {
275-
assert.equal('tokenFor1', token.accessToken);
276+
assert.isNotNull(token);
277+
assert.equal('tokenFor1', token!.accessToken);
276278
});
277279
});
278280
});

packages/database/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "../../config/tsconfig.base.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "dist",
5+
"strictNullChecks": false
56
},
67
"exclude": [
78
"dist/**/*"

packages/functions/test/callable.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('Firebase Functions > Call', () => {
6969
functions = new Service(app, region);
7070
if (useEmulator) {
7171
functions.useFunctionsEmulator(
72-
process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN
72+
process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN!
7373
);
7474
}
7575
});

packages/functions/test/service.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ describe('Firebase Functions > Service', () => {
3939
service._url('foo'),
4040
'http://localhost:5005/my-project/us-central1/foo'
4141
);
42-
service.useFunctionsEmulator(null);
4342
});
4443
});
4544

packages/logger/test/logger.test.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,28 @@
1616
*/
1717

1818
import { expect } from 'chai';
19-
import { spy as Spy } from 'sinon';
19+
import { spy as Spy, SinonSpy } from 'sinon';
2020
import { Logger, LogLevel } from '../src/logger';
2121
import { setLogLevel } from '../index';
2222
import { debug } from 'util';
2323

2424
describe('@firebase/logger', () => {
2525
const message = 'Hello there!';
2626
let client: Logger;
27-
const spies = {
28-
logSpy: null,
29-
infoSpy: null,
30-
warnSpy: null,
31-
errorSpy: null
32-
};
27+
let spies: { [key: string]: SinonSpy };
3328
/**
3429
* Before each test, instantiate a new instance of Logger and establish spies
3530
* on all of the console methods so we can assert against them as needed
3631
*/
3732
beforeEach(() => {
3833
client = new Logger('@firebase/test-logger');
3934

40-
spies.logSpy = Spy(console, 'log');
41-
spies.infoSpy = Spy(console, 'info');
42-
spies.warnSpy = Spy(console, 'warn');
43-
spies.errorSpy = Spy(console, 'error');
35+
spies = {
36+
logSpy: Spy(console, 'log'),
37+
infoSpy: Spy(console, 'info'),
38+
warnSpy: Spy(console, 'warn'),
39+
errorSpy: Spy(console, 'error')
40+
};
4441
});
4542

4643
afterEach(() => {
@@ -62,7 +59,7 @@ describe('@firebase/logger', () => {
6259
} call \`console.${channel}\` if \`.${channel}\` is called`, () => {
6360
client[channel](message);
6461
expect(
65-
spies[`${channel}Spy`] && spies[`${channel}Spy`].called,
62+
spies[`${channel}Spy`]!.called,
6663
`Expected ${channel} to ${shouldLog ? '' : 'not'} log`
6764
).to.be[shouldLog ? 'true' : 'false'];
6865
});

packages/rxfire/database/list/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import { database } from 'firebase';
1919
import { QueryChange, ListenEvent } from '../interfaces';
2020
import { Observable, of, merge, from } from 'rxjs';
21-
import { validateEventsArray, isNil } from '../utils';
21+
import { validateEventsArray } from '../utils';
2222
import { fromRef } from '../fromRef';
2323
import { switchMap, scan, distinctUntilChanged, map } from 'rxjs/operators';
2424
import { changeToData } from '../object';
@@ -42,11 +42,13 @@ export function list(
4242
query: database.Query,
4343
events?: ListenEvent[]
4444
): Observable<QueryChange[]> {
45-
events = validateEventsArray(events);
45+
const eventsList = validateEventsArray(events);
4646
return fromOnce(query).pipe(
4747
switchMap(change => {
4848
const childEvent$ = [of(change)];
49-
events.forEach(event => childEvent$.push(fromRef(query, event)));
49+
for (const event of eventsList) {
50+
childEvent$.push(fromRef(query, event));
51+
}
5052
return merge(...childEvent$).pipe(scan(buildView, []));
5153
}),
5254
distinctUntilChanged()
@@ -78,7 +80,7 @@ function positionFor(changes: QueryChange[], key) {
7880
}
7981

8082
function positionAfter(changes: QueryChange[], prevKey?: string) {
81-
if (isNil(prevKey)) {
83+
if (prevKey == null) {
8284
return 0;
8385
} else {
8486
const i = positionFor(changes, prevKey);
@@ -94,11 +96,11 @@ function buildView(current: QueryChange[], change: QueryChange) {
9496
const { snapshot, prevKey, event } = change;
9597
const { key } = snapshot;
9698
const currentKeyPosition = positionFor(current, key);
97-
const afterPreviousKeyPosition = positionAfter(current, prevKey);
99+
const afterPreviousKeyPosition = positionAfter(current, prevKey || undefined);
98100
switch (event) {
99101
case ListenEvent.value:
100102
if (change.snapshot && change.snapshot.exists()) {
101-
let prevKey = null;
103+
let prevKey: string | null = null;
102104
change.snapshot.forEach(snapshot => {
103105
const action: QueryChange = {
104106
snapshot,

packages/rxfire/database/utils.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@
1717

1818
import { ListenEvent } from './interfaces';
1919

20-
export function isNil(obj: any): boolean {
21-
return obj === undefined || obj === null;
22-
}
23-
2420
/**
2521
* Check the length of the provided array. If it is empty return an array
2622
* that is populated with all the Realtime Database child events.
2723
* @param events
2824
*/
29-
export function validateEventsArray(events?: ListenEvent[]) {
30-
if (isNil(events) || events!.length === 0) {
25+
export function validateEventsArray(events?: ListenEvent[]): ListenEvent[] {
26+
if (events == null || events.length === 0) {
3127
events = [
3228
ListenEvent.added,
3329
ListenEvent.removed,

packages/rxfire/firestore/collection/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const filterEvents = (events?: firestore.DocumentChangeType[]) =>
3737
let hasChange = false;
3838
for (let i = 0; i < changes.length; i++) {
3939
const change = changes[i];
40-
if (events.indexOf(change.type) >= 0) {
40+
if (events && events.indexOf(change.type) >= 0) {
4141
hasChange = true;
4242
break;
4343
}

packages/rxfire/firestore/document/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function docData<T>(
3636
}
3737

3838
export function snapToData(
39-
snapshot: firestore.QueryDocumentSnapshot,
39+
snapshot: firestore.DocumentSnapshot,
4040
idField?: string
4141
) {
4242
return {

packages/rxfire/test/database.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ let batch = (items: any[]) => {
4141
};
4242

4343
describe('RxFire Database', () => {
44-
let app: app.App = null;
45-
let database: database.Database = null;
44+
let app: app.App;
45+
let database: database.Database;
4646
let ref = (path: string) => {
47-
app.database().goOffline();
48-
return app.database().ref(path);
47+
app!.database().goOffline();
48+
return app!.database().ref(path);
4949
};
5050

5151
function prepareList(
@@ -147,7 +147,7 @@ describe('RxFire Database', () => {
147147
count = count + 1;
148148
const { event, snapshot } = change;
149149
expect(event).to.equal(ListenEvent.added);
150-
expect(snapshot.val()).to.eql(data[snapshot.key]);
150+
expect(snapshot.val()).to.eql(data[snapshot.key!]);
151151
if (count === items.length) {
152152
done();
153153
sub.unsubscribe();

packages/rxfire/test/firestore.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ const seedTest = firestore => {
6767
};
6868

6969
describe('RxFire Firestore', () => {
70-
let app: app.App = null;
71-
let firestore: firestore.Firestore = null;
70+
let app: app.App;
71+
let firestore: firestore.Firestore;
7272

7373
/**
7474
* Each test runs inside it's own app instance and the app

packages/storage/src/implementation/list.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Location } from './location';
2323
import * as json from './json';
2424
import * as type from './type';
2525
import { ListResult } from '../list';
26+
import * as errors from './error';
2627

2728
/**
2829
* Represents the simplified object metadata returned by List API.
@@ -58,11 +59,15 @@ function fromBackendResponse(
5859
items: [],
5960
nextPageToken: resource['nextPageToken']
6061
};
62+
const bucket = authWrapper.bucket();
63+
if (bucket === null) {
64+
throw errors.noDefaultBucket();
65+
}
6166
if (resource[PREFIXES_KEY]) {
6267
for (const path of resource[PREFIXES_KEY]) {
6368
const pathWithoutTrailingSlash = path.replace(/\/$/, '');
6469
const reference = authWrapper.makeStorageReference(
65-
new Location(authWrapper.bucket(), pathWithoutTrailingSlash)
70+
new Location(bucket, pathWithoutTrailingSlash)
6671
);
6772
listResult.prefixes.push(reference);
6873
}
@@ -71,7 +76,7 @@ function fromBackendResponse(
7176
if (resource[ITEMS_KEY]) {
7277
for (const item of resource[ITEMS_KEY]) {
7378
const reference = authWrapper.makeStorageReference(
74-
new Location(authWrapper.bucket(), item['name'])
79+
new Location(bucket, item['name'])
7580
);
7681
listResult.items.push(reference);
7782
}

packages/storage/src/implementation/location.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class Location {
6767
}
6868

6969
static makeFromUrl(url: string): Location {
70-
let location = null;
70+
let location: Location | null = null;
7171
let bucketDomain = '([A-Za-z0-9.\\-_]+)';
7272

7373
function gsModify(loc: Location) {

packages/storage/src/implementation/metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function getMappings(): Mappings {
7070
if (mappings_) {
7171
return mappings_;
7272
}
73-
let mappings = [];
73+
const mappings: Mapping[] = [];
7474
mappings.push(new Mapping('bucket'));
7575
mappings.push(new Mapping('generation'));
7676
mappings.push(new Mapping('metageneration'));

packages/storage/src/implementation/observer.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
* limitations under the License.
1616
*/
1717
import * as type from './type';
18+
import { FirebaseStorageError } from './error';
1819

1920
type NextFn<T> = (value: T) => void;
20-
type ErrorFn = (error: Error) => void;
21+
type ErrorFn = (error: Error | FirebaseStorageError) => void;
2122
type CompleteFn = () => void;
2223
type Unsubscribe = () => void;
2324

2425
type Subscribe<T> = (
25-
next: NextFn<T> | { [name: string]: string | null },
26-
error?: ErrorFn,
27-
complete?: CompleteFn
26+
next?: NextFn<T> | { [name: string]: string | null } | null,
27+
error?: ErrorFn | null,
28+
complete?: CompleteFn | null
2829
) => Unsubscribe;
2930

3031
export { NextFn, ErrorFn, CompleteFn, Unsubscribe, Subscribe };
@@ -33,12 +34,12 @@ export { NextFn, ErrorFn, CompleteFn, Unsubscribe, Subscribe };
3334
* @struct
3435
*/
3536
export class Observer<T> {
36-
next: NextFn<T> | null;
37-
error: ErrorFn | null;
38-
complete: CompleteFn | null;
37+
next?: NextFn<T> | null;
38+
error?: ErrorFn | null;
39+
complete?: CompleteFn | null;
3940

4041
constructor(
41-
nextOrObserver: NextFn<T> | { [name: string]: string | null } | null,
42+
nextOrObserver?: NextFn<T> | { [name: string]: string | null } | null,
4243
opt_error?: ErrorFn | null,
4344
opt_complete?: CompleteFn | null
4445
) {

0 commit comments

Comments
 (0)