Skip to content

Commit

Permalink
feat: new diag for missing commas between array elements
Browse files Browse the repository at this point in the history
fix: wiped commented code, clang format
  • Loading branch information
koopiehoop authored and strager committed Sep 22, 2023
1 parent b2bc1a3 commit 8087a6c
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 7 deletions.
15 changes: 15 additions & 0 deletions docs/errors/E0712.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# E0712: missing ',' between array elements

This error occurs when there is a missing comma (',') between elements in an array
declaration or initialization. In JavaScript, commas are used to separate individual
elements within an array, and the absence of a comma will lead to a syntax error.

```javascript
let myArray = [1 2 3];
```

To fix this error, you need to add a comma between each element within the array declaration or initialization

```javascript
let myArray = [1, 2, 3];
```
4 changes: 4 additions & 0 deletions po/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,10 @@ msgstr ""
msgid "missing expression in placeholder within template literal"
msgstr ""

#: src/quick-lint-js/diag/diagnostic-metadata-generated.cpp
msgid "missing ',' between array elements"
msgstr ""

#: test/test-diagnostic-formatter.cpp
#: test/test-vim-qflist-json-diag-reporter.cpp
msgid "something happened"
Expand Down
14 changes: 14 additions & 0 deletions src/quick-lint-js/diag/diagnostic-metadata-generated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6092,6 +6092,20 @@ const QLJS_CONSTINIT Diagnostic_Info all_diagnostic_infos[] = {
},
},
},

// Diag_Missing_Comma_Between_Array_Elements
{
.code = 712,
.severity = Diagnostic_Severity::error,
.message_formats = {
QLJS_TRANSLATABLE("missing ',' between array elements"),
},
.message_args = {
{
Diagnostic_Message_Arg_Info(offsetof(Diag_Missing_Comma_Between_Array_Elements, expected_comma), Diagnostic_Arg_Type::source_code_span),
},
},
},
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/quick-lint-js/diag/diagnostic-metadata-generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,11 @@ namespace quick_lint_js {
QLJS_DIAG_TYPE_NAME(Diag_Variable_Assigned_To_Self_Is_Noop) \
QLJS_DIAG_TYPE_NAME(Diag_Xor_Used_As_Exponentiation) \
QLJS_DIAG_TYPE_NAME(Diag_Expected_Expression_In_Template_Literal) \
QLJS_DIAG_TYPE_NAME(Diag_Missing_Comma_Between_Array_Elements) \
/* END */
// clang-format on

inline constexpr int Diag_Type_Count = 409;
inline constexpr int Diag_Type_Count = 410;

extern const Diagnostic_Info all_diagnostic_infos[Diag_Type_Count];
}
Expand Down
8 changes: 7 additions & 1 deletion src/quick-lint-js/diag/diagnostic-types-2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3136,8 +3136,14 @@ struct Diag_Expected_Expression_In_Template_Literal {
ARG(placeholder))]] //
Source_Code_Span placeholder;
};
}

struct Diag_Missing_Comma_Between_Array_Elements {
[[qljs::diag("E0712", Diagnostic_Severity::error)]] //
[[qljs::message("missing ',' between array elements",
ARG(expected_comma))]] //
Source_Code_Span expected_comma;
};
}
QLJS_WARNING_POP

// quick-lint-js finds bugs in JavaScript programs.
Expand Down
12 changes: 11 additions & 1 deletion src/quick-lint-js/fe/parse-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ Expression* Parser::parse_primary_expression(Parse_Visitor_Base& v,
case Token_Type::left_square: {
const Char8* left_square_begin = this->peek().begin;
const Char8* right_square_end;
bool met_expression = false;
this->skip();

Expression_Arena::Vector<Expression*> children(
Expand All @@ -588,11 +589,14 @@ Expression* Parser::parse_primary_expression(Parse_Visitor_Base& v,
this->skip();
break;
}
// TODO(strager): Require commas between expressions.

if (this->peek().type == Token_Type::comma) {
met_expression = false;
this->skip();
continue;
}
const Char8* previous_expession_end =
this->lexer_.end_of_previous_token();
const Char8* child_begin = this->peek().begin;
Expression* child =
this->parse_expression(v, Precedence{.commas = false});
Expand All @@ -612,6 +616,12 @@ Expression* Parser::parse_primary_expression(Parse_Visitor_Base& v,
right_square_end = expected_right_square;
break;
}
if (met_expression) {
this->diag_reporter_->report(Diag_Missing_Comma_Between_Array_Elements{
.expected_comma = Source_Code_Span::unit(previous_expession_end),
});
}
met_expression = true;
children.emplace_back(child);
}
Expression* ast = this->make_expression<Expression::Array>(
Expand Down
4 changes: 3 additions & 1 deletion src/quick-lint-js/i18n/translation-table-generated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ const Translation_Table translation_data = {
{0, 0, 0, 0, 0, 46}, //
{0, 0, 0, 0, 0, 56}, //
{68, 21, 0, 52, 0, 40}, //
{59, 43, 61, 49, 50, 39}, //
{0, 0, 0, 0, 0, 39}, //
{59, 43, 61, 49, 50, 35}, //
{0, 0, 0, 44, 0, 42}, //
{44, 2, 0, 64, 0, 57}, //
{35, 20, 49, 55, 39, 38}, //
Expand Down Expand Up @@ -2074,6 +2075,7 @@ const Translation_Table translation_data = {
u8"misleading use of ',' operator in conditional statement\0"
u8"misleading use of ',' operator in index\0"
u8"mismatched JSX tags; expected '</{1}>'\0"
u8"missing ',' between array elements\0"
u8"missing ',' between variable declarations\0"
u8"missing ',', ';', or newline between object type entries\0"
u8"missing '...' in JSX attribute spread\0"
Expand Down
5 changes: 3 additions & 2 deletions src/quick-lint-js/i18n/translation-table-generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace quick_lint_js {
using namespace std::literals::string_view_literals;

constexpr std::uint32_t translation_table_locale_count = 5;
constexpr std::uint16_t translation_table_mapping_table_size = 496;
constexpr std::size_t translation_table_string_table_size = 78710;
constexpr std::uint16_t translation_table_mapping_table_size = 497;
constexpr std::size_t translation_table_string_table_size = 78745;
constexpr std::size_t translation_table_locale_table_size = 35;

QLJS_CONSTEVAL std::uint16_t translation_table_const_look_up(
Expand Down Expand Up @@ -322,6 +322,7 @@ QLJS_CONSTEVAL std::uint16_t translation_table_const_look_up(
"misleading use of ',' operator in conditional statement"sv,
"misleading use of ',' operator in index"sv,
"mismatched JSX tags; expected '</{1}>'"sv,
"missing ',' between array elements"sv,
"missing ',' between variable declarations"sv,
"missing ',', ';', or newline between object type entries"sv,
"missing '...' in JSX attribute spread"sv,
Expand Down
13 changes: 12 additions & 1 deletion src/quick-lint-js/i18n/translation-table-test-generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Translated_String {
};

// clang-format off
inline const Translated_String test_translation_table[495] = {
inline const Translated_String test_translation_table[496] = {
{
"\"global-groups\" entries must be strings"_translatable,
u8"\"global-groups\" entries must be strings",
Expand Down Expand Up @@ -3284,6 +3284,17 @@ inline const Translated_String test_translation_table[495] = {
u8"mismatched JSX tags; expected '</{1}>'",
},
},
{
"missing ',' between array elements"_translatable,
u8"missing ',' between array elements",
{
u8"missing ',' between array elements",
u8"missing ',' between array elements",
u8"missing ',' between array elements",
u8"missing ',' between array elements",
u8"missing ',' between array elements",
},
},
{
"missing ',' between variable declarations"_translatable,
u8"missing ',' between variable declarations",
Expand Down
10 changes: 10 additions & 0 deletions test/test-parse-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,16 @@ TEST_F(Test_Parse_Expression, malformed_array_literal) {
});
EXPECT_EQ(summarize(ast), "array()");
}

{
Test_Parser p(u8"[a b]"_sv, capture_diags);
Expression* ast = p.parse_expression();
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Missing_Comma_Between_Array_Elements"_diag,
});
EXPECT_EQ(summarize(ast), "array(var a, var b)");
}
}

TEST_F(Test_Parse_Expression, object_literal) {
Expand Down

0 comments on commit 8087a6c

Please sign in to comment.