-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added missing schema check for theme app extension
added context
- Loading branch information
1 parent
a579d59
commit 05ae5ea
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/theme-check-common/src/checks/missing-schema/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} | ||
{ | ||
"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
37
packages/theme-check-common/src/checks/missing-schema/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters