Skip to content

Commit fc5a87c

Browse files
authored
enable eslint for rxfire (#1873)
* enable eslint in rxfire * fix rxfire lint issues * [AUTOMATED]: Prettier Code Styling * rebase on master * [AUTOMATED]: Prettier Code Styling * remove dev only file * update deps * fix type error * [AUTOMATED]: Prettier Code Styling * modify the build process * [AUTOMATED]: Prettier Code Styling
1 parent f2f6eef commit fc5a87c

File tree

18 files changed

+159
-89
lines changed

18 files changed

+159
-89
lines changed

packages/rxfire/.eslintrc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../config/.eslintrc.json",
3+
"parserOptions": {
4+
"project": "tsconfig.json"
5+
},
6+
"rules": {
7+
"import/no-extraneous-dependencies" : "off"
8+
}
9+
}

packages/rxfire/.gitignore

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1+
node_modules
2+
dist
3+
14
/rxfire*.js
25
/rxfire*.map
36
/rxfire*.gz
47
/rxfire*.tgz
5-
6-
# generated declaration files
7-
# declaration files currently break testing builds
8-
auth/index.d.ts
9-
firestore/collection/index.d.ts
10-
firestore/document/index.d.ts
11-
firestore/fromRef.d.ts
12-
firestore/index.d.ts
13-
database/fromRef.d.ts
14-
database/interfaces.d.ts
15-
database/utils.d.ts
16-
database/list/audit-trail.d.ts
17-
functions/index.d.ts
18-
index.d.ts
19-
storage/index.d.ts
20-
test/index.d.ts
21-
test/firestore.test.d.ts

packages/rxfire/auth/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
// auth is used as a namespace to access types
19+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1820
import { auth, User } from 'firebase';
1921
import { Observable, from, of } from 'rxjs';
2022
import { switchMap } from 'rxjs/operators';

packages/rxfire/database/fromRef.ts

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

1818
import { database } from 'firebase';
1919
import { Observable } from 'rxjs';
20-
import { map, delay, share } from 'rxjs/operators';
20+
import { delay } from 'rxjs/operators';
2121
import { ListenEvent, QueryChange } from './interfaces';
2222

2323
/**

packages/rxfire/database/list/audit-trail.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ import { stateChanges } from './index';
2424

2525
interface LoadedMetadata {
2626
data: QueryChange;
27-
lastKeyToLoad: any;
27+
lastKeyToLoad: unknown;
2828
}
2929

3030
export function auditTrail(
3131
query: database.Query,
3232
events?: ListenEvent[]
3333
): Observable<QueryChange[]> {
3434
const auditTrail$ = stateChanges(query, events).pipe(
35-
scan<QueryChange>((current, changes) => [...current, changes], [])
35+
scan<QueryChange, QueryChange[]>(
36+
(current, changes) => [...current, changes],
37+
[]
38+
)
3639
);
3740
return waitForLoaded(query, auditTrail$);
3841
}
@@ -59,23 +62,26 @@ function loadedData(query: database.Query): Observable<LoadedMetadata> {
5962
function waitForLoaded(
6063
query: database.Query,
6164
snap$: Observable<QueryChange[]>
62-
) {
65+
): Observable<QueryChange[]> {
6366
const loaded$ = loadedData(query);
6467
return loaded$.pipe(
6568
withLatestFrom(snap$),
6669
// Get the latest values from the "loaded" and "child" datasets
6770
// We can use both datasets to form an array of the latest values.
6871
map(([loaded, changes]) => {
6972
// Store the last key in the data set
70-
let lastKeyToLoad = loaded.lastKeyToLoad;
73+
const lastKeyToLoad = loaded.lastKeyToLoad;
7174
// Store all child keys loaded at this point
7275
const loadedKeys = changes.map(change => change.snapshot.key);
7376
return { changes, lastKeyToLoad, loadedKeys };
7477
}),
7578
// This is the magical part, only emit when the last load key
7679
// in the dataset has been loaded by a child event. At this point
7780
// we can assume the dataset is "whole".
78-
skipWhile(meta => meta.loadedKeys.indexOf(meta.lastKeyToLoad) === -1),
81+
skipWhile(
82+
meta =>
83+
meta.loadedKeys.indexOf(meta.lastKeyToLoad as string | null) === -1
84+
),
7985
// Pluck off the meta data because the user only cares
8086
// to iterate through the snapshots
8187
map(meta => meta.changes)

packages/rxfire/database/list/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import { fromRef } from '../fromRef';
2323
import { switchMap, scan, distinctUntilChanged, map } from 'rxjs/operators';
2424
import { changeToData } from '../object';
2525

26-
export function stateChanges(query: database.Query, events?: ListenEvent[]) {
26+
export function stateChanges(
27+
query: database.Query,
28+
events?: ListenEvent[]
29+
): Observable<QueryChange> {
2730
events = validateEventsArray(events);
2831
const childEvent$ = events.map(event => fromRef(query, event));
2932
return merge(...childEvent$);
@@ -69,7 +72,7 @@ export function listVal<T>(
6972
);
7073
}
7174

72-
function positionFor(changes: QueryChange[], key: string | null) {
75+
function positionFor(changes: QueryChange[], key: string | null): number {
7376
const len = changes.length;
7477
for (let i = 0; i < len; i++) {
7578
if (changes[i].snapshot.key === key) {
@@ -79,7 +82,7 @@ function positionFor(changes: QueryChange[], key: string | null) {
7982
return -1;
8083
}
8184

82-
function positionAfter(changes: QueryChange[], prevKey?: string) {
85+
function positionAfter(changes: QueryChange[], prevKey?: string): number {
8386
if (prevKey == null) {
8487
return 0;
8588
} else {
@@ -92,7 +95,7 @@ function positionAfter(changes: QueryChange[], prevKey?: string) {
9295
}
9396
}
9497

95-
function buildView(current: QueryChange[], change: QueryChange) {
98+
function buildView(current: QueryChange[], change: QueryChange): QueryChange[] {
9699
const { snapshot, prevKey, event } = change;
97100
const { key } = snapshot;
98101
const currentKeyPosition = positionFor(current, key);
@@ -117,7 +120,7 @@ function buildView(current: QueryChange[], change: QueryChange) {
117120
if (currentKeyPosition > -1) {
118121
// check that the previouskey is what we expect, else reorder
119122
const previous = current[currentKeyPosition - 1];
120-
if (((previous && previous.snapshot.key) || null) != prevKey) {
123+
if (((previous && previous.snapshot.key) || null) !== prevKey) {
121124
current = current.filter(x => x.snapshot.key !== snapshot.key);
122125
current.splice(afterPreviousKeyPosition, 0, change);
123126
}

packages/rxfire/database/object/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function objectVal<T>(
4343
);
4444
}
4545

46-
export function changeToData(change: QueryChange, keyField?: string) {
46+
export function changeToData(change: QueryChange, keyField?: string): {} {
4747
return {
4848
...change.snapshot.val(),
4949
...(keyField ? { [keyField]: change.snapshot.key } : null)

packages/rxfire/firestore/collection/index.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { firestore } from 'firebase/app';
1919
import { fromCollectionRef } from '../fromRef';
20-
import { Observable } from 'rxjs';
20+
import { Observable, MonoTypeOperatorFunction } from 'rxjs';
2121
import { map, filter, scan } from 'rxjs/operators';
2222
import { snapToData } from '../document';
2323

@@ -32,7 +32,9 @@ const ALL_EVENTS: firestore.DocumentChangeType[] = [
3232
* are specified by the event filter. If the document change type is not
3333
* in specified events array, it will not be emitted.
3434
*/
35-
const filterEvents = (events?: firestore.DocumentChangeType[]) =>
35+
const filterEvents = (
36+
events?: firestore.DocumentChangeType[]
37+
): MonoTypeOperatorFunction<firestore.DocumentChange[]> =>
3638
filter((changes: firestore.DocumentChange[]) => {
3739
let hasChange = false;
3840
for (let i = 0; i < changes.length; i++) {
@@ -67,7 +69,7 @@ function processIndividualChange(
6769
case 'added':
6870
if (
6971
combined[change.newIndex] &&
70-
combined[change.newIndex].doc.id == change.doc.id
72+
combined[change.newIndex].doc.id === change.doc.id
7173
) {
7274
// Skip duplicate emissions. This is rare.
7375
// TODO: Investigate possible bug in SDK.
@@ -78,7 +80,7 @@ function processIndividualChange(
7880
case 'modified':
7981
if (
8082
combined[change.oldIndex] == null ||
81-
combined[change.oldIndex].doc.id == change.doc.id
83+
combined[change.oldIndex].doc.id === change.doc.id
8284
) {
8385
// When an item changes position we first remove it
8486
// and then add it's new position
@@ -93,11 +95,12 @@ function processIndividualChange(
9395
case 'removed':
9496
if (
9597
combined[change.oldIndex] &&
96-
combined[change.oldIndex].doc.id == change.doc.id
98+
combined[change.oldIndex].doc.id === change.doc.id
9799
) {
98100
combined.splice(change.oldIndex, 1);
99101
}
100102
break;
103+
default: // ignore
101104
}
102105
return combined;
103106
}
@@ -113,7 +116,7 @@ function processDocumentChanges(
113116
current: firestore.DocumentChange[],
114117
changes: firestore.DocumentChange[],
115118
events: firestore.DocumentChangeType[] = ALL_EVENTS
116-
) {
119+
): firestore.DocumentChange[] {
117120
changes.forEach(change => {
118121
// skip unwanted change types
119122
if (events.indexOf(change.type) > -1) {
@@ -131,7 +134,7 @@ function processDocumentChanges(
131134
export function collectionChanges(
132135
query: firestore.Query,
133136
events: firestore.DocumentChangeType[] = ALL_EVENTS
134-
) {
137+
): Observable<firestore.DocumentChange[]> {
135138
return fromCollectionRef(query).pipe(
136139
map(snapshot => snapshot.docChanges()),
137140
filterEvents(events),
@@ -143,7 +146,9 @@ export function collectionChanges(
143146
* Return a stream of document snapshots on a query. These results are in sort order.
144147
* @param query
145148
*/
146-
export function collection(query: firestore.Query) {
149+
export function collection(
150+
query: firestore.Query
151+
): Observable<firestore.QueryDocumentSnapshot[]> {
147152
return fromCollectionRef(query).pipe(map(changes => changes.docs));
148153
}
149154

@@ -154,7 +159,7 @@ export function collection(query: firestore.Query) {
154159
export function sortedChanges(
155160
query: firestore.Query,
156161
events?: firestore.DocumentChangeType[]
157-
) {
162+
): Observable<firestore.DocumentChange[]> {
158163
return collectionChanges(query, events).pipe(
159164
scan(
160165
(

packages/rxfire/firestore/document/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import { fromDocRef } from '../fromRef';
2020
import { map } from 'rxjs/operators';
2121
import { Observable } from 'rxjs';
2222

23-
export function doc(ref: firestore.DocumentReference) {
23+
export function doc(
24+
ref: firestore.DocumentReference
25+
): Observable<firestore.DocumentSnapshot> {
2426
return fromDocRef(ref);
2527
}
2628

@@ -38,7 +40,7 @@ export function docData<T>(
3840
export function snapToData(
3941
snapshot: firestore.DocumentSnapshot,
4042
idField?: string
41-
) {
43+
): {} {
4244
return {
4345
...snapshot.data(),
4446
...(idField ? { [idField]: snapshot.id } : null)

packages/rxfire/firestore/fromRef.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@
1818
import { firestore } from 'firebase/app';
1919
import { Observable } from 'rxjs';
2020

21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2122
function _fromRef(ref: any): Observable<any> {
2223
return new Observable(subscriber => {
2324
const unsubscribe = ref.onSnapshot(subscriber);
2425
return { unsubscribe };
2526
});
2627
}
2728

28-
export function fromRef(ref: firestore.DocumentReference | firestore.Query) {
29+
export function fromRef(
30+
ref: firestore.DocumentReference | firestore.Query
31+
): Observable<{}> {
2932
return _fromRef(ref);
3033
}
3134

3235
export function fromDocRef(
3336
ref: firestore.DocumentReference
3437
): Observable<firestore.DocumentSnapshot> {
35-
return fromRef(ref);
38+
return fromRef(ref) as Observable<firestore.DocumentSnapshot>;
3639
}
3740

3841
export function fromCollectionRef(
3942
ref: firestore.Query
4043
): Observable<firestore.QuerySnapshot> {
41-
return fromRef(ref);
44+
return fromRef(ref) as Observable<firestore.QuerySnapshot>;
4245
}

packages/rxfire/functions/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
* limitations under the License.
1616
*/
1717

18+
// function is used as a namespace to access types
19+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1820
import { functions } from 'firebase/app';
19-
import { Observable, from } from 'rxjs';
21+
import { from, Observable } from 'rxjs';
2022
import { map } from 'rxjs/operators';
2123

22-
export function httpsCallable<T = any, R = any>(
24+
export function httpsCallable<T, R>(
2325
functions: functions.Functions,
2426
name: string
25-
) {
27+
): (data: T) => Observable<R> {
2628
const callable = functions.httpsCallable(name);
2729
return (data: T) => {
2830
return from(callable(data)).pipe(map(r => r.data as R));

packages/rxfire/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
"url": "https://github.com/firebase/firebase-js-sdk.git"
2222
},
2323
"scripts": {
24+
"lint": "eslint -c .eslintrc.json '**/*.ts' --ignore-path './.gitignore'",
25+
"lint:fix": "eslint --fix -c .eslintrc.json '**/*.ts' --ignore-path './.gitignore'",
2426
"build": "rollup -c",
2527
"dev": "rollup -c -w",
2628
"prepare": "yarn build",
27-
"test": "yarn test:browser",
29+
"test": "run-p lint test:browser",
2830
"test:browser": "karma start --single-run",
2931
"test:browser:debug": "karma start --browsers=Chrome --auto-watch"
3032
},
@@ -67,7 +69,12 @@
6769
"ts-node": "8.1.0",
6870
"tslint": "5.16.0",
6971
"webpack": "4.30.0",
70-
"yargs": "13.2.2"
72+
"yargs": "13.2.2",
73+
"eslint": "5.16.0",
74+
"@typescript-eslint/parser": "1.10.2",
75+
"@typescript-eslint/eslint-plugin": "1.10.2",
76+
"@typescript-eslint/eslint-plugin-tslint": "1.10.2",
77+
"eslint-plugin-import": "2.17.3"
7178
},
7279
"typings": "dist/index.d.ts",
7380
"files": [

0 commit comments

Comments
 (0)