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



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/theme_check/analyzer.rb', line 66

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

#analyze_themeObject



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

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
end

#correct_offensesObject



116
117
118
119
120
# File 'lib/theme_check/analyzer.rb', line 116

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



108
109
110
111
112
113
114
# File 'lib/theme_check/analyzer.rb', line 108

def uncorrectable_offenses
  unless @auto_correct
    return offenses
  end

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

#write_correctionsObject



122
123
124
125
126
127
# File 'lib/theme_check/analyzer.rb', line 122

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