Skip to content

Commit a53c595

Browse files
authored
Fix content linter crash when processing case statements with else tags (#59050)
1 parent 200ce2a commit a53c595

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const TAG_OPEN = '{{'
3434
export const TAG_CLOSE = '}}'
3535

3636
export const conditionalTags = ['if', 'elseif', 'unless', 'case', 'ifversion']
37-
const CONDITIONAL_TAG_NAMES = ['if', 'ifversion', 'elsif', 'else', 'endif']
3837

3938
// Token parameter uses TopLevelToken which has begin and end properties
4039
export function getPositionData(
@@ -120,13 +119,17 @@ export function getContentDeleteData(
120119
// `ifversion` tag is used.
121120
// Returns TagToken array since we filter to only Tag tokens
122121
export function getLiquidIfVersionTokens(content: string): TagToken[] {
122+
// Include 'case' and 'endcase' so we can filter out `else` tags that belong to case statements
123+
const IFVERSION_TAG_NAMES = ['if', 'ifversion', 'elsif', 'else', 'endif', 'case', 'endcase']
123124
const tokens = getLiquidTokens(content)
124125
.filter((token): token is TagToken => token.kind === TokenKind.Tag)
125-
.filter((token) => CONDITIONAL_TAG_NAMES.includes(token.name))
126+
.filter((token) => IFVERSION_TAG_NAMES.includes(token.name))
126127

127128
let inIfStatement = false
129+
let inCaseStatement = false
128130
const ifVersionTokens: TagToken[] = []
129131
for (const token of tokens) {
132+
// Filter out `if` statements and their related tags
130133
if (token.name === 'if') {
131134
inIfStatement = true
132135
continue
@@ -136,6 +139,16 @@ export function getLiquidIfVersionTokens(content: string): TagToken[] {
136139
inIfStatement = false
137140
continue
138141
}
142+
// Filter out `case` statements and their related tags (including `else`)
143+
if (token.name === 'case') {
144+
inCaseStatement = true
145+
continue
146+
}
147+
if (inCaseStatement && token.name !== 'endcase') continue
148+
if (inCaseStatement && token.name === 'endcase') {
149+
inCaseStatement = false
150+
continue
151+
}
139152
ifVersionTokens.push(token)
140153
}
141154
return ifVersionTokens

0 commit comments

Comments
 (0)