Class: RokuBuilder::LineInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/roku_builder/plugins/line_inspector.rb

Instance Method Summary collapse

Constructor Details

#initialize(inspector_config:, indent_config:) ⇒ LineInspector

Returns a new instance of LineInspector.



6
7
8
9
# File 'lib/roku_builder/plugins/line_inspector.rb', line 6

def initialize(inspector_config:, indent_config:)
  @inspector_config = inspector_config
  @indent_config = indent_config
end

Instance Method Details

#run(file_path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/roku_builder/plugins/line_inspector.rb', line 11

def run(file_path)
  @warnings = []
  File.open(file_path) do |file|
    in_xml_comment = false
    indent_inspector = IndentationInspector.new(rules: @indent_config, path: file_path) if @indent_config
    full_file = []
    file_no_comments = []
    lines_to_ignore = []
    file.readlines.each_with_index do |line, line_number|
      full_line = line.dup
      line.gsub!(/'.*/, "") if file_path.end_with?(".brs")
      if file_path.end_with?(".xml")
        if in_xml_comment
          if line.gsub!(/.*-->/, "")
            in_xml_comment = false
          else
            line = "\n"
          end
        end
        line.gsub!(/<!--.*-->/, "")
        in_xml_comment = true if line.gsub!(/<!--.*/, "")
      end
      indent_inspector.check_line(line: full_line, number: line_number, comment: in_xml_comment) if indent_inspector
      if  /'.*ignore-warning/i.match(full_line)
        lines_to_ignore.push line_number
      end
      full_file.push(full_line)
      file_no_comments.push(line)
    end
    @warnings += indent_inspector.warnings if indent_inspector
    no_comments = file_no_comments.join("")
    file = full_file.join("")
    @inspector_config.each do |line_inspector|
      unless line_inspector[:disabled]
        to_check = no_comments
        to_check = file if line_inspector[:include_comments]
        match  = nil
        start = 0
        loop do
          stop = to_check.length-1
          pass_match = nil
          if line_inspector[:pass_if_match]
            if line_inspector[:case_sensitive]
              pass_match = /#{line_inspector[:pass_test_regexp]}/.match(to_check[start..stop])
            else
              pass_match = /#{line_inspector[:pass_test_regexp]}/i.match(to_check[start..stop])
            end
            break unless pass_match
            stop = to_check.index("\n", start)
            stop ||= to_check.length-1
          end
          if line_inspector[:case_sensitive]
            match = /#{line_inspector[:regex]}/.match(to_check[start..stop])
          else
            match = /#{line_inspector[:regex]}/i.match(to_check[start..stop])
          end
          if (not line_inspector[:pass_if_match] and match) or (line_inspector[:pass_if_match] and not match)
            error_match = match
            if match
              line_number = to_check[0..match.begin(0)+start].split("\n", -1).count - 1
              start = match.end(0) + start
            else
              error_match = pass_match
              line_number = to_check[0..start].split("\n", -1).count - 1
              start = stop
            end
            unless lines_to_ignore.include?(line_number)
              add_warning(inspector: line_inspector, file: file_path, line: line_number, match: error_match)
            end
          elsif line_inspector[:pass_if_match]
            start = stop + 1
          else
            break
          end
        end
      end
    end
  end
  @warnings
end