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 in a check for theme block settings validation #620

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AribaRajput
Copy link
Contributor

@AribaRajput AribaRajput commented Nov 27, 2024

What are you adding in this PR?

First part of #602

Adding in a theme check for checking theme blocks for valid preset setting keys. The preset setting keys (and the presets of any theme block preset settings) must match the settings keys configured in either the preset settings or the nested theme block settings.

Docs PR: https://github.com/Shopify/shopify-dev/pull/51243

What's next? Any followup issues?

The next issue is to add the exact same type of theme check for section files, just with more coverage.

What did you learn?

A lot about presets, sections and theme blocks and how they are configured. That was the trickiest part of all of this (managing the types, knowing how to grab nested information, etc).

Before you deploy

  • This PR includes a new checks or changes the configuration of a check
    • I included a minor bump changeset
    • It's in the allChecks array in src/checks/index.ts
    • I ran yarn build and committed the updated configuration files
      • If applicable, I've updated the theme-app-extension.yml config
  • I included a minor bump changeset
  • My feature is backward compatible
  • I included a patch bump changeset

@AribaRajput AribaRajput force-pushed the ariba/presets-settings branch 17 times, most recently from b95fc84 to 866ea6c Compare December 2, 2024 18:30
@AribaRajput AribaRajput marked this pull request as ready for review December 2, 2024 18:37
@AribaRajput AribaRajput requested review from charlespwd and a team December 2, 2024 18:37
docs: {
description: 'Reports invalid preset settings for a theme block',
recommended: true,
url: 'https://shopify.dev/docs/storefronts/themes/tools/theme-check/checks/preset-key-exists',
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
url: 'https://shopify.dev/docs/storefronts/themes/tools/theme-check/checks/preset-key-exists',
url: 'https://shopify.dev/docs/storefronts/themes/tools/theme-check/checks/valid-block-preset-settings,

Comment on lines 9 to 12
name: 'Reports invalid preset settings for a theme block',
docs: {
description: 'Reports invalid preset settings for a theme block',
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it only theme blocks or just blocks in general?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it will validate on any block. I can remove "theme" so its more clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or, whatever block is in the presets settings.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I think abstract block would be fine here

Comment on lines 85 to 87
startIndex: 0,
endIndex: 0,
Copy link
Contributor

@charlespwd charlespwd Dec 3, 2024

Choose a reason for hiding this comment

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

We need to highlight the node. I don't think it's ok to have 0:0 for this check.

schema has a ast property you can use to find the node. We also have the nodeAtPath util that can give you the AST node for a given "JSONPath"

const schema = await getSchema();
if (!schema) return;
if (isError(schema.validSchema) || isError(schema.ast)) return;

// imagine that we somehow find out that ["presets", 0, "settings", "unknown-id"] is invalid:
// we can report an error like this:
const settingsNode = nodeAtPath(schema.ast, ['presets', 0, 'settings']) as ObjectNode;
if (!settingsNode) throw new Error('Should not be happening');

// This is a PropertyNode | undefined
const propertyNode = settingsNode.children.find((child) => child.key.value === 'unknown-id');
if (!propertyNode) throw new Error('Should not be happening');

context.report({
  message: `...`,
  startIndex: schemaNodeOffset + propertyNode.loc!.start.offset,
  endIndex: schemaNodeOffset + propertyNode.loc!.end.offset,
});

Copy link
Contributor

@charlespwd charlespwd left a comment

Choose a reason for hiding this comment

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

Position information can't be ignored for this check. We'll probably need to rewrite it and iterate over each blocks separately rather than push all of them in the same bucket.

@AribaRajput AribaRajput force-pushed the ariba/presets-settings branch 2 times, most recently from b20d7f0 to 0a7932b Compare December 4, 2024 15:06
@AribaRajput AribaRajput marked this pull request as draft December 5, 2024 21:20
@AribaRajput AribaRajput force-pushed the ariba/presets-settings branch from 05d6e6c to 07c3295 Compare December 5, 2024 21:25
@AribaRajput AribaRajput force-pushed the ariba/presets-settings branch 4 times, most recently from 3ef48eb to eee6ce7 Compare December 19, 2024 21:01
@AribaRajput AribaRajput force-pushed the ariba/presets-settings branch from 8f6b402 to 8745150 Compare December 23, 2024 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants