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

[LiquidDoc][Theme Check] Add alias support for UnrecognizedRenderSnippetParams #831

Merged
merged 2 commits into from
Feb 28, 2025

Conversation

jamesmengo
Copy link
Contributor

@jamesmengo jamesmengo commented Feb 27, 2025

What are you adding in this PR?

Closes https://github.com/Shopify/developer-tools-team/issues/605

Adds alias support to the UnrecognizedRenderSnippetParams theme check

If the alias used is unrecognized, we will report this.
We also had to handle some edge cases for removal since this follows a different format.

What's next? Any followup issues?

Need to update type checking next

Before you deploy

  • I included a minor bump changeset
  • My feature is backward compatible

@jamesmengo jamesmengo changed the base branch from jm/aliases_missing_render_snippets to graphite-base/831 February 27, 2025 20:39
@jamesmengo jamesmengo changed the title --wip-- [skip ci] [LiquidDoc][Theme Check] Report unknown parameters in render tag aliases Feb 27, 2025
@jamesmengo jamesmengo force-pushed the jm/alias_unrecognized_params branch from 376a29e to 0b1bbf4 Compare February 27, 2025 21:03
@jamesmengo jamesmengo changed the title [LiquidDoc][Theme Check] Report unknown parameters in render tag aliases [LiquidDoc][Theme Check] Add alias support for UnrecognizedRenderSnippetParams Feb 27, 2025
@jamesmengo jamesmengo added the #gsd:44310 LiquidDoc label Feb 27, 2025 — with Graphite App
@jamesmengo jamesmengo requested a review from aswamy February 27, 2025 21:07
@jamesmengo jamesmengo marked this pull request as ready for review February 27, 2025 21:07
@jamesmengo jamesmengo requested a review from a team as a code owner February 27, 2025 21:07
@jamesmengo jamesmengo force-pushed the jm/alias_unrecognized_params branch from 0b1bbf4 to 51a2db1 Compare February 27, 2025 21:09
@jamesmengo jamesmengo changed the base branch from graphite-base/831 to jm/aliases_missing_render_snippets February 27, 2025 21:09
@jamesmengo jamesmengo force-pushed the jm/alias_unrecognized_params branch from 51a2db1 to 619f11e Compare February 27, 2025 21:10
@jamesmengo jamesmengo force-pushed the jm/aliases_missing_render_snippets branch 2 times, most recently from 79481f6 to 6ce5eff Compare February 27, 2025 22:14
@jamesmengo jamesmengo force-pushed the jm/alias_unrecognized_params branch from 619f11e to e7f9907 Compare February 27, 2025 22:14
@jamesmengo jamesmengo force-pushed the jm/aliases_missing_render_snippets branch from 6ce5eff to 013ca06 Compare February 27, 2025 22:59
@jamesmengo jamesmengo force-pushed the jm/alias_unrecognized_params branch from e7f9907 to ad1f83a Compare February 27, 2025 23:03
@@ -69,9 +69,42 @@ export const UnrecognizedRenderSnippetParams: LiquidCheckDefinition = {
}
}

function reportUnknownAliases(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is handled quite differently that Named Parameters so I split this into its own function

snippetName: string,
) {
if (node.alias && !liquidDocParameters.has(node.alias) && node.variable) {
const asAliasMatch = node.source.match(new RegExp(`as\\s+${node.alias}`));
Copy link
Contributor Author

@jamesmengo jamesmengo Feb 27, 2025

Choose a reason for hiding this comment

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

This is a bit of a hack to strip as alias since we don't have that information in the AST right now. Luckily, it's a consistent pattern so this should work sufficiently.

I'm creating a separate ticket to address the parsing work, etc that would need to be done to do this with the node positions.

context.report({
message: `Unknown parameter '${node.alias}' in render tag for snippet '${snippetName}'`,
startIndex: node.variable.position.start,
endIndex: node.variable.position.end,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now this will only highlight **with 'product'** as alias . This is due to the current capabilities of the parsing for render markup.

Tracked an issue here ( same as the one mentioned above)

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
endIndex: node.variable.position.end,
endIndex: asAliasMatch ? node.variable.position.end + asAliasMatch[0].length : node.variable.position.end,

What do you think about adding asAliasMatch[0].length to the endIndex?

We could also add an offset to startIndex and endIndex to avoid spaces:

const startingOffset = node.source.slice(node.variable.position.start).indexOf(node.variable.kind);

...

context.report({
  ...
  startIndex: startingOffset + node.variable.position.start,
  endIndex: startingOffset + (asAliasMatch ? node.variable.position.end + asAliasMatch[0].length : node.variable.position.end),
})

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great idea!

@jamesmengo jamesmengo changed the base branch from jm/aliases_missing_render_snippets to graphite-base/831 February 28, 2025 20:06
@jamesmengo jamesmengo force-pushed the jm/alias_unrecognized_params branch from ad1f83a to 99aba80 Compare February 28, 2025 20:06
@jamesmengo jamesmengo changed the base branch from graphite-base/831 to main February 28, 2025 20:06
- Update check to handle 'as' alias scenarios
- Adjust fixer to correctly insert missing parameters
@jamesmengo jamesmengo force-pushed the jm/alias_unrecognized_params branch from 99aba80 to 2135423 Compare February 28, 2025 20:06
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.

Optional suggestions for improving as alias

1 optional change
1 mandatory change with node.source replace

if (node.variable) {
return fixer.remove(
node.variable.position.start,
node.source.indexOf(asAliasMatch[0]) + asAliasMatch[0].length,
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
node.source.indexOf(asAliasMatch[0]) + asAliasMatch[0].length,
node.source.slice(node.position.start).indexOf(asAliasMatch[0]) + asAliasMatch[0].length,

This is a bit dangerous. I had two render tags with as foo in it. node.source returns the entire file, so doing a indexOf could find the wrong one.

context.report({
message: `Unknown parameter '${node.alias}' in render tag for snippet '${snippetName}'`,
startIndex: node.variable.position.start,
endIndex: node.variable.position.end,
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
endIndex: node.variable.position.end,
endIndex: asAliasMatch ? node.variable.position.end + asAliasMatch[0].length : node.variable.position.end,

What do you think about adding asAliasMatch[0].length to the endIndex?

We could also add an offset to startIndex and endIndex to avoid spaces:

const startingOffset = node.source.slice(node.variable.position.start).indexOf(node.variable.kind);

...

context.report({
  ...
  startIndex: startingOffset + node.variable.position.start,
  endIndex: startingOffset + (asAliasMatch ? node.variable.position.end + asAliasMatch[0].length : node.variable.position.end),
})

- Handle multiple render tags
- Expand error reporting to highlight entire alias if found
Copy link
Contributor Author

jamesmengo commented Feb 28, 2025

Merge activity

  • Feb 28, 6:03 PM EST: A user started a stack merge that includes this pull request via Graphite.
  • Feb 28, 6:03 PM EST: A user merged this pull request with Graphite.

@jamesmengo jamesmengo merged commit d93bdc7 into main Feb 28, 2025
7 checks passed
@jamesmengo jamesmengo deleted the jm/alias_unrecognized_params branch February 28, 2025 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#gsd:44310 LiquidDoc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants