Skip to content

Enable Typescript strict flag for app, logger, and util packages #1897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ if (isBrowser() && (self as any).firebase !== undefined) {
const firebaseNamespace = createFirebaseNamespace();
const initializeApp = firebaseNamespace.initializeApp;

firebaseNamespace.initializeApp = function() {
// TODO: This disable can be removed and the 'ignoreRestArgs' option added to
// the no-explicit-any rule when ESlint releases it.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
firebaseNamespace.initializeApp = function(...args: any) {
// Environment check before initializing app
// Do the check in initializeApp, so people have a chance to disable it by setting logLevel
// in @firebase/logger
Expand All @@ -62,8 +65,7 @@ firebaseNamespace.initializeApp = function() {
https://github.com/rollup/rollup-plugin-node-resolve
`);
}

return initializeApp.apply(undefined, arguments);
return initializeApp.apply(undefined, args);
};

export const firebase = firebaseNamespace;
Expand Down
12 changes: 8 additions & 4 deletions packages/app/src/firebaseNamespaceCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,14 @@ export function createFirebaseNamespaceCore(

// Patch the FirebaseAppImpl prototype
// @ts-ignore
firebaseAppImpl.prototype[name] = function(...args) {
const serviceFxn = this._getService.bind(this, name);
return serviceFxn.apply(this, allowMultipleInstances ? args : []);
};
firebaseAppImpl.prototype[name] =
// TODO: The eslint disable can be removed and the 'ignoreRestArgs'
// option added to the no-explicit-any rule when ESlint releases it.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function(...args: any) {
const serviceFxn = this._getService.bind(this, name);
return serviceFxn.apply(this, allowMultipleInstances ? args : []);
};

return serviceNamespace;
}
Expand Down
9 changes: 4 additions & 5 deletions packages/app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"extends": "../../config/tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"resolveJsonModule": true
"resolveJsonModule": true,
"strict": true
},
"exclude": [
"dist/**/*"
]
}
"exclude": ["dist/**/*"]
}
3 changes: 2 additions & 1 deletion packages/logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../config/tsconfig.base.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
"strict": true
},
"exclude": [
"dist/**/*"
Expand Down
65 changes: 27 additions & 38 deletions packages/util/src/crypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,68 +86,55 @@ const byteArrayToString = function(bytes: number[]): string {
};

// Static lookup maps, lazily populated by init_()
export const base64 = {
class Base64 {
/**
* Maps bytes to characters.
* @type {Object}
* @private
*/
byteToCharMap_: null,
byteToCharMap_: { [key: number]: string } | null = null;

/**
* Maps characters to bytes.
* @type {Object}
* @private
*/
charToByteMap_: null,
charToByteMap_: { [key: string]: number } | null = null;

/**
* Maps bytes to websafe characters.
* @type {Object}
* @private
*/
byteToCharMapWebSafe_: null,
byteToCharMapWebSafe_: { [key: number]: string } | null = null;

/**
* Maps websafe characters to bytes.
* @type {Object}
* @private
*/
charToByteMapWebSafe_: null,
charToByteMapWebSafe_: { [key: string]: number } | null = null;

/**
* Our default alphabet, shared between
* Our default alphabet shared between
* ENCODED_VALS and ENCODED_VALS_WEBSAFE
* @type {string}
*/
ENCODED_VALS_BASE:
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789',
ENCODED_VALS_BASE: string =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789';

/**
* Our default alphabet. Value 64 (=) is special; it means "nothing."
* @type {string}
*/
get ENCODED_VALS() {
get ENCODED_VALS(): string {
return this.ENCODED_VALS_BASE + '+/=';
},
}

/**
* Our websafe alphabet.
* @type {string}
*/
get ENCODED_VALS_WEBSAFE() {
get ENCODED_VALS_WEBSAFE(): string {
return this.ENCODED_VALS_BASE + '-_.';
},
}

/**
* Whether this browser supports the atob and btoa functions. This extension
* started at Mozilla but is now implemented by many browsers. We use the
* ASSUME_* variables to avoid pulling in the full useragent detection library
* but still allowing the standard per-browser compilations.
*
* @type {boolean}
*/
HAS_NATIVE_SUPPORT: typeof atob === 'function',
HAS_NATIVE_SUPPORT: boolean = typeof atob === 'function';

/**
* Base64-encode an array of bytes.
Expand All @@ -165,11 +152,11 @@ export const base64 = {

this.init_();

const byteToCharMap: number[] = webSafe
? this.byteToCharMapWebSafe_
: this.byteToCharMap_;
const byteToCharMap = webSafe
? this.byteToCharMapWebSafe_!
: this.byteToCharMap_!;

const output: number[] = [];
const output = [];

for (let i = 0; i < input.length; i += 3) {
const byte1 = input[i];
Expand Down Expand Up @@ -200,7 +187,7 @@ export const base64 = {
}

return output.join('');
},
}

/**
* Base64-encode a string.
Expand All @@ -217,7 +204,7 @@ export const base64 = {
return btoa(input);
}
return this.encodeByteArray(stringToByteArray(input), webSafe);
},
}

/**
* Base64-decode a string.
Expand All @@ -234,7 +221,7 @@ export const base64 = {
return atob(input);
}
return byteArrayToString(this.decodeStringToByteArray(input, webSafe));
},
}

/**
* Base64-decode a string.
Expand All @@ -255,8 +242,8 @@ export const base64 = {
this.init_();

const charToByteMap = webSafe
? this.charToByteMapWebSafe_
: this.charToByteMap_;
? this.charToByteMapWebSafe_!
: this.charToByteMap_!;

const output: number[] = [];

Expand Down Expand Up @@ -294,14 +281,14 @@ export const base64 = {
}

return output;
},
}

/**
* Lazy static initialization function. Called before
* accessing any of the static map variables.
* @private
*/
init_() {
init_(): void {
if (!this.byteToCharMap_) {
this.byteToCharMap_ = {};
this.charToByteMap_ = {};
Expand All @@ -323,7 +310,7 @@ export const base64 = {
}
}
}
};
}

/**
* URL-safe base64 encoding
Expand All @@ -350,3 +337,5 @@ export const base64Decode = function(str: string): string | null {
}
return null;
};

export const base64 = new Base64();
8 changes: 4 additions & 4 deletions packages/util/src/deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

export class Deferred<R> {
promise: Promise<R>;
reject: (value?: unknown) => void;
resolve: (value?: unknown) => void;
reject: (value?: unknown) => void = () => {};
resolve: (value?: unknown) => void = () => {};
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.resolve = resolve as (value?: unknown) => void;
this.reject = reject as (value?: unknown) => void;
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/util/src/obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const getCount = function<V>(obj: UtilObject<V>): number {

export const map = function<V>(
obj: UtilObject<V>,
fn: (value: V, key: string | number, obj: UtilObject<V>) => unknown,
fn: (value: V, key: string | number, obj: UtilObject<V>) => V,
context?: unknown
) {
var res: UtilObject<V> = {};
Expand Down
12 changes: 9 additions & 3 deletions packages/util/src/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ObserverProxy<T> implements Observer<T> {
// Micro-task scheduling by calling task.then().
private task = Promise.resolve();
private finalized = false;
private finalError: Error;
private finalError?: Error;

/**
* @param executor Function which can make calls to a single Observer
Expand Down Expand Up @@ -126,7 +126,7 @@ class ObserverProxy<T> implements Observer<T> {
* call to subscribe().
*/
subscribe(
nextOrObserver: PartialObserver<T> | Function,
nextOrObserver?: PartialObserver<T> | Function,
error?: ErrorFn,
complete?: CompleteFn
): Unsubscribe {
Expand All @@ -141,7 +141,13 @@ class ObserverProxy<T> implements Observer<T> {
}

// Assemble an Observer object when passed as callback functions.
if (implementsAnyMethods(nextOrObserver, ['next', 'error', 'complete'])) {
if (
implementsAnyMethods(nextOrObserver as { [key: string]: unknown }, [
'next',
'error',
'complete'
])
) {
observer = nextOrObserver as Observer<T>;
} else {
observer = {
Expand Down
3 changes: 2 additions & 1 deletion packages/util/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../config/tsconfig.base.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
"strict": true
},
"exclude": [
"dist/**/*"
Expand Down