Skip to content

Commit 0693d4f

Browse files
docs-engineering-bot[bot]CopilotCopilot
authored
fix: replace inIfStatement boolean with depth counter in getLiquidIfVersionTokens (#62299)
Co-authored-by: docs-engineering-bot[bot] <285023527+docs-engineering-bot[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 1484a08 commit 0693d4f

2 files changed

Lines changed: 82 additions & 6 deletions

File tree

src/content-linter/lib/helpers/liquid-utils.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,26 @@ export function getLiquidIfVersionTokens(content: string): TagToken[] {
123123
.filter((token): token is TagToken => token.kind === TokenKind.Tag)
124124
.filter((token) => IFVERSION_TAG_NAMES.includes(token.name))
125125

126-
let inIfStatement = false
126+
let ifDepth = 0
127127
let inCaseStatement = false
128128
const ifVersionTokens: TagToken[] = []
129129
for (const token of tokens) {
130-
// Filter out `if` statements and their related tags
130+
// Filter out `if` statements and their related tags (supports nesting)
131131
if (token.name === 'if') {
132-
inIfStatement = true
132+
ifDepth++
133133
continue
134134
}
135-
if (inIfStatement && token.name !== 'endif') continue
136-
if (inIfStatement && token.name === 'endif') {
137-
inIfStatement = false
135+
// While we're inside a regular if subtree, `endif` can close either
136+
// `if` or `ifversion`, so count nested `ifversion` tags too.
137+
if (ifDepth > 0 && token.name === 'ifversion') {
138+
ifDepth++
138139
continue
139140
}
141+
if (ifDepth > 0 && token.name === 'endif') {
142+
ifDepth--
143+
continue
144+
}
145+
if (ifDepth > 0) continue
140146
// Filter out `case` statements and their related tags (including `else`)
141147
if (token.name === 'case') {
142148
inCaseStatement = true

src/content-linter/tests/unit/liquid-ifversion-versions.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,76 @@ describe(liquidIfversionVersions.names.join(' - '), () => {
139139
const errors = result.markdown
140140
expect(errors.length).toBe(0)
141141
})
142+
143+
test('does not crash with nested if blocks inside ifversion', async () => {
144+
const markdown = [
145+
...placeholderAllVersionsFm,
146+
'{% ifversion ghec %}',
147+
' {% if "foo" %}',
148+
' {% if "bar" %}nested{% endif %}',
149+
' {% endif %}',
150+
'{% endif %}',
151+
].join('\n')
152+
153+
const result = await runRule(liquidIfversionVersions, {
154+
strings: { markdown },
155+
})
156+
// No crash; zero errors expected for valid ifversion usage
157+
const errors = result.markdown
158+
expect(errors.length).toBe(0)
159+
})
160+
161+
test('does not crash with nested if blocks at top level', async () => {
162+
const markdown = [
163+
...placeholderAllVersionsFm,
164+
'{% if "foo" %}',
165+
' {% if "bar" %}...{% endif %}',
166+
'{% endif %}',
167+
].join('\n')
168+
169+
const result = await runRule(liquidIfversionVersions, {
170+
strings: { markdown },
171+
})
172+
const errors = result.markdown
173+
expect(errors.length).toBe(0)
174+
})
175+
176+
test('does not crash with ifversion nested inside if blocks', async () => {
177+
const markdown = [
178+
...placeholderAllVersionsFm,
179+
'{% if "foo" %}',
180+
' {% ifversion ghec %}',
181+
' {% endif %}',
182+
'{% endif %}',
183+
].join('\n')
184+
185+
const result = await runRule(liquidIfversionVersions, {
186+
strings: { markdown },
187+
})
188+
const errors = result.markdown
189+
expect(errors.length).toBe(0)
190+
})
191+
192+
test('does not crash with mixed if/ifversion nesting and else branches', async () => {
193+
const markdown = [
194+
...placeholderAllVersionsFm,
195+
'{% if "foo" %}',
196+
' {% ifversion ghec %}',
197+
' {% if "bar" %}nested{% endif %}',
198+
' {% else %}',
199+
' text',
200+
' {% endif %}',
201+
'{% else %}',
202+
' top-level else',
203+
'{% endif %}',
204+
].join('\n')
205+
206+
const result = await runRule(liquidIfversionVersions, {
207+
strings: { markdown },
208+
})
209+
const errors = result.markdown
210+
expect(errors.length).toBe(0)
211+
})
142212
})
143213

144214
describe.skip('test validateIfversionConditionalsVersions function', () => {

0 commit comments

Comments
 (0)