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

Extract Liquid.js valid and invalid variables #777

Closed
SokratisVidros opened this issue Dec 4, 2024 · 4 comments
Closed

Extract Liquid.js valid and invalid variables #777

SokratisVidros opened this issue Dec 4, 2024 · 4 comments

Comments

@SokratisVidros
Copy link

Is there a built-in way to extract Liquid.js valid and invalid variables using LiquidJS.parse?

Effectively, we are looking for a function that looks like this:

async function extractVariables (content: string): {validVariables: string[], invalidVariables: string[]} {
// Parse content with Liquid
  return {
     validVariables,
     invalidVariables
  }
}

Thank you.

@jg-rp
Copy link
Contributor

jg-rp commented Dec 4, 2024

I've been working on PR #770 that might do what you need.

Please could you elaborate on what you mean by "valid" and "invalid" variables?

#770 uses the term "globals" (subject to change) to mean variables that are not in scope from tags like assign and arguments passed to block tags. The idea being that you would need to compare these global variables to the variables you would pass to Liquid.render() in order to identify potentially erroneous names.

@SokratisVidros
Copy link
Author

SokratisVidros commented Dec 4, 2024

Hey @jg-rp, this is exactly what I am looking for. Let me know if you need help so we can merge it faster. We use Liquid.Js extensively at Novu. Currently, we have wrapped Liquid with our own logic to achieve something similar.

Currently, Liquid throws errors if an invalid syntax is used, for example, This is an example text with a {{foo..bar}} invalid example. By default, it fails fast on the first error. There is the option to get all errors simultaneously, but it would be better for DX not to fail and return two sets of liquid variables, the valid and the invalid.

Let me know if this helps.

@jg-rp
Copy link
Contributor

jg-rp commented Dec 4, 2024

Thanks @SokratisVidros, that helps. Understanding different use cases for retrieving variables is half the battle.

In theory, I can imagine a fault tolerant tokenizer/parser that can resume scanning a template on some syntax errors, giving us the opportunity to report multiple invalid expressions. It would be a fairly massive change though and @harttle has already raised concerns about bundle size for browsers.

@SokratisVidros
Copy link
Author

I see. The concern about the bundle size is valid, although it can only be validated during implementation by comparing before/after bundle sizes.

For the record, we ended up with a similar in-house parser. It would be awesome if all the Liquid internals were not exposed.

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

No branches or pull requests

2 participants