Skip to content

Commit

Permalink
Auto corrected by following Format Ruby Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Awesome Code committed Jan 28, 2021
1 parent 96601d8 commit 747420c
Show file tree
Hide file tree
Showing 37 changed files with 605 additions and 505 deletions.
4 changes: 2 additions & 2 deletions lib/synvert/core.rb
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require "synvert/core/version"
require 'synvert/core/version'

# coding: utf-8
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
9 changes: 3 additions & 6 deletions lib/synvert/core/exceptions.rb
Expand Up @@ -2,14 +2,11 @@

module Synvert::Core
# Rewriter not found exception.
class RewriterNotFound < RuntimeError
end
class RewriterNotFound < RuntimeError; end

# Gemfile.lock not found exception.
class GemfileLockNotFound < RuntimeError
end
class GemfileLockNotFound < RuntimeError; end

# Method not supported exception.
class MethodNotSupported < RuntimeError
end
class MethodNotSupported < RuntimeError; end
end
80 changes: 33 additions & 47 deletions lib/synvert/core/node_ext.rb
Expand Up @@ -281,27 +281,24 @@ def to_value
end

def to_s
if :mlhs == self.type
"(#{self.children.map(&:name).join(', ')})"
end
"(#{self.children.map(&:name).join(', ')})" if :mlhs == self.type
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.
#
# @return [String] source code.
def to_source
if self.loc.expression
self.loc.expression.source
end
self.loc.expression.source if self.loc.expression
end

# Get the indent of current node.
Expand Down Expand Up @@ -336,21 +333,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 @@ -376,9 +375,9 @@ def rewritten_source(code)
lines_count = lines.length
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])
}
lines.each_with_index do |line, index|
new_code << (index == 0 ? line : line[evaluated.first.indent - 2..-1])
end
new_code.join("\n")
else
source
Expand All @@ -397,7 +396,7 @@ def rewritten_source(code)
end
end

private
private

# Compare actual value with expected value.
#
Expand All @@ -408,15 +407,10 @@ def rewritten_source(code)
def match_value?(actual, expected)
case expected
when Symbol
if Parser::AST::Node === actual
actual.to_source == ":#{expected}"
else
actual.to_sym == expected
end
Parser::AST::Node === actual ? actual.to_source == ":#{expected}" : actual.to_sym == expected
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 All @@ -434,11 +428,7 @@ def match_value?(actual, expected)
when NilClass
actual.nil?
when Numeric
if Parser::AST::Node === actual
actual.children[0] == expected
else
actual == expected
end
Parser::AST::Node === actual ? actual.children[0] == expected : actual == expected
when TrueClass
:true == actual.type
when FalseClass
Expand Down Expand Up @@ -475,11 +465,7 @@ def flat_hash(h, k = [])
# @param multi_keys [Array<Symbol>]
# @return [Object] actual value.
def actual_value(node, multi_keys)
multi_keys.inject(node) { |n, key|
if n
key == :source ? n.send(key) : n.send(key)
end
}
multi_keys.inject(node) { |n, key| key == :source ? n.send(key) : n.send(key) if n }
end

# Get expected value from rules.
Expand Down
21 changes: 8 additions & 13 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 @@ -93,11 +93,7 @@ def call(group, name)
# @return [Boolean] true if the rewriter exist.
def exist?(group, name)
group, name = group.to_s, name.to_s
if rewriters[group] && rewriters[group][name]
true
else
false
end
rewriters[group] && rewriters[group][name] ? true : false
end

# Get all available rewriters
Expand All @@ -112,7 +108,7 @@ def clear
rewriters.clear
end

private
private

def rewriters
@rewriters ||= {}
Expand Down Expand Up @@ -178,7 +174,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 +204,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 +257,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
8 changes: 2 additions & 6 deletions lib/synvert/core/rewriter/action/append_action.rb
Expand Up @@ -9,11 +9,7 @@ class Rewriter::AppendAction < Rewriter::Action
#
# @return [Integer] begin position.
def begin_pos
if :begin == @node.type
@node.loc.expression.end_pos
else
@node.loc.expression.end_pos - @node.indent - END_LENGTH
end
:begin == @node.type ? @node.loc.expression.end_pos : @node.loc.expression.end_pos - @node.indent - END_LENGTH
end

# End position, always same to begin position.
Expand All @@ -23,7 +19,7 @@ def end_pos
begin_pos
end

private
private

# Indent of the node.
#
Expand Down

0 comments on commit 747420c

Please sign in to comment.