File tree Expand file tree Collapse file tree 2 files changed +19
-4
lines changed
lib/active_support/deprecation Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Original file line number Diff line number Diff line change @@ -142,15 +142,17 @@ def extract_callstack(callstack)
142
142
return _extract_callstack ( callstack ) if callstack . first . is_a? String
143
143
144
144
offending_line = callstack . find { |frame |
145
- frame . absolute_path && !ignored_callstack ( frame . absolute_path )
145
+ # Code generated with `eval` doesn't have an `absolute_path`, e.g. templates.
146
+ path = frame . absolute_path || frame . path
147
+ path && !ignored_callstack? ( path )
146
148
} || callstack . first
147
149
148
150
[ offending_line . path , offending_line . lineno , offending_line . label ]
149
151
end
150
152
151
153
def _extract_callstack ( callstack )
152
154
warn "Please pass `caller_locations` to the deprecation API" if $VERBOSE
153
- offending_line = callstack . find { |line | !ignored_callstack ( line ) } || callstack . first
155
+ offending_line = callstack . find { |line | !ignored_callstack? ( line ) } || callstack . first
154
156
155
157
if offending_line
156
158
if md = offending_line . match ( /^(.+?):(\d +)(?::in `(.*?)')?/ )
@@ -162,9 +164,10 @@ def _extract_callstack(callstack)
162
164
end
163
165
164
166
RAILS_GEM_ROOT = File . expand_path ( "../../../.." , __dir__ ) + "/"
167
+ LIB_DIR = RbConfig ::CONFIG [ "libdir" ]
165
168
166
- def ignored_callstack ( path )
167
- path . start_with? ( RAILS_GEM_ROOT ) || path . start_with? ( RbConfig :: CONFIG [ "rubylibdir" ] )
169
+ def ignored_callstack? ( path )
170
+ path . start_with? ( RAILS_GEM_ROOT , LIB_DIR )
168
171
end
169
172
end
170
173
end
Original file line number Diff line number Diff line change @@ -951,6 +951,18 @@ def method
951
951
assert_equal __LINE__ - 2 , @callstack . first . lineno
952
952
end
953
953
954
+ class_eval ( <<~RUBY , "/path/to/template.html.erb" , 1 )
955
+ def generated_method_that_call_deprecation(deprecator)
956
+ deprecator.warn("Here", caller_locations(0, 10))
957
+ end
958
+ RUBY
959
+
960
+ test "warn deprecation can blame code generated with eval" do
961
+ @deprecator . behavior = -> ( message , *) { @message = message }
962
+ generated_method_that_call_deprecation ( @deprecator )
963
+ assert_equal "DEPRECATION WARNING: Here (called from generated_method_that_call_deprecation at /path/to/template.html.erb:2)" , @message
964
+ end
965
+
954
966
private
955
967
def method_that_emits_deprecation ( deprecator )
956
968
deprecator . warn
You can’t perform that action at this time.
0 commit comments