Skip to content

Commit 507da7a

Browse files
Discourage use of 'window' and 'document' (#2874)
1 parent 537350e commit 507da7a

13 files changed

+41
-1
lines changed

packages/firestore/.eslintrc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ module.exports = {
1616
varsIgnorePattern: '^_',
1717
args: 'none'
1818
}
19+
],
20+
'no-restricted-globals': [
21+
'error',
22+
{
23+
'name': 'window',
24+
'message': 'Use `PlatformSupport.getPlatform().window` instead.'
25+
},
26+
{
27+
'name': 'document',
28+
'message': 'Use `PlatformSupport.getPlatform().document` instead.'
29+
}
1930
]
2031
},
2132
overrides: [

packages/firestore/src/local/simple_db.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import { Deferred } from '../util/promise';
2323
import { SCHEMA_VERSION } from './indexeddb_schema';
2424
import { PersistencePromise } from './persistence_promise';
2525

26+
// References to `window` are guarded by SimpleDb.isAvailable()
27+
/* eslint-disable no-restricted-globals */
28+
2629
const LOG_TAG = 'SimpleDb';
2730

2831
/**

packages/firestore/src/platform_browser/browser_connectivity_monitor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import {
2222
NetworkStatus
2323
} from './../remote/connectivity_monitor';
2424

25+
// References to `window` are guarded by BrowserConnectivityMonitor.isAvailable()
26+
/* eslint-disable no-restricted-globals */
27+
2528
const LOG_TAG = 'ConnectivityMonitor';
2629

2730
/**

packages/firestore/src/platform_browser/browser_platform.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import { Platform } from '../platform/platform';
2020
import { Connection } from '../remote/connection';
2121
import { JsonProtoSerializer } from '../remote/serializer';
2222
import { ConnectivityMonitor } from './../remote/connectivity_monitor';
23-
2423
import { NoopConnectivityMonitor } from '../remote/connectivity_monitor_noop';
2524
import { BrowserConnectivityMonitor } from './browser_connectivity_monitor';
2625
import { WebChannelConnection } from './webchannel_connection';
2726

27+
// Implements the Platform API for browsers and some browser-like environments
28+
// (including ReactNative).
2829
export class BrowserPlatform implements Platform {
2930
readonly useProto3Json = true;
3031
readonly base64Available: boolean;
@@ -34,10 +35,14 @@ export class BrowserPlatform implements Platform {
3435
}
3536

3637
get document(): Document | null {
38+
// `document` is not always available, e.g. in ReactNative and WebWorkers.
39+
// eslint-disable-next-line no-restricted-globals
3740
return typeof document !== 'undefined' ? document : null;
3841
}
3942

4043
get window(): Window | null {
44+
// `window` is not always available, e.g. in ReactNative and WebWorkers.
45+
// eslint-disable-next-line no-restricted-globals
4146
return typeof window !== 'undefined' ? window : null;
4247
}
4348

packages/firestore/src/platform_node/node_platform.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class NodePlatform implements Platform {
3636

3737
get window(): Window | null {
3838
if (process.env.USE_MOCK_PERSISTENCE === 'YES') {
39+
// eslint-disable-next-line no-restricted-globals
3940
return window;
4041
}
4142

packages/firestore/test/integration/browser/webchannel.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import * as api from '../../../src/protos/firestore_proto_api';
2222
import { DEFAULT_PROJECT_ID } from '../util/helpers';
2323
import { getDefaultDatabaseInfo } from '../util/internal_helpers';
2424

25+
/* eslint-disable no-restricted-globals */
26+
2527
// We need to check both `window` and `window.navigator` to make sure we are
2628
// not running in Node with IndexedDBShim.
2729
const describeFn =

packages/firestore/test/integration/util/helpers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import * as firestore from '@firebase/firestore-types';
1919
import firebase from './firebase_export';
20+
21+
/* eslint-disable no-restricted-globals */
22+
2023
/**
2124
* NOTE: These helpers are used by api/ tests and therefore may not have any
2225
* dependencies on src/ files.

packages/firestore/test/unit/local/indexeddb_persistence.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ import {
7373
TEST_SERIALIZER
7474
} from './persistence_test_helpers';
7575

76+
/* eslint-disable no-restricted-globals */
77+
7678
function withDb(
7779
schemaVersion: number,
7880
fn: (db: IDBDatabase) => Promise<void>

packages/firestore/test/unit/local/persistence_promise.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { PersistencePromise } from '../../../src/local/persistence_promise';
2020

2121
import * as chaiAsPromised from 'chai-as-promised';
2222

23+
/* eslint-disable no-restricted-globals */
24+
2325
use(chaiAsPromised);
2426

2527
describe('PersistencePromise', () => {

packages/firestore/test/unit/local/persistence_test_helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import { AsyncQueue } from '../../../src/util/async_queue';
4949
import { FirestoreError } from '../../../src/util/error';
5050
import { AutoId } from '../../../src/util/misc';
5151

52+
/* eslint-disable no-restricted-globals */
53+
5254
/** The prefix used by the keys that Firestore writes to Local Storage. */
5355
const LOCAL_STORAGE_PREFIX = 'firestore_';
5456

packages/firestore/test/unit/local/web_storage_shared_client_state.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import {
4949
populateWebStorage
5050
} from './persistence_test_helpers';
5151

52+
/* eslint-disable no-restricted-globals */
53+
5254
const AUTHENTICATED_USER = new User('test');
5355
const UNAUTHENTICATED_USER = User.UNAUTHENTICATED;
5456
const TEST_ERROR = new FirestoreError('internal', 'Test Error');

packages/firestore/test/util/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ import { PlatformSupport } from '../../src/platform/platform';
9393
import { JsonProtoSerializer } from '../../src/remote/serializer';
9494
import { Timestamp } from '../../src/api/timestamp';
9595

96+
/* eslint-disable no-restricted-globals */
97+
9698
export type TestSnapshotVersion = number;
9799

98100
/**

packages/firestore/test/util/test_platform.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { debugAssert, fail } from '../../src/util/assert';
2323
import { ConnectivityMonitor } from './../../src/remote/connectivity_monitor';
2424
import { NoopConnectivityMonitor } from './../../src/remote/connectivity_monitor_noop';
2525

26+
/* eslint-disable no-restricted-globals */
27+
2628
/**
2729
* `Window` fake that implements the event and storage API that is used by
2830
* Firestore.

0 commit comments

Comments
 (0)