Skip to content

Commit 40f0329

Browse files
committed
Refactor some more code to use JSDoc
1 parent 4676814 commit 40f0329

File tree

5 files changed

+46
-39
lines changed

5 files changed

+46
-39
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@ node_modules/
55
*.log
66
yarn.lock
77
!/index.d.ts
8-
!/lib/callable-instance.d.ts

lib/callable-instance.d.ts

-7
This file was deleted.

lib/callable-instance.js

+35-26
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1-
/**
2-
* @param {string} property
3-
*/
4-
export function CallableInstance(property) {
5-
/** @type {Function} */
6-
const self = this
7-
const constr = self.constructor
8-
// Prototypes do exist.
9-
// type-coverage:ignore-next-line
10-
const proto = /** @type {Record<string, Function>} */ (constr.prototype)
11-
const func = proto[property]
12-
const apply = function () {
13-
return func.apply(apply, arguments)
14-
}
1+
export const CallableInstance =
2+
/**
3+
* @type {new <Parameters extends Array<unknown>, Result>(property: string | symbol) => (...parameters: Parameters) => Result}
4+
*/
5+
(
6+
/** @type {unknown} */
7+
(
8+
/**
9+
* @this {Function}
10+
* @param {string | symbol} property
11+
* @returns {(...parameters: Array<unknown>) => unknown}
12+
*/
13+
function (property) {
14+
const self = this
15+
const constr = self.constructor
16+
const proto = /** @type {Record<string | symbol, Function>} */ (
17+
// Prototypes do exist.
18+
// type-coverage:ignore-next-line
19+
constr.prototype
20+
)
21+
const func = proto[property]
22+
/** @type {(...parameters: Array<unknown>) => unknown} */
23+
const apply = function () {
24+
return func.apply(apply, arguments)
25+
}
1526

16-
Object.setPrototypeOf(apply, proto)
27+
Object.setPrototypeOf(apply, proto)
1728

18-
const names = Object.getOwnPropertyNames(func)
29+
const names = Object.getOwnPropertyNames(func)
1930

20-
for (const p of names) {
21-
const descriptor = Object.getOwnPropertyDescriptor(func, p)
22-
if (descriptor) Object.defineProperty(apply, p, descriptor)
23-
}
31+
for (const p of names) {
32+
const descriptor = Object.getOwnPropertyDescriptor(func, p)
33+
if (descriptor) Object.defineProperty(apply, p, descriptor)
34+
}
2435

25-
return apply
26-
}
27-
28-
// Prototypes do exist.
29-
// type-coverage:ignore-next-line
30-
CallableInstance.prototype = Object.create(Function.prototype)
36+
return apply
37+
}
38+
)
39+
)

script/fix-types.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@ try {
1313
const result = file
1414
.replace(/declare const Processor_base: [^\n]+/, function () {
1515
console.log('Fixed `CallableInstance` import')
16-
return "declare const CallableInstance: import('./callable-instance.js').ICallableInstance"
16+
return "import {CallableInstance} from './callable-instance.js'"
1717
})
1818
.replace(/extends Processor_base/, function () {
1919
console.log('Fixed `CallableInstance` use')
2020
return 'extends CallableInstance<[], Processor<ParseTree, HeadTree, TailTree, CompileTree, CompileResult>>'
2121
})
2222
.replace(
23-
/\.\.\.parameters: Parameters_1 \| \[boolean] \| undefined/,
24-
function () {
23+
/\.\.\.parameters: (Parameters_\d) \| \[boolean] \| undefined/,
24+
/**
25+
*
26+
* @param {string} $0
27+
* @param {string} $1
28+
* @returns {string}
29+
*/
30+
function ($0, $1) {
2531
console.log(
2632
'Fixed `use` overload with plugin, and *non-optional* parameters'
2733
)
28-
return '...parameters: Parameters_1 | [boolean]'
34+
return '...parameters: ' + $1 + ' | [boolean]'
2935
}
3036
)
3137

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"target": "es2020"
1212
},
1313
"exclude": ["coverage/", "node_modules/"],
14-
"include": ["**/*.js", "lib/callable-instance.d.ts", "index.d.ts"]
14+
"include": ["**/*.js", "index.d.ts"]
1515
}

0 commit comments

Comments
 (0)