Skip to content
This repository has been archived by the owner on Mar 16, 2023. It is now read-only.

Commit

Permalink
Process iterator callback classes in sequence.
Browse files Browse the repository at this point in the history
Instead of calling the callback method for each callback class for the current
node Rlint::Iterator will now process an *entire* AST before moving on to the
next class.

Signed-off-by: Yorick Peterse <yorickpeterse@gmail.com>
  • Loading branch information
Yorick Peterse committed Oct 24, 2012
1 parent 8cb3857 commit 602973b
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 67 deletions.
2 changes: 1 addition & 1 deletion lib/rlint/cli.rb
Expand Up @@ -94,7 +94,7 @@ def index(args = [])
iterator.bind(analyzer)
end

iterator.iterate(tokens)
iterator.run(tokens)

output = formatter.format(report)

Expand Down
48 changes: 30 additions & 18 deletions lib/rlint/iterator.rb
Expand Up @@ -14,7 +14,7 @@ module Rlint
# tokens = parser.parse
# iterator = Rlint::Iterator.new
#
# iterator.iterate
# iterator.run(tokens)
#
# This particular example doesn't do anything but iterating over the nodes
# due to no callback classes being defined. How to add these classes is
Expand Down Expand Up @@ -106,37 +106,49 @@ def initialize(report = nil)
end

##
# Iterates over the specified array of token classes and executes defined
# callback methods.
# Processes the entire AST for each callback class in sequence. For each
# callback class the method {Rlint::Iterator#iterate} is called to process
# an *entire* AST before moving on to the next callback class.
#
# @param [#each] nodes An array of nodes to process.
#
def run(nodes)
@callbacks.each do |obj|
execute_callback(obj, :on_start)

iterate(obj, nodes)

execute_callback(obj, :on_finish)
end
end

##
# Processes an AST and calls callbacks methods for a specific callback
# object.
#
# @param [Rlint::Callback] callback_obj The callback object on which to
# invoke callback method.
# @param [#each] nodes An array (or a different object that responds to
# `#each()`) that contains a set of tokens to process.
# @param [TrueClass|FalseClass] When set to `true` (the default) the
# callbacks `on_start` and `on_finish` will be called before and after
# processing all the tokens.
#
def iterate(nodes, run_start_finish = true)
execute_callback(:on_start) if run_start_finish

def iterate(callback_obj, nodes)
nodes.each do |node|
next unless node.is_a?(Rlint::Token::Token)

event_name = node.event.to_s
callback_name = 'on_' + event_name
after_callback = 'after_' + event_name

execute_callback(callback_name, node)
execute_callback(callback_obj, callback_name, node)

node.child_nodes.each do |child_nodes|
if child_nodes.respond_to?(:each)
iterate(child_nodes, false)
iterate(callback_obj, child_nodes)
end
end

execute_callback(after_callback, node)
execute_callback(callback_obj, after_callback, node)
end

execute_callback(:on_finish) if run_start_finish
end

##
Expand All @@ -161,13 +173,13 @@ def bind(callback_class, options = {})
# Loops through all the bound callback classes and executes the specified
# callback method if it exists.
#
# @param [Rlint::Callback] obj The object on which to invoke the callback
# method.
# @param [String|Symbol] name The name of the callback method to execute.
# @param [Array] args Arguments to pass to the callback method.
#
def execute_callback(name, *args)
@callbacks.each do |obj|
obj.send(name, *args) if obj.respond_to?(name)
end
def execute_callback(obj, name, *args)
obj.send(name, *args) if obj.respond_to?(name)
end
end # Iterator
end # Rlint
2 changes: 1 addition & 1 deletion spec/benchmarks/memory.rb
Expand Up @@ -32,7 +32,7 @@ def benchmark_memory
iterator.bind(Rlint::Analyze::CodingStyle)
iterator.bind(Rlint::Analyze::Definitions)

iterator.iterate(tokens)
iterator.run(tokens)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/examples/lint_scope.rb
Expand Up @@ -9,6 +9,6 @@
iterator.bind(Rlint::Analyze::CodingStyle)
iterator.bind(Rlint::Analyze::Definitions)

iterator.iterate(tokens)
iterator.run(tokens)

puts formatter.format(report)
14 changes: 7 additions & 7 deletions spec/rlint/analyze/coding_style.rb
Expand Up @@ -15,7 +15,7 @@ def getNumber(theNumber = 10)
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::CodingStyle)
iterator.iterate(tokens)
iterator.run(tokens)

messages = report.messages[:info]
message = 'the use of camelCase for names is discouraged'
Expand Down Expand Up @@ -56,7 +56,7 @@ def this_method_name_is_rather_long_dont_you_think
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::CodingStyle)
iterator.iterate(tokens)
iterator.run(tokens)

messages = report.messages[:info]
message = "method and variable names should not be longer than " \
Expand All @@ -81,7 +81,7 @@ def this_method_name_is_rather_long_dont_you_think
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::CodingStyle)
iterator.iterate(tokens)
iterator.run(tokens)

info = report.messages[:info][0]

Expand Down Expand Up @@ -123,7 +123,7 @@ def this_method_name_is_rather_long_dont_you_think
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::CodingStyle)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:info].class.should == Array
report.messages[:info].length.should == 7
Expand Down Expand Up @@ -153,7 +153,7 @@ def valid_name?
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::CodingStyle)
iterator.iterate(tokens)
iterator.run(tokens)

message = 'predicate methods should end with a question mark'

Expand All @@ -176,7 +176,7 @@ def valid_name?
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::CodingStyle)
iterator.iterate(tokens)
iterator.run(tokens)

message = 'it is recommended to use the method "map" instead of "collect"'

Expand Down Expand Up @@ -206,7 +206,7 @@ def String.class_method
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::CodingStyle)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:warning].class.should == Array
report.messages[:warning].length.should == 2
Expand Down
10 changes: 5 additions & 5 deletions spec/rlint/analyze/definitions/classes.rb
Expand Up @@ -20,7 +20,7 @@ def name
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 1
Expand Down Expand Up @@ -62,7 +62,7 @@ def class_method_2
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 2
Expand Down Expand Up @@ -97,7 +97,7 @@ def Foo.class_method
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 2
Expand Down Expand Up @@ -127,7 +127,7 @@ class Foobar
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 1
Expand Down Expand Up @@ -173,7 +173,7 @@ def another_example
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].nil?.should == true
end
Expand Down
10 changes: 5 additions & 5 deletions spec/rlint/analyze/definitions/methods.rb
Expand Up @@ -14,7 +14,7 @@ def defined_method; end
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 1
Expand All @@ -40,7 +40,7 @@ def defined_method; end
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 2
Expand Down Expand Up @@ -73,7 +73,7 @@ def defined_method; end
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 3
Expand Down Expand Up @@ -108,7 +108,7 @@ def uppercase(name = 'Ruby', *args, &block)
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 1
Expand Down Expand Up @@ -145,7 +145,7 @@ def self.valid_method
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 3
Expand Down
12 changes: 6 additions & 6 deletions spec/rlint/analyze/definitions/modules.rb
Expand Up @@ -18,7 +18,7 @@ def self.class_method
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 1
Expand All @@ -44,7 +44,7 @@ module Foobar
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].nil?.should == true
end
Expand Down Expand Up @@ -74,7 +74,7 @@ def self.another_example
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].nil?.should == true
end
Expand Down Expand Up @@ -103,7 +103,7 @@ def example_instance_method
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 2
Expand Down Expand Up @@ -156,7 +156,7 @@ class Person
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 3
Expand Down Expand Up @@ -207,7 +207,7 @@ class Person
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:error].class.should == Array
report.messages[:error].length.should == 3
Expand Down
8 changes: 4 additions & 4 deletions spec/rlint/analyze/definitions/unused_variables.rb
Expand Up @@ -24,7 +24,7 @@
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:warning].class.should == Array
report.messages[:warning].length.should == 4
Expand Down Expand Up @@ -79,7 +79,7 @@ def example
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:warning].class.should == Array
report.messages[:warning].length.should == 4
Expand Down Expand Up @@ -134,7 +134,7 @@ class Example
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:warning].class.should == Array
report.messages[:warning].length.should == 4
Expand Down Expand Up @@ -189,7 +189,7 @@ module Example
iterator = Rlint::Iterator.new(report)

iterator.bind(Rlint::Analyze::Definitions)
iterator.iterate(tokens)
iterator.run(tokens)

report.messages[:warning].class.should == Array
report.messages[:warning].length.should == 4
Expand Down

0 comments on commit 602973b

Please sign in to comment.