Class: PlatformosCheck::Analyzer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(platformos_app, checks = Check.all.map(&:new), auto_correct = false, store_warnings = 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
26
27
# File 'lib/platformos_check/analyzer.rb', line 5

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

  @liquid_checks = Checks.new
  @yaml_checks = Checks.new
  @html_checks = Checks.new
  @warnings = {}

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

#warningsObject (readonly)

Returns the value of attribute warnings.



35
36
37
# File 'lib/platformos_check/analyzer.rb', line 35

def warnings
  @warnings
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


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
125
126
127
128
129
# File 'lib/platformos_check/analyzer.rb', line 87

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/platformos_check/analyzer.rb', line 50

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)
      @warnings[liquid_file.path] = liquid_file.warnings if @store_warnings && !liquid_file.warnings&.empty?
    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



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

def correct_offenses
  return unless @auto_correct

  offenses.each(&:correct)
end

#liquid_file_countObject



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

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

#offensesObject



29
30
31
32
33
# File 'lib/platformos_check/analyzer.rb', line 29

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

#total_file_countObject



45
46
47
# File 'lib/platformos_check/analyzer.rb', line 45

def total_file_count
  yaml_file_count + liquid_file_count
end

#uncorrectable_offensesObject



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

def uncorrectable_offenses
  return offenses unless @auto_correct

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

#write_correctionsObject



143
144
145
146
147
# File 'lib/platformos_check/analyzer.rb', line 143

def write_corrections
  return unless @auto_correct

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

#yaml_file_countObject



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

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