Skip to content

Commit f262408

Browse files
committed
[Fix] throw an error on non-functions as early as possible
1 parent 1a7778f commit f262408

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var bind = require('function-bind');
44
var GetIntrinsic = require('get-intrinsic');
55
var setFunctionLength = require('set-function-length');
66

7+
var $TypeError = GetIntrinsic('%TypeError%');
78
var $apply = GetIntrinsic('%Function.prototype.apply%');
89
var $call = GetIntrinsic('%Function.prototype.call%');
910
var $reflectApply = GetIntrinsic('%Reflect.apply%', true) || bind.call($call, $apply);
@@ -21,6 +22,9 @@ if ($defineProperty) {
2122
}
2223

2324
module.exports = function callBind(originalFunction) {
25+
if (typeof originalFunction !== 'function') {
26+
throw new $TypeError('a function is required');
27+
}
2428
var func = $reflectApply(bind, $call, arguments);
2529
return setFunctionLength(
2630
func,

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"lint": "eslint --ext=.js,.mjs .",
1616
"postlint": "evalmd README.md",
1717
"pretest": "npm run lint",
18-
"tests-only": "nyc tape 'test/*'",
18+
"tests-only": "nyc tape 'test/**/*.js'",
1919
"test": "npm run tests-only",
2020
"posttest": "aud --production",
2121
"version": "auto-changelog && git add CHANGELOG.md",
@@ -53,13 +53,16 @@
5353
"@ljharb/eslint-config": "^21.1.0",
5454
"aud": "^2.0.3",
5555
"auto-changelog": "^2.4.0",
56+
"es-value-fixtures": "^1.4.2",
5657
"eslint": "=8.8.0",
5758
"evalmd": "^0.0.19",
59+
"for-each": "^0.3.3",
5860
"gopd": "^1.0.1",
5961
"has-strict-mode": "^1.0.1",
6062
"in-publish": "^2.0.1",
6163
"npmignore": "^0.3.0",
6264
"nyc": "^10.3.2",
65+
"object-inspect": "^1.13.1",
6366
"safe-publish-latest": "^2.0.0",
6467
"tape": "^5.7.1"
6568
},

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ var callBind = require('../');
44
var bind = require('function-bind');
55
var gOPD = require('gopd');
66
var hasStrictMode = require('has-strict-mode')();
7+
var forEach = require('for-each');
8+
var inspect = require('object-inspect');
9+
var v = require('es-value-fixtures');
710

811
var test = require('tape');
912

@@ -18,6 +21,14 @@ var functionsHaveConfigurableLengths = !!(
1821
);
1922

2023
test('callBind', function (t) {
24+
forEach(v.nonFunctions, function (nonFunction) {
25+
t['throws'](
26+
function () { callBind(nonFunction); },
27+
TypeError,
28+
inspect(nonFunction) + ' is not a function'
29+
);
30+
});
31+
2132
var sentinel = { sentinel: true };
2233
var func = function (a, b) {
2334
// eslint-disable-next-line no-invalid-this

0 commit comments

Comments
 (0)