Skip to content

Commit 0bd6221

Browse files
committed
Improved solution to issue #179 (invalid options throwing errors in joinRegExp)
This slightly more robust solution ignores options that are not strings or regular expressions in the passed patterns array. The prior solution still accepted non-string, non-regexp options (e.g. [], {}, true, etc.). Credit goes to @soundslocke for the fix.
1 parent fdac9de commit 0bd6221

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/raven.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -644,14 +644,15 @@ function joinRegExp(patterns) {
644644
// Be mad.
645645
var sources = [], i = patterns.length;
646646
while (i--) {
647-
if (!isUndefined(patterns[i]) && patterns[i]) {
648-
sources[i] = isString(patterns[i]) ?
649-
// If it's a string, we need to escape it
650-
// Taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
651-
patterns[i].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1") :
652-
// If it's a regexp already, we want to extract the source
653-
patterns[i].source;
647+
if (isString(patterns[i])) {
648+
// If it's a string, we need to escape it
649+
// Taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
650+
sources.unshift(patterns[i].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"));
651+
} else if (!isUndefined(patterns[i]) && patterns[i] && patterns[i].source) {
652+
// If it's a regexp already, we want to extract the source
653+
sources.unshift(patterns[i].source);
654654
}
655+
// Intentionally skip other cases
655656
}
656657
return new RegExp(sources.join('|'), 'i');
657658
}

test/raven.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,12 @@ describe('globals', function() {
879879
'a', 'b', null, undefined
880880
]).source, 'a|b');
881881
});
882+
883+
it('should skip entries that are not strings or regular expressions in the passed array of patterns', function() {
884+
assert.equal(joinRegExp([
885+
'a', 'b', null, 'a.b', undefined, true, /d/, 123, {}, /[0-9]/, []
886+
]).source, 'a|b|a\\.b|d|[0-9]');
887+
});
882888
});
883889
});
884890

0 commit comments

Comments
 (0)