From 71a98b2c03666ac19d09644a26581bddcb9ea68f Mon Sep 17 00:00:00 2001 From: Murderlon Date: Wed, 6 Dec 2023 09:55:17 +0100 Subject: [PATCH] Fix build:ts --- packages/@uppy/core/src/Restricter.ts | 7 +++-- packages/@uppy/core/src/Uppy.test.ts | 2 ++ packages/@uppy/core/src/Uppy.ts | 28 +++++++++++-------- .../@uppy/core/src/mocks/acquirerPlugin1.ts | 13 +++++++-- .../@uppy/core/src/mocks/acquirerPlugin2.ts | 13 +++++++-- .../core/src/mocks/invalidPluginWithoutId.ts | 5 ++-- .../src/mocks/invalidPluginWithoutType.ts | 5 ++-- packages/@uppy/core/tsconfig.json | 1 + 8 files changed, 51 insertions(+), 23 deletions(-) diff --git a/packages/@uppy/core/src/Restricter.ts b/packages/@uppy/core/src/Restricter.ts index 3d75cd741b..22aeda6341 100644 --- a/packages/@uppy/core/src/Restricter.ts +++ b/packages/@uppy/core/src/Restricter.ts @@ -1,5 +1,8 @@ +/* eslint-disable @typescript-eslint/ban-ts-comment */ /* eslint-disable max-classes-per-file, class-methods-use-this */ +// @ts-ignore untyped import prettierBytes from '@transloadit/prettier-bytes' +// @ts-ignore untyped import match from 'mime-match' import Translator from '@uppy/utils/lib/Translator' import type { Body, Meta, UppyFile } from '@uppy/utils/lib/UppyFile' @@ -36,7 +39,7 @@ class RestrictionError extends Error { opts?: { isUserFacing?: boolean; file?: UppyFile }, ) { super(message) - this.isUserFacing = opts?.isUserFacing ?? true + this.isUserFacing = opts?.isUserFacing ?? false if (opts?.file) { this.file = opts.file // only some restriction errors are related to a particular file } @@ -186,7 +189,7 @@ class Restricter { this.i18n('missingRequiredMetaFieldOnFile', { fileName: file.name }), ) const { requiredMetaFields } = this.getOpts().restrictions - const missingFields = [] + const missingFields: string[] = [] for (const field of requiredMetaFields) { if (!Object.hasOwn(file.meta, field) || file.meta[field] === '') { diff --git a/packages/@uppy/core/src/Uppy.test.ts b/packages/@uppy/core/src/Uppy.test.ts index f9bbb6b077..8c78930efd 100644 --- a/packages/@uppy/core/src/Uppy.test.ts +++ b/packages/@uppy/core/src/Uppy.test.ts @@ -50,6 +50,7 @@ describe('src/Core', () => { const core = new Core() core.use(AcquirerPlugin1) expect( + // @ts-expect-error untyped Object.keys(core[Symbol.for('uppy test: getPlugins')]('acquirer')) .length, ).toEqual(1) @@ -68,6 +69,7 @@ describe('src/Core', () => { const core = new Core() expect(() => { + // @ts-expect-error expected core.use(InvalidPlugin) }).toThrowErrorMatchingSnapshot() }) diff --git a/packages/@uppy/core/src/Uppy.ts b/packages/@uppy/core/src/Uppy.ts index e5240327dd..663b179bda 100644 --- a/packages/@uppy/core/src/Uppy.ts +++ b/packages/@uppy/core/src/Uppy.ts @@ -2,7 +2,8 @@ /* global AggregateError */ import Translator from '@uppy/utils/lib/Translator' -// @ts-expect-error untyped +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore untyped import ee from 'namespace-emitter' import { nanoid } from 'nanoid/non-secure' import throttle from 'lodash/throttle.js' @@ -43,6 +44,11 @@ type UnknownPlugin = InstanceType< typeof BasePlugin | typeof UIPlugin > +type MinimalRequiredUppyFile = Required< + Pick, 'name' | 'data' | 'type' | 'source'> +> & + Partial, 'name' | 'data' | 'type' | 'source'>> + interface UploadResult { successful?: UppyFile[] failed?: UppyFile[] @@ -327,7 +333,8 @@ export class Uppy { // Exposing uppy object on window for debugging and testing if (this.opts.debug && typeof window !== 'undefined') { - // @ts-expect-error fine + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore window[this.opts.id] = this } @@ -798,8 +805,8 @@ export class Uppy { // create a copy of the files object only once const nextFilesState = { ...existingFiles } - const validFilesToAdd = [] - const errors = [] + const validFilesToAdd: UppyFile[] = [] + const errors: RestrictionError[] = [] for (const fileToAdd of filesToAdd) { try { @@ -891,11 +898,11 @@ export class Uppy { * try to guess file type in a clever way, check file against restrictions, * and start an upload if `autoProceed === true`. */ - addFile(file: UppyFile): UppyFile['id'] { - this.#assertNewUploadAllowed(file) + addFile(file: MinimalRequiredUppyFile): UppyFile['id'] { + this.#assertNewUploadAllowed(file as UppyFile) const { nextFilesState, validFilesToAdd, errors } = - this.#checkAndUpdateFileState([file]) + this.#checkAndUpdateFileState([file as UppyFile]) const restrictionErrors = errors.filter((error) => error.isRestriction) this.#informAndEmit(restrictionErrors) @@ -924,11 +931,11 @@ export class Uppy { * This is good for UI plugins, but not for programmatic use. * Programmatic users should usually still use `addFile()` on individual files. */ - addFiles(fileDescriptors: UppyFile[]): void { + addFiles(fileDescriptors: MinimalRequiredUppyFile[]): void { this.#assertNewUploadAllowed() const { nextFilesState, validFilesToAdd, errors } = - this.#checkAndUpdateFileState(fileDescriptors) + this.#checkAndUpdateFileState(fileDescriptors as UppyFile[]) const restrictionErrors = errors.filter((error) => error.isRestriction) this.#informAndEmit(restrictionErrors) @@ -1800,8 +1807,7 @@ export class Uppy { return uploadID } - // @ts-expect-error same as createUpload - private [Symbol.for('uppy test: createUpload')](...args): string { + private [Symbol.for('uppy test: createUpload')](...args: any[]): string { // @ts-expect-error https://github.com/microsoft/TypeScript/issues/47595 return this.#createUpload(...args) } diff --git a/packages/@uppy/core/src/mocks/acquirerPlugin1.ts b/packages/@uppy/core/src/mocks/acquirerPlugin1.ts index 5fc46ba2f3..2968f37849 100644 --- a/packages/@uppy/core/src/mocks/acquirerPlugin1.ts +++ b/packages/@uppy/core/src/mocks/acquirerPlugin1.ts @@ -1,8 +1,15 @@ import { vi } from 'vitest' // eslint-disable-line import/no-extraneous-dependencies import UIPlugin from '../UIPlugin.ts' +import type Uppy from '../Uppy.ts' + +type mock = ReturnType export default class TestSelector1 extends UIPlugin { - constructor(uppy, opts) { + name: string + + mocks: { run: mock; update: mock; uninstall: mock } + + constructor(uppy: Uppy, opts: any) { super(uppy, opts) this.type = 'acquirer' this.id = 'TestSelector1' @@ -15,7 +22,7 @@ export default class TestSelector1 extends UIPlugin { } } - run(results) { + run(results: any) { this.uppy.log({ class: this.constructor.name, method: 'run', @@ -25,7 +32,7 @@ export default class TestSelector1 extends UIPlugin { return Promise.resolve('success') } - update(state) { + update(state: any) { this.mocks.update(state) } diff --git a/packages/@uppy/core/src/mocks/acquirerPlugin2.ts b/packages/@uppy/core/src/mocks/acquirerPlugin2.ts index 15e499c0bd..01a5c2fa1c 100644 --- a/packages/@uppy/core/src/mocks/acquirerPlugin2.ts +++ b/packages/@uppy/core/src/mocks/acquirerPlugin2.ts @@ -1,8 +1,15 @@ import { vi } from 'vitest' // eslint-disable-line import/no-extraneous-dependencies import UIPlugin from '../UIPlugin.ts' +import type Uppy from '../Uppy.ts' + +type mock = ReturnType export default class TestSelector2 extends UIPlugin { - constructor(uppy, opts) { + name: string + + mocks: { run: mock; update: mock; uninstall: mock } + + constructor(uppy: Uppy, opts: any) { super(uppy, opts) this.type = 'acquirer' this.id = 'TestSelector2' @@ -15,7 +22,7 @@ export default class TestSelector2 extends UIPlugin { } } - run(results) { + run(results: any) { this.uppy.log({ class: this.constructor.name, method: 'run', @@ -25,7 +32,7 @@ export default class TestSelector2 extends UIPlugin { return Promise.resolve('success') } - update(state) { + update(state: any) { this.mocks.update(state) } diff --git a/packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts b/packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts index 93fe3e4cc7..f24dc4b8f0 100644 --- a/packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts +++ b/packages/@uppy/core/src/mocks/invalidPluginWithoutId.ts @@ -1,17 +1,18 @@ import UIPlugin from '../UIPlugin.ts' +import type Uppy from '../Uppy.ts' export default class InvalidPluginWithoutName extends UIPlugin { public type: string public name: string - constructor(uppy, opts) { + constructor(uppy: Uppy, opts: any) { super(uppy, opts) this.type = 'acquirer' this.name = this.constructor.name } - run(results) { + run(results: any) { this.uppy.log({ class: this.constructor.name, method: 'run', diff --git a/packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts b/packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts index 96ebad2c8c..3e600eaed4 100644 --- a/packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts +++ b/packages/@uppy/core/src/mocks/invalidPluginWithoutType.ts @@ -1,17 +1,18 @@ import UIPlugin from '../UIPlugin.ts' +import type Uppy from '../Uppy.ts' export default class InvalidPluginWithoutType extends UIPlugin { public id: string public name: string - constructor(uppy, opts) { + constructor(uppy: Uppy, opts: any) { super(uppy, opts) this.id = 'InvalidPluginWithoutType' this.name = this.constructor.name } - run(results) { + run(results: any) { this.uppy.log({ class: this.constructor.name, method: 'run', diff --git a/packages/@uppy/core/tsconfig.json b/packages/@uppy/core/tsconfig.json index 2362543d82..7ea5c74920 100644 --- a/packages/@uppy/core/tsconfig.json +++ b/packages/@uppy/core/tsconfig.json @@ -5,6 +5,7 @@ "noEmit": true }, "include": ["./package.json", "./src/**/*.*"], + "exclude": ["./src/Uppy.test.ts"], "references": [ { "path": "../store-default/tsconfig.build.json"