Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
Prevents bloat in themes by removing variable definitions that are no…
Browse files Browse the repository at this point in the history
…t used
  • Loading branch information
hannakebedom committed Oct 19, 2021
1 parent 7bd837f commit bac731c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/theme_check/checks/unused_assign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def on_end
@templates.each_pair do |_, info|
used = info.collect_used_assigns(@templates)
info.assign_nodes.each_pair do |name, node|
unless used.include?(name)
add_offense("`#{name}` is never used", node: node)
next if used.include?(name)
add_offense("`#{name}` is never used", node: node) do |corrector|
corrector.remove(node)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/theme_check/checks/unused_snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def on_include(node)
def on_end
missing_snippets.each do |theme_file|
add_offense("This snippet is not used", theme_file: theme_file) do |corrector|
corrector.remove(@theme, theme_file.relative_path.to_s)
corrector.remove_file(@theme, theme_file.relative_path.to_s)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/theme_check/corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def insert_before(node, content)
@theme_file.rewriter.insert_before(node, content)
end

def remove(node)
@theme_file.rewriter.remove(node)
end

def replace(node, content)
@theme_file.rewriter.replace(node, content)
node.markup = content
Expand All @@ -32,7 +36,7 @@ def create_default_locale_json(theme)
theme.default_locale_json.update_contents({})
end

def remove(theme, relative_path)
def remove_file(theme, relative_path)
theme.storage.remove(relative_path)
end

Expand Down
10 changes: 10 additions & 0 deletions lib/theme_check/liquid_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def end_index
position.end_index
end

def start_token_index
return position.start_index if inside_liquid_tag?
position.start_index - (start_token.length + 1)
end

def end_token_index
return position.end_index if inside_liquid_tag?
position.end_index + end_token.length
end

# Literals are hard-coded values in the liquid file.
def literal?
@value.is_a?(String) || @value.is_a?(Integer)
Expand Down
6 changes: 6 additions & 0 deletions lib/theme_check/theme_file_rewriter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def insert_after(node, content)
)
end

def remove(node)
@rewriter.remove(
range(node.start_token_index, node.end_token_index)
)
end

def replace(node, content)
@rewriter.replace(
range(node.start_index, node.end_index),
Expand Down
32 changes: 32 additions & 0 deletions test/checks/unused_assign_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,36 @@ def test_recursion_in_includes
)
assert_offenses("", offenses)
end

def test_removes_unused_assign
expected_sources = {
"templates/index.liquid" => "\n",
}
sources = fix_theme(
ThemeCheck::UnusedAssign.new,
"templates/index.liquid" => <<~END,
{% assign x = 1 %}
END
)
sources.each do |path, source|
assert_equal(expected_sources[path], source)
end
end

def test_removes_assign_leaves_html
expected_sources = {
"templates/index.liquid" => <<~END,
<p>test case</p>
END
}
sources = fix_theme(
ThemeCheck::UnusedAssign.new,
"templates/index.liquid" => <<~END,
<p>test case</p>{% assign x = 1 %}
END
)
sources.each do |path, source|
assert_equal(expected_sources[path], source)
end
end
end

0 comments on commit bac731c

Please sign in to comment.