From 18f55567b0ddfc1ece10d92a715480aa31ae491a Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Thu, 13 Jul 2023 11:49:16 +0200 Subject: [PATCH] [Glitch] Upgrade to `typescript-eslint` v6 Port a7253075d10ee392fa12c69b0ffccdde6eee4062 to glitch-soc Signed-off-by: Claire --- app/javascript/flavours/glitch/blurhash.ts | 5 ++--- .../glitch/components/autosuggest_hashtag.tsx | 4 ++-- .../glitch/components/display_name.tsx | 2 +- .../glitch/components/short_number.tsx | 4 ++-- .../features/emoji/emoji_compressed.d.ts | 7 ++++--- .../features/emoji/emoji_mart_data_light.ts | 2 +- .../components/column_settings.tsx | 2 +- .../flavours/glitch/locales/global_locale.ts | 12 ++++++++---- .../flavours/glitch/locales/load_locale.ts | 1 + .../glitch/polyfills/base_polyfills.ts | 4 ++-- .../flavours/glitch/polyfills/index.ts | 2 ++ .../flavours/glitch/polyfills/intl.ts | 1 + app/javascript/flavours/glitch/scroll.ts | 18 ++++++++++-------- .../glitch/store/middlewares/loading_bar.ts | 4 ++-- .../glitch/store/middlewares/sounds.ts | 6 +++--- .../flavours/glitch/utils/filters.ts | 2 +- .../flavours/glitch/utils/numbers.ts | 2 +- app/javascript/flavours/glitch/uuid.ts | 3 +-- 18 files changed, 45 insertions(+), 36 deletions(-) diff --git a/app/javascript/flavours/glitch/blurhash.ts b/app/javascript/flavours/glitch/blurhash.ts index dadf2b7f2c..cafe7b12dc 100644 --- a/app/javascript/flavours/glitch/blurhash.ts +++ b/app/javascript/flavours/glitch/blurhash.ts @@ -86,10 +86,9 @@ const DIGIT_CHARACTERS = [ export const decode83 = (str: string) => { let value = 0; - let c, digit; + let digit; - for (let i = 0; i < str.length; i++) { - c = str[i]; + for (const c of str) { digit = DIGIT_CHARACTERS.indexOf(c); value = value * 83 + digit; } diff --git a/app/javascript/flavours/glitch/components/autosuggest_hashtag.tsx b/app/javascript/flavours/glitch/components/autosuggest_hashtag.tsx index 71955e6b03..6da6200142 100644 --- a/app/javascript/flavours/glitch/components/autosuggest_hashtag.tsx +++ b/app/javascript/flavours/glitch/components/autosuggest_hashtag.tsx @@ -6,11 +6,11 @@ interface Props { tag: { name: string; url?: string; - history?: Array<{ + history?: { uses: number; accounts: string; day: string; - }>; + }[]; following?: boolean; type: 'hashtag'; }; diff --git a/app/javascript/flavours/glitch/components/display_name.tsx b/app/javascript/flavours/glitch/components/display_name.tsx index 76c21d00c9..a140394320 100644 --- a/app/javascript/flavours/glitch/components/display_name.tsx +++ b/app/javascript/flavours/glitch/components/display_name.tsx @@ -82,7 +82,7 @@ export class DisplayName extends React.PureComponent { } else if (account) { let acct = account.get('acct'); - if (acct.indexOf('@') === -1 && localDomain) { + if (!acct.includes('@') && localDomain) { acct = `${acct}@${localDomain}`; } diff --git a/app/javascript/flavours/glitch/components/short_number.tsx b/app/javascript/flavours/glitch/components/short_number.tsx index 928e371bd8..74c3c5d75e 100644 --- a/app/javascript/flavours/glitch/components/short_number.tsx +++ b/app/javascript/flavours/glitch/components/short_number.tsx @@ -29,12 +29,12 @@ export const ShortNumberRenderer: React.FC = ({ ); } - const customRenderer = children || renderer || null; + const customRenderer = children ?? renderer ?? null; const displayNumber = ; return ( - customRenderer?.(displayNumber, pluralReady(value, division)) || + customRenderer?.(displayNumber, pluralReady(value, division)) ?? displayNumber ); }; diff --git a/app/javascript/flavours/glitch/features/emoji/emoji_compressed.d.ts b/app/javascript/flavours/glitch/features/emoji/emoji_compressed.d.ts index 2b3375ccc4..96e0cc5eaf 100644 --- a/app/javascript/flavours/glitch/features/emoji/emoji_compressed.d.ts +++ b/app/javascript/flavours/glitch/features/emoji/emoji_compressed.d.ts @@ -28,9 +28,10 @@ export type SearchData = [ Emoji['unified'], ]; -export interface ShortCodesToEmojiData { - [key: ShortCodesToEmojiDataKey]: [FilenameData, SearchData]; -} +export type ShortCodesToEmojiData = Record< + ShortCodesToEmojiDataKey, + [FilenameData, SearchData] +>; export type EmojisWithoutShortCodes = FilenameData[]; export type EmojiCompressed = [ diff --git a/app/javascript/flavours/glitch/features/emoji/emoji_mart_data_light.ts b/app/javascript/flavours/glitch/features/emoji/emoji_mart_data_light.ts index 62cb84baf8..142605b4bc 100644 --- a/app/javascript/flavours/glitch/features/emoji/emoji_mart_data_light.ts +++ b/app/javascript/flavours/glitch/features/emoji/emoji_mart_data_light.ts @@ -9,7 +9,7 @@ import emojiCompressed from './emoji_compressed'; import { unicodeToUnifiedName } from './unicode_to_unified_name'; type Emojis = { - [key in keyof ShortCodesToEmojiData]: { + [key in NonNullable]: { native: BaseEmoji['native']; search: Search; short_names: Emoji['short_names']; diff --git a/app/javascript/flavours/glitch/features/home_timeline/components/column_settings.tsx b/app/javascript/flavours/glitch/features/home_timeline/components/column_settings.tsx index 7e6f79a7af..dd873c4efb 100644 --- a/app/javascript/flavours/glitch/features/home_timeline/components/column_settings.tsx +++ b/app/javascript/flavours/glitch/features/home_timeline/components/column_settings.tsx @@ -19,7 +19,7 @@ export const ColumnSettings: React.FC = () => { const dispatch = useAppDispatch(); const onChange = useCallback( (key: string, checked: boolean) => { - void dispatch(changeSetting(['home', ...key], checked)); + dispatch(changeSetting(['home', ...key], checked)); }, [dispatch], ); diff --git a/app/javascript/flavours/glitch/locales/global_locale.ts b/app/javascript/flavours/glitch/locales/global_locale.ts index 01133ca239..2d4329c764 100644 --- a/app/javascript/flavours/glitch/locales/global_locale.ts +++ b/app/javascript/flavours/glitch/locales/global_locale.ts @@ -3,15 +3,19 @@ export interface LocaleData { messages: Record; } -let loadedLocale: LocaleData; +let loadedLocale: LocaleData | undefined; export function setLocale(locale: LocaleData) { loadedLocale = locale; } -export function getLocale() { - if (!loadedLocale && process.env.NODE_ENV === 'development') { - throw new Error('getLocale() called before any locale has been set'); +export function getLocale(): LocaleData { + if (!loadedLocale) { + if (process.env.NODE_ENV === 'development') { + throw new Error('getLocale() called before any locale has been set'); + } else { + return { locale: 'unknown', messages: {} }; + } } return loadedLocale; diff --git a/app/javascript/flavours/glitch/locales/load_locale.ts b/app/javascript/flavours/glitch/locales/load_locale.ts index fa5d13bbcc..fb938d4eb4 100644 --- a/app/javascript/flavours/glitch/locales/load_locale.ts +++ b/app/javascript/flavours/glitch/locales/load_locale.ts @@ -6,6 +6,7 @@ import { isLocaleLoaded, setLocale } from './global_locale'; const localeLoadingSemaphore = new Semaphore(1); export async function loadLocale() { + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- we want to match empty strings const locale = document.querySelector('html')?.lang || 'en'; // We use a Semaphore here so only one thing can try to load the locales at diff --git a/app/javascript/flavours/glitch/polyfills/base_polyfills.ts b/app/javascript/flavours/glitch/polyfills/base_polyfills.ts index c35ba0d382..71565236cd 100644 --- a/app/javascript/flavours/glitch/polyfills/base_polyfills.ts +++ b/app/javascript/flavours/glitch/polyfills/base_polyfills.ts @@ -4,7 +4,7 @@ import 'core-js/features/symbol'; import 'core-js/features/promise/finally'; import { decode as decodeBase64 } from '../utils/base64'; -if (!HTMLCanvasElement.prototype.toBlob) { +if (!Object.hasOwn(HTMLCanvasElement.prototype, 'toBlob')) { const BASE64_MARKER = ';base64,'; Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', { @@ -17,7 +17,7 @@ if (!HTMLCanvasElement.prototype.toBlob) { const dataURL: string = this.toDataURL(type, quality); let data; - if (dataURL.indexOf(BASE64_MARKER) >= 0) { + if (dataURL.includes(BASE64_MARKER)) { const [, base64] = dataURL.split(BASE64_MARKER); data = decodeBase64(base64); } else { diff --git a/app/javascript/flavours/glitch/polyfills/index.ts b/app/javascript/flavours/glitch/polyfills/index.ts index b2dbfdac0a..e166c09d0e 100644 --- a/app/javascript/flavours/glitch/polyfills/index.ts +++ b/app/javascript/flavours/glitch/polyfills/index.ts @@ -24,6 +24,7 @@ export function loadPolyfills() { // Latest version of Firefox and Safari do not have IntersectionObserver. // Edge does not have requestIdleCallback. // This avoids shipping them all the polyfills. + /* eslint-disable @typescript-eslint/no-unnecessary-condition -- those properties might not exist in old browsers, even if they are always here in types */ const needsExtraPolyfills = !( window.AbortController && window.IntersectionObserver && @@ -31,6 +32,7 @@ export function loadPolyfills() { 'isIntersecting' in IntersectionObserverEntry.prototype && window.requestIdleCallback ); + /* eslint-enable @typescript-eslint/no-unnecessary-condition */ return Promise.all([ loadIntlPolyfills(), diff --git a/app/javascript/flavours/glitch/polyfills/intl.ts b/app/javascript/flavours/glitch/polyfills/intl.ts index 4d5ee3ccf9..b825da6621 100644 --- a/app/javascript/flavours/glitch/polyfills/intl.ts +++ b/app/javascript/flavours/glitch/polyfills/intl.ts @@ -80,6 +80,7 @@ async function loadIntlPluralRulesPolyfills(locale: string) { // } export async function loadIntlPolyfills() { + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- we want to match empty strings const locale = document.querySelector('html')?.lang || 'en'; // order is important here diff --git a/app/javascript/flavours/glitch/scroll.ts b/app/javascript/flavours/glitch/scroll.ts index 4188f64753..35e13a4527 100644 --- a/app/javascript/flavours/glitch/scroll.ts +++ b/app/javascript/flavours/glitch/scroll.ts @@ -38,11 +38,13 @@ const scroll = ( const isScrollBehaviorSupported = 'scrollBehavior' in document.documentElement.style; -export const scrollRight = (node: Element, position: number) => - isScrollBehaviorSupported - ? node.scrollTo({ left: position, behavior: 'smooth' }) - : scroll(node, 'scrollLeft', position); -export const scrollTop = (node: Element) => - isScrollBehaviorSupported - ? node.scrollTo({ top: 0, behavior: 'smooth' }) - : scroll(node, 'scrollTop', 0); +export const scrollRight = (node: Element, position: number) => { + if (isScrollBehaviorSupported) + node.scrollTo({ left: position, behavior: 'smooth' }); + else scroll(node, 'scrollLeft', position); +}; + +export const scrollTop = (node: Element) => { + if (isScrollBehaviorSupported) node.scrollTo({ top: 0, behavior: 'smooth' }); + else scroll(node, 'scrollTop', 0); +}; diff --git a/app/javascript/flavours/glitch/store/middlewares/loading_bar.ts b/app/javascript/flavours/glitch/store/middlewares/loading_bar.ts index 03217b3824..379b3758a1 100644 --- a/app/javascript/flavours/glitch/store/middlewares/loading_bar.ts +++ b/app/javascript/flavours/glitch/store/middlewares/loading_bar.ts @@ -16,7 +16,7 @@ const defaultTypeSuffixes: Config['promiseTypeSuffixes'] = [ export const loadingBarMiddleware = ( config: Config = {}, ): Middleware, RootState> => { - const promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypeSuffixes; + const promiseTypeSuffixes = config.promiseTypeSuffixes ?? defaultTypeSuffixes; return ({ dispatch }) => (next) => @@ -32,7 +32,7 @@ export const loadingBarMiddleware = ( if (action.type.match(isPending)) { dispatch(showLoading()); } else if ( - action.type.match(isFulfilled) || + action.type.match(isFulfilled) ?? action.type.match(isRejected) ) { dispatch(hideLoading()); diff --git a/app/javascript/flavours/glitch/store/middlewares/sounds.ts b/app/javascript/flavours/glitch/store/middlewares/sounds.ts index f7a07149d4..48a5762d6f 100644 --- a/app/javascript/flavours/glitch/store/middlewares/sounds.ts +++ b/app/javascript/flavours/glitch/store/middlewares/sounds.ts @@ -38,7 +38,7 @@ export const soundsMiddleware = (): Middleware< Record, RootState > => { - const soundCache: { [key: string]: HTMLAudioElement } = {}; + const soundCache: Record = {}; void ready(() => { soundCache.boop = createAudio([ @@ -56,9 +56,9 @@ export const soundsMiddleware = (): Middleware< return () => (next) => (action: AnyAction & { meta?: { sound?: string } }) => { - const sound = action?.meta?.sound; + const sound = action.meta?.sound; - if (sound && soundCache[sound]) { + if (sound && Object.hasOwn(soundCache, sound)) { play(soundCache[sound]); } diff --git a/app/javascript/flavours/glitch/utils/filters.ts b/app/javascript/flavours/glitch/utils/filters.ts index e5c6422e0a..d299e80c40 100644 --- a/app/javascript/flavours/glitch/utils/filters.ts +++ b/app/javascript/flavours/glitch/utils/filters.ts @@ -7,7 +7,7 @@ export const toServerSideType = (columnType: string) => { case 'account': return columnType; default: - if (columnType.indexOf('list:') > -1) { + if (columnType.includes('list:')) { return 'home'; } else { return 'public'; // community, account, hashtag diff --git a/app/javascript/flavours/glitch/utils/numbers.ts b/app/javascript/flavours/glitch/utils/numbers.ts index d2c729db75..7139bf8039 100644 --- a/app/javascript/flavours/glitch/utils/numbers.ts +++ b/app/javascript/flavours/glitch/utils/numbers.ts @@ -55,7 +55,7 @@ export function toShortNumber(sourceNumber: number): ShortNumber { */ export function pluralReady( sourceNumber: number, - division: DecimalUnits, + division: DecimalUnits | null, ): number { if (division == null || division < DECIMAL_UNITS.HUNDRED) { return sourceNumber; diff --git a/app/javascript/flavours/glitch/uuid.ts b/app/javascript/flavours/glitch/uuid.ts index 0b4d55beb6..4d0a8a8036 100644 --- a/app/javascript/flavours/glitch/uuid.ts +++ b/app/javascript/flavours/glitch/uuid.ts @@ -4,6 +4,5 @@ export function uuid(a?: string): string { (a as unknown as number) ^ ((Math.random() * 16) >> ((a as unknown as number) / 4)) ).toString(16) - : // eslint-disable-next-line @typescript-eslint/restrict-plus-operands - ('' + 1e7 + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid); + : ('' + 1e7 + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid); }