theme-check 0.10.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/theme-check.yml +2 -6
  3. data/CHANGELOG.md +41 -0
  4. data/README.md +39 -0
  5. data/RELEASING.md +34 -2
  6. data/Rakefile +1 -1
  7. data/config/default.yml +39 -3
  8. data/config/nothing.yml +11 -0
  9. data/config/theme_app_extension.yml +153 -0
  10. data/data/shopify_liquid/objects.yml +2 -0
  11. data/docs/checks/asset_size_app_block_css.md +52 -0
  12. data/docs/checks/asset_size_app_block_javascript.md +57 -0
  13. data/docs/checks/asset_size_css_stylesheet_tag.md +50 -0
  14. data/docs/checks/deprecate_bgsizes.md +66 -0
  15. data/docs/checks/deprecate_lazysizes.md +61 -0
  16. data/docs/checks/html_parsing_error.md +50 -0
  17. data/docs/checks/liquid_tag.md +2 -2
  18. data/docs/checks/template_length.md +12 -2
  19. data/exe/theme-check-language-server.bat +3 -0
  20. data/exe/theme-check.bat +3 -0
  21. data/lib/theme_check.rb +15 -0
  22. data/lib/theme_check/analyzer.rb +25 -21
  23. data/lib/theme_check/asset_file.rb +3 -15
  24. data/lib/theme_check/bug.rb +3 -1
  25. data/lib/theme_check/check.rb +24 -2
  26. data/lib/theme_check/checks/asset_size_app_block_css.rb +44 -0
  27. data/lib/theme_check/checks/asset_size_app_block_javascript.rb +44 -0
  28. data/lib/theme_check/checks/asset_size_css.rb +11 -74
  29. data/lib/theme_check/checks/asset_size_css_stylesheet_tag.rb +24 -0
  30. data/lib/theme_check/checks/asset_size_javascript.rb +10 -36
  31. data/lib/theme_check/checks/deprecate_bgsizes.rb +14 -0
  32. data/lib/theme_check/checks/deprecate_lazysizes.rb +16 -0
  33. data/lib/theme_check/checks/html_parsing_error.rb +12 -0
  34. data/lib/theme_check/checks/img_lazy_loading.rb +1 -6
  35. data/lib/theme_check/checks/liquid_tag.rb +2 -2
  36. data/lib/theme_check/checks/remote_asset.rb +2 -0
  37. data/lib/theme_check/checks/space_inside_braces.rb +1 -1
  38. data/lib/theme_check/checks/template_length.rb +18 -4
  39. data/lib/theme_check/cli.rb +34 -13
  40. data/lib/theme_check/config.rb +56 -10
  41. data/lib/theme_check/exceptions.rb +29 -27
  42. data/lib/theme_check/html_check.rb +2 -0
  43. data/lib/theme_check/html_visitor.rb +3 -1
  44. data/lib/theme_check/json_file.rb +2 -29
  45. data/lib/theme_check/language_server/constants.rb +8 -0
  46. data/lib/theme_check/language_server/document_link_engine.rb +40 -4
  47. data/lib/theme_check/language_server/handler.rb +1 -1
  48. data/lib/theme_check/language_server/server.rb +13 -2
  49. data/lib/theme_check/liquid_check.rb +0 -12
  50. data/lib/theme_check/parsing_helpers.rb +3 -1
  51. data/lib/theme_check/regex_helpers.rb +17 -0
  52. data/lib/theme_check/tags.rb +62 -8
  53. data/lib/theme_check/template.rb +3 -32
  54. data/lib/theme_check/theme_file.rb +40 -0
  55. data/lib/theme_check/version.rb +1 -1
  56. metadata +22 -3
@@ -0,0 +1,52 @@
1
+ # Prevent Large CSS bundles (`AssetSizeAppBlockCSS`)
2
+
3
+ This rule exists to prevent large CSS bundles from being included via Theme App Extensions (for speed).
4
+
5
+ ## Check Details
6
+
7
+ This rule disallows the use of too much CSS in themes, as configured by `threshold_in_bytes`.
8
+
9
+ :-1: Examples of **incorrect** code for this check:
10
+ ```liquid
11
+ <!-- Here, assets/app.css is **greater** than `threshold_in_bytes` compressed. -->
12
+ {% schema %}
13
+ {
14
+ ...
15
+ "stylesheet": "app.css"
16
+ }
17
+ {% endschema %}
18
+ ```
19
+
20
+ ## Check Options
21
+
22
+ The default configuration is the following:
23
+
24
+ ```yaml
25
+ AssetSizeAppBlockCSS:
26
+ enabled: false
27
+ threshold_in_bytes: 100_000
28
+ ```
29
+
30
+ ### `threshold_in_bytes`
31
+
32
+ The `threshold_in_bytes` option (default: `100_000`) determines the maximum allowed compressed size in bytes that a single CSS file can take.
33
+
34
+ This includes theme and remote stylesheets.
35
+
36
+ ## When Not To Use It
37
+
38
+ This rule should not be disabled locally since the check will be enforced when
39
+ promoting new versions of the extension.
40
+
41
+ ## Version
42
+
43
+ This check has been introduced in 1.1.0
44
+
45
+ ## Resources
46
+
47
+ - [The Performance Inequality Gap](https://infrequently.org/2021/03/the-performance-inequality-gap/)
48
+ - [Rule Source][codesource]
49
+ - [Documentation Source][docsource]
50
+
51
+ [codesource]: /lib/theme_check/checks/asset_size_app_block_css.rb
52
+ [docsource]: /docs/checks/asset_size_app_block_css.md
@@ -0,0 +1,57 @@
1
+ # Prevent Abuse on Server Rendered App Blocks (`AssetSizeAppBlockJavaScript`)
2
+
3
+ For server rendered app blocks, it is an anti-pattern to execute large JavaScript bundles on every page load
4
+
5
+ This doesn't mean they don't have a reason to exist. For instance, chat widgets are mini applications embedded inside web pages. Designing such an app with server rendered updates would be absurd. However, if only 10% of the users interact with the chat widget, the other 90% should not have to execute the entire bundle on every page load.
6
+
7
+ The natural solution to this problem is to implement the chat widget using the [Import on Interaction Pattern][ioip].
8
+
9
+ ## Check Details
10
+
11
+ This rule disallows the use of block JavaScript files and external scripts to have a compressed size greater than a configured `threshold_in_bytes`.
12
+
13
+ :-1: Examples of **incorrect** code for this check:
14
+ ```liquid
15
+ <!-- Here assets/chat-widget.js is more than 10KB gzipped. -->
16
+ {% schema %}
17
+ {
18
+ ...
19
+ "javascript": "chat-widget.js"
20
+ }
21
+ {% endschema %}
22
+ ```
23
+
24
+ ## Check Options
25
+
26
+ The default configuration is the following:
27
+
28
+ ```yaml
29
+ AssetSizeAppBlockJavaScript:
30
+ enabled: true
31
+ threshold_in_bytes: 10000
32
+ ```
33
+
34
+ ### `threshold_in_bytes`
35
+
36
+ The `threshold_in_bytes` option (default: `10000`) determines the maximum allowed compressed size in bytes that a single JavaScript file can take.
37
+
38
+ This includes theme and remote scripts.
39
+
40
+ ## When Not To Use It
41
+
42
+ This rule should not be disabled locally since the check will be enforced when
43
+ promoting new versions of the extension.
44
+
45
+ ## Version
46
+
47
+ This check has been introduced in 1.1.0
48
+
49
+ ## Resources
50
+
51
+ - [The Import On Interaction Pattern][ioip]
52
+ - [Rule Source][codesource]
53
+ - [Documentation Source][docsource]
54
+
55
+ [ioip]: https://addyosmani.com/blog/import-on-interaction/
56
+ [codesource]: /lib/theme_check/checks/asset_size_app_block_javascript.rb
57
+ [docsource]: /docs/checks/asset_size_app_block_javascript.md
@@ -0,0 +1,50 @@
1
+ # Check Title (`AssetSizeCSSStylesheetTag`)
2
+
3
+ The `stylesheet_tag` filter generates a link tag that points to a given stylesheet. This rule exists to prevent large CSS bundles (for speed).
4
+
5
+ ## Check Details
6
+
7
+ This rule disallows the use of too much CSS in themes, as configured by `threshold_in_bytes`.
8
+
9
+ :-1: Examples of **incorrect** code for this check:
10
+ ```liquid
11
+ <!-- Here, assets/theme.css is **greater** than `threshold_in_bytes` compressed. -->
12
+ {{ 'theme.css' | asset_url | stylesheet_tag }}
13
+ ```
14
+
15
+ :+1: Example of **correct** code for this check:
16
+ ```liquid
17
+ <!-- Here, assets/theme.css is **less** than `threshold_in_bytes` compressed. -->
18
+ {{ 'theme.css' | asset_url | stylesheet_tag }}
19
+ ```
20
+
21
+ ## Check Options
22
+
23
+ The default configuration for this check is the following:
24
+
25
+ ```yaml
26
+ AssetSizeCSSStylesheetTag:
27
+ enabled: false
28
+ threshold_in_bytes: 100_000
29
+ ```
30
+
31
+ ### `threshold_in_bytes`
32
+
33
+ The `threshold_in_bytes` option (default: `100_000`) determines the maximum allowed compressed size in bytes that a single CSS file can take.
34
+
35
+ ## When Not To Use It
36
+
37
+ This rule is safe to disable.
38
+
39
+ ## Version
40
+
41
+ This check has been introduced in Theme Check 1.0.0
42
+
43
+ ## Resources
44
+
45
+ - [The Performance Inequality Gap](https://infrequently.org/2021/03/the-performance-inequality-gap/)
46
+ - [Rule Source][codesource]
47
+ - [Documentation Source][docsource]
48
+
49
+ [codesource]: /lib/theme_check/checks/asset_size_css_stylesheet_tag.rb
50
+ [docsource]: /docs/checks/asset_size_css_stylesheet_tag.md
@@ -0,0 +1,66 @@
1
+ # Deprecate Bgsizes (`DeprecateBgsizes`)
2
+
3
+ The lazySizes bgset extension allows you to define multiple background images with a width descriptor. The extension will then load the best image size for the current viewport and device (https://github.com/aFarkas/lazysizes/tree/gh-pages/plugins/bgset)
4
+
5
+
6
+ ## Check Details
7
+
8
+ This check is aimed at discouraging the use of the lazySizes bgset plugin
9
+
10
+ :-1: Examples of **incorrect** code for this check:
11
+
12
+ ```liquid
13
+
14
+ <!-- Reports use of "lazyload" class and "data-bgset" attribute -->
15
+
16
+ <script src="ls.bgset.min.js"></script>
17
+ <script src="lazysizes.min.js"></script>
18
+ <div class="lazyload" data-bgset="image-200.jpg 200w, image-300.jpg 300w, image-400.jpg 400w" data-sizes="auto">
19
+ </div>
20
+
21
+ ```
22
+
23
+ :+1: Examples of **correct** code for this check:
24
+
25
+ ```liquid
26
+
27
+ <!-- Uses the CSS image-set() attribute instead of "data-bgset" -->
28
+ <!-- CSS Stylesheet -->
29
+ .box {
30
+ background-image: -webkit-image-set(
31
+ url("small-balloons.jpg") 1x,
32
+ url("large-balloons.jpg") 2x);
33
+ background-image: image-set(
34
+ url("small-balloons.jpg") 1x,
35
+ url("large-balloons.jpg") 2x);
36
+ }
37
+
38
+ <!-- HTML -->
39
+ <div class="box"></div>
40
+
41
+ ```
42
+
43
+ ## Check Options
44
+
45
+ The default configuration for this check is the following:
46
+
47
+ ```yaml
48
+ DeprecateBgsizes:
49
+ enabled: true
50
+ ```
51
+
52
+ ## When Not To Use It
53
+
54
+ You should disable this rule in older browsers that don't support the CSS image-set attribute.
55
+
56
+ ## Version
57
+
58
+ This check has been introduced in Theme Check 1.0.0.
59
+
60
+ ## Resources
61
+
62
+ - [Rule Source][codesource]
63
+ - [Documentation Source][docsource]
64
+
65
+ [codesource]: /lib/theme_check/checks/deprecate_bgsizes.rb
66
+ [docsource]: /docs/checks/deprecate_bgsizes.md
@@ -0,0 +1,61 @@
1
+ # Deprecate lazySizes (`DeprecateLazysizes`)
2
+
3
+ [lazysizes](https://github.com/aFarkas/lazysizes) is a common JavaScript library used to lazy load images, iframes and scripts.
4
+
5
+ ## Check Details
6
+
7
+ This check is aimed at discouraging the use of the lazysizes JavaScript library
8
+
9
+ :-1: Examples of **incorrect** code for this check:
10
+
11
+ ```liquid
12
+
13
+ <!-- Reports use of "lazyload" class -->
14
+ <img src="a.jpg" class="lazyload">
15
+
16
+ <!-- Reports use of "data-srcset" and "data-sizes" attribute. Reports data-sizes="auto" -->
17
+ <img
18
+ alt="House by the lake"
19
+ data-sizes="auto"
20
+ data-srcset="small.jpg 500w,
21
+ medium.jpg 640w,
22
+ big.jpg 1024w"
23
+ data-src="medium.jpg"
24
+ class="lazyload"
25
+ />
26
+
27
+ ```
28
+
29
+ :+1: Examples of **correct** code for this check:
30
+
31
+ ```liquid
32
+
33
+ <!-- Does not use lazySizes library. Instead uses native "loading" attribute -->
34
+ <img src="a.jpg" loading="lazy">
35
+
36
+ ```
37
+
38
+ ## Check Options
39
+
40
+ The default configuration for this check is the following:
41
+
42
+ ```yaml
43
+ DeprecateLazysizes:
44
+ enabled: true
45
+ ```
46
+
47
+ ## When Not To Use It
48
+
49
+ You should disable this rule if you want to support lazy loading of images in older browser that don't support the loading="lazy" attribute yet.
50
+
51
+ ## Version
52
+
53
+ This check has been introduced in Theme Check 1.0.0.
54
+
55
+ ## Resources
56
+
57
+ - [Rule Source][codesource]
58
+ - [Documentation Source][docsource]
59
+
60
+ [codesource]: /lib/theme_check/checks/deprecate_lazysizes.rb
61
+ [docsource]: /docs/checks/deprecate_lazysizes.md
@@ -0,0 +1,50 @@
1
+ # Report HTML parsing errors (`HtmlParsingError`)
2
+
3
+ Report errors preventing the HTML from being parsed and analyzed by Theme Check.
4
+
5
+ ## Check Details
6
+
7
+ This check is aimed at reporting HTML errors that prevent a file from being analyzed.
8
+
9
+ The HTML parser limits the number of attributes per element to 400, and the maximum depth of the DOM tree to 400 levels. If any one of those limits is reached, parsing stops, and all HTML offenses on this file are ignored.
10
+
11
+ :-1: Examples of **incorrect** code for this check:
12
+
13
+ ```liquid
14
+ <img src="muffin.jpeg"
15
+ data-attrbute-1=""
16
+ data-attrbute-2=""
17
+ ... up to
18
+ data-attrbute-400="">
19
+ ```
20
+
21
+ :+1: Examples of **correct** code for this check:
22
+
23
+ ```liquid
24
+ <img src="muffin.jpeg">
25
+ ```
26
+
27
+ ## Check Options
28
+
29
+ The default configuration for this check is the following:
30
+
31
+ ```yaml
32
+ HtmlParsingError:
33
+ enabled: true
34
+ ```
35
+
36
+ ## When Not To Use It
37
+
38
+ If you don't care about HTML offenses.
39
+
40
+ ## Version
41
+
42
+ This check has been introduced in Theme Check 0.10.2.
43
+
44
+ ## Resources
45
+
46
+ - [Rule Source][codesource]
47
+ - [Documentation Source][docsource]
48
+
49
+ [codesource]: /lib/theme_check/checks/html_parsing_error.rb
50
+ [docsource]: /docs/checks/html_parsing_error.md
@@ -39,12 +39,12 @@ The default configuration for this check is the following:
39
39
  ```yaml
40
40
  LiquidTag:
41
41
  enabled: true
42
- min_consecutive_statements: 4
42
+ min_consecutive_statements: 5
43
43
  ```
44
44
 
45
45
  ### `min_consecutive_statements`
46
46
 
47
- The `min_consecutive_statements` option (Default: `4`) determines the maximum (inclusive) number of consecutive statements before the check recommends a refactor.
47
+ The `min_consecutive_statements` option (Default: `5`) determines the maximum (inclusive) number of consecutive statements before the check recommends a refactor.
48
48
 
49
49
  ## When Not To Use It
50
50
 
@@ -21,8 +21,10 @@ The default configuration for this check is the following:
21
21
  ```yaml
22
22
  TemplateLength:
23
23
  enabled: true
24
- max_length: 200
24
+ max_length: 500
25
25
  exclude_schema: true
26
+ exclude_stylesheet: true
27
+ exclude_javascript: true
26
28
  ```
27
29
 
28
30
  ### `max_length`
@@ -31,7 +33,15 @@ The `max_length` (Default: `200`) option determines the maximum number of lines
31
33
 
32
34
  ### `exclude_schema`
33
35
 
34
- The `exclude_schema` (Default: `true`) option determines if the schema lines from a template should be excluded from the line count.
36
+ The `exclude_schema` (Default: `true`) option determines if the lines inside `{% schema %}` blocks from a template should be excluded from the line count.
37
+
38
+ ### `exclude_stylesheet`
39
+
40
+ The `exclude_stylesheet` (Default: `true`) option determines if the lines inside `{% stylesheet %}` blocks from a template should be excluded from the line count.
41
+
42
+ ### `exclude_javascript`
43
+
44
+ The `exclude_javascript` (Default: `true`) option determines if the lines inside `{% javascript %}` blocks from a template should be excluded from the line count.
35
45
 
36
46
  ## When Not To Use It
37
47
 
@@ -0,0 +1,3 @@
1
+ @ECHO OFF
2
+
3
+ ruby "%~dp0\theme-check-language-server" %*
@@ -0,0 +1,3 @@
1
+ @ECHO OFF
2
+
3
+ ruby "%~dp0\theme-check" %*
data/lib/theme_check.rb CHANGED
@@ -4,6 +4,7 @@ require "liquid"
4
4
  require_relative "theme_check/version"
5
5
  require_relative "theme_check/bug"
6
6
  require_relative "theme_check/exceptions"
7
+ require_relative "theme_check/theme_file"
7
8
  require_relative "theme_check/analyzer"
8
9
  require_relative "theme_check/check"
9
10
  require_relative "theme_check/checks_tracking"
@@ -45,3 +46,17 @@ Dir[__dir__ + "/theme_check/checks/*.rb"].each { |file| require file }
45
46
  # UTF-8 is the default internal and external encoding, like in Rails & Shopify.
46
47
  Encoding.default_external = Encoding::UTF_8
47
48
  Encoding.default_internal = Encoding::UTF_8
49
+
50
+ module ThemeCheck
51
+ def self.with_liquid_c_disabled
52
+ if defined?(Liquid::C)
53
+ was_enabled = Liquid::C.enabled
54
+ Liquid::C.enabled = false if was_enabled
55
+ end
56
+ yield
57
+ ensure
58
+ if defined?(Liquid::C) && was_enabled
59
+ Liquid::C.enabled = true
60
+ end
61
+ end
62
+ end
@@ -34,9 +34,11 @@ module ThemeCheck
34
34
 
35
35
  liquid_visitor = Visitor.new(@liquid_checks, @disabled_checks)
36
36
  html_visitor = HtmlVisitor.new(@html_checks)
37
- @theme.liquid.each do |template|
38
- liquid_visitor.visit_template(template)
39
- html_visitor.visit_template(template)
37
+ ThemeCheck.with_liquid_c_disabled do
38
+ @theme.liquid.each do |template|
39
+ liquid_visitor.visit_template(template)
40
+ html_visitor.visit_template(template)
41
+ end
40
42
  end
41
43
 
42
44
  @theme.json.each { |json_file| @json_checks.call(:on_file, json_file) }
@@ -47,24 +49,26 @@ module ThemeCheck
47
49
  def analyze_files(files)
48
50
  reset
49
51
 
50
- # Call all checks that run on the whole theme
51
- liquid_visitor = Visitor.new(@liquid_checks.whole_theme, @disabled_checks)
52
- html_visitor = HtmlVisitor.new(@html_checks.whole_theme)
53
- @theme.liquid.each do |template|
54
- liquid_visitor.visit_template(template)
55
- html_visitor.visit_template(template)
56
- end
57
- @theme.json.each { |json_file| @json_checks.whole_theme.call(:on_file, json_file) }
58
-
59
- # Call checks that run on a single files, only on specified file
60
- liquid_visitor = Visitor.new(@liquid_checks.single_file, @disabled_checks)
61
- html_visitor = HtmlVisitor.new(@html_checks.single_file)
62
- files.each do |file|
63
- if file.liquid?
64
- liquid_visitor.visit_template(file)
65
- html_visitor.visit_template(file)
66
- elsif file.json?
67
- @json_checks.single_file.call(:on_file, file)
52
+ ThemeCheck.with_liquid_c_disabled do
53
+ # Call all checks that run on the whole theme
54
+ liquid_visitor = Visitor.new(@liquid_checks.whole_theme, @disabled_checks)
55
+ html_visitor = HtmlVisitor.new(@html_checks.whole_theme)
56
+ @theme.liquid.each do |template|
57
+ liquid_visitor.visit_template(template)
58
+ html_visitor.visit_template(template)
59
+ end
60
+ @theme.json.each { |json_file| @json_checks.whole_theme.call(:on_file, json_file) }
61
+
62
+ # Call checks that run on a single files, only on specified file
63
+ liquid_visitor = Visitor.new(@liquid_checks.single_file, @disabled_checks)
64
+ html_visitor = HtmlVisitor.new(@html_checks.single_file)
65
+ files.each do |file|
66
+ if file.liquid?
67
+ liquid_visitor.visit_template(file)
68
+ html_visitor.visit_template(file)
69
+ elsif file.json?
70
+ @json_checks.single_file.call(:on_file, file)
71
+ end
68
72
  end
69
73
  end
70
74