Merge pull request #1331 from Shopify/pz-freeze-block

Freeze block body after parsing completes
This commit is contained in:
Peter Zhu
2020-10-27 13:17:54 -04:00
committed by GitHub
6 changed files with 19 additions and 4 deletions
+1
View File
@@ -13,6 +13,7 @@ module Liquid
@body = new_body @body = new_body
while parse_body(@body, tokens) while parse_body(@body, tokens)
end end
@body.freeze
end end
# For backwards compatibility # For backwards compatibility
+9
View File
@@ -19,6 +19,8 @@ module Liquid
end end
def parse(tokenizer, parse_context, &block) def parse(tokenizer, parse_context, &block)
raise FrozenError, "can't modify frozen Liquid::BlockBody" if frozen?
parse_context.line_number = tokenizer.line_number parse_context.line_number = tokenizer.line_number
if tokenizer.for_liquid_tag if tokenizer.for_liquid_tag
@@ -28,6 +30,11 @@ module Liquid
end end
end end
def freeze
@nodelist.freeze
super
end
private def parse_for_liquid_tag(tokenizer, parse_context) private def parse_for_liquid_tag(tokenizer, parse_context)
while (token = tokenizer.shift) while (token = tokenizer.shift)
unless token.empty? || token =~ WhitespaceOrNothing unless token.empty? || token =~ WhitespaceOrNothing
@@ -192,6 +199,8 @@ module Liquid
end end
def render_to_output_buffer(context, output) def render_to_output_buffer(context, output)
freeze unless frozen?
context.resource_limits.increment_render_score(@nodelist.length) context.resource_limits.increment_render_score(@nodelist.length)
idx = 0 idx = 0
+1
View File
@@ -22,6 +22,7 @@ module Liquid
def parse(tokenizer, parse_context) def parse(tokenizer, parse_context)
while parse_body(tokenizer) while parse_body(tokenizer)
end end
@body.freeze
rescue SyntaxError => e rescue SyntaxError => e
e.line_number ||= parse_context.line_number e.line_number ||= parse_context.line_number
raise raise
+3 -2
View File
@@ -21,8 +21,9 @@ module Liquid
def parse(tokens) def parse(tokens)
body = new_body body = new_body
body = @blocks.last.attachment while parse_body(body, tokens) body = @blocks.last.attachment while parse_body(body, tokens)
if blank? @blocks.each do |condition|
@blocks.each { |condition| condition.attachment.remove_blank_strings } condition.attachment.remove_blank_strings if blank?
condition.attachment.freeze
end end
end end
+2
View File
@@ -66,6 +66,8 @@ module Liquid
@for_block.remove_blank_strings @for_block.remove_blank_strings
@else_block&.remove_blank_strings @else_block&.remove_blank_strings
end end
@for_block.freeze
@else_block&.freeze
end end
def nodelist def nodelist
+3 -2
View File
@@ -31,8 +31,9 @@ module Liquid
def parse(tokens) def parse(tokens)
while parse_body(@blocks.last.attachment, tokens) while parse_body(@blocks.last.attachment, tokens)
end end
if blank? @blocks.each do |block|
@blocks.each { |condition| condition.attachment.remove_blank_strings } block.attachment.remove_blank_strings if blank?
block.attachment.freeze
end end
end end