Skip to content

Commit c7962c9

Browse files
committed
Throw an error if an invalid test is passed in
1 parent 9ec5500 commit c7962c9

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ function headingRange(node, options, callback) {
2222
}
2323

2424
/* Regex */
25-
if ('exec' in test) {
25+
if (test && 'exec' in test) {
2626
test = wrapExpression(test);
2727
}
2828

29+
if (typeof test !== 'function') {
30+
throw new Error(
31+
'Expected `string`, `regexp`, or `function` for `test`, ' +
32+
'not `' + test + '`'
33+
);
34+
}
35+
2936
search(node, test, ignoreFinalDefinitions, callback);
3037
}
3138

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
"xo": {
4545
"space": true,
4646
"esnext": false,
47+
"rules": {
48+
"unicorn/prefer-type-error": "off"
49+
},
4750
"ignore": [
4851
"mdast-util-heading-range.js"
4952
]

test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,26 @@ var remark = require('remark');
55
var heading = require('./');
66

77
test('mdast-util-heading-range()', function (t) {
8-
t.plan(55);
8+
t.plan(57);
99

1010
t.equal(typeof heading, 'function', 'should be a function');
1111

12+
t.throws(
13+
function () {
14+
heading({type: 'root', chilren: []}, null, function () {});
15+
},
16+
/^Error: Expected `string`, `regexp`, or `function` for `test`, not `null`$/,
17+
'should throw when `null` is passed in'
18+
);
19+
20+
t.throws(
21+
function () {
22+
heading({type: 'root', chilren: []}, undefined, function () {});
23+
},
24+
/^Error: Expected `string`, `regexp`, or `function` for `test`, not `undefined`$/,
25+
'should throw when `undefined` is passed in'
26+
);
27+
1228
t.equal(
1329
process(t, [
1430
'# Fo',

0 commit comments

Comments
 (0)