From 40f03295dc535624f91a7f086489942aa42e6340 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 14 Aug 2023 13:32:05 +0200 Subject: [PATCH] Refactor some more code to use JSDoc --- .gitignore | 1 - lib/callable-instance.d.ts | 7 ----- lib/callable-instance.js | 61 ++++++++++++++++++++++---------------- script/fix-types.js | 14 ++++++--- tsconfig.json | 2 +- 5 files changed, 46 insertions(+), 39 deletions(-) delete mode 100644 lib/callable-instance.d.ts diff --git a/.gitignore b/.gitignore index 66103597..fcb26070 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,3 @@ node_modules/ *.log yarn.lock !/index.d.ts -!/lib/callable-instance.d.ts diff --git a/lib/callable-instance.d.ts b/lib/callable-instance.d.ts deleted file mode 100644 index 0fbc88f7..00000000 --- a/lib/callable-instance.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -type Func = (...argv: Args) => Return - -export type ICallableInstance = new ( - property: string | symbol -) => Func - -export const CallableInstance: ICallableInstance diff --git a/lib/callable-instance.js b/lib/callable-instance.js index e42eb011..17c693de 100644 --- a/lib/callable-instance.js +++ b/lib/callable-instance.js @@ -1,30 +1,39 @@ -/** - * @param {string} property - */ -export function CallableInstance(property) { - /** @type {Function} */ - const self = this - const constr = self.constructor - // Prototypes do exist. - // type-coverage:ignore-next-line - const proto = /** @type {Record} */ (constr.prototype) - const func = proto[property] - const apply = function () { - return func.apply(apply, arguments) - } +export const CallableInstance = + /** + * @type {new , Result>(property: string | symbol) => (...parameters: Parameters) => Result} + */ + ( + /** @type {unknown} */ + ( + /** + * @this {Function} + * @param {string | symbol} property + * @returns {(...parameters: Array) => unknown} + */ + function (property) { + const self = this + const constr = self.constructor + const proto = /** @type {Record} */ ( + // Prototypes do exist. + // type-coverage:ignore-next-line + constr.prototype + ) + const func = proto[property] + /** @type {(...parameters: Array) => unknown} */ + const apply = function () { + return func.apply(apply, arguments) + } - Object.setPrototypeOf(apply, proto) + Object.setPrototypeOf(apply, proto) - const names = Object.getOwnPropertyNames(func) + const names = Object.getOwnPropertyNames(func) - for (const p of names) { - const descriptor = Object.getOwnPropertyDescriptor(func, p) - if (descriptor) Object.defineProperty(apply, p, descriptor) - } + for (const p of names) { + const descriptor = Object.getOwnPropertyDescriptor(func, p) + if (descriptor) Object.defineProperty(apply, p, descriptor) + } - return apply -} - -// Prototypes do exist. -// type-coverage:ignore-next-line -CallableInstance.prototype = Object.create(Function.prototype) + return apply + } + ) + ) diff --git a/script/fix-types.js b/script/fix-types.js index a17b1fcf..7495341f 100644 --- a/script/fix-types.js +++ b/script/fix-types.js @@ -13,19 +13,25 @@ try { const result = file .replace(/declare const Processor_base: [^\n]+/, function () { console.log('Fixed `CallableInstance` import') - return "declare const CallableInstance: import('./callable-instance.js').ICallableInstance" + return "import {CallableInstance} from './callable-instance.js'" }) .replace(/extends Processor_base/, function () { console.log('Fixed `CallableInstance` use') return 'extends CallableInstance<[], Processor>' }) .replace( - /\.\.\.parameters: Parameters_1 \| \[boolean] \| undefined/, - function () { + /\.\.\.parameters: (Parameters_\d) \| \[boolean] \| undefined/, + /** + * + * @param {string} $0 + * @param {string} $1 + * @returns {string} + */ + function ($0, $1) { console.log( 'Fixed `use` overload with plugin, and *non-optional* parameters' ) - return '...parameters: Parameters_1 | [boolean]' + return '...parameters: ' + $1 + ' | [boolean]' } ) diff --git a/tsconfig.json b/tsconfig.json index dd290ca4..ad1496e9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,5 +11,5 @@ "target": "es2020" }, "exclude": ["coverage/", "node_modules/"], - "include": ["**/*.js", "lib/callable-instance.d.ts", "index.d.ts"] + "include": ["**/*.js", "index.d.ts"] }