From 18a72db820e0abe252045c1e81ffcc9589b6a6cd Mon Sep 17 00:00:00 2001 From: Tobi Lutke Date: Wed, 11 Mar 2026 09:53:44 -0400 Subject: [PATCH] fix rubocop offenses: autocorrect style/layout violations --- lib/liquid/cursor.rb | 87 ++++++++++++++++++++++++++-------------- lib/liquid/expression.rb | 10 +++-- lib/liquid/tags/if.rb | 1 - lib/liquid/variable.rb | 32 +++++++-------- 4 files changed, 80 insertions(+), 50 deletions(-) diff --git a/lib/liquid/cursor.rb b/lib/liquid/cursor.rb index 07b34e8e..de79955b 100644 --- a/lib/liquid/cursor.rb +++ b/lib/liquid/cursor.rb @@ -41,11 +41,15 @@ module Liquid end # ── Position ──────────────────────────────────────────────────── - def pos; @ss.pos; end - def pos=(n); @ss.pos = n; end - def eos?; @ss.eos?; end - def peek_byte; @ss.peek_byte; end - def scan_byte; @ss.scan_byte; end + def pos = @ss.pos + + def pos=(n) + @ss.pos = n + end + + def eos? = @ss.eos? + def peek_byte = @ss.peek_byte + def scan_byte = @ss.scan_byte # Reset scanner to a new string (for reuse on sub-markup) def reset(source) @@ -65,7 +69,8 @@ module Liquid while (b = @ss.peek_byte) case b when SPACE, TAB, CR, FF then @ss.scan_byte - when NL then @ss.scan_byte; nl += 1 + when NL then @ss.scan_byte + nl += 1 else break end end @@ -79,6 +84,7 @@ module Liquid while p < len b = @source.getbyte(p) return false unless b == SPACE || b == TAB || b == NL || b == CR || b == FF + p += 1 end true @@ -90,10 +96,12 @@ module Liquid start = @ss.pos b = @ss.peek_byte return 0 unless b && ((b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == USCORE) + @ss.scan_byte while (b = @ss.peek_byte) break unless (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || - (b >= 48 && b <= 57) || b == USCORE || b == DASH + (b >= 48 && b <= 57) || b == USCORE || b == DASH + @ss.scan_byte end @ss.scan_byte if @ss.peek_byte == QMARK @@ -123,11 +131,13 @@ module Liquid def scan_id start = @ss.pos b = @ss.peek_byte - return nil unless b && ((b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == USCORE) + return unless b && ((b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == USCORE) + @ss.scan_byte while (b = @ss.peek_byte) break unless (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || - (b >= 48 && b <= 57) || b == USCORE || b == DASH + (b >= 48 && b <= 57) || b == USCORE || b == DASH + @ss.scan_byte end @ss.scan_byte if @ss.peek_byte == QMARK @@ -149,19 +159,19 @@ module Liquid def scan_number start = @ss.pos b = @ss.peek_byte - return nil unless b + return unless b if b == DASH @ss.scan_byte b = @ss.peek_byte unless b && b >= ZERO && b <= NINE @ss.pos = start - return nil + return end elsif b >= ZERO && b <= NINE # ok else - return nil + return end # Scan digits @@ -188,7 +198,8 @@ module Liquid # Scan a quoted string ('...' or "..."). Returns the content without quotes, or nil. def scan_quoted_string b = @ss.peek_byte - return nil unless b == QUOTE_S || b == QUOTE_D + return unless b == QUOTE_S || b == QUOTE_D + quote = b @ss.scan_byte start = @ss.pos @@ -201,7 +212,8 @@ module Liquid # Scan a quoted string including quotes. Returns the full "..." or '...' string, or nil. def scan_quoted_string_raw b = @ss.peek_byte - return nil unless b == QUOTE_S || b == QUOTE_D + return unless b == QUOTE_S || b == QUOTE_D + quote = b start = @ss.pos @ss.scan_byte @@ -215,7 +227,8 @@ module Liquid # Returns the string or nil def scan_dotted_id start = @ss.pos - return nil unless scan_id + return unless scan_id + while @ss.peek_byte == DOT @ss.scan_byte unless scan_id @@ -230,6 +243,7 @@ module Liquid def skip_fragment b = @ss.peek_byte return 0 unless b + start = @ss.pos if b == QUOTE_S || b == QUOTE_D quote = b @@ -239,6 +253,7 @@ module Liquid else while (b = @ss.peek_byte) break if b == SPACE || b == TAB || b == NL || b == CR || b == COMMA || b == PIPE + @ss.scan_byte end end @@ -248,13 +263,15 @@ module Liquid # Scan a "QuotedFragment" — a quoted string or non-whitespace/comma/pipe run def scan_fragment b = @ss.peek_byte - return nil unless b + return unless b + if b == QUOTE_S || b == QUOTE_D scan_quoted_string_raw else start = @ss.pos while (b = @ss.peek_byte) break if b == SPACE || b == TAB || b == NL || b == CR || b == COMMA || b == PIPE + @ss.scan_byte end len = @ss.pos - start @@ -264,8 +281,13 @@ module Liquid # ── Comparison operators ──────────────────────────────────────── COMPARISON_OPS = { - '==' => '==', '!=' => '!=', '<>' => '<>', - '<=' => '<=', '>=' => '>=', '<' => '<', '>' => '>', + '==' => '==', + '!=' => '!=', + '<>' => '<>', + '<=' => '<=', + '>=' => '>=', + '<' => '<', + '>' => '>', 'contains' => 'contains', }.freeze @@ -282,13 +304,15 @@ module Liquid end when 99 # 'c' for contains id = scan_id - return nil unless id == "contains" + return unless id == "contains" + return COMPARISON_OPS['contains'] else - return nil + return end op_str = @source.byteslice(start, @ss.pos - start) - COMPARISON_OPS[op_str] || (@ss.pos = start; nil) + COMPARISON_OPS[op_str] || (@ss.pos = start + nil) end # ── Tag parsing helpers ───────────────────────────────────────── @@ -304,7 +328,8 @@ module Liquid @ss.scan_byte if peek_byte == DASH # skip whitespace control '-' nl = skip_ws tag_name = scan_tag_name - return nil unless tag_name + return unless tag_name + nl += skip_ws # markup is everything up to optional '-' before '%}' @@ -319,7 +344,8 @@ module Liquid # Parse variable token interior: extract markup from "{{[-] ... [-]}}" def parse_variable_token(token) len = token.bytesize - return nil if len < 4 + return if len < 4 + i = 2 i = 3 if token.getbyte(i) == DASH parse_end = len - 3 @@ -337,7 +363,7 @@ module Liquid def parse_simple_condition skip_ws @cond_left = scan_fragment - return nil unless @cond_left + return unless @cond_left skip_ws if eos? @@ -347,14 +373,15 @@ module Liquid end @cond_op = scan_comparison_op - return nil unless @cond_op + return unless @cond_op skip_ws @cond_right = scan_fragment - return nil unless @cond_right + return unless @cond_right skip_ws - return nil unless eos? # trailing junk + return unless eos? # trailing junk + true end # ── For tag parser ──────────────────────────────────────────────── @@ -366,11 +393,11 @@ module Liquid def parse_for_markup skip_ws @for_var = scan_id - return nil unless @for_var + return unless @for_var skip_ws # expect "in" - return nil unless scan_id == "in" + return unless scan_id == "in" skip_ws # Collection: parenthesized range or fragment @@ -386,7 +413,7 @@ module Liquid @for_collection = @source.byteslice(start, @ss.pos - start) else @for_collection = scan_fragment - return nil unless @for_collection + return unless @for_collection end skip_ws diff --git a/lib/liquid/expression.rb b/lib/liquid/expression.rb index 981946ef..c5fca063 100644 --- a/lib/liquid/expression.rb +++ b/lib/liquid/expression.rb @@ -88,8 +88,10 @@ module Liquid if first == DASH pos += 1 return false if pos >= len + b = markup.getbyte(pos) return false if b < ZERO || b > NINE + pos += 1 elsif first >= ZERO && first <= NINE pos += 1 @@ -100,7 +102,8 @@ module Liquid # Scan digits while pos < len b = markup.getbyte(pos) - break unless b >= ZERO && b <= NINE + break if b < ZERO || b > NINE + pos += 1 end @@ -117,7 +120,8 @@ module Liquid digit_after_dot = pos while pos < len b = markup.getbyte(pos) - break unless b >= ZERO && b <= NINE + break if b < ZERO || b > NINE + pos += 1 end @@ -127,7 +131,6 @@ module Liquid elsif pos > digit_after_dot # Float followed by more dots or other chars: "1.2.3.4" # Return the float portion up to second dot - first_dot_pos = dot_pos + 1 while pos < len b = markup.getbyte(pos) if b == DOT @@ -135,6 +138,7 @@ module Liquid elsif b < ZERO || b > NINE return false end + pos += 1 end return markup.byteslice(0, pos).to_f diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index 70d6a16f..390926f3 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -85,7 +85,6 @@ module Liquid Condition.parse_expression(parse_context, markup, safe: safe) end - def lax_parse(markup) # Fastest path: simple identifier truthiness like "product.available" or "forloop.first" if (simple = Variable.simple_variable_markup(markup)) diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 77b49ff8..932ce042 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -17,7 +17,7 @@ module Liquid # Avoids regex MatchData allocation. def self.simple_variable_markup(markup) len = markup.bytesize - return nil if len == 0 + return if len == 0 # Skip leading whitespace pos = 0 @@ -26,13 +26,13 @@ module Liquid break unless b == 32 || b == 9 || b == 10 || b == 13 pos += 1 end - return nil if pos >= len + return if pos >= len start = pos # First char must be [a-zA-Z_] b = markup.getbyte(pos) - return nil unless (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == 95 + return unless (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == 95 pos += 1 # Scan segments: [\w-]* (. [\w-]*)* @@ -43,9 +43,9 @@ module Liquid elsif b == 46 # '.' pos += 1 # After dot, must have [a-zA-Z_] - return nil if pos >= len + return if pos >= len b = markup.getbyte(pos) - return nil unless (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == 95 + return unless (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == 95 pos += 1 else break @@ -57,12 +57,12 @@ module Liquid # Skip trailing whitespace while pos < len b = markup.getbyte(pos) - return nil unless b == 32 || b == 9 || b == 10 || b == 13 + return unless b == 32 || b == 9 || b == 10 || b == 13 pos += 1 end # Must have consumed everything - return nil unless pos == len + return unless pos == len if start == 0 && content_end == len markup @@ -157,15 +157,15 @@ module Liquid ss = parse_context.string_scanner first_byte = expr_markup.getbyte(0) - if first_byte == 39 || first_byte == 34 # quoted string + @name = if first_byte == 39 || first_byte == 34 # quoted string # Strip quotes for string literal - @name = expr_markup.byteslice(1, expr_markup.bytesize - 2) + expr_markup.byteslice(1, expr_markup.bytesize - 2) elsif Expression::LITERALS.key?(expr_markup) - @name = Expression::LITERALS[expr_markup] + Expression::LITERALS[expr_markup] elsif cache - @name = cache[expr_markup] || (cache[expr_markup] = VariableLookup.parse_simple(expr_markup, ss, cache).freeze) + cache[expr_markup] || (cache[expr_markup] = VariableLookup.parse_simple(expr_markup, ss, cache).freeze) else - @name = VariableLookup.parse_simple(expr_markup, ss || StringScanner.new(""), nil).freeze + VariableLookup.parse_simple(expr_markup, ss || StringScanner.new(""), nil).freeze end # End of markup? No filters. @@ -225,7 +225,7 @@ module Liquid # Skip trailing whitespace filter_pos += 1 while filter_pos < len && (b = markup.getbyte(filter_pos)) && (b == 32 || b == 9 || b == 10 || b == 13) - return false unless filter_pos >= len + return false if filter_pos < len @filters = Const::EMPTY_ARRAY if @filters.empty? true @@ -321,10 +321,10 @@ module Liquid def render_to_output_buffer(context, output) # Fast path: no filters and no global filter - if @filters.empty? && context.global_filter.nil? - obj = context.evaluate(@name) + obj = if @filters.empty? && context.global_filter.nil? + context.evaluate(@name) else - obj = render(context) + render(context) end render_obj_to_output(obj, output) output