Skip to content

Commit

Permalink
Support :category keyword to Warning.warn on Ruby 3.0
Browse files Browse the repository at this point in the history
This accepts but ignores a category keyword.  If a category keyword
is passed and non-nil, it includes it when calling super. Otherwise,
it only calls super with the warning string.
  • Loading branch information
jeremyevans committed Feb 14, 2021
1 parent 42475fc commit 22444e0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Support :category keyword to Warning.warn on Ruby 3.0 (jeremyevans)

* Fix :taint warning handling on Ruby 3.0 (jeremyevans)

* Fix :ambiguous_slash warning handling on Ruby 3.0 (jeremyevans)
Expand Down
84 changes: 45 additions & 39 deletions lib/warning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,57 +166,63 @@ def process(path='', actions=nil, &block)
nil
end

# Handle ignored warnings and warning processors. If the warning is
# not ignored, is not a duplicate warning (if checking for duplicates)
# and there is no warning processor setup for the warning
# string, then use the default behavior of writing to $stderr.
def warn(str)
synchronize{@ignore.dup}.each do |path, regexp|
if str.start_with?(path) && str =~ regexp
return
end
end

if @dedup
if synchronize{@dedup[str]}
return
if RUBY_VERSION >= '3.0'
method_args = ', category: nil'
super_ = "category ? super : super(str)"
else
super_ = "super"
end

class_eval(<<-END, __FILE__, __LINE__+1)
def warn(str#{method_args})
synchronize{@ignore.dup}.each do |path, regexp|
if str.start_with?(path) && str =~ regexp
return
end
end
synchronize{@dedup[str] = true}
end
if @dedup
if synchronize{@dedup[str]}
return
end
synchronize{@dedup[str] = true}
end
action = catch(:action) do
synchronize{@process.dup}.each do |path, block|
if str.start_with?(path)
if block.is_a?(Hash)
block.each do |regexp, blk|
if str =~ regexp
throw :action, blk.call(str)
action = catch(:action) do
synchronize{@process.dup}.each do |path, block|
if str.start_with?(path)
if block.is_a?(Hash)
block.each do |regexp, blk|
if str =~ regexp
throw :action, blk.call(str)
end
end
else
throw :action, block.call(str)
end
else
throw :action, block.call(str)
end
end
:default
end
:default
end
case action
when :default
#{super_}
when :backtrace
#{super_}
$stderr.puts caller
when :raise
raise str
else
# nothing
end
case action
when :default
super
when :backtrace
super
$stderr.puts caller
when :raise
raise str
else
# nothing
nil
end

nil
end
END

private

Expand Down
8 changes: 8 additions & 0 deletions test/test_warning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,12 @@ def test_warning_process_no_action_and_no_block
Warning.process(__FILE__, :missing_ivar=>:default){}
end
end

if RUBY_VERSION >= '3.0'
def test_warning_warn_category_keyword
assert_warning('foo') do
Warning.warn("foo", category: :deprecated)
end
end
end
end

0 comments on commit 22444e0

Please sign in to comment.