diff options
| author | Haldun Bayhantopcu <haldun@github.com> | 2023-11-21 17:36:07 +0100 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2023-11-21 16:36:12 +0000 |
| commit | 8966d06b96f2c6396d4c7e58b7a51c9bdebbb694 (patch) | |
| tree | 696fbdf04baa764bb22487bbe9614ca797cd8647 | |
| parent | a5b482837b743ae6121fc6ca52e5d52985ab93c8 (diff) | |
[ruby/prism] Warning for ENDs in methods
(https://github.com/ruby/prism/pull/1899)
https://github.com/ruby/prism/commit/1b41c2d56c
| -rw-r--r-- | lib/prism/ffi.rb | 2 | ||||
| -rw-r--r-- | prism/diagnostic.c | 1 | ||||
| -rw-r--r-- | prism/diagnostic.h | 1 | ||||
| -rw-r--r-- | prism/prism.c | 4 | ||||
| -rw-r--r-- | test/prism/errors_test.rb | 11 |
5 files changed, 18 insertions, 1 deletions
diff --git a/lib/prism/ffi.rb b/lib/prism/ffi.rb index c910fd3aae..847990ed9a 100644 --- a/lib/prism/ffi.rb +++ b/lib/prism/ffi.rb @@ -299,7 +299,7 @@ module Prism values << (options.fetch(:frozen_string_literal, false) ? 1 : 0) template << "C" - values << (options[:verbose] ? 0 : 1) + values << (options.fetch(:verbose, true) ? 0 : 1) template << "L" if (scopes = options[:scopes]) diff --git a/prism/diagnostic.c b/prism/diagnostic.c index 7da8806b37..473960e361 100644 --- a/prism/diagnostic.c +++ b/prism/diagnostic.c @@ -256,6 +256,7 @@ static const char* const diagnostic_messages[PM_DIAGNOSTIC_ID_LEN] = { [PM_WARN_AMBIGUOUS_FIRST_ARGUMENT_PLUS] = "Ambiguous first argument; put parentheses or a space even after `+` operator", [PM_WARN_AMBIGUOUS_PREFIX_STAR] = "Ambiguous `*` has been interpreted as an argument prefix", [PM_WARN_AMBIGUOUS_SLASH] = "Ambiguous `/`; wrap regexp in parentheses or add a space after `/` operator", + [PM_WARN_END_IN_METHOD] = "END in method; use at_exit", }; static const char* diff --git a/prism/diagnostic.h b/prism/diagnostic.h index 33a70229c8..68e507929f 100644 --- a/prism/diagnostic.h +++ b/prism/diagnostic.h @@ -250,6 +250,7 @@ typedef enum { PM_WARN_AMBIGUOUS_FIRST_ARGUMENT_PLUS, PM_WARN_AMBIGUOUS_PREFIX_STAR, PM_WARN_AMBIGUOUS_SLASH, + PM_WARN_END_IN_METHOD, /* This must be the last member. */ PM_DIAGNOSTIC_ID_LEN, diff --git a/prism/prism.c b/prism/prism.c index 21eb8f07fd..bf048f1883 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -14891,6 +14891,10 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power) { parser_lex(parser); pm_token_t keyword = parser->previous; + if (context_def_p(parser)) { + pm_parser_warn_token(parser, &keyword, PM_WARN_END_IN_METHOD); + } + expect1(parser, PM_TOKEN_BRACE_LEFT, PM_ERR_END_UPCASE_BRACE); pm_token_t opening = parser->previous; pm_statements_node_t *statements = parse_statements(parser, PM_CONTEXT_POSTEXE); diff --git a/test/prism/errors_test.rb b/test/prism/errors_test.rb index cf7330dfb2..9362032fe3 100644 --- a/test/prism/errors_test.rb +++ b/test/prism/errors_test.rb @@ -1708,6 +1708,12 @@ module Prism ] end + def test_upcase_end_in_def + assert_warning_messages "def foo; END { }; end", [ + "END in method; use at_exit" + ] + end + private def assert_errors(expected, source, errors, compare_ripper: RUBY_ENGINE == "ruby") @@ -1727,6 +1733,11 @@ module Prism assert_equal(errors, result.errors.map(&:message)) end + def assert_warning_messages(source, warnings) + result = Prism.parse(source) + assert_equal(warnings, result.warnings.map(&:message)) + end + def expression(source) Prism.parse(source).value.statements.body.last end |
