Optimize script recursively

This commit is contained in:
Takashi Kokubun 2015-12-24 02:08:33 +09:00
parent dd8f100de6
commit b2fe87ef38
2 changed files with 12 additions and 2 deletions

View File

@ -10,10 +10,14 @@ module Hamlit
end
def compile(node, &block)
no_children = node.children.empty?
case
when node.children.empty? && RubyExpression.string_literal?(node.value[:text])
when no_children && node.value[:escape_interpolation]
string_compile(node)
when node.children.empty? && StaticAnalyzer.static?(node.value[:text])
when no_children && RubyExpression.string_literal?(node.value[:text])
# Optimized in other filter: StringSplitter
[:multi, [:escape, node.value[:escape_html], [:dynamic, node.value[:text]]], [:newline]]
when no_children && StaticAnalyzer.static?(node.value[:text])
static_compile(node)
else
dynamic_compile(node, &block)
@ -23,6 +27,7 @@ module Hamlit
private
# String-interpolated plain text must be compiled with this method
# because we have to escape only interpolated values.
def string_compile(node)
temple = [:multi]
StringSplitter.compile(node.value[:text]).each do |type, value|

View File

@ -32,6 +32,11 @@ describe 'optimization' do
assert_equal true, compiled_code(haml).include?(%|<span>jruby9000|)
end
it 'optimizes script' do
haml = %q|= "jruby#{ "#{9000}" }#{dynamic}"|
assert_equal true, compiled_code(haml).include?(%|jruby9000|)
end
it 'detects a static part recursively' do
haml = %q|%input{ value: "#{ "hello#{ hello }" }" }|
assert_equal true, compiled_code(haml).include?(%|value='hello|)