Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change all constants to SCREAMING_SNAKE_CASE #1159

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
19 changes: 0 additions & 19 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,6 @@ Lint/InheritException:
Metrics/LineLength:
Max: 294

# Offense count: 44
Naming/ConstantName:
Exclude:
- 'lib/liquid.rb'
- 'lib/liquid/block_body.rb'
- 'lib/liquid/tags/assign.rb'
- 'lib/liquid/tags/capture.rb'
- 'lib/liquid/tags/case.rb'
- 'lib/liquid/tags/cycle.rb'
- 'lib/liquid/tags/for.rb'
- 'lib/liquid/tags/if.rb'
- 'lib/liquid/tags/include.rb'
- 'lib/liquid/tags/raw.rb'
- 'lib/liquid/tags/table_row.rb'
- 'lib/liquid/variable.rb'
- 'performance/shopify/comment_form.rb'
- 'performance/shopify/paginate.rb'
- 'test/integration/tags/include_tag_test.rb'

# Offense count: 5
Style/ClassVars:
Exclude:
Expand Down
40 changes: 21 additions & 19 deletions lib/liquid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

module Liquid
FilterSeparator = /\|/
ArgumentSeparator = ','
FilterArgumentSeparator = ':'
VariableAttributeSeparator = '.'
WhitespaceControl = '-'
TagStart = /\{\%/
TagEnd = /\%\}/
VariableSignature = /\(?[\w\-\.\[\]]\)?/
VariableSegment = /[\w\-]/
VariableStart = /\{\{/
VariableEnd = /\}\}/
VariableIncompleteEnd = /\}\}?/
QuotedString = /"[^"]*"|'[^']*'/
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/o
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o
AnyStartingTag = /#{TagStart}|#{VariableStart}/o
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o
FILTER_SEPARATOR = /\|/
ARGUMENT_SEPARATOR = ','
FILTER_ARGUMENT_SEPARATOR = ':'
VARIABLE_ATTRIBUTE_SEPARATOR = '.'
WHITESPACE_CONTROL = '-'
TAG_START = /\{\%/
TAG_END = /\%\}/
VARIABLE_SIGNATURE = /\(?[\w\-\.\[\]]\)?/
VARIABLE_SEGMENT = /[\w\-]/
VARIABLE_START = /\{\{/
VARIABLE_END = /\}\}/
VARIABLE_INCOMPLETE_END = /\}\}?/
QUOTED_STRING = /"[^"]*"|'[^']*'/
QUOTED_FRAGMENT = /#{QUOTED_STRING}|(?:[^\s,\|'"]|#{QUOTED_STRING})+/o
TAG_ATTRIBUTES = /(\w+)\s*\:\s*(#{QUOTED_FRAGMENT})/o
ANY_STARTING_TAG = /#{TAG_START}|#{VARIABLE_START}/o
PARTIAL_TEMPLATE_PARSER = /#{TAG_START}.*?#{TAG_END}|#{VARIABLE_START}.*?#{VARIABLE_INCOMPLETE_END}/om
TEMPLATE_PARSER = /(#{PARTIAL_TEMPLATE_PARSER}|#{ANY_STARTING_TAG})/om
VARIABLE_PARSER = /\[[^\]]+\]|#{VARIABLE_SEGMENT}+\??/o

singleton_class.send(:attr_accessor, :cache_classes)
self.cache_classes = true
Expand Down Expand Up @@ -85,3 +85,5 @@ module Liquid
#
Dir["#{__dir__}/liquid/tags/*.rb"].each { |f| require f }
Dir["#{__dir__}/liquid/registers/*.rb"].each { |f| require f }

require 'liquid/legacy'
shopmike marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 17 additions & 17 deletions lib/liquid/block_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

module Liquid
class BlockBody
LiquidTagToken = /\A\s*(\w+)\s*(.*?)\z/o
FullToken = /\A#{TagStart}#{WhitespaceControl}?(\s*)(\w+)(\s*)(.*?)#{WhitespaceControl}?#{TagEnd}\z/om
ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
WhitespaceOrNothing = /\A\s*\z/
TAGSTART = "{%"
VARSTART = "{{"
LIQUID_TAG_TOKEN = /\A\s*(\w+)\s*(.*?)\z/o
FULL_TOKEN = /\A#{TAG_START}#{WHITESPACE_CONTROL}?(\s*)(\w+)(\s*)(.*?)#{WHITESPACE_CONTROL}?#{TAG_END}\z/om
CONTENT_OF_VARIABLE = /\A#{VARIABLE_START}#{WHITESPACE_CONTROL}?(.*?)#{WHITESPACE_CONTROL}?#{VARIABLE_END}\z/om
WHITESPACE_OR_NOTHING = /\A\s*\z/
TAG_START_STRING = "{%"
VAR_START_STRING = "{{"

attr_reader :nodelist

Expand All @@ -28,8 +28,8 @@ def parse(tokenizer, parse_context, &block)

private def parse_for_liquid_tag(tokenizer, parse_context)
while (token = tokenizer.shift)
unless token.empty? || token =~ WhitespaceOrNothing
unless token =~ LiquidTagToken
unless token.empty? || token =~ WHITESPACE_OR_NOTHING
unless token =~ LIQUID_TAG_TOKEN
# line isn't empty but didn't match tag syntax, yield and let the
# caller raise a syntax error
return yield token, token
Expand All @@ -55,9 +55,9 @@ def parse(tokenizer, parse_context, &block)
while (token = tokenizer.shift)
next if token.empty?
case
when token.start_with?(TAGSTART)
when token.start_with?(TAG_START_STRING)
whitespace_handler(token, parse_context)
unless token =~ FullToken
unless token =~ FULL_TOKEN
raise_missing_tag_terminator(token, parse_context)
end
tag_name = Regexp.last_match(2)
Expand All @@ -82,7 +82,7 @@ def parse(tokenizer, parse_context, &block)
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
@blank &&= new_tag.blank?
@nodelist << new_tag
when token.start_with?(VARSTART)
when token.start_with?(VAR_START_STRING)
whitespace_handler(token, parse_context)
@nodelist << create_variable(token, parse_context)
@blank = false
Expand All @@ -92,7 +92,7 @@ def parse(tokenizer, parse_context, &block)
end
parse_context.trim_whitespace = false
@nodelist << token
@blank &&= !!(token =~ WhitespaceOrNothing)
@blank &&= !!(token =~ WHITESPACE_OR_NOTHING)
end
parse_context.line_number = tokenizer.line_number
end
Expand All @@ -101,13 +101,13 @@ def parse(tokenizer, parse_context, &block)
end

def whitespace_handler(token, parse_context)
if token[2] == WhitespaceControl
if token[2] == WHITESPACE_CONTROL
previous_token = @nodelist.last
if previous_token.is_a?(String)
previous_token.rstrip!
end
end
parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
parse_context.trim_whitespace = (token[-3] == WHITESPACE_CONTROL)
end

def blank?
Expand Down Expand Up @@ -180,19 +180,19 @@ def raise_if_resource_limits_reached(context, length)
end

def create_variable(token, parse_context)
token.scan(ContentOfVariable) do |content|
token.scan(CONTENT_OF_VARIABLE) do |content|
markup = content.first
return Variable.new(markup, parse_context)
end
raise_missing_variable_terminator(token, parse_context)
end

def raise_missing_tag_terminator(token, parse_context)
raise SyntaxError, parse_context.locale.t("errors.syntax.tag_termination", token: token, tag_end: TagEnd.inspect)
raise SyntaxError, parse_context.locale.t("errors.syntax.tag_termination", token: token, tag_end: TAG_END.inspect)
end

def raise_missing_variable_terminator(token, parse_context)
raise SyntaxError, parse_context.locale.t("errors.syntax.variable_termination", token: token, tag_end: VariableEnd.inspect)
raise SyntaxError, parse_context.locale.t("errors.syntax.variable_termination", token: token, tag_end: VARIABLE_END.inspect)
end

def registered_tags
Expand Down
79 changes: 79 additions & 0 deletions lib/liquid/legacy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

module Liquid
FilterSeparator = FILTER_SEPARATOR
ArgumentSeparator = ARGUMENT_SEPARATOR
FilterArgumentSeparator = FILTER_ARGUMENT_SEPARATOR
VariableAttributeSeparator = VARIABLE_ATTRIBUTE_SEPARATOR
WhitespaceControl = WHITESPACE_CONTROL
TagStart = TAG_START
TagEnd = TAG_END
VariableSignature = VARIABLE_SIGNATURE
VariableSegment = VARIABLE_SEGMENT
VariableStart = VARIABLE_START
VariableEnd = VARIABLE_END
VariableIncompleteEnd = VARIABLE_INCOMPLETE_END
QuotedString = QUOTED_STRING
QuotedFragment = QUOTED_FRAGMENT
TagAttributes = TAG_ATTRIBUTES
AnyStartingTag = ANY_STARTING_TAG
PartialTemplateParser = PARTIAL_TEMPLATE_PARSER
TemplateParser = TEMPLATE_PARSER
VariableParser = VARIABLE_PARSER

class BlockBody
FullToken = FULL_TOKEN
ContentOfVariable = CONTENT_OF_VARIABLE
WhitespaceOrNothing = WHITESPACE_OR_NOTHING
TAGSTART = TAG_START_STRING
VARSTART = VAR_START_STRING
end

class Assign < Tag
Syntax = SYNTAX
end

class Capture < Block
Syntax = SYNTAX
end

class Case < Block
Syntax = SYNTAX
WhenSyntax = WHEN_SYNTAX
end

class Cycle < Tag
SimpleSyntax = SIMPLE_SYNTAX
NamedSyntax = NAMED_SYNTAX
end

class For < Block
Syntax = SYNTAX
end

class If < Block
Syntax = SYNTAX
ExpressionsAndOperators = EXPRESSIONS_AND_OPERATORS
end

class Include < Tag
Syntax = SYNTAX
end

class Raw < Block
Syntax = SYNTAX
FullTokenPossiblyInvalid = FULL_TOKEN_POSSIBLY_INVALID
end

class TableRow < Block
Syntax = SYNTAX
end

class Variable
FilterMarkupRegex = FILTER_MARKUP_REGEX
FilterParser = FILTER_PARSER
FilterArgsRegex = FILTER_ARGS_REGEX
JustTagAttributes = JUST_TAG_ATTRIBUTES
MarkupWithQuotedFragment = MARKUP_WITH_QUOTED_FRAGMENT
end
end
4 changes: 2 additions & 2 deletions lib/liquid/tags/assign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Liquid
# {{ foo }}
#
class Assign < Tag
Syntax = /(#{VariableSignature}+)\s*=\s*(.*)\s*/om
SYNTAX = /(#{VARIABLE_SIGNATURE}+)\s*=\s*(.*)\s*/om

def self.syntax_error_translation_key
"errors.syntax.assign"
Expand All @@ -20,7 +20,7 @@ def self.syntax_error_translation_key

def initialize(tag_name, markup, options)
super
if markup =~ Syntax
if markup =~ SYNTAX
@to = Regexp.last_match(1)
@from = Variable.new(Regexp.last_match(2), options)
else
Expand Down
4 changes: 2 additions & 2 deletions lib/liquid/tags/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ module Liquid
# in a sidebar or footer.
#
class Capture < Block
Syntax = /(#{VariableSignature}+)/o
SYNTAX = /(#{VARIABLE_SIGNATURE}+)/o

def initialize(tag_name, markup, options)
super
if markup =~ Syntax
if markup =~ SYNTAX
@to = Regexp.last_match(1)
else
raise SyntaxError, options[:locale].t("errors.syntax.capture")
Expand Down
8 changes: 4 additions & 4 deletions lib/liquid/tags/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

module Liquid
class Case < Block
Syntax = /(#{QuotedFragment})/o
WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/om
SYNTAX = /(#{QUOTED_FRAGMENT})/o
WHEN_SYNTAX = /(#{QUOTED_FRAGMENT})(?:(?:\s+or\s+|\s*\,\s*)(#{QUOTED_FRAGMENT}.*))?/om

attr_reader :blocks, :left

def initialize(tag_name, markup, options)
super
@blocks = []

if markup =~ Syntax
if markup =~ SYNTAX
@left = Expression.parse(Regexp.last_match(1))
else
raise SyntaxError, options[:locale].t("errors.syntax.case")
Expand Down Expand Up @@ -59,7 +59,7 @@ def record_when_condition(markup)
body = BlockBody.new

while markup
unless markup =~ WhenSyntax
unless markup =~ WHEN_SYNTAX
raise SyntaxError, options[:locale].t("errors.syntax.case_invalid_when")
end

Expand Down
10 changes: 5 additions & 5 deletions lib/liquid/tags/cycle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ module Liquid
# <div class="green"> Item five</div>
#
class Cycle < Tag
SimpleSyntax = /\A#{QuotedFragment}+/o
NamedSyntax = /\A(#{QuotedFragment})\s*\:\s*(.*)/om
SIMPLE_SYNTAX = /\A#{QUOTED_FRAGMENT}+/o
NAMED_SYNTAX = /\A(#{QUOTED_FRAGMENT})\s*\:\s*(.*)/om

attr_reader :variables

def initialize(tag_name, markup, options)
super
case markup
when NamedSyntax
when NAMED_SYNTAX
@variables = variables_from_string(Regexp.last_match(2))
@name = Expression.parse(Regexp.last_match(1))
when SimpleSyntax
when SIMPLE_SYNTAX
@variables = variables_from_string(markup)
@name = @variables.to_s
else
Expand Down Expand Up @@ -60,7 +60,7 @@ def render_to_output_buffer(context, output)

def variables_from_string(markup)
markup.split(',').collect do |var|
var =~ /\s*(#{QuotedFragment})\s*/o
var =~ /\s*(#{QUOTED_FRAGMENT})\s*/o
Regexp.last_match(1) ? Expression.parse(Regexp.last_match(1)) : nil
end.compact
end
Expand Down
6 changes: 3 additions & 3 deletions lib/liquid/tags/for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module Liquid
# forloop.parentloop:: Provides access to the parent loop, if present.
#
class For < Block
Syntax = /\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
SYNTAX = /\A(#{VARIABLE_SEGMENT}+)\s+in\s+(#{QUOTED_FRAGMENT}+)\s*(reversed)?/o

attr_reader :collection_name, :variable_name, :limit, :from

Expand Down Expand Up @@ -87,13 +87,13 @@ def render_to_output_buffer(context, output)
protected

def lax_parse(markup)
if markup =~ Syntax
if markup =~ SYNTAX
@variable_name = Regexp.last_match(1)
collection_name = Regexp.last_match(2)
@reversed = !!Regexp.last_match(3)
@name = "#{@variable_name}-#{collection_name}"
@collection_name = Expression.parse(collection_name)
markup.scan(TagAttributes) do |key, value|
markup.scan(TAG_ATTRIBUTES) do |key, value|
set_attribute(key, value)
end
else
Expand Down