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: comment before else and and #236

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion grammar.js
Expand Up @@ -451,7 +451,7 @@ module.exports = grammar({
choice('export', 'let'),
optional('rec'),
sep1(
seq(repeat($._newline), 'and'),
seq(repeat($._newline_and_comment), 'and'),
$.let_binding
)
),
Expand Down
12 changes: 12 additions & 0 deletions src/scanner.c
Expand Up @@ -243,6 +243,18 @@ bool tree_sitter_rescript_external_scanner_scan(
in_multiline_statement = true;
}
}
} else if (lexer->lookahead == 'e') {
advance(lexer);
if (lexer->lookahead == 'l') {
advance(lexer);
if (lexer->lookahead == 's') {
advance(lexer);
if (lexer->lookahead == 'e') {
// Ignore new lines before `else` keyword
in_multiline_statement = true;
}
}
}
}

if (in_multiline_statement) {
Expand Down
36 changes: 35 additions & 1 deletion test/corpus/comments.txt
Expand Up @@ -37,6 +37,19 @@ switch foo {
// in-switch
}

if predicate {
foo
} /* comment */
else {
bar
}

/* comment */
let foo = 1
/* comment */
@baz
and bar = 2

--------------------------------------------------------------------------------

(source_file
Expand Down Expand Up @@ -80,4 +93,25 @@ switch foo {
(sequence_expression
(expression_statement
(number))))
(comment))))
(comment)))
(expression_statement
(if_expression
(value_identifier)
(block
(expression_statement
(value_identifier)))
(comment)
(else_clause
(block
(expression_statement
(value_identifier))))))
(comment)
(let_declaration
(let_binding
(value_identifier)
(number))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Looks like there's a comment missing right here before the decorator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just realized that after I made this PR. I think the _newline_and_comment node discards the comment in this particular position.

(decorator
(decorator_identifier))
(let_binding
(value_identifier)
(number))))