Class: ThemeCheck::Analyzer

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Analyzer.



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

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

  @liquid_checks = Checks.new
  @json_checks = Checks.new
  @html_checks = Checks.new

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

    case check
    when LiquidCheck
      @liquid_checks << check
    when JsonCheck
      @json_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 theme checks
Returns single file checks offenses for file in `files` + whole theme checks

When only_single_file is true:

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

When files is empty and only_single_file is false:

Only returns whole theme checks

When files is empty and only_single_file is true:

Returns empty array


81
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
# File 'lib/theme_check/analyzer.rb', line 81

def analyze_files(files, only_single_file: false)
  reset

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

    unless only_single_file
      # Call all checks that run on the whole theme
      liquid_visitor = LiquidVisitor.new(@liquid_checks.whole_theme, @disabled_checks)
      html_visitor = HtmlVisitor.new(@html_checks.whole_theme)
      total += total_file_count
      offset = total_file_count
      @theme.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

      @theme.json.each_with_index do |json_file, i|
        yield(json_file.relative_path.to_s, liquid_file_count + i, total) if block_given?
        @json_checks.whole_theme.call(:on_file, json_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)
    html_visitor = HtmlVisitor.new(@html_checks.single_file)
    files.each_with_index do |theme_file, i|
      yield(theme_file.relative_path.to_s, offset + i, total) if block_given?
      if theme_file.liquid?
        liquid_visitor.visit_liquid_file(theme_file)
        html_visitor.visit_liquid_file(theme_file)
      elsif theme_file.json?
        @json_checks.single_file.call(:on_file, theme_file)
      end
    end
  end

  finish(only_single_file)

  offenses
end

#analyze_themeObject

Returns all offenses for all files in theme



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

def analyze_theme
  reset

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

  ThemeCheck.with_liquid_c_disabled do
    @theme.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

  @theme.json.each_with_index do |json_file, i|
    yield(json_file.relative_path.to_s, liquid_file_count + i, total_file_count) if block_given?
    @json_checks.call(:on_file, json_file)
  end

  finish(false)

  offenses
end

#correct_offensesObject



133
134
135
136
137
# File 'lib/theme_check/analyzer.rb', line 133

def correct_offenses
  if @auto_correct
    offenses.each(&:correct)
  end
end

#json_file_countObject



32
33
34
# File 'lib/theme_check/analyzer.rb', line 32

def json_file_count
  @json_file_count ||= @theme.json.size
end

#liquid_file_countObject



36
37
38
# File 'lib/theme_check/analyzer.rb', line 36

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

#offensesObject



26
27
28
29
30
# File 'lib/theme_check/analyzer.rb', line 26

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

#total_file_countObject



40
41
42
# File 'lib/theme_check/analyzer.rb', line 40

def total_file_count
  json_file_count + liquid_file_count
end

#uncorrectable_offensesObject



125
126
127
128
129
130
131
# File 'lib/theme_check/analyzer.rb', line 125

def uncorrectable_offenses
  unless @auto_correct
    return offenses
  end

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

#write_correctionsObject



139
140
141
142
143
144
# File 'lib/theme_check/analyzer.rb', line 139

def write_corrections
  if @auto_correct
    @theme.liquid.each(&:write)
    @theme.json.each(&:write)
  end
end