Skip to content

Commit

Permalink
Refactor all theme file classes to inherit from ThemeFile
Browse files Browse the repository at this point in the history
Which implement all comment methods used by Analyzer

Fixes Shopify#339
  • Loading branch information
macournoyer committed Jul 5, 2021
1 parent a666c73 commit 19735bf
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 76 deletions.
1 change: 1 addition & 0 deletions lib/theme_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_relative "theme_check/version"
require_relative "theme_check/bug"
require_relative "theme_check/exceptions"
require_relative "theme_check/theme_file"
require_relative "theme_check/analyzer"
require_relative "theme_check/check"
require_relative "theme_check/checks_tracking"
Expand Down
18 changes: 3 additions & 15 deletions lib/theme_check/asset_file.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
# frozen_string_literal: true
require "pathname"
require "zlib"

module ThemeCheck
class AssetFile
class AssetFile < ThemeFile
def initialize(relative_path, storage)
@relative_path = relative_path
@storage = storage
super
@loaded = false
@content = nil
end

def path
@storage.path(@relative_path)
end

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

def content
@content ||= @storage.read(@relative_path)
end
alias_method :content, :source

def gzipped_size
@gzipped_size ||= Zlib.gzip(content).bytesize
Expand Down
31 changes: 2 additions & 29 deletions lib/theme_check/json_file.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
# frozen_string_literal: true
require "json"
require "pathname"

module ThemeCheck
class JsonFile
class JsonFile < ThemeFile
def initialize(relative_path, storage)
@relative_path = relative_path
@storage = storage
super
@loaded = false
@content = nil
@parser_error = nil
end

def path
@storage.path(@relative_path)
end

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

def source
@source ||= @storage.read(@relative_path)
end

def content
load!
@content
Expand All @@ -34,23 +20,10 @@ def parse_error
@parser_error
end

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

def json?
true
end

def liquid?
false
end

def ==(other)
other.is_a?(JsonFile) && relative_path == other.relative_path
end
alias_method :eql?, :==

private

def load!
Expand Down
33 changes: 1 addition & 32 deletions lib/theme_check/template.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
# frozen_string_literal: true
require "pathname"

module ThemeCheck
class Template
def initialize(relative_path, storage)
@storage = storage
@relative_path = relative_path
end

def path
@storage.path(@relative_path)
end

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

def source
@source ||= @storage.read(@relative_path)
end

class Template < ThemeFile
def write
content = updated_content
if source != content
Expand All @@ -28,14 +10,6 @@ def write
end
end

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

def json?
false
end

def liquid?
true
end
Expand Down Expand Up @@ -88,11 +62,6 @@ def root
parse.root
end

def ==(other)
other.is_a?(Template) && relative_path == other.relative_path
end
alias_method :eql?, :==

def self.parse(source)
Tags.register_tags!
Liquid::Template.parse(
Expand Down
40 changes: 40 additions & 0 deletions lib/theme_check/theme_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true
require "pathname"

module ThemeCheck
class ThemeFile
def initialize(relative_path, storage)
@relative_path = relative_path
@storage = storage
end

def path
@storage.path(@relative_path)
end

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

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

def source
@source ||= @storage.read(@relative_path)
end

def json?
false
end

def liquid?
false
end

def ==(other)
other.is_a?(self.class) && relative_path == other.relative_path
end
alias_method :eql?, :==
end
end
26 changes: 26 additions & 0 deletions test/analyzer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true
require "test_helper"

class AnalyzerTest < Minitest::Test
def setup
@theme = make_theme(
"assets/theme.js" => "",
"assets/theme.css" => "",
"templates/index.liquid" => "",
"snippets/product.liquid" => "",
"sections/article-template/template.liquid" => "",
"locales/en.default.json" => "",
)
@analyzer = ThemeCheck::Analyzer.new(@theme)
end

def test_analyze_theme
@analyzer.analyze_theme
refute_empty(@analyzer.offenses)
end

def test_analyze_files
@analyzer.analyze_files(@theme.all)
refute_empty(@analyzer.offenses)
end
end

0 comments on commit 19735bf

Please sign in to comment.