Skip to content

Commit 4e1f20f

Browse files
jhawthornmatzbot
authored andcommitted
[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 ruby/error_highlight@44920551dd
1 parent 4a1af72 commit 4e1f20f

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/error_highlight/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ def prism_spot_constant_path_operator_write
913913
# ^^^
914914
def prism_spot_def_for_name
915915
location = @node.name_loc
916-
location = location.join(@node.operator_loc) if @node.operator_loc
916+
location = @node.operator_loc.join(location) if @node.operator_loc
917917
prism_location(location)
918918
end
919919

test/error_highlight/test_error_highlight.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,62 @@ def test_spot_with_node
17331733
assert_equal expected_spot, actual_spot
17341734
end
17351735

1736+
module SingletonMethodWithSpacing
1737+
LINENO = __LINE__ + 1
1738+
def self . baz(x:)
1739+
x
1740+
end
1741+
end
1742+
1743+
def test_singleton_method_with_spacing_missing_keyword
1744+
lineno = __LINE__
1745+
assert_error_message(ArgumentError, <<~END) do
1746+
missing keyword: :x (ArgumentError)
1747+
1748+
caller: #{ __FILE__ }:#{ lineno + 16 }
1749+
| SingletonMethodWithSpacing.baz
1750+
^^^^
1751+
callee: #{ __FILE__ }:#{ SingletonMethodWithSpacing::LINENO }
1752+
#{
1753+
MethodDefLocationSupported ?
1754+
"| def self . baz(x:)
1755+
^^^^^" :
1756+
"(cannot highlight method definition; try Ruby 4.0 or later)"
1757+
}
1758+
END
1759+
1760+
SingletonMethodWithSpacing.baz
1761+
end
1762+
end
1763+
1764+
module SingletonMethodMultipleKwargs
1765+
LINENO = __LINE__ + 1
1766+
def self.run(shop_id:, param1:)
1767+
shop_id + param1
1768+
end
1769+
end
1770+
1771+
def test_singleton_method_multiple_missing_keywords
1772+
lineno = __LINE__
1773+
assert_error_message(ArgumentError, <<~END) do
1774+
missing keywords: :shop_id, :param1 (ArgumentError)
1775+
1776+
caller: #{ __FILE__ }:#{ lineno + 16 }
1777+
| SingletonMethodMultipleKwargs.run
1778+
^^^^
1779+
callee: #{ __FILE__ }:#{ SingletonMethodMultipleKwargs::LINENO }
1780+
#{
1781+
MethodDefLocationSupported ?
1782+
"| def self.run(shop_id:, param1:)
1783+
^^^^" :
1784+
"(cannot highlight method definition; try Ruby 4.0 or later)"
1785+
}
1786+
END
1787+
1788+
SingletonMethodMultipleKwargs.run
1789+
end
1790+
end
1791+
17361792
private
17371793

17381794
def find_node_by_id(node, node_id)

0 commit comments

Comments
 (0)