Skip to content

Commit

Permalink
Merge pull request #604 from Shopify/miaz/presets-block-order
Browse files Browse the repository at this point in the history
Check preset blocks_order
  • Loading branch information
miazbikowski authored Dec 2, 2024
2 parents e61fa07 + da25290 commit 21bffc5
Show file tree
Hide file tree
Showing 11 changed files with 541 additions and 33 deletions.
7 changes: 7 additions & 0 deletions .changeset/smooth-cycles-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@shopify/theme-check-common': minor
'@shopify/theme-check-node': minor
'@shopify/theme-check-vscode': minor
---

Add `SchemaPresetsBlockOrder` check
2 changes: 2 additions & 0 deletions packages/theme-check-common/src/checks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { MissingAsset } from './missing-asset';
import { MissingTemplate } from './missing-template';
import { PaginationSize } from './pagination-size';
import { ParserBlockingScript } from './parser-blocking-script';
import { SchemaPresetsBlockOrder } from './schema-presets-block-order';
import { RemoteAsset } from './remote-asset';
import { RequiredLayoutThemeObject } from './required-layout-theme-object';
import { TranslationKeyExists } from './translation-key-exists';
Expand Down Expand Up @@ -68,6 +69,7 @@ export const allChecks: (LiquidCheckDefinition | JSONCheckDefinition)[] = [
MissingSchema,
PaginationSize,
ParserBlockingScript,
SchemaPresetsBlockOrder,
RemoteAsset,
RequiredLayoutThemeObject,
TranslationKeyExists,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,316 @@
import { expect, describe, it } from 'vitest';
import { highlightedOffenses, runLiquidCheck, check } from '../../test';
import { SchemaPresetsBlockOrder } from './index';

const DEFAULT_FILE_NAME = 'sections/file.liquid';

describe('Module: SchemaPresetsBlockOrder', () => {
it('reports no warning when the preset blocks are in the block_order', async () => {
const sourceCode = `
{% schema %}
{
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text"
},
"block-2": {
"type": "icon"
}
},
"block_order": ["block-1", "block-2"]
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(0);
});

it('reports a warning when not all preset blocks are in the block_order', async () => {
const sourceCode = `
{% schema %}
{
"name": "Test section",
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text"
},
"block-2": {
"type": "icon"
}
},
"block_order": ["block-1"]
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(1);
expect(offenses[0].message).toEqual("block 'block-2' is missing from the block_order");

const highlights = highlightedOffenses({ [DEFAULT_FILE_NAME]: sourceCode }, offenses);
expect(highlights).toHaveLength(1);
expect(highlights[0]).toBe('["block-1"]');
});

it('reports no warning when the preset blocks has static blocks that are not in the block_order', async () => {
const sourceCode = `
{% schema %}
{
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text"
},
"block-2": {
"type": "icon",
"static": true
}
},
"block_order": ["block-1"]
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(0);
});

it('reports a warning when preset blocks has static blocks that are in the block_order', async () => {
const sourceCode = `
{% schema %}
{
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text"
},
"block-2": {
"type": "icon",
"static": true
}
},
"block_order": ["block-1", "block-2"]
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(1);
expect(offenses[0].message).toEqual("static block 'block-2' cannot be in the block_order");

const highlights = highlightedOffenses({ [DEFAULT_FILE_NAME]: sourceCode }, offenses);
expect(highlights).toHaveLength(1);
expect(highlights[0]).toBe('["block-1", "block-2"]');
});

it('reports no warning when the nested preset blocks are in the block_order', async () => {
const sourceCode = `
{% schema %}
{
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text",
"blocks": {
"nested-1": {
"type": "nested"
},
"nested-2": {
"type": "nested"
}
},
"block_order": ["nested-1", "nested-2"]
},
"block-2": {
"type": "icon"
}
},
"block_order": ["block-1", "block-2"]
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(0);
});

it('reports a warning when the nested preset blocks are not in the block_order', async () => {
const sourceCode = `
{% schema %}
{
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text",
"blocks": {
"nested-1": {
"type": "nested"
},
"nested-2": {
"type": "nested"
}
},
"block_order": ["nested-1"]
},
"block-2": {
"type": "icon"
}
},
"block_order": ["block-1", "block-2"]
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(1);
expect(offenses[0].message).toEqual("block 'nested-2' is missing from the block_order");

const highlights = highlightedOffenses({ [DEFAULT_FILE_NAME]: sourceCode }, offenses);
expect(highlights).toHaveLength(1);
expect(highlights[0]).toBe('["nested-1"]');
});

it('reports no warning when the nested preset blocks has static blocks that are not in the block_order', async () => {
const sourceCode = `
{% schema %}
{
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text",
"blocks": {
"nested-1": {
"type": "nested",
"static": true
},
"nested-2": {
"type": "nested"
}
},
"block_order": ["nested-2"]
},
"block-2": {
"type": "icon"
}
},
"block_order": ["block-1", "block-2"]
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(0);
});

it('reports a warning when the nested preset blocks has static blocks that are in the block_order', async () => {
const sourceCode = `
{% schema %}
{
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text",
"blocks": {
"nested-1": {
"type": "nested",
"static": true
},
"nested-2": {
"type": "nested"
}
},
"block_order": ["nested-1", "nested-2"]
},
"block-2": {
"type": "icon"
}
},
"block_order": ["block-1", "block-2"]
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(1);
expect(offenses[0].message).toEqual("static block 'nested-1' cannot be in the block_order");

const highlights = highlightedOffenses({ [DEFAULT_FILE_NAME]: sourceCode }, offenses);
expect(highlights).toHaveLength(1);
expect(highlights[0]).toBe('["nested-1", "nested-2"]');
});

it('reports a warning when there should be a block_order but it is missing', async () => {
const sourceCode = `
{% schema %}
{
"name": "Test section",
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text"
}
}
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(1);
expect(offenses[0].message).toEqual('block_order is missing');

const highlights = highlightedOffenses({ [DEFAULT_FILE_NAME]: sourceCode }, offenses);
expect(highlights).toHaveLength(1);
});

it('reports no warning we have all static blocks and no block_order', async () => {
const sourceCode = `
{% schema %}
{
"presets": [
{
"name": "Preset 1",
"blocks": {
"block-1": {
"type": "text",
"static": true
}
}
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(SchemaPresetsBlockOrder, sourceCode, DEFAULT_FILE_NAME);
expect(offenses).toHaveLength(0);
});
});
Loading

0 comments on commit 21bffc5

Please sign in to comment.