Skip to content

Add for_loop? to Include tag for AST-based keyword detection#2064

Open
aswamy wants to merge 1 commit intobare-bracket-self-keywordfrom
strict2-include-tablerow
Open

Add for_loop? to Include tag for AST-based keyword detection#2064
aswamy wants to merge 1 commit intobare-bracket-self-keywordfrom
strict2-include-tablerow

Conversation

@aswamy
Copy link
Copy Markdown
Contributor

@aswamy aswamy commented Mar 23, 2026

Summary

Stores @is_for_loop during parsing in the Include tag, matching the existing pattern in the Render tag. This allows consumers (like the liquid-rewriter) to determine the with/for keyword from the parsed AST instead of re-parsing raw markup with regex.

Changes:

  • Added FOR = 'for' constant
  • Added for_loop? public method
  • strict2_parse: captures the with/for token from p.id? and stores @is_for_loop
  • lax_parse: updated SYNTAX regex to capture the keyword in its own group (group 3), stores @is_for_loop
  • Regex capture groups shifted: variable name is now group 4, alias name is now group 6

No runtime behavior change — Include#render_to_output_buffer still uses variable.is_a?(Array) for iteration, not @is_for_loop.

Depends on: Shopify/liquid-rewriter-private#214

Tophat

bundle install
bundle exec irb -r liquid
# for keyword detected
t = Liquid::Template.parse("{% include 'product' for products %}")
t.root.nodelist.first.for_loop?  # => true

# with keyword detected
t = Liquid::Template.parse("{% include 'product' with product %}")
t.root.nodelist.first.for_loop?  # => false

# no keyword
t = Liquid::Template.parse("{% include 'header' %}")
t.root.nodelist.first.for_loop?  # => false

🤖 Generated with Claude Code

@aswamy aswamy force-pushed the strict2-include-tablerow branch from 19568df to 55f3a37 Compare March 23, 2026 21:34
@aswamy aswamy requested review from a team, EvilGenius13, charlespwd and graygilmore March 23, 2026 21:38
@aswamy aswamy marked this pull request as ready for review March 23, 2026 21:38
assert_match(/Unexpected character =/, error.message)
end
end
# -- for_loop? AST-based keyword detection --
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but let's remove that comment. IDK why claude loves these so much. I struggle with em too.

@aswamy aswamy force-pushed the strict2-include-tablerow branch 2 times, most recently from b9745e5 to 7253e3e Compare March 24, 2026 14:30
@aswamy aswamy added the #gsd:49922 Liquid Array Literal Support label Mar 24, 2026
Store @is_for_loop during parsing so consumers can determine the
with/for keyword from the AST instead of re-parsing raw markup.
Matches the existing pattern in the Render tag.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@aswamy aswamy force-pushed the strict2-include-tablerow branch from 7253e3e to 346166b Compare March 24, 2026 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

#gsd:49922 Liquid Array Literal Support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants