Skip to content

Commit

Permalink
Refactor some more code to use JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 14, 2023
1 parent 4676814 commit 40f0329
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ node_modules/
*.log
yarn.lock
!/index.d.ts
!/lib/callable-instance.d.ts
7 changes: 0 additions & 7 deletions lib/callable-instance.d.ts

This file was deleted.

61 changes: 35 additions & 26 deletions lib/callable-instance.js
Original file line number Diff line number Diff line change
@@ -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<string, Function>} */ (constr.prototype)
const func = proto[property]
const apply = function () {
return func.apply(apply, arguments)
}
export const CallableInstance =
/**
* @type {new <Parameters extends Array<unknown>, Result>(property: string | symbol) => (...parameters: Parameters) => Result}
*/
(
/** @type {unknown} */
(
/**
* @this {Function}
* @param {string | symbol} property
* @returns {(...parameters: Array<unknown>) => unknown}
*/
function (property) {
const self = this
const constr = self.constructor
const proto = /** @type {Record<string | symbol, Function>} */ (
// Prototypes do exist.
// type-coverage:ignore-next-line
constr.prototype
)
const func = proto[property]
/** @type {(...parameters: Array<unknown>) => 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
}
)
)
14 changes: 10 additions & 4 deletions script/fix-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>>'
})
.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]'
}
)

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

0 comments on commit 40f0329

Please sign in to comment.