Skip to content

Add strict2_parse to assign and capture tags#2065

Open
aswamy wants to merge 1 commit intostrict2-include-tablerowfrom
strict2-assign-capture
Open

Add strict2_parse to assign and capture tags#2065
aswamy wants to merge 1 commit intostrict2-include-tablerowfrom
strict2-assign-capture

Conversation

@aswamy
Copy link
Copy Markdown
Contributor

@aswamy aswamy commented Mar 25, 2026

Summary

Adds strict2_parse to the assign and capture tags so invalid variable names are rejected in strict2 mode.

Both tags previously used only regex (VariableSignature) to validate variable names across all parse modes. This allowed invalid identifiers like (a(b(c), [x.y], and a.b to be silently accepted.

assign:

  • strict2_parse uses the regex to split on =, then validates the LHS with Parser#consume(:id) + consume(:end_of_string)
  • RHS is delegated to Variable.new as before

capture:

  • strict2_parse uses Parser#consume(:id) + consume(:end_of_string) to validate the variable name

Both tags now include ParserSwitching and dispatch through strict_parse_with_error_mode_fallback. Lax mode is unchanged.

Depends on: Shopify/liquid-rewriter-private#215 (rewriter changes)

Tophat

bundle install
bundle exec irb -r liquid
# Invalid names rejected in strict2
Liquid::Template.parse("{% assign (a(b(c) = 1234 %}", error_mode: :strict2)
# => Liquid::SyntaxError

Liquid::Template.parse("{% capture [x.y %}hello{% endcapture %}", error_mode: :strict2)
# => Liquid::SyntaxError

# Valid names still work
Liquid::Template.parse("{% assign my-var = 'hello' %}", error_mode: :strict2)
# => OK

# Lax mode unchanged
Liquid::Template.parse("{% assign (a(b(c) = 1234 %}", error_mode: :lax)
# => OK

🤖 Generated with Claude Code

@aswamy aswamy added the #gsd:49922 Liquid Array Literal Support label Mar 25, 2026
Both tags previously used only regex (VariableSignature) to validate
variable names, which allowed invalid identifiers like (a(b(c) and
[x.y] in all parse modes.

- assign: strict2_parse uses Parser to validate the LHS as a valid
  identifier before delegating RHS to Variable
- capture: strict2_parse uses Parser to validate the variable name
  as a valid identifier
- Both tags now include ParserSwitching and dispatch through
  strict_parse_with_error_mode_fallback
- Lax mode is unchanged — invalid names are still accepted

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@aswamy aswamy force-pushed the strict2-assign-capture branch from f5082d6 to 0d5c15a Compare March 25, 2026 16:07
@aswamy aswamy marked this pull request as ready for review March 25, 2026 20:07
Copy link
Copy Markdown
Contributor

@graygilmore graygilmore left a comment

Choose a reason for hiding this comment

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

This all seems reasonable!

# @liquid_syntax_keyword variable_name The name of the variable being created.
# @liquid_syntax_keyword value The value you want to assign to the variable.
class Assign < Tag
include ParserSwitching
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.

I don't think you need the include here because Tag already has it.

# @liquid_syntax_keyword variable The name of the variable being created.
# @liquid_syntax_keyword value The value you want to assign to the variable.
class Capture < Block
include ParserSwitching
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.

Same here

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.

3 participants