From 14ec40ee95cf7a8f34d9abbe90bd2a2fede7c529 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 23 Mar 2022 21:43:36 -0500 Subject: [PATCH 1/7] Typescript: start fixing 'strict' mode errors --- app/soapbox/actions/instance.ts | 8 +++++--- app/soapbox/reducers/statuses.ts | 2 +- app/soapbox/storage/kv_store.ts | 6 +++--- app/soapbox/utils/config_db.ts | 2 +- app/soapbox/utils/theme.ts | 17 +++++++++++++---- tsconfig.json | 2 +- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/soapbox/actions/instance.ts b/app/soapbox/actions/instance.ts index 150e9cc86..994231c12 100644 --- a/app/soapbox/actions/instance.ts +++ b/app/soapbox/actions/instance.ts @@ -59,7 +59,7 @@ export function fetchInstance() { return api(getState).get('/api/v1/instance').then(({ data: instance }: { data: Record }) => { dispatch({ type: INSTANCE_FETCH_SUCCESS, instance }); if (needsNodeinfo(instance)) { - dispatch(fetchNodeinfo()); // Pleroma < 2.1 backwards compatibility + dispatch(fetchNodeinfo() as any); // Pleroma < 2.1 backwards compatibility } }).catch(error => { console.error(error); @@ -73,8 +73,10 @@ export function loadInstance() { return (dispatch: AppDispatch, getState: () => RootState) => { const host = getHost(getState()); - return dispatch(rememberInstance(host)).finally(() => { - return dispatch(fetchInstance()); + if (!host) return new Promise(r => r(null)); + + return dispatch(rememberInstance(host) as any).finally(() => { + return dispatch(fetchInstance() as any); }); }; } diff --git a/app/soapbox/reducers/statuses.ts b/app/soapbox/reducers/statuses.ts index 059e153da..404094b95 100644 --- a/app/soapbox/reducers/statuses.ts +++ b/app/soapbox/reducers/statuses.ts @@ -86,7 +86,7 @@ export const calculateStatus = ( const emojiMap = makeEmojiMap(status.emojis); return status.merge({ - search_index: domParser.parseFromString(searchContent, 'text/html').documentElement.textContent, + search_index: domParser.parseFromString(searchContent, 'text/html').documentElement.textContent || undefined, contentHtml: stripCompatibilityFeatures(emojify(status.content, emojiMap)), spoilerHtml: emojify(escapeTextContentForBrowser(spoilerText), emojiMap), hidden: expandSpoilers ? false : spoilerText.length > 0 || status.sensitive, diff --git a/app/soapbox/storage/kv_store.ts b/app/soapbox/storage/kv_store.ts index 5b7225c0d..941144050 100644 --- a/app/soapbox/storage/kv_store.ts +++ b/app/soapbox/storage/kv_store.ts @@ -1,17 +1,17 @@ import localforage from 'localforage'; interface IKVStore extends LocalForage { - getItemOrError?: (key: string) => Promise, + getItemOrError: (key: string) => Promise, } // localForage // https://localforage.github.io/localForage/#settings-api-config -export const KVStore: IKVStore = localforage.createInstance({ +export const KVStore = localforage.createInstance({ name: 'soapbox', description: 'Soapbox offline data store', driver: localforage.INDEXEDDB, storeName: 'keyvaluepairs', -}); +}) as IKVStore; // localForage returns 'null' when a key isn't found. // In the Redux action flow, we want it to fail harder. diff --git a/app/soapbox/utils/config_db.ts b/app/soapbox/utils/config_db.ts index 1ea8a0b8c..deb5d048a 100644 --- a/app/soapbox/utils/config_db.ts +++ b/app/soapbox/utils/config_db.ts @@ -13,7 +13,7 @@ const find = ( configs: ImmutableList, group: string, key: string, -): Config => { +): Config | undefined => { return configs.find(config => config.isSuperset(ImmutableMap({ group, key })), ); diff --git a/app/soapbox/utils/theme.ts b/app/soapbox/utils/theme.ts index d6225378e..bb83b0111 100644 --- a/app/soapbox/utils/theme.ts +++ b/app/soapbox/utils/theme.ts @@ -61,19 +61,28 @@ function hslToHex(color: Hsl): string { } // Generate accent color from brand color -export const generateAccent = (brandColor: string): string => { - const { h } = rgbToHsl(hexToRgb(brandColor)); +export const generateAccent = (brandColor: string): string | null => { + const rgb = hexToRgb(brandColor); + if (!rgb) return null; + + const { h } = rgbToHsl(rgb); return hslToHex({ h: h - 15, s: 86, l: 44 }); }; const parseShades = (obj: Record, color: string, shades: Record) => { if (typeof shades === 'string') { - const { r, g, b } = hexToRgb(shades); + const rgb = hexToRgb(shades); + if (!rgb) return obj; + + const { r, g, b } = rgb; return obj[`--color-${color}`] = `${r} ${g} ${b}`; } return Object.keys(shades).forEach(shade => { - const { r, g, b } = hexToRgb(shades[shade]); + const rgb = hexToRgb(shades[shade]); + if (!rgb) return; + + const { r, g, b } = rgb; obj[`--color-${color}-${shade}`] = `${r} ${g} ${b}`; }); }; diff --git a/tsconfig.json b/tsconfig.json index fa80d5507..6af5fd813 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "baseUrl": "app/", "sourceMap": true, - "noImplicitAny": true, + "strict": true, "module": "es6", "target": "es5", "jsx": "react", From 029c9bec6b26951935f04868a892e9a74f5913b7 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 24 Mar 2022 10:53:44 -0500 Subject: [PATCH 2/7] Prime tsconfig to turn on strict mode one rule at a time --- tsconfig.json | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 6af5fd813..2b8a0a030 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,14 @@ "compilerOptions": { "baseUrl": "app/", "sourceMap": true, - "strict": true, + "alwaysStrict": false, + "strictNullChecks": false, + "strictBindCallApply": false, + "strictFunctionTypes": false, + "strictPropertyInitialization": false, + "noImplicitAny": true, + "noImplicitThis": false, + "useUnknownInCatchVariables": false, "module": "es6", "target": "es5", "jsx": "react", From a4ff9ed752eae1d5b879bb4421d13388f8da20ed Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 24 Mar 2022 10:55:43 -0500 Subject: [PATCH 3/7] tsconfig: enable noImplicitThis --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 2b8a0a030..387d9840c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,7 @@ "strictFunctionTypes": false, "strictPropertyInitialization": false, "noImplicitAny": true, - "noImplicitThis": false, + "noImplicitThis": true, "useUnknownInCatchVariables": false, "module": "es6", "target": "es5", From 0ddee401363a927ae8d86ef6c74e5843194e1c98 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 24 Mar 2022 10:56:57 -0500 Subject: [PATCH 4/7] tsconfig: enable useUnknownInCatchVariables --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 387d9840c..2d761eef0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ "strictPropertyInitialization": false, "noImplicitAny": true, "noImplicitThis": true, - "useUnknownInCatchVariables": false, + "useUnknownInCatchVariables": true, "module": "es6", "target": "es5", "jsx": "react", From fd732f86474d987b07b4c8083cd25154f7016dc9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 24 Mar 2022 11:06:49 -0500 Subject: [PATCH 5/7] tsconfig: enable strictBindCallApply --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 2d761eef0..94b545084 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "sourceMap": true, "alwaysStrict": false, "strictNullChecks": false, - "strictBindCallApply": false, + "strictBindCallApply": true, "strictFunctionTypes": false, "strictPropertyInitialization": false, "noImplicitAny": true, From a773c245e83eb35272bdb78dd4248d1576e6a5d3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 24 Mar 2022 11:08:23 -0500 Subject: [PATCH 6/7] tsconfig: enable alwaysStrict --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 94b545084..b961c4bcb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "baseUrl": "app/", "sourceMap": true, - "alwaysStrict": false, + "alwaysStrict": true, "strictNullChecks": false, "strictBindCallApply": true, "strictFunctionTypes": false, From 161cd3d1fcd7e69d4de077c2a9e3695e015237c0 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 24 Mar 2022 11:09:30 -0500 Subject: [PATCH 7/7] Revert lazy type changes to actions/instance.ts --- app/soapbox/actions/instance.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/soapbox/actions/instance.ts b/app/soapbox/actions/instance.ts index 994231c12..150e9cc86 100644 --- a/app/soapbox/actions/instance.ts +++ b/app/soapbox/actions/instance.ts @@ -59,7 +59,7 @@ export function fetchInstance() { return api(getState).get('/api/v1/instance').then(({ data: instance }: { data: Record }) => { dispatch({ type: INSTANCE_FETCH_SUCCESS, instance }); if (needsNodeinfo(instance)) { - dispatch(fetchNodeinfo() as any); // Pleroma < 2.1 backwards compatibility + dispatch(fetchNodeinfo()); // Pleroma < 2.1 backwards compatibility } }).catch(error => { console.error(error); @@ -73,10 +73,8 @@ export function loadInstance() { return (dispatch: AppDispatch, getState: () => RootState) => { const host = getHost(getState()); - if (!host) return new Promise(r => r(null)); - - return dispatch(rememberInstance(host) as any).finally(() => { - return dispatch(fetchInstance() as any); + return dispatch(rememberInstance(host)).finally(() => { + return dispatch(fetchInstance()); }); }; }