Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Add Liquid Variable expression support #41

Merged
merged 8 commits into from
Aug 8, 2022

Conversation

charlespwd
Copy link
Collaborator

This PR is about "proper" parsing and printing of the following Liquid in Liquid Drops:

Strings
{{ 'I said: "Hi muffin!"' }}
{{ "string o' string " }}

Numbers
{{ -1 }}{{ 0 }}{{ 1.0 }}

Literals
{{ true }}
{{ false }}
{{ empty }}
{{ blank }}
{{ nil }}
{{ null }}

Ranges
{{ (0..5) }}
{{ ( a .. b ) }}

VariableLookups
{{ x }}
{{ x.y }}
{{ x.y.z }}
{{ x[0] }}
{{ x[y].z }}
{{ x.with-dashes }}
{{ x["string"] }}
{{ [x] }}

It exists as ground work for everything else, because we'll use those.

Fixes #40

@charlespwd charlespwd force-pushed the feature/40-liquid-variable-expression branch from b591d8c to afc4caa Compare August 3, 2022 13:09
This kicks off the "proper" parsing of Liquid.

Herein, we start extending the ohm grammar to support Liquid constructs
inside the markup of `LiquidDrop`s.

It goes:
```
LiquidDrop {
  markup: LiquidVariable {
    expression: LiquidString
    filters: LiquidFilter[]
  }
}
```

And if we can't parse the string, we fallback to the old implementation
that didn't parse anything.

This means `{{ 'string' }}` will get replaced to `{{ "string" }}` unless
`prettierOptions.singleQuote`
true, false, empty, blank, nil, null
```liquid
{{ (0..5) }}
{{ (true..false) }}
etc...
```
@charlespwd charlespwd force-pushed the feature/40-liquid-variable-expression branch from 1e19895 to 72d344d Compare August 3, 2022 19:29
@charlespwd charlespwd requested a review from a team August 3, 2022 19:29
@charlespwd charlespwd marked this pull request as ready for review August 3, 2022 19:29
grammar/liquid-html.ohm Outdated Show resolved Hide resolved
e.g.

```liquid
{{ x }}
{{ x.y }}
{{ x.y.z }}
{{ x[y].z }}
```

Look at test/liquid-drop-variable-lookup for more examples
grammar/liquid-html.ohm Outdated Show resolved Hide resolved
As @pdubroy graciously pointed out in [1], we would have eagerly matched
on variables with keywords in them. e.g. `true_thing` would have eagerly
matched on the literal rule instead of the variable lookup rule.

This commit prevents this from happening for all our literals.

[1]: #41 (comment)
Copy link
Contributor

@karreiro karreiro left a comment

Choose a reason for hiding this comment

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

Thank you, @charlespwd! LGTM, I've left only one minor/optional comment :)

test/liquid-drop-liquid-literal/index.spec.ts Show resolved Hide resolved
@charlespwd charlespwd force-pushed the feature/40-liquid-variable-expression branch from c2f0349 to e7b7e61 Compare August 8, 2022 14:33
It looks like we prefer single quotes inside Liquid. In a similar
fashion to jsxSingleQuote, this PR adds liquidSingleQuote and makes it
so we prefer single quotes inside liquid tags and drops.
@charlespwd charlespwd force-pushed the feature/40-liquid-variable-expression branch from e7b7e61 to 79a00f7 Compare August 8, 2022 14:33
@charlespwd charlespwd merged commit 86f5eac into main Aug 8, 2022
@charlespwd charlespwd deleted the feature/40-liquid-variable-expression branch August 8, 2022 14:47
@shopify-shipit shopify-shipit bot temporarily deployed to production August 8, 2022 15:01 Inactive
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prettier Liquid: VariableLookup in drops
3 participants