Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-options-by-name-spaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme-check-common': patch
---

Fix loop argument validation for quoted `options_by_name` keys containing spaces.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ describe('detectInvalidLoopArguments', async () => {
const testCases = [
`{% for i in array reversed %}{% endfor %}`,
`{% for i in array reversed offset: 1 %}{% endfor %}`,
`{% for value in product.options_by_name['hello world'].values %}{% endfor %}`,
`{% for value in product.options_by_name["hello world"].values %}{% endfor %}`,
`{% tablerow x in array limit: 10 %}{% endtablerow %}`,
`{% tablerow x in array cols: 2 limit: 10 %}{% endtablerow %}`,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ export function getValuesInMarkup(markup: string) {

const DOUBLE_QUOTED_STRING = `"[^"]*"`;
const SINGLE_QUOTED_STRING = `'[^']*'`;
const QUOTED_STRING = `(${DOUBLE_QUOTED_STRING}|${SINGLE_QUOTED_STRING})`;
const VARIABLE_LOOKUP_WITH_QUOTED_INDEX = `[^\\s,\\[]+(?:\\s*\\[\\s*${QUOTED_STRING}\\s*\\][^\\s,\\[]*)+`;
// Lax parser does NOT complain about leading/trailing spaces inside ranges (e.g. `(1 .. 10 )`) and
// within the parenthesis (e.g. `( 1 .. 10 )`), but fails to render the liquid when using the gem.
// Strict parser does NOT complain, but still renders it.
// To avoid any issues, we will remove extra spaces.
const RANGE_MARKUP_COMPONENT_REGEX = `\\s*(-?\\d+(?:\\.\\d+)?|\\w+(?:\\.\\w+)*)\\s*`;
const RANGE_MARKUP_REGEX = `\\(\\s*${RANGE_MARKUP_COMPONENT_REGEX}(\\.{2,})\\s*${RANGE_MARKUP_COMPONENT_REGEX}\\s*\\)`;
const REGULAR_TOKEN = `[^\\s,]+`; // tokens separated by commas or spaces

// Quoted strings pattern (combination of double and single quoted)
const QUOTED_STRING = `(${DOUBLE_QUOTED_STRING}|${SINGLE_QUOTED_STRING})`;
const REGULAR_TOKEN = `(${VARIABLE_LOOKUP_WITH_QUOTED_INDEX}|[^\\s,]+)`; // tokens separated by commas or spaces

// Value pattern for key-value pairs (can be quoted, parenthesized, or regular token)
const VALUE_PATTERN = `(${QUOTED_STRING}|${RANGE_MARKUP_REGEX}|${REGULAR_TOKEN})`;
Expand Down
Loading