Skip to content

More typing #1679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions lib/chai/assertion.js
Original file line number Diff line number Diff line change
@@ -5,9 +5,13 @@
* MIT Licensed
*/

import { config } from "./config.js";
import { AssertionError } from "assertion-error";
import * as util from "./utils/index.js";
import {config} from './config.js';
import {AssertionError} from 'assertion-error';
import * as util from './utils/index.js';

export class Assertion {
/** @type {{}} */
__flags = {}

/**
* Creates object for chaining.
@@ -39,51 +43,49 @@ import * as util from "./utils/index.js";
*
* - `eql`: This flag contains the deepEqual function to be used by the assertion.
*
* @param {unknown} ?obj target of the assertion
* @param {string} ?msg (optional) custom error message
* @param {Function} ?ssfi (optional) starting point for removing stack frames
* @param {boolean} ?lockSsfi (optional) whether or not the ssfi flag is locked
* @returns {unknown}
* @param {unknown} obj target of the assertion
* @param {string} [msg] (optional) custom error message
* @param {Function} [ssfi] (optional) starting point for removing stack frames
* @param {boolean} [lockSsfi] (optional) whether or not the ssfi flag is locked
*/
export class Assertion {
constructor(obj, msg, ssfi, lockSsfi) {
util.flag(this, "ssfi", ssfi || Assertion);
util.flag(this, "lockSsfi", lockSsfi);
util.flag(this, "object", obj);
util.flag(this, "message", msg);
util.flag(this, "eql", config.deepEqual || util.eql);
util.flag(this, 'ssfi', ssfi || Assertion);
util.flag(this, 'lockSsfi', lockSsfi);
util.flag(this, 'object', obj);
util.flag(this, 'message', msg);
util.flag(this, 'eql', config.deepEqual || util.eql);

return util.proxify(this);
}

/** @returns {boolean} */
static get includeStack() {
console.warn(
"Assertion.includeStack is deprecated, use chai.config.includeStack instead.",
'Assertion.includeStack is deprecated, use chai.config.includeStack instead.'
);
return config.includeStack;
}

/** @param {boolean} value */
static set includeStack(value) {
console.warn(
"Assertion.includeStack is deprecated, use chai.config.includeStack instead.",
'Assertion.includeStack is deprecated, use chai.config.includeStack instead.'
);
config.includeStack = value;
}

/** @returns {boolean} */
static get showDiff() {
console.warn(
"Assertion.showDiff is deprecated, use chai.config.showDiff instead.",
'Assertion.showDiff is deprecated, use chai.config.showDiff instead.'
);
return config.showDiff;
}

/** @param {boolean} value */
static set showDiff(value) {
console.warn(
"Assertion.showDiff is deprecated, use chai.config.showDiff instead.",
'Assertion.showDiff is deprecated, use chai.config.showDiff instead.'
);
config.showDiff = value;
}
@@ -150,6 +152,7 @@ export class Assertion {
* @param {unknown} expected value (remember to check for negation)
* @param {unknown} _actual (optional) will default to `this.obj`
* @param {boolean} showDiff (optional) when set to `true`, assert will display a diff in addition to the message if expression fails
* @returns {void}
*/
assert(_expr, msg, _negateMsg, expected, _actual, showDiff) {
var ok = util.test(this, arguments);
@@ -160,10 +163,11 @@ export class Assertion {
if (!ok) {
msg = util.getMessage(this, arguments);
var actual = util.getActual(this, arguments);
/** @type {Record<PropertyKey, unknown>} */
var assertionErrorObjectProperties = {
actual: actual,
expected: expected,
showDiff: showDiff,
showDiff: showDiff
};

var operator = util.getOperator(this, arguments);
@@ -174,7 +178,8 @@ export class Assertion {
throw new AssertionError(
msg,
assertionErrorObjectProperties,
config.includeStack ? this.assert : util.flag(this, "ssfi"),
// @ts-expect-error Not sure what to do about these types yet
config.includeStack ? this.assert : util.flag(this, 'ssfi')
);
}
}
@@ -185,7 +190,7 @@ export class Assertion {
* @returns {unknown}
*/
get _obj() {
return util.flag(this, "object");
return util.flag(this, 'object');
}

/**
@@ -194,6 +199,6 @@ export class Assertion {
* @param {unknown} val
*/
set _obj(val) {
util.flag(this, "object", val);
util.flag(this, 'object', val);
}
}
5 changes: 3 additions & 2 deletions lib/chai/utils/flag.js
Original file line number Diff line number Diff line change
@@ -15,9 +15,10 @@
* utils.flag(this, 'foo', 'bar'); // setter
* utils.flag(this, 'foo'); // getter, returns `bar`
*
* @param {object} obj object constructed Assertion
* @template {{__flags?: {[key: PropertyKey]: unknown}}} T
* @param {T} obj object constructed Assertion
* @param {string} key
* @param {unknown} value (optional)
* @param {unknown} [value]
* @namespace Utils
* @name flag
* @returns {unknown | undefined}
16 changes: 8 additions & 8 deletions lib/chai/utils/getMessage.js
Original file line number Diff line number Diff line change
@@ -21,19 +21,19 @@ import {objDisplay} from './objDisplay.js';
* - `#{exp}` expected value
*
* @param {object} obj object (constructed Assertion)
* @param {unknown} args chai.Assertion.prototype.assert arguments
* @returns {unknown}
* @param {IArguments} args chai.Assertion.prototype.assert arguments
* @returns {string}
* @namespace Utils
* @name getMessage
* @public
*/
export function getMessage(obj, args) {
let negate = flag(obj, 'negate'),
val = flag(obj, 'object'),
expected = args[3],
actual = getActual(obj, args),
msg = negate ? args[2] : args[1],
flagMsg = flag(obj, 'message');
let negate = flag(obj, 'negate');
let val = flag(obj, 'object');
let expected = args[3];
let actual = getActual(obj, args);
let msg = negate ? args[2] : args[1];
let flagMsg = flag(obj, 'message');

if (typeof msg === 'function') msg = msg();
msg = msg || '';
9 changes: 5 additions & 4 deletions lib/chai/utils/proxify.js
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import {isProxyEnabled} from './isProxyEnabled.js';
* MIT Licensed
*/

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

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