Class: ThemeCheck::LiquidFile

Inherits:
ThemeFile show all
Defined in:
lib/theme_check/liquid_file.rb

Instance Attribute Summary

Attributes inherited from ThemeFile

#storage, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ThemeFile

#==, #initialize, #json?, #name, #path, #relative_path, #source

Constructor Details

This class inherits a constructor from ThemeCheck::ThemeFile

Class Method Details

.parse(source) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/theme_check/liquid_file.rb', line 66

def self.parse(source)
  Tags.register_tags!
  Liquid::Template.parse(
    source,
    line_numbers: true,
    error_mode: :warn,
    disable_liquid_c_nodes: true,
  )
end

Instance Method Details

#liquid?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/theme_check/liquid_file.rb', line 14

def liquid?
  true
end

#parseObject



54
55
56
# File 'lib/theme_check/liquid_file.rb', line 54

def parse
  @ast ||= self.class.parse(source)
end

#rewriterObject



30
31
32
# File 'lib/theme_check/liquid_file.rb', line 30

def rewriter
  @rewriter ||= ThemeFileRewriter.new(@relative_path, source)
end

#rootObject



62
63
64
# File 'lib/theme_check/liquid_file.rb', line 62

def root
  parse.root
end

#section?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/theme_check/liquid_file.rb', line 22

def section?
  name.start_with?('sections')
end

#snippet?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/theme_check/liquid_file.rb', line 26

def snippet?
  name.start_with?('snippets')
end

#source_excerpt(line) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/theme_check/liquid_file.rb', line 34

def source_excerpt(line)
  original_lines = source.split("\n")
  original_lines[bounded(0, line - 1, original_lines.size - 1)].strip
rescue => e
  ThemeCheck.bug(<<~EOS)
    Exception while running `source_excerpt(#{line})`:
    ```
    #{e.class}: #{e.message}
      #{e.backtrace.join("\n  ")}
    ```

    path: #{path}

    source:
    ```
    #{source}
    ```
  EOS
end

#template?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/theme_check/liquid_file.rb', line 18

def template?
  name.start_with?('templates')
end

#warningsObject



58
59
60
# File 'lib/theme_check/liquid_file.rb', line 58

def warnings
  @ast.warnings
end

#writeObject



5
6
7
8
9
10
11
12
# File 'lib/theme_check/liquid_file.rb', line 5

def write
  content = rewriter.to_s
  if source != content
    @storage.write(@relative_path, content.gsub("\n", @eol))
    @source = content
    @rewriter = nil
  end
end