Skip to content

Commit

Permalink
Warn on redundant semicolon after else
Browse files Browse the repository at this point in the history
  • Loading branch information
someoneigna committed May 16, 2021
1 parent 06af6e3 commit 2ea8012
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/quick-lint-js/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,12 @@
{ source_code_span continue_statement; }, \
.error(QLJS_TRANSLATABLE("continue can only be used inside of a loop"), \
continue_statement)) \
\
QLJS_ERROR_TYPE( \
error_redundant_semicolon_after_else, "E202", \
{ source_code_span semicolon; }, \
.warning(QLJS_TRANSLATABLE("redundant semicolon after else"), \
semicolon)) \
/* END */

namespace quick_lint_js {
Expand Down
7 changes: 7 additions & 0 deletions src/quick-lint-js/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -2516,6 +2516,13 @@ class parser {

if (this->peek().type == token_type::kw_else) {
this->skip();
if (this->peek().type == token_type::semicolon) {
source_code_span semicolon = this->peek().span();
this->error_reporter_->report(error_redundant_semicolon_after_else{
.semicolon = source_code_span(
semicolon.begin(), semicolon.end()),
});
}
parse_and_visit_body();
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/test-parse-statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,22 @@ TEST(test_parse, else_without_if) {
}
}

TEST(test_parse, else_redundant_semicolon) {
{
spy_visitor v;
padded_string code(u8"if (cond) { body; } else; { body; }"_sv);
parser p(&code, &v);
EXPECT_TRUE(p.parse_and_visit_statement(v));
EXPECT_THAT(v.visits, ElementsAre("visit_variable_use", // cond
"visit_enter_block_scope", // (if)
"visit_variable_use", // body
"visit_exit_block_scope")); // (else)
EXPECT_THAT(v.errors, ElementsAre(ERROR_TYPE_FIELD(
error_redundant_semicolon_after_else, semicolon,
offsets_matcher(&code, strlen(u8"if (cond) { body; } else"), u8";"))));
}
}

TEST(test_parse, block_statement) {
{
spy_visitor v = parse_and_visit_statement(u8"{ }"_sv);
Expand Down

0 comments on commit 2ea8012

Please sign in to comment.