diff --git a/lib/synvert/core.rb b/lib/synvert/core.rb index e1cecdb6..fe8fe6ab 100644 --- a/lib/synvert/core.rb +++ b/lib/synvert/core.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "synvert/core/version" +require 'synvert/core/version' require 'bundler' require 'parser' require 'parser/current' diff --git a/lib/synvert/core/engine/erb.rb b/lib/synvert/core/engine/erb.rb index 34dcadf7..68b0a972 100644 --- a/lib/synvert/core/engine/erb.rb +++ b/lib/synvert/core/engine/erb.rb @@ -4,17 +4,17 @@ module Synvert::Core module Engine - ERUBY_EXPR_SPLITTER = "; ;" - ERUBY_STMT_SPLITTER = "; ;" + ERUBY_EXPR_SPLITTER = '; ;' + ERUBY_STMT_SPLITTER = '; ;' class ERB - class <", "%>"), :escape => false, :trim => false).src + Erubis.new(source.gsub('-%>', '%>'), escape: false, trim: false).src end # convert ruby code to erb. @@ -28,25 +28,30 @@ def decode(source) source = remove_erubis_buf(source) end - private + private def decode_ruby_stmt(source) source.gsub(/#{ERUBY_STMT_SPLITTER}(.+?)#{ERUBY_STMT_SPLITTER}/m) { "<%#{$1}%>" } end def decode_ruby_output(source) - source.gsub(/@output_buffer.append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m) { "<%=#{$1}%>" } - .gsub(/@output_buffer.append= (.+?)\s+(do|\{)(\s*\|[^|]*\|)?\s*#{ERUBY_EXPR_SPLITTER}/m) { |m| "<%=#{m.sub("@output_buffer.append= ", "").sub(ERUBY_EXPR_SPLITTER, "")}%>" } + source.gsub(/@output_buffer.append=\((.+?)\);#{ERUBY_EXPR_SPLITTER}/m) { "<%=#{$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($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) } end def remove_erubis_buf(source) - source.sub("@output_buffer = output_buffer || ActionView::OutputBuffer.new;", "").sub("@output_buffer.to_s", "") + source + .sub('@output_buffer = output_buffer || ActionView::OutputBuffer.new;', '') + .sub('@output_buffer.to_s', '') end def reverse_escape_text(source) @@ -59,7 +64,7 @@ def reverse_escape_text(source) class Erubis < ::Erubis::Eruby def add_preamble(src) @newline_pending = 0 - src << "@output_buffer = output_buffer || ActionView::OutputBuffer.new;" + src << '@output_buffer = output_buffer || ActionView::OutputBuffer.new;' end def add_text(src, text) @@ -102,22 +107,23 @@ def add_expr_literal(src, code) def add_expr_escaped(src, code) flush_newline_if_pending(src) if code =~ BLOCK_EXPR - src << "@output_buffer.safe_append= " << code << ERUBY_EXPR_SPLITTER + src << '@output_buffer.safe_append= ' << code << ERUBY_EXPR_SPLITTER else - src << "@output_buffer.safe_append=(" << code << ");" << ERUBY_EXPR_SPLITTER + src << '@output_buffer.safe_append=(' << code << ');' << ERUBY_EXPR_SPLITTER end end def add_stmt(src, code) flush_newline_if_pending(src) - if code != "\n" && code != "" - index = if code =~ /\A(\s*)\r?\n/ - $1.length - elsif code =~ /\A(\s+)/ - $1.end_with?(' ') ? $1.length - 1 : $1.length - else - 0 - end + if code != "\n" && code != '' + index = + if code =~ /\A(\s*)\r?\n/ + $1.length + elsif code =~ /\A(\s+)/ + $1.end_with?(' ') ? $1.length - 1 : $1.length + else + 0 + end code.insert(index, ERUBY_STMT_SPLITTER) code.insert(-1, ERUBY_STMT_SPLITTER[0...-1]) end diff --git a/lib/synvert/core/node_ext.rb b/lib/synvert/core/node_ext.rb index 046adbfb..fb05b815 100644 --- a/lib/synvert/core/node_ext.rb +++ b/lib/synvert/core/node_ext.rb @@ -287,12 +287,13 @@ def to_s end def debug_info - "\n" + [ - "file: #{self.loc.expression.source_buffer.name}", - "line: #{self.loc.expression.line}", - "source: #{self.to_source}", - "node: #{self.inspect}" - ].join("\n") + "\n" + + [ + "file: #{self.loc.expression.source_buffer.name}", + "line: #{self.loc.expression.line}", + "source: #{self.to_source}", + "node: #{self.inspect}" + ].join("\n") end # Get the source code of current node. @@ -336,21 +337,23 @@ def recursive_children # @param rules [Hash] rules to match. # @return true if matches. def match?(rules) - flat_hash(rules).keys.all? do |multi_keys| - if multi_keys.last == :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 - actual = actual_value(self, multi_keys[0...-1]) - expected = expected_value(rules, multi_keys) - !match_value?(actual, expected) - else - actual = actual_value(self, multi_keys) - expected = expected_value(rules, multi_keys) - match_value?(actual, expected) + flat_hash(rules) + .keys + .all? do |multi_keys| + if multi_keys.last == :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 + actual = actual_value(self, multi_keys[0...-1]) + expected = expected_value(rules, multi_keys) + !match_value?(actual, expected) + else + actual = actual_value(self, multi_keys) + expected = expected_value(rules, multi_keys) + match_value?(actual, expected) + end end - end end # Get rewritten source code. @@ -377,7 +380,7 @@ def rewritten_source(code) if lines_count > 1 && lines_count == evaluated.size new_code = [] lines.each_with_index { |line, index| - new_code << (index == 0 ? line : line[evaluated.first.indent-2..-1]) + new_code << (index == 0 ? line : line[evaluated.first.indent - 2..-1]) } new_code.join("\n") else @@ -397,7 +400,7 @@ def rewritten_source(code) end end - private + private # Compare actual value with expected value. # @@ -415,8 +418,7 @@ def match_value?(actual, expected) end when String if Parser::AST::Node === actual - actual.to_source == expected || - (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) || + actual.to_source == expected || (actual.to_source[0] == ':' && actual.to_source[1..-1] == expected) || actual.to_source[1...-1] == expected else actual.to_s == expected diff --git a/lib/synvert/core/rewriter.rb b/lib/synvert/core/rewriter.rb index f436d843..aeac5a15 100644 --- a/lib/synvert/core/rewriter.rb +++ b/lib/synvert/core/rewriter.rb @@ -42,7 +42,7 @@ class Rewriter autoload :RubyVersion, 'synvert/core/rewriter/ruby_version' autoload :GemSpec, 'synvert/core/rewriter/gem_spec' - class < 1 - "\n\n" + rewritten_source.split("\n").map { |line| - indent(@node) + line - }.join("\n") + "\n\n" + rewritten_source.split("\n").map { |line| indent(@node) + line }.join("\n") else "\n" + indent(@node) + rewritten_source end diff --git a/lib/synvert/core/rewriter/action/append_action.rb b/lib/synvert/core/rewriter/action/append_action.rb index a3dc820d..156a6677 100644 --- a/lib/synvert/core/rewriter/action/append_action.rb +++ b/lib/synvert/core/rewriter/action/append_action.rb @@ -23,7 +23,7 @@ def end_pos begin_pos end - private + private # Indent of the node. # diff --git a/lib/synvert/core/rewriter/action/insert_action.rb b/lib/synvert/core/rewriter/action/insert_action.rb index 56edad63..f1de8981 100644 --- a/lib/synvert/core/rewriter/action/insert_action.rb +++ b/lib/synvert/core/rewriter/action/insert_action.rb @@ -3,7 +3,7 @@ module Synvert::Core # InsertAction to insert code to the top of node body. class Rewriter::InsertAction < Rewriter::Action - DO_LENGTH = " do".length + DO_LENGTH = ' do'.length # Begin position to insert code. # @@ -11,7 +11,11 @@ class Rewriter::InsertAction < Rewriter::Action def begin_pos case @node.type when :block - @node.children[1].children.empty? ? @node.children[0].loc.expression.end_pos + DO_LENGTH : @node.children[1].loc.expression.end_pos + if @node.children[1].children.empty? + @node.children[0].loc.expression.end_pos + DO_LENGTH + else + @node.children[1].loc.expression.end_pos + end when :class @node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos else @@ -26,7 +30,7 @@ def end_pos begin_pos end - private + private # Indent of the node. # diff --git a/lib/synvert/core/rewriter/action/insert_after_action.rb b/lib/synvert/core/rewriter/action/insert_after_action.rb index 10d73ed8..8737d1b4 100644 --- a/lib/synvert/core/rewriter/action/insert_after_action.rb +++ b/lib/synvert/core/rewriter/action/insert_after_action.rb @@ -17,7 +17,7 @@ def end_pos begin_pos end - private + private # Indent of the node. # diff --git a/lib/synvert/core/rewriter/action/remove_action.rb b/lib/synvert/core/rewriter/action/remove_action.rb index 36eb8744..cda308aa 100644 --- a/lib/synvert/core/rewriter/action/remove_action.rb +++ b/lib/synvert/core/rewriter/action/remove_action.rb @@ -3,7 +3,7 @@ module Synvert::Core # RemoveAction to remove code. class Rewriter::RemoveAction < Rewriter::Action - def initialize(instance, code=nil) + def initialize(instance, code = nil) super end diff --git a/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb b/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb index 84489277..f028793b 100644 --- a/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +++ b/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb @@ -4,7 +4,7 @@ module Synvert::Core # ReplaceErbStmtWithExprAction to replace erb stmt code to expr, # e.g. <% form_for ... %> => <%= form_for ... %>. class Rewriter::ReplaceErbStmtWithExprAction < Rewriter::Action - def initialize(instance, code=nil) + def initialize(instance, code = nil) super end @@ -23,7 +23,7 @@ def begin_pos # @return [Integer] end position. def end_pos node_begin_pos = @node.loc.expression.begin_pos - node_begin_pos += @node.loc.expression.source.index "do" + node_begin_pos += @node.loc.expression.source.index 'do' while @node.loc.expression.source_buffer.source[node_begin_pos += 1] != '@' end node_begin_pos @@ -33,7 +33,8 @@ def end_pos # # @return [String] rewritten code. def rewritten_code - @node.loc.expression.source_buffer.source[begin_pos...end_pos].sub(Engine::ERUBY_STMT_SPLITTER, "@output_buffer.append= ") + @node.loc.expression.source_buffer.source[begin_pos...end_pos] + .sub(Engine::ERUBY_STMT_SPLITTER, '@output_buffer.append= ') .sub(Engine::ERUBY_STMT_SPLITTER, Engine::ERUBY_EXPR_SPLITTER) end end diff --git a/lib/synvert/core/rewriter/action/replace_with_action.rb b/lib/synvert/core/rewriter/action/replace_with_action.rb index f40a4e71..26c8b6d2 100644 --- a/lib/synvert/core/rewriter/action/replace_with_action.rb +++ b/lib/synvert/core/rewriter/action/replace_with_action.rb @@ -23,16 +23,18 @@ def end_pos def rewritten_code if rewritten_source.split("\n").length > 1 new_code = [] - rewritten_source.split("\n").each_with_index { |line, index| - new_code << (index == 0 || !@options[:autoindent] ? line : indent(@node) + line) - } + rewritten_source + .split("\n") + .each_with_index { |line, index| + new_code << (index == 0 || !@options[:autoindent] ? line : indent(@node) + line) + } new_code.join("\n") else rewritten_source end end - private + private # Indent of the node # diff --git a/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb b/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb index 1407d623..22a9a7b7 100644 --- a/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +++ b/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb @@ -5,8 +5,7 @@ module Synvert::Core class Rewriter::IfOnlyExistCondition < Rewriter::Condition # check if only have one child node and the child node matches rules. def match? - @instance.current_node.body.size == 1 && - @instance.current_node.body.first.match?(@rules) + @instance.current_node.body.size == 1 && @instance.current_node.body.first.match?(@rules) end end end diff --git a/lib/synvert/core/rewriter/gem_spec.rb b/lib/synvert/core/rewriter/gem_spec.rb index 7cf646e0..109526e9 100644 --- a/lib/synvert/core/rewriter/gem_spec.rb +++ b/lib/synvert/core/rewriter/gem_spec.rb @@ -3,7 +3,7 @@ module Synvert::Core # GemSpec checks and compares gem version. class Rewriter::GemSpec - OPERATORS = {eq: '==', lt: '<', gt: '>', lte: '<=', gte: '>=', ne: '!='} + OPERATORS = { eq: '==', lt: '<', gt: '>', lte: '<=', gte: '>=', ne: '!=' } # Initialize a gem_spec. # @@ -27,6 +27,7 @@ def initialize(name, comparator) # @raise [Synvert::Core::GemfileLockNotFound] raise if Gemfile.lock does not exist. def match? gemfile_lock_path = File.join(Configuration.instance.get(:path), 'Gemfile.lock') + # if Gemfile.lock does not exist, just ignore this check return true unless File.exist?(gemfile_lock_path) diff --git a/lib/synvert/core/rewriter/helper.rb b/lib/synvert/core/rewriter/helper.rb index 98da9e68..5b6772fa 100644 --- a/lib/synvert/core/rewriter/helper.rb +++ b/lib/synvert/core/rewriter/helper.rb @@ -34,9 +34,9 @@ def add_receiver_if_necessary(code) # if current_node has argument, it returns "({{arguments}})" def add_arguments_with_parenthesis_if_necessary if node.arguments.size > 0 - "({{arguments}})" + '({{arguments}})' else - "" + '' end end @@ -65,9 +65,7 @@ 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(/^\((.*)\)$/) { $1 }.sub(/^\[(.*)\]$/) { $1 }.sub(/^{(.*)}$/) { $1 } end end end diff --git a/lib/synvert/core/rewriter/instance.rb b/lib/synvert/core/rewriter/instance.rb index 9125b72c..564a5e96 100644 --- a/lib/synvert/core/rewriter/instance.rb +++ b/lib/synvert/core/rewriter/instance.rb @@ -8,7 +8,7 @@ module Synvert::Core class Rewriter::Instance include Rewriter::Helper - class < 0 - @actions.sort_by! { |action| action.send(@options[:sort_by]) } - conflict_actions = get_conflict_actions - @actions.reverse_each do |action| - source[action.begin_pos...action.end_pos] = action.rewritten_code - source = remove_code_or_whole_line(source, action.line) + + if @actions.length > 0 + @actions.sort_by! { |action| action.send(@options[:sort_by]) } + conflict_actions = get_conflict_actions + @actions.reverse_each do |action| + source[action.begin_pos...action.end_pos] = action.rewritten_code + source = remove_code_or_whole_line(source, action.line) + end + @actions = [] + + self.class.write_file(file_path, source) end - @actions = [] - - self.class.write_file(file_path, source) - end - rescue Parser::SyntaxError - puts "[Warn] file #{file_path} was not parsed correctly." - # do nothing, iterate next file - end while !conflict_actions.empty? + rescue Parser::SyntaxError + puts "[Warn] file #{file_path} was not parsed correctly." + # do nothing, iterate next file + end while !conflict_actions.empty? + end end - end end # Gets current node, it allows to get current node in block code. @@ -220,7 +222,7 @@ def if_only_exist_node(rules, &block) # # @param code [String] code need to be appended. # @param options [Hash] action options. - def append(code, options={}) + def append(code, options = {}) @actions << Rewriter::AppendAction.new(self, code, options) end @@ -229,7 +231,7 @@ def append(code, options={}) # # @param code [String] code need to be inserted. # @param options [Hash] action options. - def insert(code, options={}) + def insert(code, options = {}) @actions << Rewriter::InsertAction.new(self, code, options) end @@ -238,7 +240,7 @@ def insert(code, options={}) # # @param code [String] code need to be inserted. # @param options [Hash] action options. - def insert_after(node, options={}) + def insert_after(node, options = {}) @actions << Rewriter::InsertAfterAction.new(self, node, options) end @@ -247,7 +249,7 @@ def insert_after(node, options={}) # # @param code [String] code need to be replaced with. # @param options [Hash] action options. - def replace_with(code, options={}) + def replace_with(code, options = {}) @actions << Rewriter::ReplaceWithAction.new(self, code, options) end @@ -269,7 +271,7 @@ def warn(message) @rewriter.add_warning Rewriter::Warning.new(self, message) end - private + private # It changes source code from bottom to top, and it can change source code twice at the same time, # So if there is an overlap between two actions, it removes the conflict actions and operate them in the next loop. @@ -301,7 +303,8 @@ def remove_code_or_whole_line(source, line) source_arr = source.split("\n") if source_arr[line - 1] && source_arr[line - 1].strip.empty? source_arr.delete_at(line - 1) - if source_arr[line - 2] && source_arr[line - 2].strip.empty? && source_arr[line - 1] && source_arr[line - 1].strip.empty? + if source_arr[line - 2] && source_arr[line - 2].strip.empty? && source_arr[line - 1] && + source_arr[line - 1].strip.empty? source_arr.delete_at(line - 1) end source_arr.join("\n") + (newline_at_end_of_line ? "\n" : '') diff --git a/lib/synvert/core/version.rb b/lib/synvert/core/version.rb index 4b30948a..9c7858bb 100644 --- a/lib/synvert/core/version.rb +++ b/lib/synvert/core/version.rb @@ -2,6 +2,6 @@ module Synvert module Core - VERSION = "0.17.0" + VERSION = '0.17.0' end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d0fda429..296217d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'synvert/core' diff --git a/spec/synvert/core/engine/erb_spec.rb b/spec/synvert/core/engine/erb_spec.rb index ddcde4bf..1ff8304d 100644 --- a/spec/synvert/core/engine/erb_spec.rb +++ b/spec/synvert/core/engine/erb_spec.rb @@ -4,8 +4,8 @@ module Synvert::Core describe Engine::ERB do - it "encodes / decodes" do - source =<<-EOF + it 'encodes / decodes' do + source = <<-EOF <%content_for :head do%>