theme-check 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,7 @@ module ThemeCheck
14
14
 
15
15
  def initialize(server)
16
16
  @server = server
17
+ @previously_reported_files = Set.new
17
18
  end
18
19
 
19
20
  def on_initialize(id, params)
@@ -41,28 +42,40 @@ module ThemeCheck
41
42
  def analyze_and_send_offenses(file_path)
42
43
  root = ThemeCheck::Config.find(file_path) || @root_path
43
44
  config = ThemeCheck::Config.from_path(root)
44
- theme = ThemeCheck::Theme.new(config.root)
45
- analyzer = ThemeCheck::Analyzer.new(theme, config.enabled_checks)
45
+ storage = ThemeCheck::FileSystemStorage.new(
46
+ config.root,
47
+ ignored_patterns: config.ignored_patterns
48
+ )
49
+ theme = ThemeCheck::Theme.new(storage)
50
+
51
+ offenses = analyze(theme, config)
52
+ log("Found #{theme.all.size} templates, and #{offenses.size} offenses")
53
+ send_diagnostics(offenses)
54
+ end
46
55
 
56
+ def analyze(theme, config)
57
+ analyzer = ThemeCheck::Analyzer.new(theme, config.enabled_checks)
47
58
  log("Checking #{config.root}")
48
59
  analyzer.analyze_theme
49
- log("Found #{theme.all.size} templates, and #{analyzer.offenses.size} offenses")
50
- send_diagnostics(analyzer.offenses, theme.all)
60
+ analyzer.offenses
51
61
  end
52
62
 
53
- def send_diagnostics(offenses, templates)
54
- contains_offenses = []
63
+ def send_diagnostics(offenses)
64
+ reported_files = Set.new
55
65
 
56
66
  offenses.group_by(&:template).each do |template, template_offenses|
57
67
  next unless template
58
68
  send_diagnostic(template.path, template_offenses)
59
- contains_offenses.push(template.path)
69
+ reported_files << template.path
60
70
  end
61
71
 
62
- # Publish diagnostics with empty array if template does not contain error
63
- templates.select { |t| !contains_offenses.include?(t.path) }.each do |template|
64
- send_diagnostic(template.path, [])
72
+ # Publish diagnostics with empty array if all issues on a previously reported template
73
+ # have been solved.
74
+ (@previously_reported_files - reported_files).each do |path|
75
+ send_diagnostic(path, [])
65
76
  end
77
+
78
+ @previously_reported_files = reported_files
66
79
  end
67
80
 
68
81
  def send_diagnostic(path, offenses)
@@ -9,6 +9,8 @@ module ThemeCheck
9
9
  class IncompatibleStream < StandardError; end
10
10
 
11
11
  class Server
12
+ attr_reader :handler
13
+
12
14
  def initialize(
13
15
  in_stream: STDIN,
14
16
  out_stream: STDOUT,
@@ -87,8 +89,6 @@ module ThemeCheck
87
89
 
88
90
  if @handler.respond_to?(method_name)
89
91
  @handler.send(method_name, id, params)
90
- else
91
- log("Handler does not respond to #{method_name}")
92
92
  end
93
93
  end
94
94
 
@@ -11,6 +11,12 @@ module ThemeCheck
11
11
  YAML.load(File.read("#{__dir__}/../../../data/shopify_liquid/objects.yml"))
12
12
  end
13
13
  end
14
+
15
+ def plus_labels
16
+ @plus_labels ||= begin
17
+ YAML.load(File.read("#{__dir__}/../../../data/shopify_liquid/plus_objects.yml"))
18
+ end
19
+ end
14
20
  end
15
21
  end
16
22
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ThemeCheck
4
+ class Storage
5
+ def read(relative_path)
6
+ raise NotImplementedError
7
+ end
8
+
9
+ def write(relative_path, content)
10
+ raise NotImplementedError
11
+ end
12
+
13
+ def path(relative_path)
14
+ raise NotImplementedError
15
+ end
16
+
17
+ def files
18
+ raise NotImplementedError
19
+ end
20
+
21
+ def directories
22
+ raise NotImplementedError
23
+ end
24
+ end
25
+ end
@@ -3,16 +3,29 @@ require "pathname"
3
3
 
4
4
  module ThemeCheck
5
5
  class Template
6
- attr_reader :path
6
+ def initialize(relative_path, storage)
7
+ @storage = storage
8
+ @relative_path = relative_path
9
+ end
7
10
 
8
- def initialize(path, root)
9
- @path = Pathname(path)
10
- @root = Pathname(root)
11
- @updated = false
11
+ def path
12
+ @storage.path(@relative_path)
12
13
  end
13
14
 
14
15
  def relative_path
15
- @path.relative_path_from(@root)
16
+ @relative_pathname ||= Pathname.new(@relative_path)
17
+ end
18
+
19
+ def source
20
+ @source ||= @storage.read(@relative_path)
21
+ end
22
+
23
+ def write
24
+ content = updated_content
25
+ if source != content
26
+ @storage.write(@relative_path, content)
27
+ @source = content
28
+ end
16
29
  end
17
30
 
18
31
  def name
@@ -31,15 +44,17 @@ module ThemeCheck
31
44
  name.start_with?('snippets')
32
45
  end
33
46
 
34
- def source
35
- @source ||= @path.read
36
- end
37
-
38
47
  def lines
39
48
  # Retain trailing newline character
40
49
  @lines ||= source.split("\n", -1)
41
50
  end
42
51
 
52
+ # Not entirely obvious but lines is mutable, corrections are to be
53
+ # applied on @lines.
54
+ def updated_content
55
+ lines.join("\n")
56
+ end
57
+
43
58
  def excerpt(line)
44
59
  lines[line - 1].strip
45
60
  end
@@ -65,18 +80,8 @@ module ThemeCheck
65
80
  parse.root
66
81
  end
67
82
 
68
- def update!
69
- @updated = true
70
- end
71
-
72
- def write
73
- if @updated
74
- @path.write(lines.join("\n"))
75
- end
76
- end
77
-
78
83
  def ==(other)
79
- other.is_a?(Template) && @path == other.path
84
+ other.is_a?(Template) && relative_path == other.relative_path
80
85
  end
81
86
 
82
87
  def self.parse(source)
@@ -4,18 +4,27 @@ require "pathname"
4
4
  module ThemeCheck
5
5
  class Theme
6
6
  DEFAULT_LOCALE_REGEXP = %r{^locales/(.*)\.default$}
7
- attr_reader :root
7
+ LIQUID_REGEX = /\.liquid$/i
8
+ JSON_REGEX = /\.json$/i
8
9
 
9
- def initialize(root)
10
- @root = Pathname.new(root)
10
+ def initialize(storage)
11
+ @storage = storage
11
12
  end
12
13
 
13
14
  def liquid
14
- @liquid ||= @root.glob("**/*.liquid").map { |path| Template.new(path, @root) }
15
+ @liquid ||= @storage.files
16
+ .select { |path| LIQUID_REGEX.match?(path) }
17
+ .map { |path| Template.new(path, @storage) }
15
18
  end
16
19
 
17
20
  def json
18
- @json ||= @root.glob("**/*.json").map { |path| JsonFile.new(path, @root) }
21
+ @json ||= @storage.files
22
+ .select { |path| JSON_REGEX.match?(path) }
23
+ .map { |path| JsonFile.new(path, @storage) }
24
+ end
25
+
26
+ def directories
27
+ @storage.directories
19
28
  end
20
29
 
21
30
  def default_locale_json
@@ -52,9 +61,5 @@ module ThemeCheck
52
61
  def snippets
53
62
  liquid.select(&:snippet?)
54
63
  end
55
-
56
- def directories
57
- @directories ||= @root.glob('*').select { |f| File.directory?(f) }.map { |f| f.relative_path_from(@root) }
58
- end
59
64
  end
60
65
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ThemeCheck
3
- VERSION = "0.2.2"
3
+ VERSION = "0.3.0"
4
4
  end
@@ -6,12 +6,15 @@ module ThemeCheck
6
6
  end
7
7
 
8
8
  def visit_template(template)
9
+ @disabled_checks = DisabledChecks.new
9
10
  visit(Node.new(template.root, nil, template))
10
11
  rescue Liquid::Error => exception
11
12
  exception.template_name = template.name
12
13
  call_checks(:on_error, exception)
13
14
  end
14
15
 
16
+ private
17
+
15
18
  def visit(node)
16
19
  call_checks(:on_node, node)
17
20
  call_checks(:on_tag, node) if node.tag?
@@ -22,16 +25,24 @@ module ThemeCheck
22
25
  call_checks(:after_tag, node) if node.tag?
23
26
  call_checks(:after_node, node)
24
27
  end
25
- end
26
28
 
27
- private
29
+ @disabled_checks.update(node) if node.comment?
30
+ end
28
31
 
29
32
  def visit_children(node)
30
33
  node.children.each { |child| visit(child) }
31
34
  end
32
35
 
33
36
  def call_checks(method, *args)
34
- @checks.call(method, *args)
37
+ checks.call(method, *args)
38
+ end
39
+
40
+ def checks
41
+ return @checks unless @disabled_checks.any?
42
+
43
+ return @checks.always_enabled if @disabled_checks.all_disabled?
44
+
45
+ @checks.except_for(@disabled_checks)
35
46
  end
36
47
  end
37
48
  end
@@ -61,12 +61,16 @@ class ThemeCheck < Formula
61
61
  ENV['PATH'] = ENV['PATH'].sub(HOMEBREW_SHIMS_PATH.to_s, '/usr/local/bin')
62
62
  end
63
63
 
64
- system("#{ruby_bin}/gem", "install", cached_download,
65
- "--no-document",
66
- "--no-wrapper",
67
- "--no-user-install",
68
- "--install-dir", prefix,
69
- "--bindir", bin)
64
+ system(
65
+ "#{ruby_bin}/gem",
66
+ "install",
67
+ cached_download,
68
+ "--no-document",
69
+ "--no-wrapper",
70
+ "--no-user-install",
71
+ "--install-dir", prefix,
72
+ "--bindir", bin
73
+ )
70
74
 
71
75
  raise "gem install 'theme-check' failed with status #{$CHILD_STATUS.exitstatus}" unless $CHILD_STATUS.success?
72
76
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: theme-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marc-André Cournoyer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-22 00:00:00.000000000 Z
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liquid
@@ -71,12 +71,14 @@ files:
71
71
  - Guardfile
72
72
  - LICENSE.md
73
73
  - README.md
74
+ - RELEASING.md
74
75
  - Rakefile
75
76
  - bin/liquid-server
76
77
  - config/default.yml
77
78
  - data/shopify_liquid/deprecated_filters.yml
78
79
  - data/shopify_liquid/filters.yml
79
80
  - data/shopify_liquid/objects.yml
81
+ - data/shopify_liquid/plus_objects.yml
80
82
  - dev.yml
81
83
  - docs/preview.png
82
84
  - exe/theme-check
@@ -91,9 +93,11 @@ files:
91
93
  - lib/theme_check/checks/liquid_tag.rb
92
94
  - lib/theme_check/checks/matching_schema_translations.rb
93
95
  - lib/theme_check/checks/matching_translations.rb
96
+ - lib/theme_check/checks/missing_enable_comment.rb
94
97
  - lib/theme_check/checks/missing_required_template_files.rb
95
98
  - lib/theme_check/checks/missing_template.rb
96
99
  - lib/theme_check/checks/nested_snippet.rb
100
+ - lib/theme_check/checks/parser_blocking_javascript.rb
97
101
  - lib/theme_check/checks/required_directories.rb
98
102
  - lib/theme_check/checks/required_layout_theme_object.rb
99
103
  - lib/theme_check/checks/space_inside_braces.rb
@@ -111,6 +115,9 @@ files:
111
115
  - lib/theme_check/cli.rb
112
116
  - lib/theme_check/config.rb
113
117
  - lib/theme_check/corrector.rb
118
+ - lib/theme_check/disabled_checks.rb
119
+ - lib/theme_check/file_system_storage.rb
120
+ - lib/theme_check/in_memory_storage.rb
114
121
  - lib/theme_check/json_check.rb
115
122
  - lib/theme_check/json_file.rb
116
123
  - lib/theme_check/json_helpers.rb
@@ -128,6 +135,7 @@ files:
128
135
  - lib/theme_check/shopify_liquid/deprecated_filter.rb
129
136
  - lib/theme_check/shopify_liquid/filter.rb
130
137
  - lib/theme_check/shopify_liquid/object.rb
138
+ - lib/theme_check/storage.rb
131
139
  - lib/theme_check/tags.rb
132
140
  - lib/theme_check/template.rb
133
141
  - lib/theme_check/theme.rb