Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing schema check for theme app extension #613

Merged
merged 1 commit into from
Nov 22, 2024
Merged
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
6 changes: 6 additions & 0 deletions .changeset/dirty-terms-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/theme-check-common': minor
'@shopify/theme-check-node': minor
---

Added MissingSchema theme check to identify missing schemas in theme app extensions.
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 @@ -40,6 +40,7 @@ import { ValidSchema } from './valid-schema';
import { ValidSchemaName } from './valid-schema-name';
import { ValidStaticBlockType } from './valid-static-block-type';
import { VariableName } from './variable-name';
import { MissingSchema } from './missing-schema';

export const allChecks: (LiquidCheckDefinition | JSONCheckDefinition)[] = [
AppBlockValidTags,
Expand All @@ -62,6 +63,7 @@ export const allChecks: (LiquidCheckDefinition | JSONCheckDefinition)[] = [
MatchingTranslations,
MissingAsset,
MissingTemplate,
MissingSchema,
PaginationSize,
ParserBlockingScript,
RemoteAsset,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { describe, expect, it } from 'vitest';
import { runLiquidCheck } from '../../test';
import { MissingSchema } from './index';
import { Config } from '../../types';

describe('MissingSchema', () => {
it('should report an error when schema tag is missing on a theme app extension', async () => {
const sourceCode = `
<footer class="footer">
{% for block in section.blocks %}
{% case block.type %}
{% when 'link' %}
<div class="link" {{ block.shopify_attributes }}>
{{ block.settings.linktext | link_to: block.settings.linkurl }}
</div>

{% when 'custom-text' %}
<div class="text" {{ block.shopify_attributes }}>
{{ block.settings.custom-text-field }}
</div>
{% endcase %}
{% endfor %}
</footer>
`;

const offenses = await runLiquidCheck(MissingSchema, sourceCode);
expect(offenses).to.have.lengthOf(1);
});

it('should not report when the schema is present on a theme app extension', async () => {
const sourceCode = `
<footer class="footer">
{% for block in section.blocks %}
{% case block.type %}
{% when 'link' %}
<div class="link" {{ block.shopify_attributes }}>
{{ block.settings.linktext | link_to: block.settings.linkurl }}
</div>

{% when 'custom-text' %}
<div class="text" {{ block.shopify_attributes }}>
{{ block.settings.custom-text-field }}
</div>
{% endcase %}
{% endfor %}
</footer>

{% schema %}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
"name": "Footer",
"max_blocks": 8,
"blocks": [
{
"type": "link",
"name": "Link",
"settings": [
{
"id": "linkurl",
"type": "url",
"label": "URL link"
},
{
"id": "linktext",
"type": "text",
"label": "Link text"
}
]
},
{
"type": "custom-text",
"name": "Custom Text",
"settings": [
{
"id": "custom-text-field",
"type": "textarea",
"label": "Text"
}
]
}
]
}
{% endschema %}`;

const offenses = await runLiquidCheck(MissingSchema, sourceCode);
expect(offenses).to.have.lengthOf(0);
});
});
37 changes: 37 additions & 0 deletions packages/theme-check-common/src/checks/missing-schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ConfigTarget, LiquidCheckDefinition, Severity, SourceCodeType } from '../../types';

export const MissingSchema: LiquidCheckDefinition = {
meta: {
code: 'MissingSchema',
name: 'Missing schema definitions in theme app extensions should be avoided',
docs: {
description: 'Report missing schema definitions in theme app extensions',
recommended: true,
url: 'https://shopify.dev/docs/storefronts/themes/tools/theme-check/checks/missing-schema',
},
severity: Severity.ERROR,
type: SourceCodeType.LiquidHtml,
schema: {},
targets: [ConfigTarget.ThemeAppExtension],
},

create(context) {
let foundSchema = false;

return {
async LiquidRawTag(node) {
if (node.name == 'schema') foundSchema = true;
},

async onCodePathEnd() {
if (!foundSchema) {
context.report({
message: `The schema does not exist`,
startIndex: 0,
endIndex: 0,
});
}
},
};
},
};
3 changes: 3 additions & 0 deletions packages/theme-check-node/configs/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ MatchingTranslations:
MissingAsset:
enabled: true
severity: 0
MissingSchema:
enabled: false
severity: 0
MissingTemplate:
enabled: true
severity: 0
Expand Down
3 changes: 3 additions & 0 deletions packages/theme-check-node/configs/theme-app-extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ AssetSizeAppBlockJavaScript:
enabled: true
severity: 0
thresholdInBytes: 10000
MissingSchema:
enabled: true
severity: 0
RequiredLayoutThemeObject:
enabled: false
severity: 0
Loading