From 2b3ea25bf052c1be731cf871f14ad15f1571d0b9 Mon Sep 17 00:00:00 2001 From: Awesome Code Date: Sun, 17 Jan 2021 05:49:45 +0000 Subject: [PATCH 1/6] Auto corrected by following Lint Ruby Style/RaiseArgs --- lib/synvert/core/node_ext.rb | 40 +++++++++++++-------------- lib/synvert/core/rewriter.rb | 4 +-- lib/synvert/core/rewriter/gem_spec.rb | 2 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/synvert/core/node_ext.rb b/lib/synvert/core/node_ext.rb index 046adbfb..a148b986 100644 --- a/lib/synvert/core/node_ext.rb +++ b/lib/synvert/core/node_ext.rb @@ -39,7 +39,7 @@ def name when :mlhs self else - raise Synvert::Core::MethodNotSupported.new "name is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "name is not handled for #{self.debug_info}" end end @@ -51,7 +51,7 @@ def parent_class if :class == self.type self.children[1] else - raise Synvert::Core::MethodNotSupported.new "parent_class is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "parent_class is not handled for #{self.debug_info}" end end @@ -63,7 +63,7 @@ def parent_const if :const == self.type self.children[0] else - raise Synvert::Core::MethodNotSupported.new "parent_const is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "parent_const is not handled for #{self.debug_info}" end end @@ -75,7 +75,7 @@ def receiver if :send == self.type self.children[0] else - raise Synvert::Core::MethodNotSupported.new "receiver is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "receiver is not handled for #{self.debug_info}" end end @@ -90,7 +90,7 @@ def message when :send self.children[1] else - raise Synvert::Core::MethodNotSupported.new "message is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "message is not handled for #{self.debug_info}" end end @@ -109,7 +109,7 @@ def arguments when :defined? self.children else - raise Synvert::Core::MethodNotSupported.new "arguments is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "arguments is not handled for #{self.debug_info}" end end @@ -121,7 +121,7 @@ def caller if :block == self.type self.children[0] else - raise Synvert::Core::MethodNotSupported.new "caller is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "caller is not handled for #{self.debug_info}" end end @@ -142,7 +142,7 @@ def body :begin == self.children[3].type ? self.children[3].body : self.children[3..-1] else - raise Synvert::Core::MethodNotSupported.new "body is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "body is not handled for #{self.debug_info}" end end @@ -154,7 +154,7 @@ def condition if :if == self.type self.children[0] else - raise Synvert::Core::MethodNotSupported.new "condition is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "condition is not handled for #{self.debug_info}" end end @@ -166,7 +166,7 @@ def keys if :hash == self.type self.children.map { |child| child.children[0] } else - raise Synvert::Core::MethodNotSupported.new "keys is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "keys is not handled for #{self.debug_info}" end end @@ -178,7 +178,7 @@ def values if :hash == self.type self.children.map { |child| child.children[1] } else - raise Synvert::Core::MethodNotSupported.new "keys is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "keys is not handled for #{self.debug_info}" end end @@ -191,7 +191,7 @@ def has_key?(key) if :hash == self.type self.children.any? { |pair_node| pair_node.key.to_value == key } else - raise Synvert::Core::MethodNotSupported.new "has_key? is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "has_key? is not handled for #{self.debug_info}" end end @@ -205,7 +205,7 @@ def hash_value(key) value_node = self.children.find { |pair_node| pair_node.key.to_value == key } value_node ? value_node.value : nil else - raise Synvert::Core::MethodNotSupported.new "has_key? is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "has_key? is not handled for #{self.debug_info}" end end @@ -217,7 +217,7 @@ def key if :pair == self.type self.children.first else - raise Synvert::Core::MethodNotSupported.new "key is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "key is not handled for #{self.debug_info}" end end @@ -229,7 +229,7 @@ def value if :pair == self.type self.children.last else - raise Synvert::Core::MethodNotSupported.new "value is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "value is not handled for #{self.debug_info}" end end @@ -241,7 +241,7 @@ def left_value if %i[masgn lvasgn ivasgn].include? self.type self.children[0] else - raise Synvert::Core::MethodNotSupported.new "left_value is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "left_value is not handled for #{self.debug_info}" end end @@ -253,7 +253,7 @@ def right_value if %i[masgn lvasgn ivasgn].include? self.type self.children[1] else - raise Synvert::Core::MethodNotSupported.new "right_value is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{self.debug_info}" end end @@ -276,7 +276,7 @@ def to_value when :begin self.children.first.to_value else - raise Synvert::Core::MethodNotSupported.new "to_value is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "to_value is not handled for #{self.debug_info}" end end @@ -389,7 +389,7 @@ def rewritten_source(code) when NilClass 'nil' else - raise Synvert::Core::MethodNotSupported.new "rewritten_source is not handled for #{evaluated.inspect}" + raise Synvert::Core::MethodNotSupported, "rewritten_source is not handled for #{evaluated.inspect}" end else "{{#{old_code}}}" @@ -446,7 +446,7 @@ def match_value?(actual, expected) when Parser::AST::Node actual == expected else - raise Synvert::Core::MethodNotSupported.new "#{expected.class} is not handled for match_value?" + raise Synvert::Core::MethodNotSupported, "#{expected.class} is not handled for match_value?" end end diff --git a/lib/synvert/core/rewriter.rb b/lib/synvert/core/rewriter.rb index f436d843..43760511 100644 --- a/lib/synvert/core/rewriter.rb +++ b/lib/synvert/core/rewriter.rb @@ -65,7 +65,7 @@ def fetch(group, name) if exist? group, name rewriters[group][name] else - raise RewriterNotFound.new "Rewriter #{group} #{name} not found" + raise RewriterNotFound, "Rewriter #{group} #{name} not found" end end @@ -82,7 +82,7 @@ def call(group, name) rewriter.process rewriter else - raise RewriterNotFound.new "Rewriter #{group}/#{name} not found" + raise RewriterNotFound, "Rewriter #{group}/#{name} not found" end end diff --git a/lib/synvert/core/rewriter/gem_spec.rb b/lib/synvert/core/rewriter/gem_spec.rb index 6d5ad45c..469e7d7d 100644 --- a/lib/synvert/core/rewriter/gem_spec.rb +++ b/lib/synvert/core/rewriter/gem_spec.rb @@ -35,7 +35,7 @@ def match? false end else - raise GemfileLockNotFound.new 'Gemfile.lock does not exist' + raise GemfileLockNotFound, 'Gemfile.lock does not exist' end end end From 379cdce49773028bcbb6f8a07b56be16998092de Mon Sep 17 00:00:00 2001 From: Awesome Code Date: Sun, 17 Jan 2021 05:50:02 +0000 Subject: [PATCH 2/6] Auto corrected by following Lint Ruby Style/SafeNavigation --- lib/synvert/core/node_ext.rb | 4 +--- lib/synvert/core/rewriter/condition/if_exist_condition.rb | 2 +- lib/synvert/core/rewriter/condition/unless_exist_condition.rb | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/synvert/core/node_ext.rb b/lib/synvert/core/node_ext.rb index 046adbfb..07101a93 100644 --- a/lib/synvert/core/node_ext.rb +++ b/lib/synvert/core/node_ext.rb @@ -299,9 +299,7 @@ def debug_info # # @return [String] source code. def to_source - if self.loc.expression - self.loc.expression.source - end + self.loc.expression&.source end # Get the indent of current node. diff --git a/lib/synvert/core/rewriter/condition/if_exist_condition.rb b/lib/synvert/core/rewriter/condition/if_exist_condition.rb index 16d74ec3..836e7238 100644 --- a/lib/synvert/core/rewriter/condition/if_exist_condition.rb +++ b/lib/synvert/core/rewriter/condition/if_exist_condition.rb @@ -7,7 +7,7 @@ class Rewriter::IfExistCondition < Rewriter::Condition def match? match = false @instance.current_node.recursive_children do |child_node| - match = match || (child_node && child_node.match?(@rules)) + match = match || (child_node&.match?(@rules)) end match end diff --git a/lib/synvert/core/rewriter/condition/unless_exist_condition.rb b/lib/synvert/core/rewriter/condition/unless_exist_condition.rb index 3dc27eac..ef3e0ee7 100644 --- a/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +++ b/lib/synvert/core/rewriter/condition/unless_exist_condition.rb @@ -7,7 +7,7 @@ class Rewriter::UnlessExistCondition < Rewriter::Condition def match? match = false @instance.current_node.recursive_children do |child_node| - match = match || (child_node && child_node.match?(@rules)) + match = match || (child_node&.match?(@rules)) end !match end From 5e3d01d2c7d70ff29fe41797f192e7f309cd1e1a Mon Sep 17 00:00:00 2001 From: Richard Huang Date: Sun, 17 Jan 2021 16:55:28 +0800 Subject: [PATCH 3/6] within_scope supports array of nodes --- .../core/rewriter/scope/within_scope.rb | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/synvert/core/rewriter/scope/within_scope.rb b/lib/synvert/core/rewriter/scope/within_scope.rb index 229fe564..cf238548 100644 --- a/lib/synvert/core/rewriter/scope/within_scope.rb +++ b/lib/synvert/core/rewriter/scope/within_scope.rb @@ -22,23 +22,20 @@ def process current_node = @instance.current_node return unless current_node - @instance.process_with_node current_node do - matching_nodes = [] - matching_nodes << current_node if current_node.match? @rules - if @options[:recursive] - current_node.recursive_children do |child_node| - matching_nodes << child_node if child_node.match? @rules - end - else - current_node.children do |child_node| - matching_nodes << child_node if child_node.match? @rules - end - end - matching_nodes.each do |matching_node| - @instance.process_with_node matching_node do + child_nodes = current_node.is_a?(Parser::AST::Node) ? current_node.children : current_node + process_with_nodes(child_nodes) + end + + private + + def process_with_nodes(nodes) + nodes.compact.select { |node| node.is_a?(Parser::AST::Node) }.each do |node| + if node.match?(@rules) + @instance.process_with_node(node) do @instance.instance_eval &@block end end + process_with_nodes(node.children) if @options[:recursive] end end end From 0893b59e4a2e5b94716d0a8e7269688b45f36f20 Mon Sep 17 00:00:00 2001 From: Awesome Code Date: Sun, 17 Jan 2021 09:00:17 +0000 Subject: [PATCH 4/6] Auto corrected by following Lint Ruby Style/RedundantSelf --- lib/synvert/core/node_ext.rb | 154 +++++++++++++------------- lib/synvert/core/rewriter.rb | 4 +- lib/synvert/core/rewriter/instance.rb | 6 +- 3 files changed, 82 insertions(+), 82 deletions(-) diff --git a/lib/synvert/core/node_ext.rb b/lib/synvert/core/node_ext.rb index f2c89516..05e15643 100644 --- a/lib/synvert/core/node_ext.rb +++ b/lib/synvert/core/node_ext.rb @@ -31,15 +31,15 @@ class Node # @return [Parser::AST::Node] name node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def name - case self.type + case type when :class, :module, :def, :arg, :blockarg, :restarg - self.children[0] + children[0] when :defs, :const - self.children[1] + children[1] when :mlhs self else - raise Synvert::Core::MethodNotSupported, "name is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "name is not handled for #{debug_info}" end end @@ -48,10 +48,10 @@ def name # @return [Parser::AST::Node] parent_class node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def parent_class - if :class == self.type - self.children[1] + if :class == type + children[1] else - raise Synvert::Core::MethodNotSupported, "parent_class is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "parent_class is not handled for #{debug_info}" end end @@ -60,10 +60,10 @@ def parent_class # @return [Parser::AST::Node] parent const node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def parent_const - if :const == self.type - self.children[0] + if :const == type + children[0] else - raise Synvert::Core::MethodNotSupported, "parent_const is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "parent_const is not handled for #{debug_info}" end end @@ -72,10 +72,10 @@ def parent_const # @return [Parser::AST::Node] receiver node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def receiver - if :send == self.type - self.children[0] + if :send == type + children[0] else - raise Synvert::Core::MethodNotSupported, "receiver is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "receiver is not handled for #{debug_info}" end end @@ -84,13 +84,13 @@ def receiver # @return [Parser::AST::Node] mesage node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def message - case self.type + case type when :super, :zsuper :super when :send - self.children[1] + children[1] else - raise Synvert::Core::MethodNotSupported, "message is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "message is not handled for #{debug_info}" end end @@ -99,17 +99,17 @@ def message # @return [Array] arguments node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def arguments - case self.type + case type when :def, :block - ArgumentsNode.new self.children[1] + ArgumentsNode.new children[1] when :defs - ArgumentsNode.new self.children[2] + ArgumentsNode.new children[2] when :send - self.children[2..-1] + children[2..-1] when :defined? - self.children + children else - raise Synvert::Core::MethodNotSupported, "arguments is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "arguments is not handled for #{debug_info}" end end @@ -118,10 +118,10 @@ def arguments # @return [Parser::AST::Node] caller node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def caller - if :block == self.type - self.children[0] + if :block == type + children[0] else - raise Synvert::Core::MethodNotSupported, "caller is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "caller is not handled for #{debug_info}" end end @@ -130,19 +130,19 @@ def caller # @return [Array] body node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def body - case self.type + case type when :begin - self.children + children when :def, :block - return [] if self.children[2].nil? + return [] if children[2].nil? - :begin == self.children[2].type ? self.children[2].body : self.children[2..-1] + :begin == children[2].type ? children[2].body : children[2..-1] when :defs - return [] if self.children[3].nil? + return [] if children[3].nil? - :begin == self.children[3].type ? self.children[3].body : self.children[3..-1] + :begin == children[3].type ? children[3].body : children[3..-1] else - raise Synvert::Core::MethodNotSupported, "body is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "body is not handled for #{debug_info}" end end @@ -151,10 +151,10 @@ def body # @return [Parser::AST::Node] condition node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def condition - if :if == self.type - self.children[0] + if :if == type + children[0] else - raise Synvert::Core::MethodNotSupported, "condition is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "condition is not handled for #{debug_info}" end end @@ -163,10 +163,10 @@ def condition # @return [Array] keys node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def keys - if :hash == self.type - self.children.map { |child| child.children[0] } + if :hash == type + children.map { |child| child.children[0] } else - raise Synvert::Core::MethodNotSupported, "keys is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "keys is not handled for #{debug_info}" end end @@ -175,10 +175,10 @@ def keys # @return [Array] values node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def values - if :hash == self.type - self.children.map { |child| child.children[1] } + if :hash == type + children.map { |child| child.children[1] } else - raise Synvert::Core::MethodNotSupported, "keys is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "keys is not handled for #{debug_info}" end end @@ -188,10 +188,10 @@ def values # @return [Boolean] true if specified key exists. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def has_key?(key) - if :hash == self.type - self.children.any? { |pair_node| pair_node.key.to_value == key } + if :hash == type + children.any? { |pair_node| pair_node.key.to_value == key } else - raise Synvert::Core::MethodNotSupported, "has_key? is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "has_key? is not handled for #{debug_info}" end end @@ -201,11 +201,11 @@ def has_key?(key) # @return [Parser::AST::Node] value node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def hash_value(key) - if :hash == self.type - value_node = self.children.find { |pair_node| pair_node.key.to_value == key } + if :hash == type + value_node = children.find { |pair_node| pair_node.key.to_value == key } value_node ? value_node.value : nil else - raise Synvert::Core::MethodNotSupported, "has_key? is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "has_key? is not handled for #{debug_info}" end end @@ -214,10 +214,10 @@ def hash_value(key) # @return [Parser::AST::Node] key node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def key - if :pair == self.type - self.children.first + if :pair == type + children.first else - raise Synvert::Core::MethodNotSupported, "key is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "key is not handled for #{debug_info}" end end @@ -226,10 +226,10 @@ def key # @return [Parser::AST::Node] value node. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def value - if :pair == self.type - self.children.last + if :pair == type + children.last else - raise Synvert::Core::MethodNotSupported, "value is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "value is not handled for #{debug_info}" end end @@ -238,10 +238,10 @@ def value # @return [Parser::AST::Node] variable nodes. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def left_value - if %i[masgn lvasgn ivasgn].include? self.type - self.children[0] + if %i[masgn lvasgn ivasgn].include? type + children[0] else - raise Synvert::Core::MethodNotSupported, "left_value is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "left_value is not handled for #{debug_info}" end end @@ -250,10 +250,10 @@ def left_value # @return [Array] variable nodes. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def right_value - if %i[masgn lvasgn ivasgn].include? self.type - self.children[1] + if %i[masgn lvasgn ivasgn].include? type + children[1] else - raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "right_value is not handled for #{debug_info}" end end @@ -262,36 +262,36 @@ def right_value # @return [Object] exact value. # @raise [Synvert::Core::MethodNotSupported] if calls on other node. def to_value - case self.type + case type when :int, :str, :sym - self.children.last + children.last when :true true when :false false when :array - self.children.map(&:to_value) + children.map(&:to_value) when :irange - (self.children.first.to_value..self.children.last.to_value) + (children.first.to_value..children.last.to_value) when :begin - self.children.first.to_value + children.first.to_value else - raise Synvert::Core::MethodNotSupported, "to_value is not handled for #{self.debug_info}" + raise Synvert::Core::MethodNotSupported, "to_value is not handled for #{debug_info}" end end def to_s - if :mlhs == self.type - "(#{self.children.map(&:name).join(', ')})" + if :mlhs == type + "(#{children.map(&:name).join(', ')})" end end def debug_info "\n" + [ - "file: #{self.loc.expression.source_buffer.name}", - "line: #{self.loc.expression.line}", - "source: #{self.to_source}", - "node: #{self.inspect}" + "file: #{loc.expression.source_buffer.name}", + "line: #{loc.expression.line}", + "source: #{to_source}", + "node: #{inspect}" ].join("\n") end @@ -299,21 +299,21 @@ def debug_info # # @return [String] source code. def to_source - self.loc.expression&.source + loc.expression&.source end # Get the indent of current node. # # @return [Integer] indent. def indent - self.loc.expression.column + loc.expression.column end # Get the line of current node. # # @return [Integer] line. def line - self.loc.expression.line + loc.expression.line end # Recursively iterate all child nodes of current node. @@ -321,7 +321,7 @@ def line # @yield [child] Gives a child node. # @yieldparam child [Parser::AST::Node] child node def recursive_children - self.children.each do |child| + children.each do |child| if Parser::AST::Node === child yield child child.recursive_children { |c| yield c } @@ -361,8 +361,8 @@ def match?(rules) def rewritten_source(code) code.gsub(/{{(.*?)}}/m) do old_code = $1 - if self.respond_to? old_code.split(/\.|\[/).first - evaluated = self.instance_eval old_code + if respond_to? old_code.split(/\.|\[/).first + evaluated = instance_eval old_code case evaluated when Parser::AST::Node evaluated.loc.expression.source diff --git a/lib/synvert/core/rewriter.rb b/lib/synvert/core/rewriter.rb index 43760511..a58829fd 100644 --- a/lib/synvert/core/rewriter.rb +++ b/lib/synvert/core/rewriter.rb @@ -151,14 +151,14 @@ def initialize(group, name, &block) # Process the rewriter. # It will call the block. def process - self.instance_eval &@block + instance_eval &@block end # Process rewriter with sandbox mode. # It will call the block but doesn't change any file. def process_with_sandbox @sandbox = true - self.process + process @sandbox = false end diff --git a/lib/synvert/core/rewriter/instance.rb b/lib/synvert/core/rewriter/instance.rb index e9c71672..438fa338 100644 --- a/lib/synvert/core/rewriter/instance.rb +++ b/lib/synvert/core/rewriter/instance.rb @@ -79,7 +79,7 @@ def initialize(rewriter, file_pattern, options={}, &block) @file_pattern = file_pattern @options = DEFAULT_OPTIONS.merge(options) @block = block - rewriter.helpers.each { |helper| self.singleton_class.send(:define_method, helper[:name], &helper[:block]) } + rewriter.helpers.each { |helper| singleton_class.send(:define_method, helper[:name], &helper[:block]) } end # Process the instance. @@ -96,7 +96,7 @@ def process @current_file = file_path - self.process_with_node ast do + process_with_node ast do begin instance_eval &@block rescue NoMethodError @@ -146,7 +146,7 @@ def process_with_node(node) # @param node [Parser::AST::Node] node set to current_node # @yield process def process_with_other_node(node) - original_node = self.current_node + original_node = current_node self.current_node = node yield self.current_node = original_node From f5e4bf6f115dc6d55b6e910b35e1f033c8591612 Mon Sep 17 00:00:00 2001 From: Awesome Code Date: Sun, 17 Jan 2021 09:00:25 +0000 Subject: [PATCH 5/6] Auto corrected by following Lint Ruby Style/SelfAssignment --- lib/synvert/core/rewriter/condition/if_exist_condition.rb | 2 +- lib/synvert/core/rewriter/condition/unless_exist_condition.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/synvert/core/rewriter/condition/if_exist_condition.rb b/lib/synvert/core/rewriter/condition/if_exist_condition.rb index 836e7238..d3b8dfc4 100644 --- a/lib/synvert/core/rewriter/condition/if_exist_condition.rb +++ b/lib/synvert/core/rewriter/condition/if_exist_condition.rb @@ -7,7 +7,7 @@ class Rewriter::IfExistCondition < Rewriter::Condition def match? match = false @instance.current_node.recursive_children do |child_node| - match = match || (child_node&.match?(@rules)) + match ||= (child_node&.match?(@rules)) end match end diff --git a/lib/synvert/core/rewriter/condition/unless_exist_condition.rb b/lib/synvert/core/rewriter/condition/unless_exist_condition.rb index ef3e0ee7..a66105ed 100644 --- a/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +++ b/lib/synvert/core/rewriter/condition/unless_exist_condition.rb @@ -7,7 +7,7 @@ class Rewriter::UnlessExistCondition < Rewriter::Condition def match? match = false @instance.current_node.recursive_children do |child_node| - match = match || (child_node&.match?(@rules)) + match ||= (child_node&.match?(@rules)) end !match end From 8eca7c88897721368961f9ab2c0182415ccb0a2f Mon Sep 17 00:00:00 2001 From: Awesome Code Date: Sun, 17 Jan 2021 09:05:53 +0000 Subject: [PATCH 6/6] Auto corrected by following Lint Ruby Style/PerlBackrefs --- lib/synvert/core/engine/erb.rb | 14 +++++++------- lib/synvert/core/node_ext.rb | 2 +- lib/synvert/core/rewriter/helper.rb | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/synvert/core/engine/erb.rb b/lib/synvert/core/engine/erb.rb index 34dcadf7..64136cc3 100644 --- a/lib/synvert/core/engine/erb.rb +++ b/lib/synvert/core/engine/erb.rb @@ -31,18 +31,18 @@ def decode(source) private def decode_ruby_stmt(source) - source.gsub(/#{ERUBY_STMT_SPLITTER}(.+?)#{ERUBY_STMT_SPLITTER}/m) { "<%#{$1}%>" } + source.gsub(/#{ERUBY_STMT_SPLITTER}(.+?)#{ERUBY_STMT_SPLITTER}/m) { "<%#{Regexp.last_match(1)}%>" } end def decode_ruby_output(source) - source.gsub(/@output_buffer.append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m) { "<%=#{$1}%>" } + source.gsub(/@output_buffer.append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m) { "<%=#{Regexp.last_match(1)}%>" } .gsub(/@output_buffer.append= (.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/m) { |m| "<%=#{m.sub("@output_buffer.append= ", "").sub(ERUBY_EXPR_SPLITTER, "")}%>" } end def decode_html_output(source) - source.gsub(/@output_buffer.safe_append='(.+?)'.freeze;/m) { reverse_escape_text($1) } - .gsub(/@output_buffer.safe_append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m) { reverse_escape_text($1) } - .gsub(/@output_buffer.safe_append=(.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/m) { reverse_escape_text($1) } + source.gsub(/@output_buffer.safe_append='(.+?)'.freeze;/m) { reverse_escape_text(Regexp.last_match(1)) } + .gsub(/@output_buffer.safe_append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m) { reverse_escape_text(Regexp.last_match(1)) } + .gsub(/@output_buffer.safe_append=(.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/m) { reverse_escape_text(Regexp.last_match(1)) } end def remove_erubis_buf(source) @@ -112,9 +112,9 @@ def add_stmt(src, code) flush_newline_if_pending(src) if code != "\n" && code != "" index = if code =~ /\A(\s*)\r?\n/ - $1.length + Regexp.last_match(1).length elsif code =~ /\A(\s+)/ - $1.end_with?(' ') ? $1.length - 1 : $1.length + Regexp.last_match(1).end_with?(' ') ? Regexp.last_match(1).length - 1 : Regexp.last_match(1).length else 0 end diff --git a/lib/synvert/core/node_ext.rb b/lib/synvert/core/node_ext.rb index 05e15643..28b6d0b8 100644 --- a/lib/synvert/core/node_ext.rb +++ b/lib/synvert/core/node_ext.rb @@ -360,7 +360,7 @@ def match?(rules) # @raise [Synvert::Core::MethodNotSupported] if string in block {{ }} does not support. def rewritten_source(code) code.gsub(/{{(.*?)}}/m) do - old_code = $1 + old_code = Regexp.last_match(1) if respond_to? old_code.split(/\.|\[/).first evaluated = instance_eval old_code case evaluated diff --git a/lib/synvert/core/rewriter/helper.rb b/lib/synvert/core/rewriter/helper.rb index 98da9e68..b63980db 100644 --- a/lib/synvert/core/rewriter/helper.rb +++ b/lib/synvert/core/rewriter/helper.rb @@ -65,9 +65,9 @@ def add_curly_brackets_if_necessary(code) # # strip_brackets("(1..100)") #=> "1..100" def strip_brackets(code) - code.sub(/^\((.*)\)$/) { $1 } - .sub(/^\[(.*)\]$/) { $1 } - .sub(/^{(.*)}$/) { $1 } + code.sub(/^\((.*)\)$/) { Regexp.last_match(1) } + .sub(/^\[(.*)\]$/) { Regexp.last_match(1) } + .sub(/^{(.*)}$/) { Regexp.last_match(1) } end end end