Skip to content

Commit 37b98e9

Browse files
authored
Turn on eslint in @firebase/database (#2429)
* auto fix some database lint issues with eslint * fix more lint errors * [AUTOMATED]: Prettier Code Styling * run lint before test * remove unnecessary eslint rules * address comments * fix some explicit any * fix lint errors * [AUTOMATED]: Prettier Code Styling * [AUTOMATED]: License Headers * [AUTOMATED]: Prettier Code Styling * move comment location changed by prettier * address comments
1 parent c0d6c15 commit 37b98e9

File tree

93 files changed

+2213
-1849
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+2213
-1849
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
"typescript": "3.7.3",
119119
"watch": "1.0.2",
120120
"webpack": "4.41.2",
121-
"yargs": "15.0.2"
121+
"yargs": "15.0.2",
122+
"lodash": "4.17.15"
122123
}
123124
}

packages/database/.eslintrc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
extends: '../../config/.eslintrc.js',
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
// to make vscode-eslint work with monorepo
6+
// https://github.com/typescript-eslint/typescript-eslint/issues/251#issuecomment-463943250
7+
tsconfigRootDir: __dirname
8+
},
9+
rules: {
10+
'@typescript-eslint/no-unused-vars': 'off',
11+
'@typescript-eslint/no-floating-promises': 'off',
12+
'@typescript-eslint/explicit-function-return-type': 'off',
13+
'no-restricted-properties': 'off',
14+
'no-restricted-globals': 'off',
15+
'no-throw-literal': 'off',
16+
'id-blacklist': 'off'
17+
}
18+
};

packages/database/index.node.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function initStandalone(app: FirebaseApp, url: string, version: string) {
7474
new Component(
7575
'auth-internal',
7676
// firebase-admin-node's app.INTERNAL implements FirebaseAuthInternal interface
77-
// eslint-disable-next-line @eslint-tslint/no-explicit-any
77+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7878
() => (app as any).INTERNAL,
7979
ComponentType.PRIVATE
8080
)
@@ -146,6 +146,7 @@ try {
146146
// Previously firebase-admin depends on @firebase/app, which causes version conflict on
147147
// @firebase/app when used together with the js sdk. More detail:
148148
// https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596
149+
// eslint-disable-next-line import/no-extraneous-dependencies, @typescript-eslint/no-require-imports
149150
const firebase = require('@firebase/app').default;
150151
registerDatabase(firebase);
151152
} catch (err) {

packages/database/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
17+
// eslint-disable-next-line import/no-extraneous-dependencies
1818
import firebase from '@firebase/app';
1919
import { FirebaseNamespace } from '@firebase/app-types';
2020
import { _FirebaseNamespace } from '@firebase/app-types/private';

packages/database/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
"dist"
1212
],
1313
"scripts": {
14+
"lint": "eslint -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
15+
"lint:fix": "eslint --fix -c .eslintrc.js '**/*.ts' --ignore-path '../../.gitignore'",
1416
"build": "rollup -c",
1517
"dev": "rollup -c -w",
16-
"test": "yarn test:emulator",
17-
"test:all": "run-p test:browser test:node",
18+
"test": "run-p lint test:emulator",
19+
"test:all": "run-p lint test:browser test:node",
1820
"test:browser": "karma start --single-run",
1921
"test:node": "TS_NODE_FILES=true TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/{,!(browser)/**/}*.test.ts' --file index.node.ts --opts ../../config/mocha.node.opts",
2022
"test:emulator": "ts-node --compiler-options='{\"module\":\"commonjs\"}' ../../scripts/emulator-testing/database-test-runner.ts",

packages/database/src/api/DataSnapshot.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class DataSnapshot {
4646
*
4747
* @return {*} JSON representation of the DataSnapshot contents, or null if empty.
4848
*/
49-
val(): any {
49+
val(): unknown {
5050
validateArgCount('DataSnapshot.val', 0, 0, arguments.length);
5151
return this.node_.val();
5252
}
@@ -56,14 +56,14 @@ export class DataSnapshot {
5656
* the entire node contents.
5757
* @return {*} JSON representation of the DataSnapshot contents, or null if empty.
5858
*/
59-
exportVal(): any {
59+
exportVal(): unknown {
6060
validateArgCount('DataSnapshot.exportVal', 0, 0, arguments.length);
6161
return this.node_.val(true);
6262
}
6363

6464
// Do not create public documentation. This is intended to make JSON serialization work but is otherwise unnecessary
6565
// for end-users
66-
toJSON(): any {
66+
toJSON(): unknown {
6767
// Optional spacer argument is unnecessary because we're depending on recursion rather than stringifying the content
6868
validateArgCount('DataSnapshot.toJSON', 0, 1, arguments.length);
6969
return this.exportVal();
@@ -138,7 +138,9 @@ export class DataSnapshot {
138138
validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length);
139139
validateCallback('DataSnapshot.forEach', 1, action, false);
140140

141-
if (this.node_.isLeafNode()) return false;
141+
if (this.node_.isLeafNode()) {
142+
return false;
143+
}
142144

143145
const childrenNode = this.node_ as ChildrenNode;
144146
// Sanitize the return value to a boolean. ChildrenNode.forEachChild has a weird return type...
@@ -156,8 +158,11 @@ export class DataSnapshot {
156158
hasChildren(): boolean {
157159
validateArgCount('DataSnapshot.hasChildren', 0, 0, arguments.length);
158160

159-
if (this.node_.isLeafNode()) return false;
160-
else return !this.node_.isEmpty();
161+
if (this.node_.isLeafNode()) {
162+
return false;
163+
} else {
164+
return !this.node_.isEmpty();
165+
}
161166
}
162167

163168
get key() {

packages/database/src/api/Database.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ export class Database implements FirebaseService {
7373
/**
7474
* Returns a reference to the root or to the path specified in the provided
7575
* argument.
76-
76+
*
7777
* @param {string|Reference=} path The relative string path or an existing
7878
* Reference to a database location.
7979
* @throws If a Reference is provided, throws if it does not belong to the
8080
* same project.
8181
* @return {!Reference} Firebase reference.
82-
**/
82+
*/
8383
ref(path?: string): Reference;
8484
ref(path?: Reference): Reference;
8585
ref(path?: string | Reference): Reference {
@@ -109,14 +109,14 @@ export class Database implements FirebaseService {
109109
validateUrl(apiName, 1, parsedURL);
110110

111111
const repoInfo = parsedURL.repoInfo;
112-
if (repoInfo.host !== ((this.repo_ as any).repoInfo_ as RepoInfo).host) {
112+
if (repoInfo.host !== (this.repo_.repoInfo_ as RepoInfo).host) {
113113
fatal(
114114
apiName +
115115
': Host name does not match the current database: ' +
116116
'(found ' +
117117
repoInfo.host +
118118
' but expected ' +
119-
((this.repo_ as any).repoInfo_ as RepoInfo).host +
119+
(this.repo_.repoInfo_ as RepoInfo).host +
120120
')'
121121
);
122122
}
@@ -153,10 +153,13 @@ export class DatabaseInternals {
153153

154154
/** @return {Promise<void>} */
155155
async delete(): Promise<void> {
156+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
156157
(this.database as any).checkDeleted_('delete');
158+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
157159
RepoManager.getInstance().deleteRepo((this.database as any).repo_ as Repo);
158-
160+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
159161
(this.database as any).repo_ = null;
162+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
160163
(this.database as any).root_ = null;
161164
this.database.INTERNAL = null;
162165
this.database = null;

packages/database/src/api/Query.ts

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

18-
import { assert } from '@firebase/util';
18+
import {
19+
assert,
20+
errorPrefix,
21+
validateArgCount,
22+
validateCallback,
23+
validateContextObject,
24+
Deferred
25+
} from '@firebase/util';
1926
import { KEY_INDEX } from '../core/snap/indexes/KeyIndex';
2027
import { PRIORITY_INDEX } from '../core/snap/indexes/PriorityIndex';
2128
import { VALUE_INDEX } from '../core/snap/indexes/ValueIndex';
@@ -29,18 +36,13 @@ import {
2936
validateFirebaseDataArg,
3037
validateKey
3138
} from '../core/util/validation';
32-
import {
33-
errorPrefix,
34-
validateArgCount,
35-
validateCallback,
36-
validateContextObject
37-
} from '@firebase/util';
39+
3840
import {
3941
ValueEventRegistration,
4042
ChildEventRegistration,
4143
EventRegistration
4244
} from '../core/view/EventRegistration';
43-
import { Deferred } from '@firebase/util';
45+
4446
import { Repo } from '../core/Repo';
4547
import { QueryParams } from '../core/view/QueryParams';
4648
import { Reference } from './Reference';
@@ -49,7 +51,7 @@ import { DataSnapshot } from './DataSnapshot';
4951
let __referenceConstructor: new (repo: Repo, path: Path) => Query;
5052

5153
export interface SnapshotCallback {
52-
(a: DataSnapshot, b?: string | null): any;
54+
(a: DataSnapshot, b?: string | null): unknown;
5355
}
5456

5557
/**
@@ -99,15 +101,15 @@ export class Query {
99101
'or equalTo() must be a string.';
100102
if (params.hasStart()) {
101103
const startName = params.getIndexStartName();
102-
if (startName != MIN_NAME) {
104+
if (startName !== MIN_NAME) {
103105
throw new Error(tooManyArgsError);
104106
} else if (typeof startNode !== 'string') {
105107
throw new Error(wrongArgTypeError);
106108
}
107109
}
108110
if (params.hasEnd()) {
109111
const endName = params.getIndexEndName();
110-
if (endName != MAX_NAME) {
112+
if (endName !== MAX_NAME) {
111113
throw new Error(tooManyArgsError);
112114
} else if (typeof endNode !== 'string') {
113115
throw new Error(wrongArgTypeError);
@@ -198,8 +200,8 @@ export class Query {
198200
on(
199201
eventType: string,
200202
callback: SnapshotCallback,
201-
cancelCallbackOrContext?: ((a: Error) => any) | Object | null,
202-
context?: Object | null
203+
cancelCallbackOrContext?: ((a: Error) => unknown) | object | null,
204+
context?: object | null
203205
): SnapshotCallback {
204206
validateArgCount('Query.on', 2, 4, arguments.length);
205207
validateEventType('Query.on', 1, eventType, false);
@@ -230,7 +232,7 @@ export class Query {
230232
protected onValueEvent(
231233
callback: (a: DataSnapshot) => void,
232234
cancelCallback: ((a: Error) => void) | null,
233-
context: Object | null
235+
context: object | null
234236
) {
235237
const container = new ValueEventRegistration(
236238
callback,
@@ -248,8 +250,8 @@ export class Query {
248250
*/
249251
onChildEvent(
250252
callbacks: { [k: string]: SnapshotCallback },
251-
cancelCallback: ((a: Error) => any) | null,
252-
context: Object | null
253+
cancelCallback: ((a: Error) => unknown) | null,
254+
context: object | null
253255
) {
254256
const container = new ChildEventRegistration(
255257
callbacks,
@@ -267,7 +269,7 @@ export class Query {
267269
off(
268270
eventType?: string,
269271
callback?: SnapshotCallback,
270-
context?: Object | null
272+
context?: object | null
271273
): void {
272274
validateArgCount('Query.off', 0, 3, arguments.length);
273275
validateEventType('Query.off', 1, eventType, true);
@@ -304,8 +306,8 @@ export class Query {
304306
once(
305307
eventType: string,
306308
userCallback?: SnapshotCallback,
307-
failureCallbackOrContext?: ((a: Error) => void) | Object | null,
308-
context?: Object | null
309+
failureCallbackOrContext?: ((a: Error) => void) | object | null,
310+
context?: object | null
309311
): Promise<DataSnapshot> {
310312
validateArgCount('Query.once', 1, 4, arguments.length);
311313
validateEventType('Query.once', 1, eventType, false);
@@ -347,7 +349,9 @@ export class Query {
347349
/*cancel=*/ err => {
348350
this.off(eventType, onceCallback);
349351

350-
if (ret.cancel) ret.cancel.bind(ret.context)(err);
352+
if (ret.cancel) {
353+
ret.cancel.bind(ret.context)(err);
354+
}
351355
deferred.reject(err);
352356
}
353357
);
@@ -591,7 +595,7 @@ export class Query {
591595
* An object representation of the query parameters used by this Query.
592596
* @return {!Object}
593597
*/
594-
queryObject(): Object {
598+
queryObject(): object {
595599
return this.queryParams_.getQueryObject();
596600
}
597601

@@ -635,12 +639,12 @@ export class Query {
635639
*/
636640
private static getCancelAndContextArgs_(
637641
fnName: string,
638-
cancelOrContext?: ((a: Error) => void) | Object | null,
639-
context?: Object | null
640-
): { cancel: ((a: Error) => void) | null; context: Object | null } {
642+
cancelOrContext?: ((a: Error) => void) | object | null,
643+
context?: object | null
644+
): { cancel: ((a: Error) => void) | null; context: object | null } {
641645
const ret: {
642646
cancel: ((a: Error) => void) | null;
643-
context: Object | null;
647+
context: object | null;
644648
} = { cancel: null, context: null };
645649
if (cancelOrContext && context) {
646650
ret.cancel = cancelOrContext as (a: Error) => void;

0 commit comments

Comments
 (0)