lib/theme_check/theme_file.rb in theme-check-1.5.2 vs lib/theme_check/theme_file.rb in theme-check-1.6.0

- old
+ new

@@ -4,10 +4,12 @@ module ThemeCheck class ThemeFile def initialize(relative_path, storage) @relative_path = relative_path @storage = storage + @source = nil + @eol = "\n" end def path @storage.path(@relative_path) end @@ -18,11 +20,26 @@ def name relative_path.sub_ext('').to_s end + # 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. def source - @source ||= @storage.read(@relative_path) + return @source if @source + @source = @storage.read(@relative_path) + @eol = @source.include?("\r\n") ? "\r\n" : "\n" + @source = @source.gsub("\r\n", "\n") end def json? false end