-
Notifications
You must be signed in to change notification settings - Fork 37
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
Theme check for settings keys inside presets and default #742
Merged
+474
−15
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 @@ | ||
--- | ||
'@shopify/theme-check-common': minor | ||
'@shopify/theme-check-node': minor | ||
--- | ||
|
||
Theme check verifies if setting key exists within block schemas and section schemas | ||
|
||
- Check if the keys inside `presets.[].settings` and `default.settings` exist as `settings.[].id` in the same file | ||
- Check if the keys inside `presets.[](recursive .blocks.[]).settings` and `default.blocks.[].settings` exist as `settings.[].id` inside the referenced block's file |
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
1 change: 0 additions & 1 deletion
1
packages/theme-check-common/src/checks/valid-block-target/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
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
321 changes: 321 additions & 0 deletions
321
packages/theme-check-common/src/checks/valid-settings-key/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,321 @@ | ||
import { expect, describe, it } from 'vitest'; | ||
import { ValidSettingsKey } from './index'; | ||
import { check } from '../../test'; | ||
|
||
describe('Moduel: ValidSettingsKey', () => { | ||
const schemaTemplate = { | ||
name: 'Example', | ||
settings: [ | ||
{ | ||
id: 'existent-setting', | ||
type: 'text', | ||
label: 'Example Text Setting', | ||
}, | ||
], | ||
}; | ||
|
||
describe('default settings', () => { | ||
it('does not report an error when default setting exists', async () => { | ||
const theme = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
default: { | ||
settings: { | ||
'existent-setting': 'value', | ||
}, | ||
}, | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it('reports an error when default setting does not exist', async () => { | ||
const theme = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
default: { | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(1); | ||
expect(offenses[0].message).to.equal( | ||
`Setting 'non-existent-setting' does not exist in schema.`, | ||
); | ||
}); | ||
|
||
it('does not report an error when missing default settings is referenced from a block schema', async () => { | ||
const theme = { | ||
'blocks/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
default: { | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
}); | ||
|
||
describe('presets settings', () => { | ||
it('does not report an error when presets setting exists', async () => { | ||
const theme = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
presets: [ | ||
{ | ||
settings: { | ||
'existent-setting': 'value', | ||
}, | ||
}, | ||
], | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it('reports an error when presets setting does not exist', async () => { | ||
const theme = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
presets: [ | ||
{ | ||
settings: { | ||
'non-existent-setting-1': 'value', | ||
}, | ||
}, | ||
{ | ||
settings: { | ||
'non-existent-setting-2': 'value', | ||
}, | ||
}, | ||
], | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(2); | ||
expect(offenses[0].message).to.equal( | ||
`Setting 'non-existent-setting-1' does not exist in schema.`, | ||
); | ||
expect(offenses[1].message).to.equal( | ||
`Setting 'non-existent-setting-2' does not exist in schema.`, | ||
); | ||
}); | ||
}); | ||
|
||
const tests = [ | ||
{ | ||
label: 'default', | ||
blockTemplate: (blocks: any[]) => { | ||
return { | ||
default: { | ||
blocks, | ||
}, | ||
}; | ||
}, | ||
}, | ||
{ | ||
label: 'presets', | ||
blockTemplate: (blocks: any[]) => { | ||
return { | ||
presets: [ | ||
{ | ||
blocks, | ||
}, | ||
], | ||
}; | ||
}, | ||
}, | ||
]; | ||
|
||
tests.forEach(({ label, blockTemplate }) => { | ||
describe(`${label} block settings`, () => { | ||
describe('referenced blocks', () => { | ||
const referencedBlock = { | ||
'blocks/referenced.liquid': toLiquidFile(schemaTemplate), | ||
}; | ||
|
||
it(`does not report an error when ${label} block setting exists in referenced file`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'referenced', | ||
settings: { | ||
'existent-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check( | ||
{ | ||
...referencedBlock, | ||
...fileToTest, | ||
}, | ||
[ValidSettingsKey], | ||
); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it('does not report an error when referenced file does not exist', async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'non-existent-file', | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check( | ||
{ | ||
...referencedBlock, | ||
...fileToTest, | ||
}, | ||
[ValidSettingsKey], | ||
); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it(`reports an error when ${label} block setting does not exist in referenced file`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'referenced', | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check( | ||
{ | ||
...referencedBlock, | ||
...fileToTest, | ||
}, | ||
[ValidSettingsKey], | ||
); | ||
expect(offenses).to.have.length(1); | ||
expect(offenses[0].message).to.equal( | ||
`Setting 'non-existent-setting' does not exist in 'blocks/referenced.liquid'.`, | ||
); | ||
}); | ||
}); | ||
|
||
describe('local blocks', () => { | ||
const localBlocksTemplate = { | ||
blocks: [ | ||
{ | ||
type: 'local-block', | ||
name: 'Local block', | ||
settings: [ | ||
{ | ||
id: 'local-setting', | ||
type: 'text', | ||
label: 'Local Setting', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
it(`reports an error when ${label} block setting does not exist in existing local block`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...localBlocksTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'local-block', | ||
settings: { | ||
'non-existent-setting-1': 'value', | ||
'non-existent-setting-2': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check(fileToTest, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(2); | ||
expect(offenses[0].message).to.equal( | ||
`Setting 'non-existent-setting-1' does not exist in schema.`, | ||
); | ||
expect(offenses[1].message).to.equal( | ||
`Setting 'non-existent-setting-2' does not exist in schema.`, | ||
); | ||
}); | ||
|
||
it(`does not report an error when ${label} block setting does not exist in non-existent local block`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...localBlocksTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'non-existent-local-block', | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check(fileToTest, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it(`does not report an error when ${label} block setting exists in local block`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...localBlocksTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'local-block', | ||
settings: { | ||
'local-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check(fileToTest, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
function toLiquidFile(content: any) { | ||
return ` | ||
{% schema %} | ||
${JSON.stringify(content)} | ||
{% endschema %} | ||
`; | ||
} |
129 changes: 129 additions & 0 deletions
129
packages/theme-check-common/src/checks/valid-settings-key/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,129 @@ | ||
import { | ||
LiquidCheckDefinition, | ||
Severity, | ||
SourceCodeType, | ||
Preset, | ||
Section, | ||
ThemeBlock, | ||
JSONNode, | ||
Setting, | ||
} from '../../types'; | ||
import { nodeAtPath } from '../../json'; | ||
import { getSchema, isSectionSchema } from '../../to-schema'; | ||
import { BlockNodeWithPath, getBlocks, reportWarning } from '../../utils'; | ||
|
||
export const ValidSettingsKey: LiquidCheckDefinition = { | ||
meta: { | ||
code: 'ValidSettingsKey', | ||
name: 'Validate settings key in presets', | ||
docs: { | ||
description: | ||
"Ensures settings key only references valid settings defined in it's respective schema", | ||
recommended: true, | ||
url: 'https://shopify.dev/docs/storefronts/themes/tools/theme-check/checks/valid-settings-key', | ||
}, | ||
type: SourceCodeType.LiquidHtml, | ||
severity: Severity.ERROR, | ||
schema: {}, | ||
targets: [], | ||
}, | ||
|
||
create(context) { | ||
return { | ||
async LiquidRawTag(node) { | ||
if (node.name !== 'schema' || node.body.kind !== 'json') return; | ||
|
||
const offset = node.blockStartPosition.end; | ||
const schema = await getSchema(context); | ||
|
||
const { validSchema, ast } = schema ?? {}; | ||
if (!validSchema || validSchema instanceof Error) return; | ||
if (!ast || ast instanceof Error) return; | ||
|
||
const { rootLevelLocalBlocks, presetLevelBlocks } = getBlocks(validSchema); | ||
|
||
// Check if presets settings match schema-level settings | ||
if (validSchema.presets) { | ||
for (let i = 0; i < validSchema.presets.length; i++) { | ||
const settingsNode = nodeAtPath(ast, ['presets', i, 'settings']); | ||
|
||
validateSettingsKey(context, offset, settingsNode, validSchema.settings); | ||
} | ||
} | ||
|
||
if (isSectionSchema(schema) && 'default' in validSchema && validSchema.default) { | ||
// Check if default settings match schema-level settings | ||
const settingsNode = nodeAtPath(ast, ['default', 'settings']); | ||
|
||
validateSettingsKey(context, offset, settingsNode, validSchema.settings); | ||
|
||
// Check if default block settings match the settings defined in the block file's schema | ||
validSchema.default.blocks?.forEach(async (block, i) => { | ||
const settingsNode = nodeAtPath(ast, ['default', 'blocks', i, 'settings']); | ||
|
||
validateReferencedBlock(context, offset, settingsNode, rootLevelLocalBlocks, block); | ||
}); | ||
} | ||
|
||
// Check if preset block settings match the settings defined in the block file's schema | ||
for (let [_depthStr, blocks] of Object.entries(presetLevelBlocks)) { | ||
blocks.forEach(async ({ node: blockNode, path }) => { | ||
const settingsNode = nodeAtPath(ast, path.slice(0, -1).concat('settings')); | ||
|
||
validateReferencedBlock(context, offset, settingsNode, rootLevelLocalBlocks, blockNode); | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
async function validateReferencedBlock( | ||
context: any, | ||
offset: number, | ||
settingsNode: JSONNode | undefined, | ||
localBlocks: BlockNodeWithPath[], | ||
referencedBlock: Preset.Block | Section.Block | ThemeBlock.Block, | ||
) { | ||
if (localBlocks.length > 0) { | ||
const localBlock = localBlocks.find( | ||
(localBlock) => localBlock.node.type === referencedBlock.type, | ||
); | ||
|
||
if (!localBlock) return; | ||
|
||
const localBlockNode = localBlock.node as Section.LocalBlock; | ||
|
||
validateSettingsKey(context, offset, settingsNode, localBlockNode.settings); | ||
} else { | ||
const blockSchema = await context.getBlockSchema?.(referencedBlock.type); | ||
const { validSchema: validBlockSchema } = blockSchema ?? {}; | ||
if (!validBlockSchema || validBlockSchema instanceof Error) return; | ||
|
||
validateSettingsKey(context, offset, settingsNode, validBlockSchema.settings, referencedBlock); | ||
} | ||
} | ||
|
||
function validateSettingsKey( | ||
context: any, | ||
offset: number, | ||
settingsNode: JSONNode | undefined, | ||
validSettings: Setting.Any[] | undefined, | ||
blockNode?: Section.Block | ThemeBlock.Block | Preset.Block, | ||
) { | ||
if (!settingsNode || settingsNode.type !== 'Object') return; | ||
|
||
for (const setting of settingsNode.children) { | ||
const settingExists = validSettings?.find( | ||
(validSetting) => validSetting?.id === setting.key.value, | ||
); | ||
|
||
if (!settingExists) { | ||
const errorMessage = blockNode | ||
? `Setting '${setting.key.value}' does not exist in 'blocks/${blockNode.type}.liquid'.` | ||
: `Setting '${setting.key.value}' does not exist in schema.`; | ||
|
||
reportWarning(errorMessage, offset, setting.key, context); | ||
} | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a case for
n>1 offenses
+ a case where we have 1 missing 1 valid?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can definitely do 2 missing settings. But checking across multiple rules wouldn't be unit testing this rule.