Skip to content

Commit f5af62c

Browse files
jackschuamaanq
andauthored
fix: comment optimization should not cause automatic-semi insert for single-line block comment
Co-authored-by: Amaan Qureshi <[email protected]>
1 parent c4739fe commit f5af62c

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/scanner.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,18 @@ static bool scan_template_chars(TSLexer *lexer) {
4747
}
4848
}
4949

50-
static bool scan_whitespace_and_comments(TSLexer *lexer, bool *scanned_comment) {
50+
typedef enum {
51+
REJECT, // Semicolon is illegal, ie a syntax error occurred
52+
NO_NEWLINE, // Unclear if semicolon will be legal, continue
53+
ACCEPT, // Semicolon is legal, assuming a comment was encountered
54+
} WhitespaceResult;
55+
56+
/**
57+
* @param consume If false, only consume enough to check if comment indicates semicolon-legality
58+
*/
59+
static WhitespaceResult scan_whitespace_and_comments(TSLexer *lexer, bool *scanned_comment, bool consume) {
60+
bool saw_block_newline = false;
61+
5162
for (;;) {
5263
while (iswspace(lexer->lookahead)) {
5364
skip(lexer);
@@ -71,17 +82,25 @@ static bool scan_whitespace_and_comments(TSLexer *lexer, bool *scanned_comment)
7182
if (lexer->lookahead == '/') {
7283
skip(lexer);
7384
*scanned_comment = true;
85+
86+
if (lexer->lookahead != '/' && !consume) {
87+
return saw_block_newline ? ACCEPT : NO_NEWLINE;
88+
}
89+
7490
break;
7591
}
92+
} else if (lexer->lookahead == '\n' || lexer->lookahead == 0x2028 || lexer->lookahead == 0x2029) {
93+
saw_block_newline = true;
94+
skip(lexer);
7695
} else {
7796
skip(lexer);
7897
}
7998
}
8099
} else {
81-
return false;
100+
return REJECT;
82101
}
83102
} else {
84-
return true;
103+
return ACCEPT;
85104
}
86105
}
87106
}
@@ -96,10 +115,12 @@ static bool scan_automatic_semicolon(TSLexer *lexer, bool comment_condition, boo
96115
}
97116

98117
if (lexer->lookahead == '/') {
99-
if (!scan_whitespace_and_comments(lexer, scanned_comment)) {
118+
WhitespaceResult result = scan_whitespace_and_comments(lexer, scanned_comment, false);
119+
if (result == REJECT) {
100120
return false;
101121
}
102-
if (comment_condition && lexer->lookahead != ',' && lexer->lookahead != '=') {
122+
123+
if (result == ACCEPT && comment_condition && lexer->lookahead != ',' && lexer->lookahead != '=') {
103124
return true;
104125
}
105126
}
@@ -125,7 +146,7 @@ static bool scan_automatic_semicolon(TSLexer *lexer, bool comment_condition, boo
125146

126147
skip(lexer);
127148

128-
if (!scan_whitespace_and_comments(lexer, scanned_comment)) {
149+
if (scan_whitespace_and_comments(lexer, scanned_comment, true) == REJECT) {
129150
return false;
130151
}
131152

test/corpus/semicolon_insertion.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ let b /* comment between declarators */, c
272272

273273
let d
274274

275+
let e
276+
/* back to back *//* comments */
277+
278+
class C {
279+
method/*comment*/() {}
280+
}
281+
282+
b
283+
/* interleaved non-semi-insertion */
284+
.c
275285
---
276286

277287
(program
@@ -286,4 +296,14 @@ let d
286296
(comment)
287297
(comment)
288298
(comment)
289-
(lexical_declaration (variable_declarator (identifier))))
299+
(lexical_declaration (variable_declarator (identifier)))
300+
(lexical_declaration (variable_declarator (identifier)))
301+
(comment)
302+
(comment)
303+
(class_declaration (identifier) (class_body (method_definition
304+
(property_identifier)
305+
(comment)
306+
(formal_parameters)
307+
(statement_block))))
308+
(expression_statement
309+
(member_expression (identifier) (comment) (property_identifier))))

0 commit comments

Comments
 (0)