Skip to content

Commit 5902abe

Browse files
committed
Better way to find deprecated method location
Instead of relying on hard coded indexes, we can look through the called method stack backwards (i.e. the first method called will be the first in the list). If it has a suffix for one of our APIs, then we check to see if there exists a `public_` prefix on the view, if it does it's the first method called that can be deprecated so we point to that.
1 parent 03533fe commit 5902abe

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

lib/sprockets/rails/helper.rb

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,23 @@ def deprecate_invalid_asset_lookup(name, call_stack)
284284
message << "The public fallback behavior is being deprecated and will be removed.\n"
285285

286286
append = nil
287-
# Search for the most likely top level method where deprecation occured.
288-
# Hash contains, in order, the suffix we're looking for, and where it would appear
289-
# in the call stack.
290-
# This is needed because every deprecated method eventually calls `asset_path`.
291-
{ "_url" => [0, 1] , "_tag" => [1, 2], "_path" => [0] }.detect do |suffix, positions|
292-
293-
positions.detect do |position|
294-
public_method_name = "public_" + extract_method_from_call_frame(call_stack[position])
295-
296-
if public_method_name.end_with?(suffix) && respond_to?(public_method_name)
297-
position.times { call_stack.shift }
298-
append = "please use the `public_*` helper instead. For example `#{ public_method_name }`.\n"
299-
else
300-
false
301-
end
287+
288+
# We need to figure out the actually deprecated method. This is complicated since
289+
# the deprecation always comes from `asset_path` but that might not be the API that
290+
# is being used. To solve this we look at the call stack methods backwards and figure out the first
291+
# `*_url`, `*_tag` or `*_path` method that is called. If that method also has a `public_*`
292+
# counterpart it is the first method that was called that could be deprecated.
293+
call_stack.reverse_each.detect.with_index do |call_frame, index|
294+
method_name = extract_method_from_call_frame(call_frame)
295+
next if !method_name.end_with?("_url".freeze) &&
296+
!method_name.end_with?("_tag".freeze) &&
297+
!method_name.end_with?("_path".freeze)
298+
299+
if self.respond_to?("public_#{ method_name }")
300+
append = "please use the `public_*` helper instead. For example `#{ "public_#{ method_name }" }`.\n"
301+
call_stack.shift(call_stack.length - index - 1)
302302
end
303303
end
304-
305304
append ||= "please use the `public_*` helper instead for example `public_asset_path`.\n"
306305
message << append
307306

0 commit comments

Comments
 (0)