From e9e504b40dde8691c32c933c7e27e28cfad1c8b5 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 19 Dec 2023 01:17:22 -0500 Subject: Template type improvements (#380) * Default templates to unknown * Default typescript templates to unknown * More template type updates --- dev/manifest-util.js | 4 ++-- ext/js/comm/frame-client.js | 2 +- ext/js/core.js | 6 +++--- ext/js/data/database.js | 6 +++--- ext/js/data/json-schema.js | 2 +- ext/js/display/display-generator.js | 2 +- ext/js/dom/selector-observer.js | 2 +- ext/js/general/cache-map.js | 3 ++- ext/js/general/task-accumulator.js | 3 ++- ext/js/language/translator.js | 2 +- .../pages/settings/keyboard-mouse-input-field.js | 2 +- ext/js/pages/settings/profile-controller.js | 2 +- test/json-schema.test.js | 2 +- types/ext/anki-templates-internal.d.ts | 2 +- types/ext/api.d.ts | 2 +- types/ext/cache-map.d.ts | 2 +- types/ext/core.d.ts | 2 +- types/ext/dictionary-data-util.d.ts | 2 +- types/ext/dictionary-database.d.ts | 6 +++--- types/ext/dictionary-worker.d.ts | 6 +++--- types/ext/dom-data-binder.d.ts | 24 +++++++++++----------- types/ext/dynamic-property.d.ts | 2 +- types/ext/selector-observer.d.ts | 12 +++++------ types/ext/task-accumulator.d.ts | 2 +- 24 files changed, 51 insertions(+), 49 deletions(-) diff --git a/dev/manifest-util.js b/dev/manifest-util.js index ac9b58db..6a53c8d6 100644 --- a/dev/manifest-util.js +++ b/dev/manifest-util.js @@ -25,7 +25,7 @@ import {parseJson} from './json.js'; const dirname = path.dirname(fileURLToPath(import.meta.url)); /** - * @template T + * @template [T=unknown] * @param {T} value * @returns {T} */ @@ -240,7 +240,7 @@ export class ManifestUtil { } /** - * @template T + * @template [T=unknown] * @param {T[]} array1 * @param {T[]} array2 * @param {number} lengthOffset diff --git a/ext/js/comm/frame-client.js b/ext/js/comm/frame-client.js index b07bb796..5e997622 100644 --- a/ext/js/comm/frame-client.js +++ b/ext/js/comm/frame-client.js @@ -56,7 +56,7 @@ export class FrameClient { } /** - * @template T + * @template [T=unknown] * @param {T} data * @returns {import('frame-client').Message} * @throws {Error} diff --git a/ext/js/core.js b/ext/js/core.js index d16a2099..d2372352 100644 --- a/ext/js/core.js +++ b/ext/js/core.js @@ -48,7 +48,7 @@ export function stringReverse(string) { /** * Creates a deep clone of an object or value. This is similar to `parseJson(JSON.stringify(value))`. - * @template T + * @template [T=unknown] * @param {T} value The value to clone. * @returns {T} A new clone of the value. * @throws An error if the value is circular and cannot be cloned. @@ -245,7 +245,7 @@ export function generateId(length) { /** * Creates an unresolved promise that can be resolved later, outside the promise's executor function. - * @template T + * @template [T=unknown] * @returns {import('core').DeferredPromiseDetails} An object `{promise, resolve, reject}`, containing the promise and the resolve/reject functions. */ export function deferPromise() { @@ -514,7 +514,7 @@ export class EventListenerCollection { /** * Class representing a generic value with an override stack. * Changes can be observed by listening to the 'change' event. - * @template T + * @template [T=unknown] * @augments EventDispatcher */ export class DynamicProperty extends EventDispatcher { diff --git a/ext/js/data/database.js b/ext/js/data/database.js index c8047b41..43ac9e5b 100644 --- a/ext/js/data/database.js +++ b/ext/js/data/database.js @@ -153,7 +153,7 @@ export class Database { } /** - * @template TPredicateArg + * @template [TPredicateArg=unknown] * @template [TResult=unknown] * @template [TResultDefault=unknown] * @param {TObjectStoreName} objectStoreName @@ -174,8 +174,8 @@ export class Database { } /** - * @template TData - * @template TPredicateArg + * @template [TData=unknown] + * @template [TPredicateArg=unknown] * @template [TResult=unknown] * @template [TResultDefault=unknown] * @param {IDBObjectStore|IDBIndex} objectStoreOrIndex diff --git a/ext/js/data/json-schema.js b/ext/js/data/json-schema.js index d63cfd1a..52a55d85 100644 --- a/ext/js/data/json-schema.js +++ b/ext/js/data/json-schema.js @@ -249,7 +249,7 @@ export class JsonSchema { } /** - * @template T + * @template [T=unknown] * @param {T} value * @returns {T} */ diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js index 7bedfa12..ccdd49cf 100644 --- a/ext/js/display/display-generator.js +++ b/ext/js/display/display-generator.js @@ -845,7 +845,7 @@ export class DisplayGenerator { } /** - * @template TItem + * @template [TItem=unknown] * @template [TExtraArg=void] * @param {HTMLElement} container * @param {(item: TItem, arg: TExtraArg) => ?Node} createItem diff --git a/ext/js/dom/selector-observer.js b/ext/js/dom/selector-observer.js index 8dd18242..cff2985c 100644 --- a/ext/js/dom/selector-observer.js +++ b/ext/js/dom/selector-observer.js @@ -18,7 +18,7 @@ /** * Class which is used to observe elements matching a selector in specific element. - * @template T + * @template [T=unknown] */ export class SelectorObserver { /** diff --git a/ext/js/general/cache-map.js b/ext/js/general/cache-map.js index cc706380..a995b8c7 100644 --- a/ext/js/general/cache-map.js +++ b/ext/js/general/cache-map.js @@ -18,7 +18,8 @@ /** - * @template K,V + * @template [K=unknown] + * @template [V=unknown] * Class which caches a map of values, keeping the most recently accessed values. */ export class CacheMap { diff --git a/ext/js/general/task-accumulator.js b/ext/js/general/task-accumulator.js index cb136908..86cb9e3e 100644 --- a/ext/js/general/task-accumulator.js +++ b/ext/js/general/task-accumulator.js @@ -19,7 +19,8 @@ import {log} from '../core.js'; /** - * @template K,V + * @template [K=unknown] + * @template [V=unknown] */ export class TaskAccumulator { /** diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index e33ea4d4..bf22ee6b 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -1887,7 +1887,7 @@ export class Translator { // Miscellaneous /** - * @template T + * @template [T=unknown] * @param {Set} set * @param {T[]} values * @returns {boolean} diff --git a/ext/js/pages/settings/keyboard-mouse-input-field.js b/ext/js/pages/settings/keyboard-mouse-input-field.js index 99af3941..f50ca112 100644 --- a/ext/js/pages/settings/keyboard-mouse-input-field.js +++ b/ext/js/pages/settings/keyboard-mouse-input-field.js @@ -315,7 +315,7 @@ export class KeyboardMouseInputField extends EventDispatcher { } /** - * @template T + * @template [T=unknown] * @param {T[]} array1 * @param {T[]} array2 * @returns {boolean} diff --git a/ext/js/pages/settings/profile-controller.js b/ext/js/pages/settings/profile-controller.js index 6e0710a8..c54bfe73 100644 --- a/ext/js/pages/settings/profile-controller.js +++ b/ext/js/pages/settings/profile-controller.js @@ -595,7 +595,7 @@ export class ProfileController { } /** - * @template T + * @template [T=unknown] * @param {T} currentValue * @param {T} value1 * @param {T} value2 diff --git a/test/json-schema.test.js b/test/json-schema.test.js index e6817d23..fb7644de 100644 --- a/test/json-schema.test.js +++ b/test/json-schema.test.js @@ -50,7 +50,7 @@ function createProxy(schema, value) { } /** - * @template T + * @template [T=unknown] * @param {T} value * @returns {T} */ diff --git a/types/ext/anki-templates-internal.d.ts b/types/ext/anki-templates-internal.d.ts index 4cb50050..2842765d 100644 --- a/types/ext/anki-templates-internal.d.ts +++ b/types/ext/anki-templates-internal.d.ts @@ -53,7 +53,7 @@ export type CreateDetails = { media?: AnkiTemplates.Media; }; -export type CachedValue = { +export type CachedValue = { getter: () => T; hasValue: boolean; value: T | undefined; diff --git a/types/ext/api.d.ts b/types/ext/api.d.ts index 2d78cc06..41d3a96d 100644 --- a/types/ext/api.d.ts +++ b/types/ext/api.d.ts @@ -34,7 +34,7 @@ import type * as Translator from './translator'; // Generic -export type Handler = ( +export type Handler = ( details: TDetails, sender: (THasSender extends true ? chrome.runtime.MessageSender : void) ) => (TResult | Promise); diff --git a/types/ext/cache-map.d.ts b/types/ext/cache-map.d.ts index 0af7109a..fcf969e8 100644 --- a/types/ext/cache-map.d.ts +++ b/types/ext/cache-map.d.ts @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -export type Node = { +export type Node = { key: K | null; value: V | null; previous: Node | null; diff --git a/types/ext/core.d.ts b/types/ext/core.d.ts index b83e6a74..d1a4ef8f 100644 --- a/types/ext/core.d.ts +++ b/types/ext/core.d.ts @@ -41,7 +41,7 @@ export type TokenString = string; export type TokenObject = Record; -export type DeferredPromiseDetails = { +export type DeferredPromiseDetails = { promise: Promise; resolve: (value: T) => void; reject: (reason?: RejectionReason) => void; diff --git a/types/ext/dictionary-data-util.d.ts b/types/ext/dictionary-data-util.d.ts index 75128368..b78e6439 100644 --- a/types/ext/dictionary-data-util.d.ts +++ b/types/ext/dictionary-data-util.d.ts @@ -45,7 +45,7 @@ export type KanjiFrequenciesMap2Data = { export type TermFrequenciesMap3 = Map; -export type DictionaryFrequency = { +export type DictionaryFrequency = { dictionary: string; frequencies: T[]; }; diff --git a/types/ext/dictionary-database.d.ts b/types/ext/dictionary-database.d.ts index 6569f76b..3202ef60 100644 --- a/types/ext/dictionary-database.d.ts +++ b/types/ext/dictionary-database.d.ts @@ -230,11 +230,11 @@ export type FindMultiBulkData = { indexIndex: number; }; -export type CreateQuery = (item: TItem) => (IDBValidKey | IDBKeyRange | null); +export type CreateQuery = (item: TItem) => (IDBValidKey | IDBKeyRange | null); -export type FindPredicate = (row: TRow, item: TItem) => boolean; +export type FindPredicate = (row: TRow, item: TItem) => boolean; -export type CreateResult = (row: TRow, data: FindMultiBulkData) => TResult; +export type CreateResult = (row: TRow, data: FindMultiBulkData) => TResult; export type DictionarySet = { has(value: string): boolean; diff --git a/types/ext/dictionary-worker.d.ts b/types/ext/dictionary-worker.d.ts index 17030691..ac076ab8 100644 --- a/types/ext/dictionary-worker.d.ts +++ b/types/ext/dictionary-worker.d.ts @@ -29,7 +29,7 @@ export type InvokeDetails = { formatResult: ((result: TResponseRaw) => TResponse) | null; }; -export type MessageCompleteData = { +export type MessageCompleteData = { action: 'complete'; params: MessageCompleteParams; }; @@ -44,7 +44,7 @@ export type MessageGetImageDetailsData = { params: MessageGetImageDetailsParams; }; -export type MessageCompleteParams = Core.Response; +export type MessageCompleteParams = Core.Response; export type MessageProgressParams = { args: unknown[]; @@ -56,7 +56,7 @@ export type MessageGetImageDetailsParams = { mediaType: string; }; -export type MessageData = MessageCompleteData | MessageProgressData | MessageGetImageDetailsData; +export type MessageData = MessageCompleteData | MessageProgressData | MessageGetImageDetailsData; export type MessageCompleteResultSerialized = { result: DictionaryImporter.Summary; diff --git a/types/ext/dom-data-binder.d.ts b/types/ext/dom-data-binder.d.ts index ca0036c3..5de941ff 100644 --- a/types/ext/dom-data-binder.d.ts +++ b/types/ext/dom-data-binder.d.ts @@ -17,28 +17,28 @@ import type * as TaskAccumulator from './task-accumulator'; -export type CreateElementMetadataCallback = (element: Element) => T | undefined; +export type CreateElementMetadataCallback = (element: Element) => T | undefined; -export type CompareElementMetadataCallback = (metadata1: T, metadata2: T) => boolean; +export type CompareElementMetadataCallback = (metadata1: T, metadata2: T) => boolean; -export type GetValuesCallback = (args: GetValuesDetails[]) => Promise; +export type GetValuesCallback = (args: GetValuesDetails[]) => Promise; -export type SetValuesCallback = (args: SetValuesDetails[]) => Promise; +export type SetValuesCallback = (args: SetValuesDetails[]) => Promise; -export type GetValuesDetails = { +export type GetValuesDetails = { element: Element; metadata: T; }; -export type SetValuesDetails = { +export type SetValuesDetails = { element: Element; metadata: T; value: ValueType; }; -export type OnErrorCallback = (error: Error, stale: boolean, element: Element, metadata: T) => void; +export type OnErrorCallback = (error: Error, stale: boolean, element: Element, metadata: T) => void; -export type ConstructorDetails = { +export type ConstructorDetails = { selector: string; createElementMetadata: CreateElementMetadataCallback; compareElementMetadata: CompareElementMetadataCallback; @@ -47,7 +47,7 @@ export type ConstructorDetails = { onError?: OnErrorCallback | null; }; -export type ElementObserver = { +export type ElementObserver = { element: Element; type: NormalizedElementType; value: unknown; @@ -70,17 +70,17 @@ export type AssignTaskValue = {value: ValueType}; export type ValueType = boolean | string | number | null; -export type UpdateTask = [ +export type UpdateTask = [ key: ElementObserver | null, task: TaskAccumulator.Task, ]; -export type AssignTask = [ +export type AssignTask = [ key: ElementObserver | null, task: TaskAccumulator.Task, ]; -export type ApplyTarget = [ +export type ApplyTarget = [ observer: ElementObserver, task: TaskAccumulator.Task | TaskAccumulator.Task | null, ]; diff --git a/types/ext/dynamic-property.d.ts b/types/ext/dynamic-property.d.ts index 5ec886d2..ba15257a 100644 --- a/types/ext/dynamic-property.d.ts +++ b/types/ext/dynamic-property.d.ts @@ -17,6 +17,6 @@ export type EventType = 'change'; -export type ChangeEventDetails = { +export type ChangeEventDetails = { value: T; }; diff --git a/types/ext/selector-observer.d.ts b/types/ext/selector-observer.d.ts index a84b2add..2b3e9381 100644 --- a/types/ext/selector-observer.d.ts +++ b/types/ext/selector-observer.d.ts @@ -15,15 +15,15 @@ * along with this program. If not, see . */ -export type OnAddedCallback = (element: Element) => T | undefined; +export type OnAddedCallback = (element: Element) => T | undefined; -export type OnRemovedCallback = (element: Element, data: T) => void; +export type OnRemovedCallback = (element: Element, data: T) => void; -export type OnChildrenUpdatedCallback = (element: Element, data: T) => void; +export type OnChildrenUpdatedCallback = (element: Element, data: T) => void; -export type IsStaleCallback = (element: Element, data: T) => boolean; +export type IsStaleCallback = (element: Element, data: T) => boolean; -export type ConstructorDetails = { +export type ConstructorDetails = { /** A string CSS selector used to find elements. */ selector: string; /** A string CSS selector used to filter elements, or `null` for no filtering. */ @@ -48,7 +48,7 @@ export type MutationRecordLike = { target: Node; }; -export type Observer = { +export type Observer = { element: Element; ancestors: Node[]; data: T; diff --git a/types/ext/task-accumulator.d.ts b/types/ext/task-accumulator.d.ts index f02d449b..e96ba6a6 100644 --- a/types/ext/task-accumulator.d.ts +++ b/types/ext/task-accumulator.d.ts @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -export type Task = { +export type Task = { data: V; stale: boolean; }; -- cgit v1.2.3