Skip to content

Commit 0bd771b

Browse files
authored
feat: correct some more types (#1859)
Internal only, just adding some more type defs.
1 parent 812c57d commit 0bd771b

8 files changed

Lines changed: 62 additions & 42 deletions

File tree

‎lib/chai.js‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ import {Assertion} from './chai/assertion.js';
1313
import * as should from './chai/interface/should.js';
1414
import {assert} from './chai/interface/assert.js';
1515

16+
/**
17+
* @typedef {object} ChaiExports
18+
* @property {typeof use} use The use function
19+
* @property {typeof AssertionError} AssertionError AssertionError constructor
20+
* @property {typeof util} util Utility functions
21+
* @property {typeof config} config Configuration object
22+
* @property {typeof expect} expect Expect interface
23+
* @property {typeof assert} assert Assert interface
24+
* @property {typeof Assertion} Assertion Assertion constructor
25+
* @property {typeof should.should} should Should interface
26+
* @property {typeof should.Should} Should Should interface
27+
*/
28+
29+
/**
30+
* @typedef {(chai: ChaiExports, utils: typeof util) => void} ChaiPlugin
31+
*/
32+
33+
/** @type {ChaiPlugin[]} */
1634
const used = [];
1735

1836
// Assertion Error
@@ -23,8 +41,8 @@ export {AssertionError};
2341
*
2442
* Provides a way to extend the internals of Chai.
2543
*
26-
* @param {Function} fn
27-
* @returns {this} for chaining
44+
* @param {ChaiPlugin} fn
45+
* @returns {ChaiExports} for chaining
2846
* @public
2947
*/
3048
export function use(fn) {

‎lib/chai/assertion.js‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ export class Assertion {
4444
* - `eql`: This flag contains the deepEqual function to be used by the assertion.
4545
*
4646
* @param {unknown} obj target of the assertion
47-
* @param {string} [msg] (optional) custom error message
48-
* @param {Function} [ssfi] (optional) starting point for removing stack frames
49-
* @param {boolean} [lockSsfi] (optional) whether or not the ssfi flag is locked
47+
* @param {string=} msg (optional) custom error message
48+
* @param {Function=} ssfi (optional) starting point for removing stack frames
49+
* @param {boolean=} lockSsfi (optional) whether or not the ssfi flag is locked
5050
*/
5151
constructor(obj, msg, ssfi, lockSsfi) {
5252
util.flag(this, 'ssfi', ssfi || Assertion);
@@ -92,49 +92,49 @@ export class Assertion {
9292

9393
/**
9494
* @param {string} name
95-
* @param {Function} fn
95+
* @param {(...args: never) => unknown} fn
9696
*/
9797
static addProperty(name, fn) {
9898
util.addProperty(this.prototype, name, fn);
9999
}
100100

101101
/**
102102
* @param {string} name
103-
* @param {Function} fn
103+
* @param {(...args: never) => unknown} fn
104104
*/
105105
static addMethod(name, fn) {
106106
util.addMethod(this.prototype, name, fn);
107107
}
108108

109109
/**
110110
* @param {string} name
111-
* @param {Function} fn
112-
* @param {Function} chainingBehavior
111+
* @param {(...args: never) => unknown} fn
112+
* @param {(...args: never) => unknown} chainingBehavior
113113
*/
114114
static addChainableMethod(name, fn, chainingBehavior) {
115115
util.addChainableMethod(this.prototype, name, fn, chainingBehavior);
116116
}
117117

118118
/**
119119
* @param {string} name
120-
* @param {Function} fn
120+
* @param {(...args: never) => unknown} fn
121121
*/
122122
static overwriteProperty(name, fn) {
123123
util.overwriteProperty(this.prototype, name, fn);
124124
}
125125

126126
/**
127127
* @param {string} name
128-
* @param {Function} fn
128+
* @param {(...args: never) => unknown} fn
129129
*/
130130
static overwriteMethod(name, fn) {
131131
util.overwriteMethod(this.prototype, name, fn);
132132
}
133133

134134
/**
135135
* @param {string} name
136-
* @param {Function} fn
137-
* @param {Function} chainingBehavior
136+
* @param {(...args: never) => unknown} fn
137+
* @param {(...args: never) => unknown} chainingBehavior
138138
*/
139139
static overwriteChainableMethod(name, fn, chainingBehavior) {
140140
util.overwriteChainableMethod(this.prototype, name, fn, chainingBehavior);
@@ -178,7 +178,6 @@ export class Assertion {
178178
throw new AssertionError(
179179
msg,
180180
assertionErrorObjectProperties,
181-
// @ts-expect-error Not sure what to do about these types yet
182181
config.includeStack ? this.assert : util.flag(this, 'ssfi')
183182
);
184183
}

‎lib/chai/interface/expect.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,23 @@ export {expect};
3333
* expect.fail(1, 2, undefined, ">");
3434
*
3535
* @name fail
36-
* @param {unknown} actual
37-
* @param {unknown} expected
38-
* @param {string} message
39-
* @param {string} operator
36+
* @param {unknown=} actual
37+
* @param {unknown=} expected
38+
* @param {string=} message
39+
* @param {string=} operator
40+
* @returns {never}
4041
* @namespace expect
4142
* @public
4243
*/
4344
expect.fail = function (actual, expected, message, operator) {
45+
let msg = message;
4446
if (arguments.length < 2) {
45-
message = actual;
47+
msg = /** @type {string | undefined} */ (actual);
4648
actual = undefined;
4749
}
4850

49-
message = message || 'expect.fail()';
5051
throw new AssertionError(
51-
message,
52+
msg || 'expect.fail()',
5253
{
5354
actual: actual,
5455
expected: expected,

‎lib/chai/utils/addChainableMethod.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ export function addChainableMethod(ctx, name, method, chainingBehavior) {
113113

114114
/**
115115
* @this {T}
116+
* @param {...unknown} args
116117
*/
117-
function chainableMethodWrapper() {
118+
function chainableMethodWrapper(...args) {
118119
// Setting the `ssfi` flag to `chainableMethodWrapper` causes this
119120
// function to be the starting point for removing implementation
120121
// frames from the stack trace of a failed assertion.
@@ -136,7 +137,7 @@ export function addChainableMethod(ctx, name, method, chainingBehavior) {
136137

137138
let result = /** @type {(...args: unknown[]) => unknown} */ (
138139
chainableBehavior.method
139-
).apply(this, arguments);
140+
).apply(this, args);
140141
if (result !== undefined) {
141142
return result;
142143
}

‎lib/chai/utils/flag.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @overload
3232
* @param {T} obj object constructed Assertion
3333
* @param {K} key
34-
* @param {K extends keyof FlagMap ? FlagMap[K] : unknown} value
34+
* @param {K extends keyof FlagMap ? (FlagMap[K] | undefined) : unknown} value
3535
* @returns {void}
3636
*/
3737

‎lib/chai/utils/getActual.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Returns the `actual` value for an Assertion.
1515
*
1616
* @param {Assertion} obj object (constructed Assertion)
17-
* @param {unknown[]} args chai.Assertion.prototype.assert arguments
17+
* @param {ArrayLike<unknown>} args chai.Assertion.prototype.assert arguments
1818
* @returns {unknown}
1919
* @namespace Utils
2020
* @name getActual

‎lib/chai/utils/getMessage.js‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {flag} from './flag.js';
88
import {getActual} from './getActual.js';
99
import {objDisplay} from './objDisplay.js';
1010

11+
/**
12+
* @typedef {import('../assertion.js').Assertion} Assertion
13+
*/
14+
1115
/**
1216
* ### .getMessage(object, message, negateMessage)
1317
*
@@ -20,8 +24,8 @@ import {objDisplay} from './objDisplay.js';
2024
* - `#{act}` actual value
2125
* - `#{exp}` expected value
2226
*
23-
* @param {object} obj object (constructed Assertion)
24-
* @param {IArguments} args chai.Assertion.prototype.assert arguments
27+
* @param {Assertion} obj object (constructed Assertion)
28+
* @param {ArrayLike<unknown>} args chai.Assertion.prototype.assert arguments
2529
* @returns {string}
2630
* @namespace Utils
2731
* @name getMessage
@@ -32,23 +36,20 @@ export function getMessage(obj, args) {
3236
let val = flag(obj, 'object');
3337
let expected = args[3];
3438
let actual = getActual(obj, args);
35-
let msg = negate ? args[2] : args[1];
39+
let rawMsg = negate ? args[2] : args[1];
3640
let flagMsg = flag(obj, 'message');
3741

38-
if (typeof msg === 'function') msg = msg();
39-
msg = msg || '';
40-
msg = /** @type {string} */ (msg).replace(
41-
/#\{(this|act|exp)\}/g,
42-
function (_, tag) {
43-
if (tag === 'this') {
44-
return objDisplay(val);
45-
}
46-
if (tag === 'act') {
47-
return objDisplay(actual);
48-
}
49-
return objDisplay(expected);
42+
if (typeof rawMsg === 'function') rawMsg = rawMsg();
43+
const template = /** @type {string} */ (rawMsg) || '';
44+
const msg = template.replace(/#\{(this|act|exp)\}/g, function (_, tag) {
45+
if (tag === 'this') {
46+
return objDisplay(val);
47+
}
48+
if (tag === 'act') {
49+
return objDisplay(actual);
5050
}
51-
);
51+
return objDisplay(expected);
52+
});
5253

5354
return flagMsg ? flagMsg + ': ' + msg : msg;
5455
}

‎lib/chai/utils/getOperator.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function isObjectType(obj) {
2222
* Returns the `operator` or `undefined` value for an Assertion.
2323
*
2424
* @param {object} obj object (constructed Assertion)
25-
* @param {unknown[]} args chai.Assertion.prototype.assert arguments
25+
* @param {ArrayLike<unknown>} args chai.Assertion.prototype.assert arguments
2626
* @returns {string | undefined}
2727
* @namespace Utils
2828
* @name getOperator

0 commit comments

Comments
 (0)