Skip to content

Commit 2df8b65

Browse files
Allow Liquid code if settings input type is liquid (#1096)
1 parent 52c1483 commit 2df8b65

3 files changed

Lines changed: 65 additions & 15 deletions

File tree

.changeset/little-boxes-shave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/theme-check-common': patch
3+
---
4+
5+
Allow Liquid in "liquid" input type

packages/theme-check-common/src/checks/liquid-free-settings/index.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@ describe('LiquidFreeSettings validation', () => {
5151
expect(offenses).to.have.length(0);
5252
});
5353

54+
it(`should not report errors for valid settings with liquid type field in ${path} bucket`, async () => {
55+
const theme: MockTheme = {
56+
[`${path}/test-section.liquid`]: `
57+
{% schema %}
58+
{
59+
"name": "Section name",
60+
"settings": [
61+
{
62+
"id": "text_value",
63+
"type": "liquid",
64+
"label": "Text Value",
65+
"default": "{% render 'block' %}"
66+
}
67+
]
68+
}
69+
{% endschema %}
70+
`,
71+
};
72+
73+
const offenses = await check(theme, [LiquidFreeSettings]);
74+
expect(offenses).to.have.length(0);
75+
});
76+
5477
it(`should report an error when settings value contains Liquid logic in ${path} bucket`, async () => {
5578
const theme: MockTheme = {
5679
[`${path}/test-section.liquid`]: `

packages/theme-check-common/src/checks/liquid-free-settings/index.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,27 @@ export const LiquidFreeSettings: LiquidCheckDefinition = {
4040

4141
visit<SourceCodeType.JSON, void>(jsonFile, {
4242
Property(schemaNode, ancestors) {
43-
if (isInArrayWithParentKey(ancestors, 'settings') && isLiteralNode(schemaNode.value)) {
44-
const { value, loc } = schemaNode.value;
45-
const propertyValue = schemaNode.key.value;
46-
if (
47-
typeof value === 'string' &&
48-
propertyValue !== 'visible_if' &&
49-
value.includes('{%') &&
50-
value.includes('%}')
51-
) {
52-
context.report({
53-
message: 'Settings values cannot contain liquid logic.',
54-
startIndex: node.blockStartPosition.end + loc!.start.offset,
55-
endIndex: node.blockStartPosition.end + loc!.end.offset,
56-
});
57-
}
43+
if (
44+
!isInArrayWithParentKey(ancestors, 'settings') ||
45+
!isLiteralNode(schemaNode.value) ||
46+
isLiquidType(ancestors)
47+
) {
48+
return;
49+
}
50+
51+
const { value, loc } = schemaNode.value;
52+
const propertyValue = schemaNode.key.value;
53+
if (
54+
typeof value === 'string' &&
55+
propertyValue !== 'visible_if' &&
56+
value.includes('{%') &&
57+
value.includes('%}')
58+
) {
59+
context.report({
60+
message: 'Settings values cannot contain liquid logic.',
61+
startIndex: node.blockStartPosition.end + loc!.start.offset,
62+
endIndex: node.blockStartPosition.end + loc!.end.offset,
63+
});
5864
}
5965
},
6066
});
@@ -67,6 +73,22 @@ function isLiteralNode(node: JSONNode): node is LiteralNode {
6773
return node.type === 'Literal';
6874
}
6975

76+
function isLiquidType(ancestors: JSONNode[]): boolean {
77+
const parentJsonNode = ancestors.at(-1)
78+
79+
if (!parentJsonNode || parentJsonNode.type !== 'Object') {
80+
return false;
81+
}
82+
83+
return parentJsonNode.children.some(({ key, value }) => {
84+
if (key.value !== 'type' || !isLiteralNode(value)) {
85+
return false;
86+
}
87+
88+
return value.value === 'liquid';
89+
});
90+
}
91+
7092
function isInArrayWithParentKey(ancestors: JSONNode[], parentKey: string): boolean {
7193
return ancestors.some((ancestor, index) => {
7294
const parent = ancestors[index - 1];

0 commit comments

Comments
 (0)