Skip to content

Commit a5e4d57

Browse files
Optimizer: fix \{, \} and \- unescpae
1 parent e11db71 commit a5e4d57

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/optimizer/transforms/__tests__/char-escape-unescape-transform-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ describe('\e -> e', () => {
1818
});
1919

2020
it('preserve escape', () => {
21-
const re = transform(/\*\^\$\(\)\[/, [
21+
const re = transform(/\*\^\$\(\)\[\{\}/, [
2222
charUnescape,
2323
]);
24-
expect(re.toString()).toBe(/\*\^\$\(\)\[/.toString());
24+
expect(re.toString()).toBe(/\*\^\$\(\)\[\{\}/.toString());
2525
});
2626

2727
it('char class', () => {
28-
const re = transform(/[\e\*\(\]\ \^\$]\(\n/, [
28+
const re = transform(/[\e\*\(\]\ \^\$\-]\(\n/, [
2929
charUnescape,
3030
]);
31-
expect(re.toString()).toBe(/[e*(\] ^$]\(\n/.toString());
31+
expect(re.toString()).toBe(/[e*(\] ^$\-]\(\n/.toString());
3232
});
3333

3434
});

src/optimizer/transforms/char-escape-unescape-transform.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ function shouldUnescape(path) {
3838
}
3939

4040
/**
41-
* \], \\
41+
* \], \\, \-
42+
*
43+
* Note: \- always preserved to avoid `[a\-z]` turning into `[a-z]`.
44+
* TODO: more sophisticated analisys.
4245
*/
4346
function preservesInCharClass(value) {
44-
return /[\]\\]/.test(value);
47+
return /[\]\\\\-]/.test(value);
4548
}
4649

50+
// Note: \{ and \} are always preserved to avoid `a\{2\}` turning
51+
// into `a{2}`. TODO: more sophisticated analisys.
52+
4753
function preservesEscape(value) {
48-
return /[*\[\]()+?^$.\/\\]/.test(value);
54+
return /[*\[\]()+?^$.\/\\\{\}]/.test(value);
4955
}

0 commit comments

Comments
 (0)