Skip to content

Commit

Permalink
Merge pull request #656 from 3flex/prefer-nullish-coalescing
Browse files Browse the repository at this point in the history
Enable tseslint stylistic type checked rule set
  • Loading branch information
nielsvanvelzen authored Oct 30, 2024
2 parents 58d4094 + 9ccd21a commit aa8e7b1
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 133 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default [
jsdoc.configs['flat/recommended'],
eslintPluginPrettierRecommended,
...tseslint.configs.strict,
...tseslint.configs.stylisticTypeChecked,
promise.configs['flat/recommended'],
importPlugin.flatConfigs.errors,
importPlugin.flatConfigs.warnings,
Expand Down
2 changes: 1 addition & 1 deletion src/components/__tests__/jellyfinApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JellyfinApi } from '../jellyfinApi';
import { version } from '../../../package.json';

const setupMockCastSenders = (): void => {
const getSenders = (): Array<any> => [{ id: 'thisIsSenderId' }]; // eslint-disable-line @typescript-eslint/no-explicit-any
const getSenders = (): any[] => [{ id: 'thisIsSenderId' }]; // eslint-disable-line @typescript-eslint/no-explicit-any
const getInstance = (): any => ({ getSenders }); // eslint-disable-line @typescript-eslint/no-explicit-any

// @ts-expect-error cast is already defined globally, however since we're mocking it we need to redefine it.
Expand Down
14 changes: 7 additions & 7 deletions src/components/codecSupportHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export function getH265LevelSupport(deviceId: DeviceIds): number {
* Get VPX (VP8, VP9) codecs supported by the active Cast device.
* @returns Supported VPX codecs.
*/
export function getSupportedVPXVideoCodecs(): Array<string> {
export function getSupportedVPXVideoCodecs(): string[] {
const codecs = [];

if (hasVP8Support()) {
Expand All @@ -209,7 +209,7 @@ export function getSupportedVPXVideoCodecs(): Array<string> {
* Get supported video codecs suitable for use in an MP4 container.
* @returns Supported MP4 video codecs.
*/
export function getSupportedMP4VideoCodecs(): Array<string> {
export function getSupportedMP4VideoCodecs(): string[] {
const codecs = ['h264'];

if (hasH265Support()) {
Expand All @@ -224,7 +224,7 @@ export function getSupportedMP4VideoCodecs(): Array<string> {
* Get supported audio codecs suitable for use in an MP4 container.
* @returns Supported MP4 audio codecs.
*/
export function getSupportedMP4AudioCodecs(): Array<string> {
export function getSupportedMP4AudioCodecs(): string[] {
const codecs = [];

if (hasEAC3Support()) {
Expand All @@ -245,7 +245,7 @@ export function getSupportedMP4AudioCodecs(): Array<string> {
* Get supported video codecs suitable for use with HLS.
* @returns Supported HLS video codecs.
*/
export function getSupportedHLSVideoCodecs(): Array<string> {
export function getSupportedHLSVideoCodecs(): string[] {
// Currently the server does not support fmp4 which is required
// by the HLS spec for streaming H.265 video.
return ['h264'];
Expand All @@ -255,7 +255,7 @@ export function getSupportedHLSVideoCodecs(): Array<string> {
* Get supported audio codecs suitable for use with HLS.
* @returns All supported HLS audio codecs.
*/
export function getSupportedHLSAudioCodecs(): Array<string> {
export function getSupportedHLSAudioCodecs(): string[] {
// HLS basically supports whatever MP4 supports.
return getSupportedMP4AudioCodecs();
}
Expand All @@ -264,14 +264,14 @@ export function getSupportedHLSAudioCodecs(): Array<string> {
* Get supported audio codecs suitable for use in a WebM container.
* @returns All supported WebM audio codecs.
*/
export function getSupportedWebMAudioCodecs(): Array<string> {
export function getSupportedWebMAudioCodecs(): string[] {
return ['vorbis', 'opus'];
}

/**
* Get supported audio codecs suitable for use in a WebM container.
* @returns All supported WebM audio codecs.
*/
export function getSupportedAudioCodecs(): Array<string> {
export function getSupportedAudioCodecs(): string[] {
return ['opus', 'mp3', 'aac', 'flac', 'webma', 'wav'];
}
26 changes: 13 additions & 13 deletions src/components/commandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,36 @@ export abstract class CommandHandler {
}

static playNextHandler(data: DataMessage): void {
translateItems(data, <PlayRequest>data.options, data.command);
translateItems(data, data.options as PlayRequest, data.command);
}

static playNowHandler(data: DataMessage): void {
translateItems(data, <PlayRequest>data.options, data.command);
translateItems(data, data.options as PlayRequest, data.command);
}

static playLastHandler(data: DataMessage): void {
translateItems(data, <PlayRequest>data.options, data.command);
translateItems(data, data.options as PlayRequest, data.command);
}

static shuffleHandler(data: DataMessage): void {
shuffle(
data,
<PlayRequest>data.options,
(<PlayRequest>data.options).items[0]
data.options as PlayRequest,
(data.options as PlayRequest).items[0]
);
}

static instantMixHandler(data: DataMessage): void {
instantMix(
data,
<PlayRequest>data.options,
(<PlayRequest>data.options).items[0]
data.options as PlayRequest,
(data.options as PlayRequest).items[0]
);
}

static displayContentHandler(data: DataMessage): void {
if (!PlaybackManager.isPlaying()) {
DocumentManager.showItemId((<DisplayRequest>data.options).ItemId);
DocumentManager.showItemId((data.options as DisplayRequest).ItemId);
}
}

Expand All @@ -103,14 +103,14 @@ export abstract class CommandHandler {
static setAudioStreamIndexHandler(data: DataMessage): void {
setAudioStreamIndex(
PlaybackManager.playbackState,
(<SetIndexRequest>data.options).index
(data.options as SetIndexRequest).index
);
}

static setSubtitleStreamIndexHandler(data: DataMessage): void {
setSubtitleStreamIndex(
PlaybackManager.playbackState,
(<SetIndexRequest>data.options).index
(data.options as SetIndexRequest).index
);
}

Expand Down Expand Up @@ -155,7 +155,7 @@ export abstract class CommandHandler {
static SeekHandler(data: DataMessage): void {
seek(
PlaybackManager.playbackState,
(<SeekRequest>data.options).position * TicksPerSecond
(data.options as SeekRequest).position * TicksPerSecond
);
}

Expand Down Expand Up @@ -189,7 +189,7 @@ export abstract class CommandHandler {
}

static SetRepeatModeHandler(data: DataMessage): void {
window.repeatMode = (<SetRepeatModeRequest>data.options).RepeatMode;
window.repeatMode = (data.options as SetRepeatModeRequest).RepeatMode;
window.reportEventType = 'repeatmodechange';
}

Expand All @@ -200,7 +200,7 @@ export abstract class CommandHandler {
// We should avoid using a defaulthandler that has a purpose other than informing the dev/user
// Currently all unhandled commands will be treated as play commands.
static defaultHandler(data: DataMessage): void {
translateItems(data, <PlayRequest>data.options, 'play');
translateItems(data, data.options as PlayRequest, 'play');
}

static processMessage(data: DataMessage, command: string): void {
Expand Down
4 changes: 1 addition & 3 deletions src/components/credentialManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ export interface ServerCredential {
serverBasePath?: string;
}

interface CredentialStore {
[id: string]: ServerCredential;
}
type CredentialStore = Record<string, ServerCredential>;

export class credentialManager {
/**
Expand Down
18 changes: 9 additions & 9 deletions src/components/deviceprofileBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ function createProfileCondition(
* @todo Why does this always return an empty array?
* @returns Container profiles.
*/
function getContainerProfiles(): Array<ContainerProfile> {
function getContainerProfiles(): ContainerProfile[] {
return [];
}

/**
* Get direct play profiles
* @returns Direct play profiles.
*/
function getDirectPlayProfiles(): Array<DirectPlayProfile> {
const DirectPlayProfiles: Array<DirectPlayProfile> = [];
function getDirectPlayProfiles(): DirectPlayProfile[] {
const DirectPlayProfiles: DirectPlayProfile[] = [];

if (currentDeviceId !== DeviceIds.AUDIO) {
const mp4VideoCodecs = getSupportedMP4VideoCodecs();
Expand Down Expand Up @@ -140,8 +140,8 @@ function getDirectPlayProfiles(): Array<DirectPlayProfile> {
* Get codec profiles
* @returns Codec profiles.
*/
function getCodecProfiles(): Array<CodecProfile> {
const CodecProfiles: Array<CodecProfile> = [];
function getCodecProfiles(): CodecProfile[] {
const CodecProfiles: CodecProfile[] = [];

const audioConditions: CodecProfile = {
Codec: 'flac',
Expand Down Expand Up @@ -285,8 +285,8 @@ function getCodecProfiles(): Array<CodecProfile> {
* Get transcoding profiles
* @returns Transcoding profiles.
*/
function getTranscodingProfiles(): Array<TranscodingProfile> {
const TranscodingProfiles: Array<TranscodingProfile> = [];
function getTranscodingProfiles(): TranscodingProfile[] {
const TranscodingProfiles: TranscodingProfile[] = [];

const hlsAudioCodecs = getSupportedHLSAudioCodecs();
const audioChannels: number = hasSurroundSupport() ? 6 : 2;
Expand Down Expand Up @@ -364,8 +364,8 @@ function getTranscodingProfiles(): Array<TranscodingProfile> {
* Get subtitle profiles
* @returns Subtitle profiles.
*/
function getSubtitleProfiles(): Array<SubtitleProfile> {
const subProfiles: Array<SubtitleProfile> = [];
function getSubtitleProfiles(): SubtitleProfile[] {
const subProfiles: SubtitleProfile[] = [];

if (hasTextTrackSupport(currentDeviceId)) {
subProfiles.push({
Expand Down
39 changes: 15 additions & 24 deletions src/components/documentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,7 @@ export abstract class DocumentManager {
let src: string | null = null;

if (item != null) {
if (
item.BackdropImageTags &&
item.BackdropImageTags.length &&
item.Id
) {
if (item.BackdropImageTags?.length && item.Id) {
// get first backdrop of image if applicable
src = JellyfinApi.createImageUrl(
item.Id,
Expand All @@ -317,8 +313,7 @@ export abstract class DocumentManager {
);
} else if (
item.ParentBackdropItemId &&
item.ParentBackdropImageTags &&
item.ParentBackdropImageTags.length
item.ParentBackdropImageTags?.length
) {
// otherwise get first backdrop from parent
src = JellyfinApi.createImageUrl(
Expand Down Expand Up @@ -376,7 +371,7 @@ export abstract class DocumentManager {
let src: string | null = null;
let item: BaseItemDto | null = null;

if (result.Items && result.Items[0]) {
if (result.Items?.[0]) {
item = result.Items[0];
src = await DocumentManager.getWaitingBackdropUrl(item);
}
Expand Down Expand Up @@ -459,20 +454,15 @@ export abstract class DocumentManager {

let backdropUrl: string | null = null;

if (
item.BackdropImageTags &&
item.BackdropImageTags.length &&
item.Id
) {
if (item.BackdropImageTags?.length && item.Id) {
backdropUrl = JellyfinApi.createImageUrl(
item.Id,
'Backdrop',
item.BackdropImageTags[0]
);
} else if (
item.ParentBackdropItemId &&
item.ParentBackdropImageTags &&
item.ParentBackdropImageTags.length
item.ParentBackdropImageTags?.length
) {
backdropUrl = JellyfinApi.createImageUrl(
item.ParentBackdropItemId,
Expand Down Expand Up @@ -521,7 +511,8 @@ export abstract class DocumentManager {
* @param item - source for the displayed name
*/
private static setDisplayName(item: BaseItemDto): void {
const name: string = item.EpisodeTitle ?? <string>item.Name;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const name: string = item.EpisodeTitle ?? item.Name!;

let displayName: string = name;

Expand Down Expand Up @@ -555,7 +546,7 @@ export abstract class DocumentManager {
private static setGenres(name: string | null): void {
const element = this.querySelector('.genres');

element.innerHTML = name || '';
element.innerHTML = name ?? '';
}

/**
Expand All @@ -565,7 +556,7 @@ export abstract class DocumentManager {
private static setOverview(name: string | null): void {
const element = this.querySelector('.overview');

element.innerHTML = name || '';
element.innerHTML = name ?? '';
}

/**
Expand All @@ -574,9 +565,9 @@ export abstract class DocumentManager {
* @param value - Percentage to set
*/
private static setPlayedPercentage(value = 0): void {
const element = <HTMLInputElement>(
this.querySelector('.itemProgressBar')
);
const element = this.querySelector(
'.itemProgressBar'
) as HTMLInputElement;

element.value = value.toString();
}
Expand All @@ -590,9 +581,9 @@ export abstract class DocumentManager {
const element = this.querySelector('.detailImageProgressContainer');

if (value) {
(<HTMLElement>element).classList.remove('d-none');
(element as HTMLElement).classList.remove('d-none');
} else {
(<HTMLElement>element).classList.add('d-none');
(element as HTMLElement).classList.add('d-none');
}
}

Expand Down Expand Up @@ -643,7 +634,7 @@ export abstract class DocumentManager {
* @param item - to look up
*/
private static setMiscInfo(item: BaseItemDto): void {
const info: Array<string> = [];
const info: string[] = [];

if (item.Type == 'Episode') {
if (item.PremiereDate) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/fetchhelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export async function ajax(request: any): Promise<Response | string> {
request.dataType === 'text' ||
(response.headers.get('Content-Type') || '')
.toLowerCase()
.indexOf('text/') === 0
.startsWith('text/')
) {
return response.text();
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/components/jellyfinApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ export abstract class JellyfinApi {
};

if (this.accessToken) {
parameters['Token'] = this.accessToken;
parameters.Token = this.accessToken;
}

if (this.deviceId) {
parameters['DeviceId'] = this.deviceId;
parameters.DeviceId = this.deviceId;
}

if (this.deviceName) {
parameters['Device'] = this.deviceName;
parameters.Device = this.deviceName;
}

let header = 'MediaBrowser';
Expand All @@ -93,7 +93,7 @@ export abstract class JellyfinApi {
}

// Remove leading slashes
while (path.charAt(0) === '/') {
while (path.startsWith('/')) {
path = path.substring(1);
}

Expand All @@ -104,7 +104,7 @@ export abstract class JellyfinApi {
public static createUserUrl(path: string | null = null): string {
if (path) {
// Remove leading slashes
while (path.charAt(0) === '/') {
while (path.startsWith('/')) {
path = path.substring(1);
}

Expand Down
Loading

0 comments on commit aa8e7b1

Please sign in to comment.