From 19735bf98f9a7dac7791718288cbe19b4496405b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Cournoyer?= Date: Mon, 5 Jul 2021 10:27:13 -0400 Subject: [PATCH] Refactor all theme file classes to inherit from ThemeFile Which implement all comment methods used by Analyzer Fixes #339 --- lib/theme_check.rb | 1 + lib/theme_check/asset_file.rb | 18 +++------------- lib/theme_check/json_file.rb | 31 ++------------------------- lib/theme_check/template.rb | 33 +---------------------------- lib/theme_check/theme_file.rb | 40 +++++++++++++++++++++++++++++++++++ test/analyzer_test.rb | 26 +++++++++++++++++++++++ 6 files changed, 73 insertions(+), 76 deletions(-) create mode 100644 lib/theme_check/theme_file.rb create mode 100644 test/analyzer_test.rb diff --git a/lib/theme_check.rb b/lib/theme_check.rb index 28be0a3a..a9f870dc 100644 --- a/lib/theme_check.rb +++ b/lib/theme_check.rb @@ -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" diff --git a/lib/theme_check/asset_file.rb b/lib/theme_check/asset_file.rb index 03a05c0b..d646ed8d 100644 --- a/lib/theme_check/asset_file.rb +++ b/lib/theme_check/asset_file.rb @@ -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 diff --git a/lib/theme_check/json_file.rb b/lib/theme_check/json_file.rb index 4159700c..ec87026e 100644 --- a/lib/theme_check/json_file.rb +++ b/lib/theme_check/json_file.rb @@ -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 @@ -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! diff --git a/lib/theme_check/template.rb b/lib/theme_check/template.rb index 8b4cc3b2..731da4a5 100644 --- a/lib/theme_check/template.rb +++ b/lib/theme_check/template.rb @@ -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 @@ -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 @@ -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( diff --git a/lib/theme_check/theme_file.rb b/lib/theme_check/theme_file.rb new file mode 100644 index 00000000..ba62032d --- /dev/null +++ b/lib/theme_check/theme_file.rb @@ -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 diff --git a/test/analyzer_test.rb b/test/analyzer_test.rb new file mode 100644 index 00000000..f800a8d4 --- /dev/null +++ b/test/analyzer_test.rb @@ -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