Skip to content

Fix UnusedAssign false positive inside inline {% liquid ... style ... endstyle %}#1182

Open
rushikeshmore wants to merge 1 commit intoShopify:mainfrom
rushikeshmore:fix/unused-assign-inline-liquid-style
Open

Fix UnusedAssign false positive inside inline {% liquid ... style ... endstyle %}#1182
rushikeshmore wants to merge 1 commit intoShopify:mainfrom
rushikeshmore:fix/unused-assign-inline-liquid-style

Conversation

@rushikeshmore
Copy link
Copy Markdown

What are you adding in this PR?

Fixes a false-positive UnusedAssign warning for variables that are only referenced inside a style block within an inline {% liquid %} tag.

Repro from the issue:

{%- liquid
  assign shopName = shop.name
  capture example
    echo 'Shopify'
  endcapture

  style
    echo example
    echo shopName
  endstyle
-%}

Before the fix, both example and shopName get reported as "assigned but not used". After the fix, no offense is reported.

Root cause

The CST mapping in packages/liquid-html-parser/src/stage-1-cst.ts has two liquidRawTagImpl entries — one for the main Liquid grammar (top-level {% style %}...{% endstyle %}) and one for the LiquidStatement grammar (inline {% liquid ... style ... endstyle %}).

The top-level mapping already distinguishes:

  • schema / rawTextNodeGrammar (raw string, not visited)
  • everything else (style, stylesheet, javascript) → re-parsed with grammars.Liquid so children end up in the AST

The inline mapping forced every raw tag body through TextNodeGrammar. That meant the echo example and echo shopName inside the inline style block never produced VariableLookup nodes, and UnusedAssign (which walks VariableLookup to decide "used") never registered them as used.

Fix

Mirror the top-level switch in the inline mapping. schema and raw still stay as raw text; every other raw tag is re-parsed with grammars.LiquidStatement (the inline-liquid grammar) so references inside style and friends become first-class AST nodes.

Adds a regression test in unused-assign/index.spec.ts using the exact snippet from #465.

Related

This supersedes the grammar-level attempt in #683 — the real bug was not in the grammar (style is a valid raw tag name in both the top-level and inline-liquid grammars) but in how the CST mapping re-parsed the raw body.

What's next? Any followup issues?

The inline form of {% liquid ... javascript %} / {% liquid ... stylesheet %} / {% liquid ... schema %} is exotic but now produces the same AST shape as their top-level counterparts. No follow-ups I can see.

Before you deploy

  • I included a patch bump changeset for @shopify/liquid-html-parser
  • Full vitest run green — 294 files, 1860 tests, 1 pre-existing skip
  • yarn format:check clean
  • yarn workspace ... type-check clean for @shopify/liquid-html-parser, @shopify/theme-check-common, @shopify/theme-check-node, @shopify/theme-language-server-common, @shopify/prettier-plugin-liquid

Closes #465

The inline-liquid raw-tag CST mapping parsed every raw-tag body as
plain text. As a result, variable references inside
{% liquid ... style ... echo foo ... endstyle %} never produced
AST nodes, so checks that walk the tree saw no usage and flagged
assigns as unused.

The top-level liquidRawTagImpl already distinguishes schema/raw
(kept as text) from other raw tags like style (re-parsed as Liquid
so children are part of the AST). The inline-liquid mapping now
mirrors this: schema and raw stay text; everything else is
re-parsed with the LiquidStatement grammar so references inside
style and friends are preserved.

Added a regression test in UnusedAssign that reproduces the
exact snippet from the original report. Full vitest suite
(294 files, 1860 tests) remains green.

Closes Shopify#465
@rushikeshmore rushikeshmore requested a review from a team as a code owner April 18, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False Positive "Variable Assigned but Not Used" in Style Tags

1 participant