[ruby/prism] Warn & interpreted as argument prefix

This PR makes `Prism` warn `&` interpreted as argument prefix.
This carries a similar meaning to the following Ruby warning:

```console
$ ruby -cwe "foo &bar"
-e:1: warning: `&' interpreted as argument prefix
Syntax OK
```

Previously, it did not issue a warning:

```console
$ bundle exec ruby -rprism -ve "p Prism.parse('foo &bar').warnings"
ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22]
[]
```

From now on, it will issue a warning similar to Ruby's:

```console
$ bundle exec ruby -rprism -ve "p Prism.parse('foo &bar').warnings"
ruby 3.3.0 (2023-12-25 revision https://github.com/ruby/prism/commit/5124f9ac75) [x86_64-darwin22]
[#<Prism::ParseWarning @type=:ambiguous_prefix_amp @message="ambiguous `&` has been interpreted as an argument prefix"
@location=#<Prism::Location @start_offset=4 @length=1 start_line=1> @level=:verbose>]
```

https://github.com/ruby/prism/commit/312f99cd1e
This commit is contained in:
Koichi ITO 2024-03-13 10:46:10 +09:00 committed by git
parent 824e3e6c3d
commit 5fa28ce015
3 changed files with 6 additions and 1 deletions

View File

@ -233,6 +233,7 @@ errors:
warnings:
- AMBIGUOUS_FIRST_ARGUMENT_MINUS
- AMBIGUOUS_FIRST_ARGUMENT_PLUS
- AMBIGUOUS_PREFIX_AMPERSAND
- AMBIGUOUS_PREFIX_STAR
- AMBIGUOUS_PREFIX_STAR_STAR
- AMBIGUOUS_SLASH

View File

@ -9912,7 +9912,10 @@ parser_lex(pm_parser_t *parser) {
}
pm_token_type_t type = PM_TOKEN_AMPERSAND;
if (lex_state_spcarg_p(parser, space_seen) || lex_state_beg_p(parser)) {
if (lex_state_spcarg_p(parser, space_seen)) {
pm_parser_warn_token(parser, &parser->current, PM_WARN_AMBIGUOUS_PREFIX_AMPERSAND);
type = PM_TOKEN_UAMPERSAND;
} else if (lex_state_beg_p(parser)) {
type = PM_TOKEN_UAMPERSAND;
}

View File

@ -313,6 +313,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
// Warnings
[PM_WARN_AMBIGUOUS_FIRST_ARGUMENT_MINUS] = { "ambiguous first argument; put parentheses or a space even after `-` operator", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_AMBIGUOUS_FIRST_ARGUMENT_PLUS] = { "ambiguous first argument; put parentheses or a space even after `+` operator", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_AMBIGUOUS_PREFIX_AMPERSAND] = { "ambiguous `&` has been interpreted as an argument prefix", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_AMBIGUOUS_PREFIX_STAR] = { "ambiguous `*` has been interpreted as an argument prefix", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_AMBIGUOUS_PREFIX_STAR_STAR] = { "ambiguous `**` has been interpreted as an argument prefix", PM_WARNING_LEVEL_VERBOSE },
[PM_WARN_AMBIGUOUS_SLASH] = { "ambiguous `/`; wrap regexp in parentheses or add a space after `/` operator", PM_WARNING_LEVEL_VERBOSE },