Skip to content

Commit

Permalink
Merge pull request #2757 from casperisfine/fix-anonymous-eval
Browse files Browse the repository at this point in the history
Get rid of anonymous eval calls
  • Loading branch information
flavorjones committed Jan 11, 2023
2 parents d3add70 + 50f78dc commit 7c4c54f
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/nokogiri/css/xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ def visit_combinator(node)
"descendant_selector" => "//",
"child_selector" => "/",
}.each do |k, v|
class_eval %{
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def visit_#{k} node
"\#{node.value.first.accept(self) if node.value.first}#{v}\#{node.value.last.accept(self)}"
end
}
RUBY
end

def visit_conditional_selector(node)
Expand Down
4 changes: 2 additions & 2 deletions lib/nokogiri/xml/node/save_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def initialize(options = 0)
end

constants.each do |constant|
class_eval %{
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{constant.downcase}
@options |= #{constant}
self
Expand All @@ -58,7 +58,7 @@ def #{constant.downcase}
def #{constant.downcase}?
#{constant} & @options == #{constant}
end
}
RUBY
end

alias_method :to_i, :options
Expand Down
4 changes: 2 additions & 2 deletions lib/nokogiri/xml/parse_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def initialize(options = STRICT)
constants.each do |constant|
next if constant.to_sym == :STRICT

class_eval %{
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{constant.downcase}
@options |= #{constant}
self
Expand All @@ -183,7 +183,7 @@ def no#{constant.downcase}
def #{constant.downcase}?
#{constant} & @options == #{constant}
end
}
RUBY
end

def strict
Expand Down
4 changes: 3 additions & 1 deletion lib/xsd/xmlparser/nokogiri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def end_element_namespace(name, prefix = nil, uri = nil)
end

["xmldecl", "start_document", "end_document", "comment"].each do |name|
class_eval %{ def #{name}(*args); end }
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{name}(*args); end
RUBY
end

add_factory(self)
Expand Down
4 changes: 2 additions & 2 deletions test/xml/node/test_save_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ module XML
class Node
class TestSaveOptions < Nokogiri::TestCase
SaveOptions.constants.each do |constant|
class_eval <<-EOEVAL
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def test_predicate_#{constant.downcase}
options = SaveOptions.new(SaveOptions::#{constant})
assert options.#{constant.downcase}?
assert SaveOptions.new.#{constant.downcase}.#{constant.downcase}?
end
EOEVAL
RUBY
end

def test_default_xml_save_options
Expand Down
8 changes: 4 additions & 4 deletions test/xml/node/test_subclass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ class TestSubclass < Nokogiri::TestCase
Nokogiri::XML::Node => '"foo", doc',
Nokogiri::XML::Text => '"foo", doc',
}.each do |klass, constructor|
class_eval %{
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def test_subclass_#{klass.name.gsub("::", "_")}
doc = Nokogiri::XML::Document.new
klass = Class.new(#{klass.name})
node = klass.new(#{constructor})
assert_instance_of klass, node
end
}
RUBY

class_eval <<-eocode, __FILE__, __LINE__ + 1
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def test_subclass_initialize_#{klass.name.gsub("::", "_")}
doc = Nokogiri::XML::Document.new
klass = Class.new(#{klass.name}) do
Expand All @@ -38,7 +38,7 @@ def initialize *args
node = klass.new(#{constructor}, 1)
assert_equal [#{constructor}, 1], node.initialized_with
end
eocode
RUBY
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/xml/test_parse_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def test_to_i
ParseOptions.constants.each do |constant|
next if constant == "STRICT"

class_eval %{
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def test_predicate_#{constant.downcase}
options = ParseOptions.new(ParseOptions::#{constant})
assert options.#{constant.downcase}?
assert ParseOptions.new.#{constant.downcase}.#{constant.downcase}?
end
}
RUBY
end

def test_strict_noent
Expand Down

0 comments on commit 7c4c54f

Please sign in to comment.