|
| 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