Skip to content

Commit 2ec2782

Browse files
committed
Parser: When u flag is set, throw error on invalid unicode escape
1 parent e2f8ab4 commit 2ec2782

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/parser/__tests__/parser-basic-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,4 +1068,10 @@ describe('basic', () => {
10681068
});
10691069
});
10701070

1071+
it('throws error on invalid Unicode escape', () => {
1072+
expect(() => parser.parse('/\\p/u')).toThrowError(SyntaxError);
1073+
expect(() => parser.parse('/\\e/u')).toThrowError(SyntaxError);
1074+
expect(() => parser.parse('/\\g/u')).toThrowError(SyntaxError);
1075+
});
1076+
10711077
});

src/parser/generated/regexp-tree.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,13 @@ const lexRules = [[/^#[^\n]+/, function() { /* skip comments */ }],
360360
[/^\\x[0-9a-fA-F]{2}/, function() { return 'HEX_CODE' }],
361361
[/^\\[tnrdDsSwWvf]/, function() { return 'META_CHAR' }],
362362
[/^\\\//, function() { return 'ESC_CHAR' }],
363-
[/^\\[^*?+\[()]/, function() { return 'ESC_CHAR' }],
363+
[/^\\[^*?+\[()]/, function() {
364+
const s = this.getCurrentState();
365+
if (s === 'u' || s === 'xu' || s === 'u_class') {
366+
throw new SyntaxError(`invalid Unicode escape ${yytext}`);
367+
}
368+
return 'ESC_CHAR';
369+
}],
364370
[/^\\[*?+\[()]/, function() { return 'ESC_CHAR' }],
365371
[/^\(/, function() { return 'CHAR' }],
366372
[/^\)/, function() { return 'CHAR' }],

src/parser/regexp.bnf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ GROUP_NAME ([\w$]|\\'u'[0-9a-fA-F]{4}|\\'u{'[0-9a-fA-F]{1,}'}')+
103103

104104
{ESC}'/' return 'ESC_CHAR'
105105

106-
{ESC}{CHAR} return 'ESC_CHAR'
106+
{ESC}{CHAR} {
107+
const s = this.getCurrentState();
108+
if (s === 'u' || s === 'xu' || s === 'u_class') {
109+
throw new SyntaxError(`invalid Unicode escape ${yytext}`);
110+
}
111+
return 'ESC_CHAR';
112+
}
113+
107114
{ESC}[*?+\[()] return 'ESC_CHAR'
108115

109116
<class,u_class>'(' return 'CHAR'

0 commit comments

Comments
 (0)