diff options
| author | John Hawthorn <john@hawthorn.email> | 2025-11-19 13:08:26 -0800 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2025-11-19 22:27:28 +0000 |
| commit | 4e1f20fee6d97b6dc65e0d4eac1f9cc37312bd5f (patch) | |
| tree | e5449bf621a2a9f0e206c56d99f31fd71f0ef360 | |
| parent | 4a1af72a13d41dcc38af7d69ea1f44856265d43f (diff) | |
[ruby/error_highlight] Fix prism_spot_def_for_name for singletons
Previously calling a singleton method with invalid arguments would give:
RuntimeError: Incompatible locations
This is because `join` wants the operator to come before the location
https://github.com/ruby/error_highlight/commit/44920551dd
| -rw-r--r-- | lib/error_highlight/base.rb | 2 | ||||
| -rw-r--r-- | test/error_highlight/test_error_highlight.rb | 56 |
2 files changed, 57 insertions, 1 deletions
diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb index 22e0bf0dc3..bc4a62c9d6 100644 --- a/lib/error_highlight/base.rb +++ b/lib/error_highlight/base.rb @@ -913,7 +913,7 @@ module ErrorHighlight # ^^^ def prism_spot_def_for_name location = @node.name_loc - location = location.join(@node.operator_loc) if @node.operator_loc + location = @node.operator_loc.join(location) if @node.operator_loc prism_location(location) end diff --git a/test/error_highlight/test_error_highlight.rb b/test/error_highlight/test_error_highlight.rb index 1276a0a0d9..d3ca99021b 100644 --- a/test/error_highlight/test_error_highlight.rb +++ b/test/error_highlight/test_error_highlight.rb @@ -1733,6 +1733,62 @@ wrong number of arguments (given 1, expected 2) (ArgumentError) assert_equal expected_spot, actual_spot end + module SingletonMethodWithSpacing + LINENO = __LINE__ + 1 + def self . baz(x:) + x + end + end + + def test_singleton_method_with_spacing_missing_keyword + lineno = __LINE__ + assert_error_message(ArgumentError, <<~END) do +missing keyword: :x (ArgumentError) + + caller: #{ __FILE__ }:#{ lineno + 16 } + | SingletonMethodWithSpacing.baz + ^^^^ + callee: #{ __FILE__ }:#{ SingletonMethodWithSpacing::LINENO } + #{ + MethodDefLocationSupported ? + "| def self . baz(x:) + ^^^^^" : + "(cannot highlight method definition; try Ruby 4.0 or later)" + } + END + + SingletonMethodWithSpacing.baz + end + end + + module SingletonMethodMultipleKwargs + LINENO = __LINE__ + 1 + def self.run(shop_id:, param1:) + shop_id + param1 + end + end + + def test_singleton_method_multiple_missing_keywords + lineno = __LINE__ + assert_error_message(ArgumentError, <<~END) do +missing keywords: :shop_id, :param1 (ArgumentError) + + caller: #{ __FILE__ }:#{ lineno + 16 } + | SingletonMethodMultipleKwargs.run + ^^^^ + callee: #{ __FILE__ }:#{ SingletonMethodMultipleKwargs::LINENO } + #{ + MethodDefLocationSupported ? + "| def self.run(shop_id:, param1:) + ^^^^" : + "(cannot highlight method definition; try Ruby 4.0 or later)" + } + END + + SingletonMethodMultipleKwargs.run + end + end + private def find_node_by_id(node, node_id) |
