Class: PlatformosCheck::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/platformos_check/analyzer.rb

Instance Method Summary collapse

Constructor Details

#initialize(platformos_app, checks = Check.all.map(&:new), auto_correct = false) ⇒ Analyzer

Returns a new instance of Analyzer.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/platformos_check/analyzer.rb', line 5

def initialize(platformos_app, checks = Check.all.map(&:new), auto_correct = false)
  @platformos_app = platformos_app
  @auto_correct = auto_correct

  @liquid_checks = Checks.new
  @yaml_checks = Checks.new
  @html_checks = Checks.new

  checks.each do |check|
    check.platformos_app = @platformos_app

    case check
    when LiquidCheck
      @liquid_checks << check
    when YamlCheck
      @yaml_checks << check
    when HtmlCheck
      @html_checks << check
    end
  end
end

Instance Method Details

#analyze_files(files, only_single_file: false) ⇒ Object

When only_single_file is false:

Runs single file checks for each file in `files`
Runs whole platformos_app checks
Returns single file checks offenses for file in `files` + whole platformos_app checks

When only_single_file is true:

Runs single file checks for each file in `files`
Does not run whole platformos_app checks
Returns single file checks offenses for file in `files`

When files is empty and only_single_file is false:

Only returns whole platformos_app checks

When files is empty and only_single_file is true:

Returns empty array


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/platformos_check/analyzer.rb', line 82

def analyze_files(files, only_single_file: false)
  reset

  PlatformosCheck.with_liquid_c_disabled do
    total = files.size
    offset = 0

    unless only_single_file
      # Call all checks that run on the whole platformos_app
      liquid_visitor = LiquidVisitor.new(@liquid_checks.whole_platformos_app, @disabled_checks)
      html_visitor = HtmlVisitor.new(@html_checks.whole_platformos_app)
      total += total_file_count
      offset = total_file_count
      @platformos_app.liquid.each_with_index do |liquid_file, i|
        yield(liquid_file.relative_path.to_s, i, total) if block_given?
        liquid_visitor.visit_liquid_file(liquid_file)
        html_visitor.visit_liquid_file(liquid_file)
      end

      @platformos_app.yaml.each_with_index do |yaml_file, i|
        yield(yaml_file.relative_path.to_s, liquid_file_count + i, total) if block_given?
        @yaml_checks.whole_platformos_app.call(:on_file, yaml_file)
      end
    end

    # Call checks that run on a single files, only on specified file
    liquid_visitor = LiquidVisitor.new(@liquid_checks.single_file, @disabled_checks, only_single_file:)
    html_visitor = HtmlVisitor.new(@html_checks.single_file)
    files.each_with_index do |app_file, i|
      yield(app_file.relative_path.to_s, offset + i, total) if block_given?
      if app_file.liquid?
        liquid_visitor.visit_liquid_file(app_file)
        html_visitor.visit_liquid_file(app_file)
      elsif app_file.yaml?
        @yaml_checks.single_file.call(:on_file, app_file)
      end
    end
  end

  finish(only_single_file)

  offenses
end

#analyze_platformos_appObject

Returns all offenses for all files in platformos_app



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/platformos_check/analyzer.rb', line 46

def analyze_platformos_app
  reset

  liquid_visitor = LiquidVisitor.new(@liquid_checks, @disabled_checks)
  html_visitor = HtmlVisitor.new(@html_checks)

  PlatformosCheck.with_liquid_c_disabled do
    @platformos_app.liquid.each_with_index do |liquid_file, i|
      yield(liquid_file.relative_path.to_s, i, total_file_count) if block_given?
      liquid_visitor.visit_liquid_file(liquid_file)
      html_visitor.visit_liquid_file(liquid_file)
    end
  end

  @platformos_app.yaml.each_with_index do |yaml_file, i|
    yield(yaml_file.relative_path.to_s, liquid_file_count + i, total_file_count) if block_given?
    @yaml_checks.call(:on_file, yaml_file)
  end

  finish(false)

  offenses
end

#correct_offensesObject



132
133
134
135
136
# File 'lib/platformos_check/analyzer.rb', line 132

def correct_offenses
  return unless @auto_correct

  offenses.each(&:correct)
end

#liquid_file_countObject



37
38
39
# File 'lib/platformos_check/analyzer.rb', line 37

def liquid_file_count
  @liquid_file_count ||= @platformos_app.liquid.size
end

#offensesObject



27
28
29
30
31
# File 'lib/platformos_check/analyzer.rb', line 27

def offenses
  @liquid_checks.flat_map(&:offenses) +
    @yaml_checks.flat_map(&:offenses) +
    @html_checks.flat_map(&:offenses)
end

#total_file_countObject



41
42
43
# File 'lib/platformos_check/analyzer.rb', line 41

def total_file_count
  yaml_file_count + liquid_file_count
end

#uncorrectable_offensesObject



126
127
128
129
130
# File 'lib/platformos_check/analyzer.rb', line 126

def uncorrectable_offenses
  return offenses unless @auto_correct

  offenses.select { |offense| !offense.correctable? }
end

#write_correctionsObject



138
139
140
141
142
# File 'lib/platformos_check/analyzer.rb', line 138

def write_corrections
  return unless @auto_correct

  @platformos_app.liquid.each(&:write)
end

#yaml_file_countObject



33
34
35
# File 'lib/platformos_check/analyzer.rb', line 33

def yaml_file_count
  @yaml_file_count ||= @platformos_app.yaml.size
end