From ead13fe1aa723eedb3f842df3042f1943bd529db Mon Sep 17 00:00:00 2001 From: Matthew Haughton <3flex@users.noreply.github.com> Date: Wed, 30 Oct 2024 09:31:45 +1100 Subject: [PATCH] Enable prefer-nullish-coalescing ts eslint rule --- eslint.config.mjs | 1 + src/components/documentManager.ts | 4 ++-- src/components/maincontroller.ts | 9 ++------- src/helpers.ts | 20 ++++++++++---------- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 27bfc245..0f282bbc 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -60,6 +60,7 @@ export default [ '@typescript-eslint/no-explicit-any': 'warn', '@typescript-eslint/no-unused-expressions': 'warn', '@typescript-eslint/no-unused-vars': 'error', + '@typescript-eslint/prefer-nullish-coalescing': 'error', '@typescript-eslint/prefer-ts-expect-error': 'error', curly: 'error', 'import/newline-after-import': 'error', diff --git a/src/components/documentManager.ts b/src/components/documentManager.ts index c493bff7..cc432324 100644 --- a/src/components/documentManager.ts +++ b/src/components/documentManager.ts @@ -555,7 +555,7 @@ export abstract class DocumentManager { private static setGenres(name: string | null): void { const element = this.querySelector('.genres'); - element.innerHTML = name || ''; + element.innerHTML = name ?? ''; } /** @@ -565,7 +565,7 @@ export abstract class DocumentManager { private static setOverview(name: string | null): void { const element = this.querySelector('.overview'); - element.innerHTML = name || ''; + element.innerHTML = name ?? ''; } /** diff --git a/src/components/maincontroller.ts b/src/components/maincontroller.ts index f5d82680..51ea7275 100644 --- a/src/components/maincontroller.ts +++ b/src/components/maincontroller.ts @@ -519,17 +519,12 @@ export async function changeStream( // @ts-expect-error is possible here return await PlaybackManager.playItemInternal(state.item, { - audioStreamIndex: - params.AudioStreamIndex == null - ? state.audioStreamIndex - : params.AudioStreamIndex, + audioStreamIndex: params.AudioStreamIndex ?? state.audioStreamIndex, liveStreamId: state.liveStreamId, mediaSourceId: state.mediaSourceId, startPositionTicks: ticks, subtitleStreamIndex: - params.SubtitleStreamIndex == null - ? state.subtitleStreamIndex - : params.SubtitleStreamIndex + params.SubtitleStreamIndex ?? state.subtitleStreamIndex }); } diff --git a/src/helpers.ts b/src/helpers.ts index 04a13eb1..e57e9663 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -93,7 +93,7 @@ export function getSenderReportingData( const nowPlayingItem = state.NowPlayingItem; nowPlayingItem.ServerId = item.ServerId; - nowPlayingItem.Chapters = item.Chapters || []; + nowPlayingItem.Chapters = item.Chapters ?? []; const mediaSource = item.MediaSources?.filter((m: MediaSourceInfo) => { return m.Id == reportingData.MediaSourceId; @@ -116,7 +116,7 @@ export function getSenderReportingData( nowPlayingItem.Album = item.Album; nowPlayingItem.Artists = item.Artists; - const imageTags = item.ImageTags || {}; + const imageTags = item.ImageTags ?? {}; if (item.SeriesPrimaryImageTag) { nowPlayingItem.PrimaryImageItemId = item.SeriesId; @@ -239,7 +239,7 @@ export function getMetadata(item: BaseItemDto): any { } // previously: p.PersonType == 'Type'.. wtf? - const composer = (item.People || []).filter( + const composer = (item.People ?? []).filter( (p: BaseItemPerson) => p.Type == 'Composer' )[0]; @@ -326,7 +326,7 @@ export function createStreamInfo( `videos/${item.Id}/stream.${mediaSource.Container}?mediaSourceId=${mediaSource.Id}&api_key=${JellyfinApi.accessToken}&static=true${seekParam}` ); isStatic = true; - playerStartPositionTicks = startPosition || 0; + playerStartPositionTicks = startPosition ?? 0; } else { // TODO deal with !TranscodingUrl mediaUrl = JellyfinApi.createUrl( @@ -335,7 +335,7 @@ export function createStreamInfo( if (isHlsStream(mediaSource)) { mediaUrl += seekParam; - playerStartPositionTicks = startPosition || 0; + playerStartPositionTicks = startPosition ?? 0; contentType = 'application/x-mpegURL'; streamContainer = 'm3u8'; } else { @@ -355,13 +355,13 @@ export function createStreamInfo( if (mediaSource.SupportsDirectPlay) { mediaUrl = mediaSource.Path; isStatic = true; - playerStartPositionTicks = startPosition || 0; + playerStartPositionTicks = startPosition ?? 0; } else { const isDirectStream = mediaSource.SupportsDirectStream; if (isDirectStream) { const outputContainer = ( - mediaSource.Container || '' + mediaSource.Container ?? '' ).toLowerCase(); mediaUrl = JellyfinApi.createUrl( @@ -382,7 +382,7 @@ export function createStreamInfo( // TODO: Remove the second half of the expression by supporting changing the mediaElement src dynamically. // It is a pain and will require unbinding all event handlers during the operation - const canSeek = (mediaSource.RunTimeTicks || 0) > 0; + const canSeek = (mediaSource.RunTimeTicks ?? 0) > 0; // eslint-disable-next-line @typescript-eslint/no-explicit-any const info: any = { @@ -560,7 +560,7 @@ export async function getItemsForPlayback( query: ItemQuery ): Promise { query.UserId = userId; - query.Limit = query.Limit || 100; + query.Limit = query.Limit ?? 100; query.Fields = requiredItemFields; query.ExcludeLocationTypes = 'Virtual'; @@ -720,7 +720,7 @@ export async function translateRequestedItems( } ); - episodesResult.TotalRecordCount = episodesResult.Items?.length || 0; + episodesResult.TotalRecordCount = episodesResult.Items?.length ?? 0; return episodesResult; }