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/little-boxes-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme-check-common': patch
---

Allow Liquid in "liquid" input type
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ describe('LiquidFreeSettings validation', () => {
expect(offenses).to.have.length(0);
});

it(`should not report errors for valid settings with liquid type field in ${path} bucket`, async () => {
const theme: MockTheme = {
[`${path}/test-section.liquid`]: `
{% schema %}
{
"name": "Section name",
"settings": [
{
"id": "text_value",
"type": "liquid",
"label": "Text Value",
"default": "{% render 'block' %}"
}
]
}
{% endschema %}
`,
};

const offenses = await check(theme, [LiquidFreeSettings]);
expect(offenses).to.have.length(0);
});

it(`should report an error when settings value contains Liquid logic in ${path} bucket`, async () => {
const theme: MockTheme = {
[`${path}/test-section.liquid`]: `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,27 @@ export const LiquidFreeSettings: LiquidCheckDefinition = {

visit<SourceCodeType.JSON, void>(jsonFile, {
Property(schemaNode, ancestors) {
if (isInArrayWithParentKey(ancestors, 'settings') && isLiteralNode(schemaNode.value)) {
const { value, loc } = schemaNode.value;
const propertyValue = schemaNode.key.value;
if (
typeof value === 'string' &&
propertyValue !== 'visible_if' &&
value.includes('{%') &&
value.includes('%}')
) {
context.report({
message: 'Settings values cannot contain liquid logic.',
startIndex: node.blockStartPosition.end + loc!.start.offset,
endIndex: node.blockStartPosition.end + loc!.end.offset,
});
}
if (
!isInArrayWithParentKey(ancestors, 'settings') ||
!isLiteralNode(schemaNode.value) ||
isLiquidType(ancestors)
) {
return;
}

const { value, loc } = schemaNode.value;
const propertyValue = schemaNode.key.value;
if (
typeof value === 'string' &&
propertyValue !== 'visible_if' &&
value.includes('{%') &&
value.includes('%}')
) {
context.report({
message: 'Settings values cannot contain liquid logic.',
startIndex: node.blockStartPosition.end + loc!.start.offset,
endIndex: node.blockStartPosition.end + loc!.end.offset,
});
}
},
});
Expand All @@ -67,6 +73,22 @@ function isLiteralNode(node: JSONNode): node is LiteralNode {
return node.type === 'Literal';
}

function isLiquidType(ancestors: JSONNode[]): boolean {
return ancestors.some((ancestor) => {
if (ancestor.type !== 'Object') {
return false;
}

return ancestor.children.some(({ key, value }) => {
if (key.value !== 'type' || !isLiteralNode(value)) {
return false;
}

return value.value === 'liquid';
});
});
}

function isInArrayWithParentKey(ancestors: JSONNode[], parentKey: string): boolean {
return ancestors.some((ancestor, index) => {
const parent = ancestors[index - 1];
Expand Down