Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/synvert/core/engine/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ def add_expr_escaped(src, code)
def add_stmt(src, code)
flush_newline_if_pending(src)
if code != "\n" && code != ""
index = if code =~ /\A(\s*)\r?\n/
index = case code
when /\A(\s*)\r?\n/
$1.length
elsif code =~ /\A(\s+)/
when /\A(\s+)/
$1.end_with?(' ') ? $1.length - 1 : $1.length
else
0
Expand Down
49 changes: 24 additions & 25 deletions lib/synvert/core/node_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -337,11 +335,12 @@ def recursive_children
# @return true if matches.
def match?(rules)
flat_hash(rules).keys.all? do |multi_keys|
if multi_keys.last == :any
case multi_keys.last
when :any
actual_values = actual_value(self, multi_keys[0...-1])
expected = expected_value(rules, multi_keys)
actual_values.any? { |actual| match_value?(actual, expected) }
elsif multi_keys.last == :not
when :not
actual = actual_value(self, multi_keys[0...-1])
expected = expected_value(rules, multi_keys)
!match_value?(actual, expected)
Expand Down Expand Up @@ -389,7 +388,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}}}"
Expand Down Expand Up @@ -446,7 +445,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

Expand Down
4 changes: 2 additions & 2 deletions lib/synvert/core/rewriter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/synvert/core/rewriter/condition/if_exist_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/synvert/core/rewriter/gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 11 additions & 14 deletions lib/synvert/core/rewriter/scope/within_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down