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

Better building blocks for dealing with {% schema %} content #599

Merged
merged 3 commits into from
Nov 18, 2024

Conversation

charlespwd
Copy link
Contributor

@charlespwd charlespwd commented Nov 18, 2024

What are you adding in this PR?

  • Add getSectionSchema and getBlockSchema context utils in theme check
    • The validSchema property of the returned value (when no errors happened parsing and the schema is validated by our JSON schema) is a type-safe representation of the schema.
  • Add {ThemeBlock,Section}.Schema TypeScript types
  • Provide better building blocks for dealing with schemas
  • Refactor ValidSchemaName as a proof of concept
new.building.blocks.mp4

What's next? Any followup issues?

  • Translate the app block schemas into typescript interfaces and fill in the blanks for app blocks
  • Figure out a way to avoid breaking drift btn Shopify/theme-liquid-docs JSON schemas & our types.

Before you deploy

  • I included a minor bump changeset

    The getBlockSchema & getSectionSchema are technically in the public API even though you don't need to provide them in theme-check-node nor to theme-language-server

- Batteries are included in `@shopify/theme-check-node`
- Language server stores the information in the document manager and injects it
JSON Schema conversions of the theme-liquid-docs JSON schemas into
TypeScript interfaces
@charlespwd charlespwd force-pushed the fix/572-schema-registries branch from 0802d09 to cbd3872 Compare November 18, 2024 16:51
@charlespwd
Copy link
Contributor Author

cc @navdeep5 @miazbikowski

Comment on lines -39 to -48
const promises = visit<SourceCodeType.JSON, Promise<void>>(jsonSchemaAst as JSONNode, {
async Property(nameNode, ancestors) {
if (
nameNode.key.value === 'name' &&
ancestors.length === ROOT_NODE_ANCESTORS_COUNT &&
isLiteralNode(nameNode.value)
) {
const name = getLiteralValue(nameNode.value);
const startIndex = node.blockStartPosition.end + getLiteralLocStart(nameNode.value);
const endIndex = node.blockStartPosition.end + getLiteralLocEnd(nameNode.value);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO this was a bit too hard just to get the "name" property of the parsed schema.

const name = validSchema.name;
if (!name) return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Node that validSchema.name is autocompleted here:
Screenshot 2024-11-18 at 11 53 44

Something that wasn't true with the previous method.

Comment on lines +322 to +342
/**
* Asynchronously get the block schema for 'blocks/${name}.liquid'
* May return undefined when the theme isn't preloaded.
* See {@link ThemeBlockSchema} for more information
*/
getBlockSchema?: (name: string) => Promise<ThemeBlockSchema | undefined>;

/**
* Asynchronously get the section schema for 'section/${name}.liquid'
* May return undefined when the theme isn't preloaded or if there are none.
* See {@link SectionSchema} for more information
*/
getSectionSchema?: (name: string) => Promise<SectionSchema | undefined>;

/**
* (In theme app extension mode)
* Asynchronously get the block schema for 'blocks/${name}.liquid'
* May return undefined when the theme isn't preloaded.
* See {@link AppBlockSchema} for more information
*/
getAppBlockSchema?: (name: string) => Promise<AppBlockSchema | undefined>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The important part of the diff. New APIs that are injected into theme-check. These promises can be cached in the language server so that the result is parsed/cached/validated at most once per file version.

The result can be cached between subsequent runs if the file didn't change.

export type ValidateJSON<T extends SourceCodeType> = (
file: SourceCode<T>,
export type ValidateJSON = (
uri: string,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified since we only need the URI

Copy link
Contributor

@aswamy aswamy left a comment

Choose a reason for hiding this comment

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

super minor things

@charlespwd charlespwd force-pushed the fix/572-schema-registries branch from f244557 to e7b28a1 Compare November 18, 2024 21:45
@charlespwd charlespwd merged commit add2445 into main Nov 18, 2024
6 checks passed
@charlespwd charlespwd deleted the fix/572-schema-registries branch November 18, 2024 21:56
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