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
4 changes: 2 additions & 2 deletions lib/synvert/core.rb
Original file line number Diff line number Diff line change
@@ -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
51 changes: 29 additions & 22 deletions lib/synvert/core/engine/erb.rb
Original file line number Diff line number Diff line change
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,31 @@ 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 +65,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 +108,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

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

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

# Method not supported exception.
class MethodNotSupported < Exception
end
class MethodNotSupported < Exception; end
end
32 changes: 18 additions & 14 deletions lib/synvert/core/node_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ def method_missing(meth, *args, &block)
end

# Parser::AST::Node monkey patch.

class Node
# Get name node of :class, :module, :const, :mlhs, :def and :defs node.
#
# @return [Parser::AST::Node] name node.
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.

def name
case self.type
when :class, :module, :def, :arg, :blockarg, :restarg
Expand Down Expand Up @@ -98,6 +100,7 @@ def message
#
# @return [Array<Parser::AST::Node>] arguments node.
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.

def arguments
case self.type
when :def, :block
Expand Down Expand Up @@ -129,6 +132,7 @@ def caller
#
# @return [Array<Parser::AST::Node>] body node.
# @raise [Synvert::Core::MethodNotSupported] if calls on other node.

def body
case self.type
when :begin
Expand Down Expand Up @@ -287,12 +291,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 @@ -376,9 +381,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 +402,7 @@ def rewritten_source(code)
end
end

private
private

# Compare actual value with expected value.
#
Expand All @@ -415,8 +420,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 Expand Up @@ -475,11 +479,11 @@ 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|
multi_keys.inject(node) do |n, key|
if n
key == :source ? n.send(key) : n.send(key)
end
}
end
end

# Get expected value from rules.
Expand Down
15 changes: 7 additions & 8 deletions lib/synvert/core/rewriter.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def end_pos
begin_pos
end

private
private

# Indent of the node.
#
Expand Down
9 changes: 6 additions & 3 deletions lib/synvert/core/rewriter/action/insert_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,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
@node.children[1].children.empty? ?
@node.children[0].loc.expression.end_pos + DO_LENGTH :
@node.children[1].loc.expression.end_pos
when :class
@node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
else
Expand All @@ -26,7 +29,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading