From 1a0f8872711f64b8353c30ebcfdf0ceeab404bab Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Wed, 27 Sep 2023 10:24:39 +0100 Subject: [PATCH] make --- chai.js | 109 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 41 deletions(-) diff --git a/chai.js b/chai.js index 85b0a41..38efb8f 100644 --- a/chai.js +++ b/chai.js @@ -9251,6 +9251,7 @@ AssertionError.prototype.toJSON = function (stack) { * MIT Licensed */ +var getFunctionName = require('get-func-name'); /** * ### .checkError * @@ -9330,34 +9331,6 @@ function compatibleMessage(thrown, errMatcher) { return false; } -/** - * ### .getFunctionName(constructorFn) - * - * Returns the name of a function. - * This also includes a polyfill function if `constructorFn.name` is not defined. - * - * @name getFunctionName - * @param {Function} constructorFn - * @namespace Utils - * @api private - */ - -var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\(\/]+)/; -function getFunctionName(constructorFn) { - var name = ''; - if (typeof constructorFn.name === 'undefined') { - // Here we run a polyfill if constructorFn.name is not defined - var match = String(constructorFn).match(functionNameMatch); - if (match) { - name = match[1]; - } - } else { - name = constructorFn.name; - } - - return name; -} - /** * ### .getConstructorName(errorLike) * @@ -9377,8 +9350,11 @@ function getConstructorName(errorLike) { // If `err` is not an instance of Error it is an error constructor itself or another function. // If we've got a common function we get its name, otherwise we may need to create a new instance // of the error just in case it's a poorly-constructed error. Please see chaijs/chai/issues/45 to know more. - constructorName = getFunctionName(errorLike).trim() || - getFunctionName(new errorLike()); // eslint-disable-line new-cap + constructorName = getFunctionName(errorLike); + if (constructorName === '') { + var newConstructorName = getFunctionName(new errorLike()); // eslint-disable-line new-cap + constructorName = newConstructorName || constructorName; + } } return constructorName; @@ -9416,7 +9392,7 @@ module.exports = { getConstructorName: getConstructorName, }; -},{}],35:[function(require,module,exports){ +},{"get-func-name":36}],35:[function(require,module,exports){ 'use strict'; /* globals Symbol: false, Uint8Array: false, WeakMap: false */ /*! @@ -9811,8 +9787,15 @@ function getEnumerableKeys(target) { return keys; } -function getNonEnumerableSymbols(target) { - var keys = Object.getOwnPropertySymbols(target); +function getEnumerableSymbols(target) { + var keys = []; + var allKeys = Object.getOwnPropertySymbols(target); + for (var i = 0; i < allKeys.length; i += 1) { + var key = allKeys[i]; + if (Object.getOwnPropertyDescriptor(target, key).enumerable) { + keys.push(key); + } + } return keys; } @@ -9851,8 +9834,8 @@ function keysEqual(leftHandOperand, rightHandOperand, keys, options) { function objectEqual(leftHandOperand, rightHandOperand, options) { var leftHandKeys = getEnumerableKeys(leftHandOperand); var rightHandKeys = getEnumerableKeys(rightHandOperand); - var leftHandSymbols = getNonEnumerableSymbols(leftHandOperand); - var rightHandSymbols = getNonEnumerableSymbols(rightHandOperand); + var leftHandSymbols = getEnumerableSymbols(leftHandOperand); + var rightHandSymbols = getEnumerableSymbols(rightHandOperand); leftHandKeys = leftHandKeys.concat(leftHandSymbols); rightHandKeys = rightHandKeys.concat(rightHandSymbols); @@ -9928,6 +9911,7 @@ function mapSymbols(arr) { var toString = Function.prototype.toString; var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\s\(\/]+)/; +var maxFunctionSourceLength = 512; function getFuncName(aFunc) { if (typeof aFunc !== 'function') { return null; @@ -9935,8 +9919,15 @@ function getFuncName(aFunc) { var name = ''; if (typeof Function.prototype.name === 'undefined' && typeof aFunc.name === 'undefined') { + // eslint-disable-next-line prefer-reflect + var functionSource = toString.call(aFunc); + // To avoid unconstrained resource consumption due to pathalogically large function names, + // we limit the available return value to be less than 512 characters. + if (functionSource.indexOf('(') > maxFunctionSourceLength) { + return name; + } // Here we run a polyfill if Function does not support the `name` property and if aFunc.name is not defined - var match = toString.call(aFunc).match(functionNameMatch); + var match = functionSource.match(functionNameMatch); if (match) { name = match[1]; } @@ -10332,9 +10323,15 @@ module.exports = getFuncName; } function inspectDate(dateObject, options) { - // If we need to - truncate the time portion, but never the date - var split = dateObject.toJSON().split('T'); - var date = split[0]; + var stringRepresentation = dateObject.toJSON(); + + if (stringRepresentation === null) { + return 'Invalid Date'; + } + + var split = stringRepresentation.split('T'); + var date = split[0]; // If we need to - truncate the time portion, but never the date + return options.stylize("".concat(date, "T").concat(truncate(split[1], options.truncate - date.length - 1)), 'date'); } @@ -10631,7 +10628,32 @@ module.exports = getFuncName; nodeInspect = false; } - var constructorMap = new WeakMap(); + function FakeMap() { + // eslint-disable-next-line prefer-template + this.key = 'chai/loupe__' + Math.random() + Date.now(); + } + + FakeMap.prototype = { + // eslint-disable-next-line object-shorthand + get: function get(key) { + return key[this.key]; + }, + // eslint-disable-next-line object-shorthand + has: function has(key) { + return this.key in key; + }, + // eslint-disable-next-line object-shorthand + set: function set(key, value) { + if (Object.isExtensible(key)) { + Object.defineProperty(key, this.key, { + // eslint-disable-next-line object-shorthand + value: value, + configurable: true + }); + } + } + }; + var constructorMap = new (typeof WeakMap === 'function' ? WeakMap : FakeMap)(); var stringTagMap = {}; var baseTypesMap = { undefined: function undefined$1(value, options) { @@ -10765,6 +10787,11 @@ module.exports = getFuncName; } // If it is an object with an anonymous prototype, display it as an object. + return inspectObject(value, options); + } // last chance to check if it's an object + + + if (value === Object(value)) { return inspectObject(value, options); } // We have run out of options! Just stringify the value @@ -10776,7 +10803,7 @@ module.exports = getFuncName; return false; } - constructorMap.add(constructor, inspector); + constructorMap.set(constructor, inspector); return true; } function registerStringTag(stringTag, inspector) {