Skip to content

Commit

Permalink
fix: don't parse html comments inside regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Feb 7, 2024
1 parent 24b9885 commit aa186dc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ module.exports = grammar({
$._ternary_qmark,
$.html_comment,
'||',
// We use escape sequence to tell the scanner if we're currently inside a string or template string, in which case
// We use escape sequence and regex pattern to tell the scanner if we're currently inside a string or template string, in which case
// it should NOT parse html comments.
$.escape_sequence,
$.regex_pattern,
],

extras: $ => [
Expand Down
5 changes: 4 additions & 1 deletion src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions src/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum TokenType {
HTML_COMMENT,
LOGICAL_OR,
ESCAPE_SEQUENCE,
REGEX_PATTERN,
};

void *tree_sitter_javascript_external_scanner_create() { return NULL; }
Expand Down Expand Up @@ -283,7 +284,8 @@ bool tree_sitter_javascript_external_scanner_scan(void *payload, TSLexer *lexer,
return scan_ternary_qmark(lexer);
}

if (valid_symbols[HTML_COMMENT] && !valid_symbols[LOGICAL_OR] && !valid_symbols[ESCAPE_SEQUENCE]) {
if (valid_symbols[HTML_COMMENT] && !valid_symbols[LOGICAL_OR] && !valid_symbols[ESCAPE_SEQUENCE] &&
!valid_symbols[REGEX_PATTERN]) {
return scan_html_comment(lexer);
}

Expand Down
8 changes: 7 additions & 1 deletion test/corpus/statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ Comments within expressions

y // comment
* z;
var a = /<!--/;

---

Expand All @@ -1030,7 +1031,12 @@ y // comment
(binary_expression
(identifier)
(comment)
(identifier))))
(identifier)))
(variable_declaration
(variable_declarator
(identifier)
(regex
(regex_pattern)))))

==========================================
HTML Comments
Expand Down

0 comments on commit aa186dc

Please sign in to comment.