Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't parse html comments inside regexes #296

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading