Skip to content

Commit ce4c403

Browse files
authored
More typing (#1679)
* convert Assertion function to a class * fix more types * format
1 parent 97b218e commit ce4c403

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

lib/chai/assertion.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
* MIT Licensed
66
*/
77

8-
import { config } from "./config.js";
9-
import { AssertionError } from "assertion-error";
10-
import * as util from "./utils/index.js";
8+
import {config} from './config.js';
9+
import {AssertionError} from 'assertion-error';
10+
import * as util from './utils/index.js';
11+
12+
export class Assertion {
13+
/** @type {{}} */
14+
__flags = {}
1115

1216
/**
1317
* Creates object for chaining.
@@ -39,51 +43,49 @@ import * as util from "./utils/index.js";
3943
*
4044
* - `eql`: This flag contains the deepEqual function to be used by the assertion.
4145
*
42-
* @param {unknown} ?obj target of the assertion
43-
* @param {string} ?msg (optional) custom error message
44-
* @param {Function} ?ssfi (optional) starting point for removing stack frames
45-
* @param {boolean} ?lockSsfi (optional) whether or not the ssfi flag is locked
46-
* @returns {unknown}
46+
* @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
4750
*/
48-
export class Assertion {
4951
constructor(obj, msg, ssfi, lockSsfi) {
50-
util.flag(this, "ssfi", ssfi || Assertion);
51-
util.flag(this, "lockSsfi", lockSsfi);
52-
util.flag(this, "object", obj);
53-
util.flag(this, "message", msg);
54-
util.flag(this, "eql", config.deepEqual || util.eql);
52+
util.flag(this, 'ssfi', ssfi || Assertion);
53+
util.flag(this, 'lockSsfi', lockSsfi);
54+
util.flag(this, 'object', obj);
55+
util.flag(this, 'message', msg);
56+
util.flag(this, 'eql', config.deepEqual || util.eql);
5557

5658
return util.proxify(this);
5759
}
5860

5961
/** @returns {boolean} */
6062
static get includeStack() {
6163
console.warn(
62-
"Assertion.includeStack is deprecated, use chai.config.includeStack instead.",
64+
'Assertion.includeStack is deprecated, use chai.config.includeStack instead.'
6365
);
6466
return config.includeStack;
6567
}
6668

6769
/** @param {boolean} value */
6870
static set includeStack(value) {
6971
console.warn(
70-
"Assertion.includeStack is deprecated, use chai.config.includeStack instead.",
72+
'Assertion.includeStack is deprecated, use chai.config.includeStack instead.'
7173
);
7274
config.includeStack = value;
7375
}
7476

7577
/** @returns {boolean} */
7678
static get showDiff() {
7779
console.warn(
78-
"Assertion.showDiff is deprecated, use chai.config.showDiff instead.",
80+
'Assertion.showDiff is deprecated, use chai.config.showDiff instead.'
7981
);
8082
return config.showDiff;
8183
}
8284

8385
/** @param {boolean} value */
8486
static set showDiff(value) {
8587
console.warn(
86-
"Assertion.showDiff is deprecated, use chai.config.showDiff instead.",
88+
'Assertion.showDiff is deprecated, use chai.config.showDiff instead.'
8789
);
8890
config.showDiff = value;
8991
}
@@ -150,6 +152,7 @@ export class Assertion {
150152
* @param {unknown} expected value (remember to check for negation)
151153
* @param {unknown} _actual (optional) will default to `this.obj`
152154
* @param {boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails
155+
* @returns {void}
153156
*/
154157
assert(_expr, msg, _negateMsg, expected, _actual, showDiff) {
155158
var ok = util.test(this, arguments);
@@ -160,10 +163,11 @@ export class Assertion {
160163
if (!ok) {
161164
msg = util.getMessage(this, arguments);
162165
var actual = util.getActual(this, arguments);
166+
/** @type {Record<PropertyKey, unknown>} */
163167
var assertionErrorObjectProperties = {
164168
actual: actual,
165169
expected: expected,
166-
showDiff: showDiff,
170+
showDiff: showDiff
167171
};
168172

169173
var operator = util.getOperator(this, arguments);
@@ -174,7 +178,8 @@ export class Assertion {
174178
throw new AssertionError(
175179
msg,
176180
assertionErrorObjectProperties,
177-
config.includeStack ? this.assert : util.flag(this, "ssfi"),
181+
// @ts-expect-error Not sure what to do about these types yet
182+
config.includeStack ? this.assert : util.flag(this, 'ssfi')
178183
);
179184
}
180185
}
@@ -185,7 +190,7 @@ export class Assertion {
185190
* @returns {unknown}
186191
*/
187192
get _obj() {
188-
return util.flag(this, "object");
193+
return util.flag(this, 'object');
189194
}
190195

191196
/**
@@ -194,6 +199,6 @@ export class Assertion {
194199
* @param {unknown} val
195200
*/
196201
set _obj(val) {
197-
util.flag(this, "object", val);
202+
util.flag(this, 'object', val);
198203
}
199204
}

lib/chai/utils/flag.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
* utils.flag(this, 'foo', 'bar'); // setter
1616
* utils.flag(this, 'foo'); // getter, returns `bar`
1717
*
18-
* @param {object} obj object constructed Assertion
18+
* @template {{__flags?: {[key: PropertyKey]: unknown}}} T
19+
* @param {T} obj object constructed Assertion
1920
* @param {string} key
20-
* @param {unknown} value (optional)
21+
* @param {unknown} [value]
2122
* @namespace Utils
2223
* @name flag
2324
* @returns {unknown | undefined}

lib/chai/utils/getMessage.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ import {objDisplay} from './objDisplay.js';
2121
* - `#{exp}` expected value
2222
*
2323
* @param {object} obj object (constructed Assertion)
24-
* @param {unknown} args chai.Assertion.prototype.assert arguments
25-
* @returns {unknown}
24+
* @param {IArguments} args chai.Assertion.prototype.assert arguments
25+
* @returns {string}
2626
* @namespace Utils
2727
* @name getMessage
2828
* @public
2929
*/
3030
export function getMessage(obj, args) {
31-
let negate = flag(obj, 'negate'),
32-
val = flag(obj, 'object'),
33-
expected = args[3],
34-
actual = getActual(obj, args),
35-
msg = negate ? args[2] : args[1],
36-
flagMsg = flag(obj, 'message');
31+
let negate = flag(obj, 'negate');
32+
let val = flag(obj, 'object');
33+
let expected = args[3];
34+
let actual = getActual(obj, args);
35+
let msg = negate ? args[2] : args[1];
36+
let flagMsg = flag(obj, 'message');
3737

3838
if (typeof msg === 'function') msg = msg();
3939
msg = msg || '';

lib/chai/utils/proxify.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {isProxyEnabled} from './isProxyEnabled.js';
99
* MIT Licensed
1010
*/
1111

12+
/** @type {PropertyKey[]} */
1213
const builtins = ['__flags', '__methods', '_obj', 'assert'];
1314

1415
/**
@@ -24,11 +25,11 @@ const builtins = ['__flags', '__methods', '_obj', 'assert'];
2425
* If proxies are unsupported or disabled via the user's Chai config, then
2526
* return object without modification.
2627
*
27-
* @param {object} obj
28-
* @param {string} nonChainableMethodName
29-
* @returns {unknown}
3028
* @namespace Utils
31-
* @name proxify
29+
* @template {object} T
30+
* @param {T} obj
31+
* @param {string} [nonChainableMethodName]
32+
* @returns {T}
3233
*/
3334
export function proxify(obj, nonChainableMethodName) {
3435
if (!isProxyEnabled()) return obj;

0 commit comments

Comments
 (0)