From 50f78dc6dd541193c1066649faefeccf674fcaee Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 11 Jan 2023 11:06:55 +0100 Subject: [PATCH] Get rid of anonymous eval calls Things declared in anonymous eval are always annoying to locate. (e.g. profling etc). --- lib/nokogiri/css/xpath_visitor.rb | 4 ++-- lib/nokogiri/xml/node/save_options.rb | 4 ++-- lib/nokogiri/xml/parse_options.rb | 4 ++-- lib/xsd/xmlparser/nokogiri.rb | 4 +++- test/xml/node/test_save_options.rb | 4 ++-- test/xml/node/test_subclass.rb | 8 ++++---- test/xml/test_parse_options.rb | 4 ++-- 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/nokogiri/css/xpath_visitor.rb b/lib/nokogiri/css/xpath_visitor.rb index a87a4fc13c3..c8d45cf9c72 100644 --- a/lib/nokogiri/css/xpath_visitor.rb +++ b/lib/nokogiri/css/xpath_visitor.rb @@ -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) diff --git a/lib/nokogiri/xml/node/save_options.rb b/lib/nokogiri/xml/node/save_options.rb index a856fda27ac..2d4a0c4c29e 100644 --- a/lib/nokogiri/xml/node/save_options.rb +++ b/lib/nokogiri/xml/node/save_options.rb @@ -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 @@ -58,7 +58,7 @@ def #{constant.downcase} def #{constant.downcase}? #{constant} & @options == #{constant} end - } + RUBY end alias_method :to_i, :options diff --git a/lib/nokogiri/xml/parse_options.rb b/lib/nokogiri/xml/parse_options.rb index ff25cc13d5d..ea687f61ceb 100644 --- a/lib/nokogiri/xml/parse_options.rb +++ b/lib/nokogiri/xml/parse_options.rb @@ -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 @@ -183,7 +183,7 @@ def no#{constant.downcase} def #{constant.downcase}? #{constant} & @options == #{constant} end - } + RUBY end def strict diff --git a/lib/xsd/xmlparser/nokogiri.rb b/lib/xsd/xmlparser/nokogiri.rb index f75f963ebe7..5dc4377e2ee 100644 --- a/lib/xsd/xmlparser/nokogiri.rb +++ b/lib/xsd/xmlparser/nokogiri.rb @@ -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) diff --git a/test/xml/node/test_save_options.rb b/test/xml/node/test_save_options.rb index d42e351cc7f..69aeb949c7c 100644 --- a/test/xml/node/test_save_options.rb +++ b/test/xml/node/test_save_options.rb @@ -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 diff --git a/test/xml/node/test_subclass.rb b/test/xml/node/test_subclass.rb index 235c255ae2f..2d04f11a92f 100644 --- a/test/xml/node/test_subclass.rb +++ b/test/xml/node/test_subclass.rb @@ -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 @@ -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 diff --git a/test/xml/test_parse_options.rb b/test/xml/test_parse_options.rb index 9acb06a39f5..d167ac3cf2b 100644 --- a/test/xml/test_parse_options.rb +++ b/test/xml/test_parse_options.rb @@ -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