Skip to content

Commit 9660b90

Browse files
committed
XRegExp.matchRecursive: Add delimiter and pos info when unbalanced delimiters are found (closes #293)
1 parent dbb0def commit 9660b90

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/addons/matchrecursive.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ export default (XRegExp) => {
180180
}
181181
}
182182
} else {
183-
throw new Error('Unbalanced delimiter found in string');
183+
const delimSide = rightMatch ? 'right' : 'left';
184+
const errorPos = rightMatch ? delimStart : outerStart;
185+
throw new Error(`Unbalanced ${delimSide} delimiter found in string at position ${errorPos}`);
184186
}
185187
// If the delimiter matched an empty string, avoid an infinite loop
186188
if (delimStart === delimEnd) {

tests/spec/s-addons-matchrecursive.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,36 @@ describe('XRegExp.matchRecursive addon:', function() {
4747
expect(XRegExp.matchRecursive(str, '<', '>', 'gy')).toEqual(['1', '<<2>>', '3']);
4848
});
4949

50+
it('should throw for unbalanced left delimiter in first match without flag g', function() {
51+
expect(function() {XRegExp.matchRecursive('<', '<', '>');}).toThrow();
52+
expect(function() {XRegExp.matchRecursive('<<>', '<', '>');}).toThrow();
53+
});
54+
55+
it('should not throw for unbalanced left delimiter after first match without flag g', function() {
56+
expect(function() {XRegExp.matchRecursive('<><', '<', '>');}).not.toThrow();
57+
});
58+
59+
it('should throw for unbalanced left delimiter anywhere in string with flag g', function() {
60+
expect(function() {XRegExp.matchRecursive('<', '<', '>', 'g');}).toThrow();
61+
expect(function() {XRegExp.matchRecursive('<<>', '<', '>', 'g');}).toThrow();
62+
expect(function() {XRegExp.matchRecursive('<><', '<', '>', 'g');}).toThrow();
63+
expect(function() {XRegExp.matchRecursive('.<.<>><', '<', '>', 'g');}).toThrow();
64+
});
65+
66+
it('should throw for unbalanced right delimiter in first match without flag g', function() {
67+
expect(function() {XRegExp.matchRecursive('>', '<', '>');}).toThrow();
68+
});
69+
70+
it('should not throw for unbalanced right delimiter after first match without flag g', function() {
71+
expect(function() {XRegExp.matchRecursive('<>>', '<', '>');}).not.toThrow();
72+
});
73+
74+
it('should throw for unbalanced right delimiter anywhere in string with flag g', function() {
75+
expect(function() {XRegExp.matchRecursive('>', '<', '>', 'g');}).toThrow();
76+
expect(function() {XRegExp.matchRecursive('<>>', '<', '>', 'g');}).toThrow();
77+
expect(function() {XRegExp.matchRecursive('.<.<>>>', '<', '>', 'g');}).toThrow();
78+
});
79+
5080
// TODO: Add complete specs
5181

5282
});

0 commit comments

Comments
 (0)