Address comments

This commit is contained in:
Peter Zhu
2020-10-26 15:11:00 -04:00
parent fe66edb825
commit 0bedc71854
7 changed files with 23 additions and 30 deletions
+1 -1
View File
@@ -22,6 +22,6 @@ group :test do
gem 'rubocop-performance', require: false gem 'rubocop-performance', require: false
platform :mri, :truffleruby do platform :mri, :truffleruby do
gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'pz-block-body-buffer' gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'master'
end end
end end
+15 -5
View File
@@ -11,9 +11,9 @@ module Liquid
def parse(tokens) def parse(tokens)
@body = new_body @body = new_body
while parse_body(@body, tokens) while parse_body(@body, tokens, partial: true)
end end
@body.freeze(parse_context) @body.freeze
end end
# For backwards compatibility # For backwards compatibility
@@ -68,7 +68,9 @@ module Liquid
end end
# @api public # @api public
def parse_body(body, tokens) def parse_body(body, tokens, partial: false)
block_terminated = true
if parse_context.depth >= MAX_DEPTH if parse_context.depth >= MAX_DEPTH
raise StackLevelError, "Nesting too deep" raise StackLevelError, "Nesting too deep"
end end
@@ -77,7 +79,10 @@ module Liquid
body.parse(tokens, parse_context) do |end_tag_name, end_tag_params| body.parse(tokens, parse_context) do |end_tag_name, end_tag_params|
@blank &&= body.blank? @blank &&= body.blank?
return false if end_tag_name == block_delimiter if end_tag_name == block_delimiter
block_terminated = false
next
end
raise_tag_never_closed(block_name) unless end_tag_name raise_tag_never_closed(block_name) unless end_tag_name
# this tag is not registered with the system # this tag is not registered with the system
@@ -88,7 +93,12 @@ module Liquid
parse_context.depth -= 1 parse_context.depth -= 1
end end
true unless partial
body.remove_blank_strings if body.blank?
body.freeze
end
block_terminated
end end
end end
end end
+5 -5
View File
@@ -16,11 +16,10 @@ module Liquid
def initialize def initialize
@nodelist = [] @nodelist = []
@blank = true @blank = true
@frozen = false
end end
def parse(tokenizer, parse_context, &block) def parse(tokenizer, parse_context, &block)
raise FrozenError, "can't modify frozen Liquid::BlockBody" if @frozen raise FrozenError, "can't modify frozen Liquid::BlockBody" if frozen?
parse_context.line_number = tokenizer.line_number parse_context.line_number = tokenizer.line_number
@@ -31,8 +30,9 @@ module Liquid
end end
end end
def freeze(_context) def freeze
@frozen = true @nodelist.freeze
super
end end
private def parse_for_liquid_tag(tokenizer, parse_context) private def parse_for_liquid_tag(tokenizer, parse_context)
@@ -199,7 +199,7 @@ module Liquid
end end
def render_to_output_buffer(context, output) def render_to_output_buffer(context, output)
raise "Can only render when frozen" unless @frozen raise "Can only render when frozen" unless frozen?
context.resource_limits.increment_render_score(@nodelist.length) context.resource_limits.increment_render_score(@nodelist.length)
+1 -1
View File
@@ -22,7 +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(parse_context) @body.freeze
rescue SyntaxError => e rescue SyntaxError => e
e.line_number ||= parse_context.line_number e.line_number ||= parse_context.line_number
raise raise
+1 -8
View File
@@ -20,14 +20,7 @@ module Liquid
def parse(tokens) def parse(tokens)
body = new_body body = new_body
while parse_body(body, tokens) body = @blocks.last.attachment while parse_body(body, tokens)
body.freeze(parse_context)
body = @blocks.last.attachment
end
body.freeze(parse_context)
if blank?
@blocks.each { |condition| condition.attachment.remove_blank_strings }
end
end end
def nodelist def nodelist
-6
View File
@@ -61,12 +61,6 @@ module Liquid
def parse(tokens) def parse(tokens)
if parse_body(@for_block, tokens) if parse_body(@for_block, tokens)
parse_body(@else_block, tokens) parse_body(@else_block, tokens)
@else_block.freeze(parse_context)
end
@for_block.freeze(parse_context)
if blank?
@for_block.remove_blank_strings
@else_block&.remove_blank_strings
end end
end end
-4
View File
@@ -31,10 +31,6 @@ module Liquid
def parse(tokens) def parse(tokens)
while parse_body(@blocks.last.attachment, tokens) while parse_body(@blocks.last.attachment, tokens)
end end
@blocks.each { |block| block.attachment.freeze(parse_context) }
if blank?
@blocks.each { |condition| condition.attachment.remove_blank_strings }
end
end end
def unknown_tag(tag, markup, tokens) def unknown_tag(tag, markup, tokens)