Skip to content

Commit

Permalink
fix: renamed functions, improved diag messages
Browse files Browse the repository at this point in the history
  • Loading branch information
koopiehoop committed Sep 26, 2023
1 parent a8f6515 commit fd0edfb
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 51 deletions.
4 changes: 2 additions & 2 deletions docs/errors/E0713.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# E0713: '*' keyword is not allowed on getters or setters
# E0713: getters and setters cannot be generators

Use of the '*' character, defining generator functions, is not allowed on getters or setters.
Getters and setters are synchronous operations and do not support the generator functionality.
Expand All @@ -18,7 +18,7 @@ class C {
}
```

To fix this error define with a getter or setter, using regular function syntax.
To fix this error define a getter or setter, using regular function syntax.

```javascript
class C {
Expand Down
4 changes: 2 additions & 2 deletions docs/errors/E0714.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class C {
```

To fix this error simply remove 'async' keyword from getters and setters,
so they can function properly as synchronous operations
so they can function properly as synchronous operations.


```javascript
Expand All @@ -41,7 +41,7 @@ class C {
```

However, if you require asynchronous behavior within getters or setters,
you can achieve this by implementing separate asynchronous methods
you can achieve this by implementing separate asynchronous methods.


```javascript
Expand Down
2 changes: 1 addition & 1 deletion po/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,7 @@ msgid "missing ',' between array elements"
msgstr ""

#: src/quick-lint-js/diag/diagnostic-metadata-generated.cpp
msgid "'*' keyword is not allowed on getters or setters"
msgid "getters and setters cannot be generators"
msgstr ""

#: src/quick-lint-js/diag/diagnostic-metadata-generated.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/quick-lint-js/diag/diagnostic-metadata-generated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6331,12 +6331,12 @@ const QLJS_CONSTINIT Diagnostic_Info all_diagnostic_infos[] = {
.code = 713,
.severity = Diagnostic_Severity::error,
.message_formats = {
QLJS_TRANSLATABLE("'*' keyword is not allowed on getters or setters"),
QLJS_TRANSLATABLE("getters and setters cannot be generators"),
QLJS_TRANSLATABLE("'{0}' here"),
},
.message_args = {
{
Diagnostic_Message_Arg_Info(offsetof(Diag_Class_Generator_On_Getter_Or_Setter, generator_keyword), Diagnostic_Arg_Type::source_code_span),
Diagnostic_Message_Arg_Info(offsetof(Diag_Class_Generator_On_Getter_Or_Setter, star_token), Diagnostic_Arg_Type::source_code_span),
},
{
Diagnostic_Message_Arg_Info(offsetof(Diag_Class_Generator_On_Getter_Or_Setter, getter_setter_keyword), Diagnostic_Arg_Type::source_code_span),
Expand Down
8 changes: 3 additions & 5 deletions src/quick-lint-js/diag/diagnostic-types-2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3264,11 +3264,10 @@ struct Diag_Missing_Comma_Between_Array_Elements {

struct Diag_Class_Generator_On_Getter_Or_Setter {
[[qljs::diag("E0713", Diagnostic_Severity::error)]] //
[[qljs::message("'*' keyword is not allowed on getters or setters",
ARG(generator_keyword))]] //
[[qljs::message("getters and setters cannot be generators",
ARG(star_token))]] //
[[qljs::message("'{0}' here", ARG(getter_setter_keyword))]] //
Source_Code_Span method_start;
Source_Code_Span generator_keyword;
Source_Code_Span star_token;
Source_Code_Span getter_setter_keyword;
};

Expand All @@ -3277,7 +3276,6 @@ struct Diag_Class_Async_On_Getter_Or_Setter {
[[qljs::message("'async' keyword is not allowed on getters or setters",
ARG(async_keyword))]] //
[[qljs::message("'{0}' here", ARG(getter_setter_keyword))]] //
Source_Code_Span method_start;
Source_Code_Span async_keyword;
Source_Code_Span getter_setter_keyword;
};
Expand Down
17 changes: 6 additions & 11 deletions src/quick-lint-js/fe/parse-class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,8 +1155,8 @@ void Parser::parse_and_visit_class_or_interface_member(
void check_modifiers_for_method() {
error_if_accessor_method();
error_if_declare_method();
error_if_generator_method();
error_if_async_method();
error_if_generator_getter_or_setter();
error_if_async_getter_or_setter();
error_if_readonly_method();
error_if_async_or_generator_without_method_body();
error_if_invalid_access_specifier();
Expand Down Expand Up @@ -1326,40 +1326,35 @@ void Parser::parse_and_visit_class_or_interface_member(
}
}

void error_if_generator_method() {
void error_if_generator_getter_or_setter() {
if (const Modifier *star_modifier = find_modifier(Token_Type::star)) {
Source_Code_Span method_start = p->peek().span();
if (const Modifier *get_modifier = find_modifier(Token_Type::kw_get)) {
p->diag_reporter_->report(Diag_Class_Generator_On_Getter_Or_Setter{
.method_start = method_start,
.generator_keyword = star_modifier->span,
.star_token = star_modifier->span,
.getter_setter_keyword = get_modifier->span,
});
} else if (const Modifier *set_modifier =
find_modifier(Token_Type::kw_set)) {
p->diag_reporter_->report(Diag_Class_Generator_On_Getter_Or_Setter{
.method_start = method_start,
.generator_keyword = star_modifier->span,
.star_token = star_modifier->span,
.getter_setter_keyword = set_modifier->span,
});
}
}
}

void error_if_async_method() {
void error_if_async_getter_or_setter() {
if (const Modifier *async_modifier =
find_modifier(Token_Type::kw_async)) {
Source_Code_Span method_start = p->peek().span();
if (const Modifier *get_modifier = find_modifier(Token_Type::kw_get)) {
p->diag_reporter_->report(Diag_Class_Async_On_Getter_Or_Setter{
.method_start = method_start,
.async_keyword = async_modifier->span,
.getter_setter_keyword = get_modifier->span,
});
} else if (const Modifier *set_modifier =
find_modifier(Token_Type::kw_set)) {
p->diag_reporter_->report(Diag_Class_Async_On_Getter_Or_Setter{
.method_start = method_start,
.async_keyword = async_modifier->span,
.getter_setter_keyword = set_modifier->span,
});
Expand Down
6 changes: 3 additions & 3 deletions src/quick-lint-js/i18n/translation-table-generated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const Translation_Table translation_data = {
{74, 87, 79, 56, 0, 59}, //
{71, 80, 60, 58, 0, 52}, //
{0, 0, 0, 0, 0, 28}, //
{0, 0, 0, 0, 0, 63}, //
{31, 56, 0, 32, 0, 49}, //
{31, 56, 0, 32, 0, 63}, //
{0, 0, 0, 0, 0, 67}, //
{0, 0, 0, 70, 0, 26}, //
{79, 25, 30, 63, 49593, 66}, //
Expand Down Expand Up @@ -284,6 +283,7 @@ const Translation_Table translation_data = {
{84, 28, 66, 75, 25, 54}, //
{0, 0, 0, 70, 0, 52}, //
{0, 0, 0, 0, 0, 45}, //
{0, 0, 0, 0, 0, 41}, //
{70, 27, 0, 57, 0, 52}, //
{0, 0, 0, 5, 0, 5}, //
{5, 11, 66, 52, 54, 42}, //
Expand Down Expand Up @@ -1807,7 +1807,6 @@ const Translation_Table translation_data = {
u8"\"globals\" descriptor must be a boolean or an object\0"
u8"\"globals\" must be an object\0"
u8"'!' here treated as the TypeScript non-null assertion operator\0"
u8"'*' keyword is not allowed on getters or setters\0"
u8"'**' operator cannot be used after unary '{1}' without parentheses\0"
u8"',' should be ';' instead\0"
u8"'.' is not allowed after generic arguments; write [\"{1}\"] instead\0"
Expand Down Expand Up @@ -2072,6 +2071,7 @@ const Translation_Table translation_data = {
u8"generator function '*' belongs after keyword function\0"
u8"generator function '*' belongs before function name\0"
u8"generic arrow function needs ',' here in TSX\0"
u8"getters and setters cannot be generators\0"
u8"getters and setters cannot have overload signatures\0"
u8"here\0"
u8"here is the assignment assertion operator\0"
Expand Down
4 changes: 2 additions & 2 deletions src/quick-lint-js/i18n/translation-table-generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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 = 518;
constexpr std::size_t translation_table_string_table_size = 79741;
constexpr std::size_t translation_table_string_table_size = 79733;
constexpr std::size_t translation_table_locale_table_size = 35;

QLJS_CONSTEVAL std::uint16_t translation_table_const_look_up(
Expand All @@ -33,7 +33,6 @@ QLJS_CONSTEVAL std::uint16_t translation_table_const_look_up(
"\"globals\" descriptor must be a boolean or an object"sv,
"\"globals\" must be an object"sv,
"'!' here treated as the TypeScript non-null assertion operator"sv,
"'*' keyword is not allowed on getters or setters"sv,
"'**' operator cannot be used after unary '{1}' without parentheses"sv,
"',' should be ';' instead"sv,
"'.' is not allowed after generic arguments; write [\"{1}\"] instead"sv,
Expand Down Expand Up @@ -298,6 +297,7 @@ QLJS_CONSTEVAL std::uint16_t translation_table_const_look_up(
"generator function '*' belongs after keyword function"sv,
"generator function '*' belongs before function name"sv,
"generic arrow function needs ',' here in TSX"sv,
"getters and setters cannot be generators"sv,
"getters and setters cannot have overload signatures"sv,
"here"sv,
"here is the assignment assertion operator"sv,
Expand Down
22 changes: 11 additions & 11 deletions src/quick-lint-js/i18n/translation-table-test-generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ inline const Translated_String test_translation_table[517] = {
u8"'!' here treated as the TypeScript non-null assertion operator",
},
},
{
"'*' keyword is not allowed on getters or setters"_translatable,
u8"'*' keyword is not allowed on getters or setters",
{
u8"'*' keyword is not allowed on getters or setters",
u8"'*' keyword is not allowed on getters or setters",
u8"'*' keyword is not allowed on getters or setters",
u8"'*' keyword is not allowed on getters or setters",
u8"'*' keyword is not allowed on getters or setters",
},
},
{
"'**' operator cannot be used after unary '{1}' without parentheses"_translatable,
u8"'**' operator cannot be used after unary '{1}' without parentheses",
Expand Down Expand Up @@ -3020,6 +3009,17 @@ inline const Translated_String test_translation_table[517] = {
u8"generic arrow function needs ',' here in TSX",
},
},
{
"getters and setters cannot be generators"_translatable,
u8"getters and setters cannot be generators",
{
u8"getters and setters cannot be generators",
u8"getters and setters cannot be generators",
u8"getters and setters cannot be generators",
u8"getters and setters cannot be generators",
u8"getters and setters cannot be generators",
},
},
{
"getters and setters cannot have overload signatures"_translatable,
u8"getters and setters cannot have overload signatures",
Expand Down
18 changes: 6 additions & 12 deletions test/test-parse-class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,41 +839,35 @@ TEST_F(Test_Parse_Class, accessor_cannot_be_method) {
TEST_F(Test_Parse_Class, async_keyword_on_getters_and_setters) {
test_parse_and_visit_statement(
u8"class C { get async myMethod() { } }"_sv, //
u8" ^ Diag_Class_Async_On_Getter_Or_Setter.method_start\n"_diag
u8" ^^^^^ .async_keyword\n"_diag
u8" ^^^^^ Diag_Class_Async_On_Getter_Or_Setter.async_keyword\n"_diag
u8" ^^^ .getter_setter_keyword"_diag,
javascript_options);
test_parse_and_visit_statement(
u8"class C { set async myMethod() { } }"_sv, //
u8" ^ Diag_Class_Async_On_Getter_Or_Setter.method_start\n"_diag
u8" ^^^^^ .async_keyword\n"_diag
u8" ^^^^^ Diag_Class_Async_On_Getter_Or_Setter.async_keyword\n"_diag
u8" ^^^ .getter_setter_keyword"_diag,
javascript_options);
test_parse_and_visit_statement(
u8"class C { async get myMethod() { } }"_sv, //
u8" ^ Diag_Class_Async_On_Getter_Or_Setter.method_start\n"_diag
u8" ^^^ .getter_setter_keyword\n"_diag
u8" ^^^ Diag_Class_Async_On_Getter_Or_Setter.getter_setter_keyword\n"_diag
u8" ^^^^^ .async_keyword"_diag,
javascript_options);
test_parse_and_visit_statement(
u8"class C { async set myMethod() { } }"_sv, //
u8" ^ Diag_Class_Async_On_Getter_Or_Setter.method_start\n"_diag
u8" ^^^ .getter_setter_keyword\n"_diag
u8" ^^^ Diag_Class_Async_On_Getter_Or_Setter.getter_setter_keyword\n"_diag
u8" ^^^^^ .async_keyword"_diag,
javascript_options);
}

TEST_F(Test_Parse_Class, getters_and_setters_cannot_be_generator) {
test_parse_and_visit_statement(
u8"class C { get *myMethod() { } }"_sv, //
u8" ^ Diag_Class_Generator_On_Getter_Or_Setter.method_start\n"_diag
u8" ^ .generator_keyword\n"_diag
u8" ^ Diag_Class_Generator_On_Getter_Or_Setter.star_token\n"_diag
u8" ^^^ .getter_setter_keyword"_diag,
javascript_options);
test_parse_and_visit_statement(
u8"class C { set *myMethod() { } }"_sv, //
u8" ^ Diag_Class_Generator_On_Getter_Or_Setter.method_start\n"_diag
u8" ^ .generator_keyword\n"_diag
u8" ^ Diag_Class_Generator_On_Getter_Or_Setter.star_token\n"_diag
u8" ^^^ .getter_setter_keyword"_diag,
javascript_options);
}
Expand Down

0 comments on commit fd0edfb

Please sign in to comment.