Skip to content

Commit

Permalink
Make Template#freeze_string_literals? work correctly with Template#co…
Browse files Browse the repository at this point in the history
…mpiled_path

This moves the frozen-string-literal magic comment to the first line
when writing the compiled template file, since otherwise it has no
effect and results in a verbose warning.
  • Loading branch information
jeremyevans committed Aug 18, 2023
1 parent 6927599 commit 53a8661
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Remove deprecated support for CoffeeScriptTemplate.default_no_wrap{,=} (jeremyevans)
* Remove deprecated support for RedCarpet 1.x (jeremyevans)
* Remove deprecated support for Tilt.current_template (jeremyevans)
* Make Template#freeze_string_literals? work correctly with Template#compiled_path (jeremyevans)

## 2.2.0 (2023-06-05)

Expand Down
6 changes: 5 additions & 1 deletion lib/tilt/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@ def bind_compiled_method(method_source, offset, scope_class)
path << ".rb"

# Wrap method source in a class block for the scope, so constant lookup works
method_source = "class #{scope_class.name}\n#{method_source}\nend"
if freeze_string_literals?
method_source_prefix = "# frozen-string-literal: true\n"
method_source = method_source.sub(/\A# frozen-string-literal: true\n/, '')
end
method_source = "#{method_source_prefix}class #{scope_class.name}\n#{method_source}\nend"

load_compiled_method(path, method_source)
else
Expand Down
32 changes: 32 additions & 0 deletions test/tilt_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ def precompiled_template(locals)
end
end

_FrozenSourceGeneratingMockTemplate = Class.new(_SourceGeneratingMockTemplate) do
def freeze_string_literals?
true
end
end

it "template_source with locals" do
inst = _SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' }
assert_equal "Hey Joe!", inst.render(Object.new, :name => 'Joe')
Expand Down Expand Up @@ -270,6 +276,32 @@ def precompiled_template(locals)
end
end

it "template with compiled_path and freezing string literals option" do
Dir.mktmpdir('tilt') do |dir|
base = File.join(dir, 'template')
inst = _FrozenSourceGeneratingMockTemplate.new { |t| 'Hey' }
inst.compiled_path = base

tempfile = "#{base}.rb"
assert_equal false, File.file?(tempfile)
assert_equal 'Hey', inst.render
assert_equal true, File.file?(tempfile)
assert_match(/\A# frozen-string-literal: true\nclass Object/, File.read(tempfile))

tempfile = "#{base}-1.rb"
assert_equal false, File.file?(tempfile)
assert_equal 'Hey', inst.render("")
assert_equal true, File.file?(tempfile)
assert_match(/\A# frozen-string-literal: true\nclass String/, File.read(tempfile))

tempfile = "#{base}-2.rb"
assert_equal false, File.file?(tempfile)
assert_equal 'Hey', inst.render(Tilt::Mapping.new)
assert_equal true, File.file?(tempfile)
assert_match(/\A# frozen-string-literal: true\nclass Tilt::Mapping/, File.read(tempfile))
end
end

_CustomGeneratingMockTemplate = Class.new(_PreparingMockTemplate) do
def precompiled_template(locals)
data
Expand Down

0 comments on commit 53a8661

Please sign in to comment.