Skip to content

Commit

Permalink
Fix: invalid regex used in cutAfterJS (#1137)
Browse files Browse the repository at this point in the history
* support regex in `utils#cutAfterJSON`

* fix test and code

* add support for single and backtick quoted strings to cutAfterJSON

* yet another unit-test

* update comments

* fix regex & add another test
  • Loading branch information
TimeForANinja committed Sep 3, 2022
1 parent a299081 commit f16e8c8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/utils.js
Expand Up @@ -60,7 +60,7 @@ const ESCAPING_SEQUENZES = [
{ start: "'", end: "'" },
{ start: '`', end: '`' },
// RegeEx
{ start: '/', end: '/', startPrefix: /(^|[[{:;,])\s+$/ },
{ start: '/', end: '/', startPrefix: /(^|[[{:;,])\s?$/ },
];

/**
Expand Down
10 changes: 9 additions & 1 deletion test/utils-test.js
Expand Up @@ -64,9 +64,17 @@ describe('utils.cutAfterJS()', () => {
'{"a": "\\"}1", "b": 1, "c": /[0-9]}}\\/}/}',
);
});
it('Tolerant to string with regexes in arrays ', () => {
assert.strictEqual(
// This does also tests "startPrefix" of the RegEx-Escaping-Sequenze
// I already screwed up the preceding whitespace rule once...
utils.cutAfterJS('{"a": [-1929233002,b,/,][}",],()}(\\[)/,2070160835,1561177444]}abcd'),
'{"a": [-1929233002,b,/,][}",],()}(\\[)/,2070160835,1561177444]}',
);
});
it('does not fail for division followed by a regex', () => {
assert.strictEqual(
utils.cutAfterJS('{"a": "\\"}1", "b": 1, "c": [4/6, /[0-9]}}\\/}/]}abcd', true),
utils.cutAfterJS('{"a": "\\"}1", "b": 1, "c": [4/6, /[0-9]}}\\/}/]}abcd'),
'{"a": "\\"}1", "b": 1, "c": [4/6, /[0-9]}}\\/}/]}',
);
});
Expand Down

0 comments on commit f16e8c8

Please sign in to comment.