Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto corrected by following Format Ruby Code #119

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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'
Expand Down
50 changes: 28 additions & 22 deletions lib/synvert/core/engine/erb.rb
Expand Up @@ -4,17 +4,17 @@

module Synvert::Core
module Engine
ERUBY_EXPR_SPLITTER = "; ;"
ERUBY_STMT_SPLITTER = "; ;"
ERUBY_EXPR_SPLITTER = '; ;'
ERUBY_STMT_SPLITTER = '; ;'

class ERB
class <<self
class << self
# convert erb to ruby code.
#
# @param source [String] erb source code.
# @return [String] ruby source code.
def encode(source)
Erubis.new(source.gsub("-%>", "%>"), :escape => false, :trim => false).src
Erubis.new(source.gsub('-%>', '%>'), escape: false, trim: false).src
end

# convert ruby code to erb.
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
50 changes: 26 additions & 24 deletions lib/synvert/core/node_ext.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -397,7 +400,7 @@ def rewritten_source(code)
end
end

private
private

# Compare actual value with expected value.
#
Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions lib/synvert/core/rewriter.rb
Expand Up @@ -42,7 +42,7 @@ class Rewriter
autoload :RubyVersion, 'synvert/core/rewriter/ruby_version'
autoload :GemSpec, 'synvert/core/rewriter/gem_spec'

class <<self
class << self
# Register a rewriter with its group and name.
#
# @param group [String] the rewriter group.
Expand Down Expand Up @@ -112,7 +112,7 @@ def clear
rewriters.clear
end

private
private

def rewriters
@rewriters ||= {}
Expand Down Expand Up @@ -178,7 +178,7 @@ def add_warning(warning)
#
# @param description [String] rewriter description.
# @return rewriter description.
def description(description=nil)
def description(description = nil)
if description
@description = description
else
Expand Down Expand Up @@ -208,11 +208,10 @@ def if_gem(name, comparator)
# @param file_pattern [String] pattern to find files, e.g. spec/**/*_spec.rb
# @param options [Hash] instance options.
# @param block [Block] the block to rewrite code in the matching files.
def within_files(file_pattern, options={}, &block)
def within_files(file_pattern, options = {}, &block)
return if @sandbox

if (!@ruby_version || @ruby_version.match?) &&
(!@gem_spec || @gem_spec.match?)
if (!@ruby_version || @ruby_version.match?) && (!@gem_spec || @gem_spec.match?)
Rewriter::Instance.new(self, file_pattern, options, &block).process
end
end
Expand Down Expand Up @@ -262,15 +261,15 @@ def add_snippet(group, name)
# @param name [String] helper method name.
# @param block [Block] helper method block.
def helper_method(name, &block)
@helpers << {name: name, block: block}
@helpers << { name: name, block: block }
end

# Parse todo dsl, it sets todo of the rewriter.
# Or get todo.
#
# @param todo_list [String] rewriter todo.
# @return [String] rewriter todo.
def todo(todo=nil)
def todo(todo = nil)
if todo
@todo = todo
else
Expand Down
6 changes: 2 additions & 4 deletions lib/synvert/core/rewriter/action.rb
Expand Up @@ -11,7 +11,7 @@ class Rewriter::Action
# @param instance [Synvert::Core::Rewriter::Instance]
# @param code [String] new code to add, replace or remove.
# @param options [Hash] action options, it includes :autoindent.
def initialize(instance, code, options={})
def initialize(instance, code, options = {})
@instance = instance
@code = code
@options = DEFAULT_OPTIONS.merge(options)
Expand All @@ -30,9 +30,7 @@ def line
# @return [String] rewritten code.
def rewritten_code
if rewritten_source.split("\n").length > 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
Expand Down
2 changes: 1 addition & 1 deletion lib/synvert/core/rewriter/action/append_action.rb
Expand Up @@ -23,7 +23,7 @@ def end_pos
begin_pos
end

private
private

# Indent of the node.
#
Expand Down
10 changes: 7 additions & 3 deletions lib/synvert/core/rewriter/action/insert_action.rb
Expand Up @@ -3,15 +3,19 @@
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.
#
# @return [Integer] begin position.
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
Expand All @@ -26,7 +30,7 @@ def end_pos
begin_pos
end

private
private

# Indent of the node.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/synvert/core/rewriter/action/insert_after_action.rb
Expand Up @@ -17,7 +17,7 @@ def end_pos
begin_pos
end

private
private

# Indent of the node.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/synvert/core/rewriter/action/remove_action.rb
Expand Up @@ -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

Expand Down
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions lib/synvert/core/rewriter/action/replace_with_action.rb
Expand Up @@ -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
#
Expand Down
Expand Up @@ -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