Class: ThemeCheck::ThemeFile

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

Direct Known Subclasses

AssetFile, JsonFile, LiquidFile

Instance Method Summary collapse

Constructor Details

#initialize(relative_path, storage) ⇒ ThemeFile



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

def initialize(relative_path, storage)
  @relative_path = relative_path
  @storage = storage
  @source = nil
  @eol = "\n"
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



52
53
54
# File 'lib/theme_check/theme_file.rb', line 52

def ==(other)
  other.is_a?(self.class) && relative_path == other.relative_path
end

#json?Boolean



44
45
46
# File 'lib/theme_check/theme_file.rb', line 44

def json?
  false
end

#liquid?Boolean



48
49
50
# File 'lib/theme_check/theme_file.rb', line 48

def liquid?
  false
end

#nameObject



21
22
23
# File 'lib/theme_check/theme_file.rb', line 21

def name
  relative_path.sub_ext('').to_s
end

#pathObject



13
14
15
# File 'lib/theme_check/theme_file.rb', line 13

def path
  @storage.path(@relative_path)
end

#relative_pathObject



17
18
19
# File 'lib/theme_check/theme_file.rb', line 17

def relative_path
  @relative_pathname ||= Pathname.new(@relative_path)
end

#sourceObject

For the corrector to work properly, we should have a simple mental model of the internal representation of eol characters (Windows uses \r\n, Linux uses \n).

Parser::Source::Buffer strips the \r from the source file, so if you are autocorrecting the file you might lose that info and cause a git diff. It also makes the node.start_index/end_index calculation break. That’s not cool.

So in here we track whether the source file has \r\n in it and we’ll make sure that the file we write has the same eol as the source file.



37
38
39
40
41
42
# File 'lib/theme_check/theme_file.rb', line 37

def source
  return @source if @source
  @source = @storage.read(@relative_path)
  @eol = @source.include?("\r\n") ? "\r\n" : "\n"
  @source = @source.gsub("\r\n", "\n")
end