Skip to content

Commit

Permalink
Merge pull request #1557 from haldun/haldun/reject-numbered-parameter
Browse files Browse the repository at this point in the history
Reject numbered parameters in block parameters
  • Loading branch information
kddnewton authored Sep 21, 2023
2 parents 0066cda + ae02dad commit 54231b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/yarp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4814,10 +4814,20 @@ yp_parser_local_add_owned(yp_parser_t *parser, const uint8_t *start, size_t leng
if (constant_id != 0) yp_parser_local_add(parser, constant_id);
}

static inline bool
token_is_numbered_parameter(const uint8_t *start, const uint8_t *end) {
return (end - start == 2) && (start[0] == '_') && (start[1] != '0') && (yp_char_is_decimal_digit(start[1]));
}

// Add a parameter name to the current scope and check whether the name of the
// parameter is unique or not.
static void
yp_parser_parameter_name_check(yp_parser_t *parser, yp_token_t *name) {
// We want to check whether the parameter name is a numbered parameter or not.
if (token_is_numbered_parameter(name->start, name->end)) {
yp_diagnostic_list_append(&parser->error_list, name->start, name->end, YP_ERR_PARAMETER_NUMBERED_RESERVED);
}

// We want to ignore any parameter name that starts with an underscore.
if ((*name->start == '_')) return;

Expand Down Expand Up @@ -4898,11 +4908,6 @@ char_is_global_name_punctuation(const uint8_t b) {
return (yp_global_name_punctuation_hash[(i - 0x20) / 32] >> (i % 32)) & 1;
}

static inline bool
token_is_numbered_parameter(const uint8_t *start, const uint8_t *end) {
return (end - start == 2) && (start[0] == '_') && (start[1] != '0') && (yp_char_is_decimal_digit(start[1]));
}

static inline bool
token_is_setter_name(yp_token_t *token) {
return (
Expand Down
7 changes: 7 additions & 0 deletions test/yarp/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,13 @@ def test_alnum_delimiters
assert_error_messages "%sXfooX", error_messages
end

def test_numbered_parameters_in_block_arguments
source = "foo { |_1| }"
assert_errors expression(source), source, [
["Token reserved for a numbered parameter", 7..9],
]
end

def test_conditional_predicate_closed
source = "if 0 0; end\nunless 0 0; end"
assert_errors expression(source), source, [
Expand Down

0 comments on commit 54231b9

Please sign in to comment.