forked from troessner/reek
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
185b1e8
commit 0554d66
Showing
18 changed files
with
129 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,10 @@ | ||
require 'ruby2ruby' | ||
|
||
module Reek | ||
class SexpFormatter | ||
def self.format(sexp) | ||
return sexp.to_s unless Array === sexp | ||
first = sexp[1] | ||
second = sexp[2] | ||
third = sexp[3] | ||
case sexp[0] | ||
when :array | ||
format_all(sexp, ', ') | ||
when :attrasgn | ||
result = format(first) | ||
if second == :[]= | ||
result += "[#{format(third[1])}] = #{format(third[2])}" | ||
end | ||
result | ||
when :call | ||
result = format(first) | ||
if second.to_s == '[]' | ||
result += (third.nil? ? '[]' : "[#{format(third)}]") | ||
else | ||
result += ".#{second}" + (third ? "(#{format(third)})" : '') | ||
end | ||
result | ||
when :colon2 | ||
format_all(sexp, '::') | ||
when :const, :cvar, :dvar | ||
format(first) | ||
when :dot2 | ||
format_all(sexp, '..') | ||
when :dstr | ||
'"' + format_all(sexp, '') + '"' | ||
when :evstr | ||
"\#\{#{format(first)}\}" | ||
when :fcall, :vcall | ||
result = first.to_s | ||
result += "(#{format(second)})" if second | ||
result | ||
when :iter | ||
'block' | ||
when :lasgn | ||
format_all(sexp, '=') | ||
when :nth_ref | ||
"$#{first}" | ||
when :str | ||
first | ||
when :xstr | ||
"`#{first}`" | ||
else | ||
sexp[-1].to_s | ||
end | ||
end | ||
|
||
def self.format_all(sexp, glue) | ||
sexp[1..-1].map {|arg| format(arg)}.join(glue) | ||
sexp = YAML::load(YAML::dump(sexp)) | ||
Ruby2Ruby.new.process(sexp) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper.rb' | ||
|
||
require 'reek/block_context' | ||
require 'reek/method_context' | ||
|
||
include Reek | ||
|
||
describe BlockContext do | ||
|
||
it "should record single parameter" do | ||
element = StopContext.new | ||
element = BlockContext.new(element, s(s(:lasgn, :x), nil)) | ||
element.variable_names.should == [Name.new(:x)] | ||
end | ||
|
||
it "should record single parameter within a method" do | ||
element = StopContext.new | ||
element = MethodContext.new(element, s(:defn, :help)) | ||
element = BlockContext.new(element, s(s(:lasgn, :x), nil)) | ||
element.variable_names.should == [Name.new(:x)] | ||
end | ||
|
||
it "records multiple parameters" do | ||
element = StopContext.new | ||
element = BlockContext.new(element, s(s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))), nil)) | ||
element.variable_names.should == [Name.new(:x), Name.new(:y)] | ||
end | ||
|
||
it "should not pass parameters upward" do | ||
mc = MethodContext.new(StopContext.new, s(:defn, :help)) | ||
element = BlockContext.new(mc, s(s(:lasgn, :x))) | ||
mc.variable_names.should be_empty | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.