Skip to content

Commit 0b34f7a

Browse files
committed
Split our conceptual analysis and static analysis
1 parent 8491ae4 commit 0b34f7a

File tree

6 files changed

+282
-193
lines changed

6 files changed

+282
-193
lines changed

lib/analyzer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
require_relative 'analyze_solution'
2222

2323
require_relative 'analyzers/exercise_analyzer'
24+
require_relative "analyzers/solution_representation"
2425
EXERCISES.each do |exercise|
2526
require_relative "analyzers/#{exercise}/analyze"
27+
require_relative "analyzers/#{exercise}/representation"
2628
end
2729

2830
module Analyzer

lib/analyzers/exercise_analyzer.rb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class ExerciseAnalyzer
2-
include SA::InlineHelpers
32

43
# This is just flow-control for quickly exiting the
54
# analysis. We probably don't want to do things this
@@ -8,9 +7,11 @@ class FinishedFlowControlException < RuntimeError
87
end
98

109
def initialize(code_to_analyze)
11-
@code_to_analyze = code_to_analyze
1210
@status = nil
1311
@comments = []
12+
13+
solution_class = "#{self.class.name.split("::").first}::Representation".constantize
14+
@solution = solution_class.new(code_to_analyze)
1415
end
1516

1617
def call
@@ -29,19 +30,8 @@ def results
2930
end
3031

3132
private
32-
attr_reader :code_to_analyze
33+
attr_reader :solution
3334

3435
protected
3536
attr_accessor :status, :comments
36-
37-
def root_node
38-
@root_node ||= begin
39-
buffer = Parser::Source::Buffer.new(nil)
40-
buffer.source = code_to_analyze
41-
builder = RuboCop::AST::Builder.new
42-
parser = Parser::CurrentRuby.new(builder)
43-
44-
parser.parse(buffer)
45-
end
46-
end
4737
end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class SolutionRepresentation
2+
include Mandate
3+
include SA::InlineHelpers
4+
5+
def initialize(code_to_analyze)
6+
@code_to_analyze = code_to_analyze
7+
end
8+
9+
# REFACTOR: This could be refactored to strip blank
10+
# lines and then use each_cons(2).
11+
def indentation_is_sensible?
12+
previous_line = nil
13+
code_to_analyze.lines.each do |line|
14+
# If the previous line or this line is
15+
# just a whitespace line, don't consider it
16+
# when checking for indentation
17+
unless previous_line == nil ||
18+
previous_line =~ /^\s*\n*$/ ||
19+
line =~ /^\s*\n*$/
20+
21+
previous_line_lspace = previous_line[/^ */].size
22+
line_lspace = line[/^ */].size
23+
24+
return false if (previous_line_lspace - line_lspace).abs > 2
25+
end
26+
27+
previous_line = line
28+
end
29+
30+
true
31+
end
32+
33+
def has_target_module?
34+
target_module
35+
end
36+
37+
def has_target_method?
38+
target_method
39+
end
40+
41+
# MOVE BELOW HERE TO PRIVATE
42+
43+
memoize
44+
def target_method
45+
SA::Helpers.extract_module_method(target_module, "two_fer")
46+
end
47+
48+
private
49+
attr_reader :code_to_analyze
50+
51+
memoize
52+
def target_module
53+
SA::Helpers.extract_module_or_class(root_node, "TwoFer")
54+
end
55+
56+
def default_argument_value
57+
default_argument.children[0]
58+
end
59+
60+
def root_node
61+
@root_node ||= begin
62+
buffer = Parser::Source::Buffer.new(nil)
63+
buffer.source = code_to_analyze
64+
builder = RuboCop::AST::Builder.new
65+
parser = Parser::CurrentRuby.new(builder)
66+
67+
parser.parse(buffer)
68+
end
69+
end
70+
end

0 commit comments

Comments
 (0)