Skip to content

Commit

Permalink
Support :freeze option for StringTemplate to support frozen string li…
Browse files Browse the repository at this point in the history
…terals
  • Loading branch information
jeremyevans committed Aug 18, 2023
1 parent 53a8661 commit 29428f6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* 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)
* Support :freeze option for StringTemplate to support frozen string literals (jeremyevans)

## 2.2.0 (2023-06-05)

Expand Down
5 changes: 5 additions & 0 deletions lib/tilt/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Tilt
class StringTemplate < Template
def prepare
hash = "TILT#{@data.hash.abs}"
@freeze_string_literals = !!@options[:freeze]
@code = String.new("<<#{hash}.chomp\n#{@data}\n#{hash}")
end

Expand All @@ -18,5 +19,9 @@ def precompiled(locals)
source, offset = super
[source, offset + 1]
end

def freeze_string_literals?
@freeze_string_literals
end
end
end
7 changes: 7 additions & 0 deletions test/tilt_stringtemplate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,11 @@
assert_equal '6', line
end
end

if RUBY_VERSION >= '2.3'
it "uses frozen literal strings if :freeze option is used" do
template = Tilt::StringTemplate.new(nil, :freeze => true) { |t| '#{"".frozen?}' }
assert_equal "true", template.render
end
end
end

0 comments on commit 29428f6

Please sign in to comment.