lib/pelusa/lint/indentation_level.rb in pelusa-0.2.2 vs lib/pelusa/lint/indentation_level.rb in pelusa-0.2.3
- old
+ new
@@ -4,11 +4,10 @@
def initialize
@violations = Set.new
end
def check(klass)
- initialize
iterate_lines!(klass)
return SuccessfulAnalysis.new(name) if @violations.empty?
FailedAnalysis.new(name, @violations) do |violations|
@@ -21,27 +20,36 @@
def name
"Doesn't use more than one indentation level inside methods"
end
def iterate_lines!(klass)
- iterator = Iterator.new do |node|
+ # we want to find all nodes inside define blocks that
+ # contain > 1 indentation levels
+ # this method totally fails the IndentationLevel level lint :P
+ ClassAnalyzer.walk(klass) do |node|
if node.is_a?(Rubinius::AST::Define)
- _iterate = Iterator.new do |node|
- __iterate = Iterator.new do |node|
- if body = get_body_from_node[node]
- if node.line != [body].flatten.first.line
- @violations.merge Array(body).map(&:line)
+ # we're inside a method body, so see if we indent anywhere
+ ClassAnalyzer.walk(node) do |inner_node|
+ if inner_body = get_body_from_node[inner_node]
+ # if it turns out there's an indented value in there, that
+ # could be okay -- walk that node to see if there's a 2nd level
+ if inner_node.line != [inner_body].flatten.first.line
+ # walk the third level, see if there's another indent
+ ClassAnalyzer.walk(inner_node) do |innermost_node|
+ if innermost_body = get_body_from_node[innermost_node]
+ if innermost_node.line != [innermost_body].flatten.first.line
+ # there's yet another level of indent -- violation!
+ # note: this also catches bad outdents, possibly should
+ # flag those separately
+ @violations.merge Array(innermost_body).map(&:line)
+ end
+ end
end
end
end
-
- Array(get_body_from_node[node]).each(&__iterate)
end
- node.body.array.each(&_iterate)
end
end
-
- Array(klass).each(&iterator)
end
def get_body_from_node
lambda do |node|
if node.respond_to?(:body) && !node.body.is_a?(Rubinius::AST::NilLiteral)