Shopify / prettier-plugin-liquid

Prettier Liquid/HTML plugin
https://npm.im/@shopify/prettier-plugin-liquid
MIT License
93 stars 15 forks source link

Add support for strict Liquid parsing #187

Closed charlespwd closed 1 year ago

charlespwd commented 1 year ago

In this PR

What this means

Right now, the LiquidHTML parser has default "fall through" cases for liquidTagOpen and liquidTag.

That is, we used to do something like this:

liquidTag = 
  | liquidTagAssign
  | liquidTagEcho
  | ...
  | liquidTagBaseCase

i.e., try the strict rule and, if that fails, parse the .markup as a string.

> toLiquidHTMLAST(`{% echo var %}`, { mode: 'tolerant' }) 
[
  {
    type: 'LiquidTag',
    name: 'echo',
    markup: {
      type: 'VariableLookup',
      expression: {/* ... /*}
      lookups: [],
    },
  }
]

> toLiquidHTMLAST(`{% echo var } error! { %}`, { mode: 'tolerant' }) 
[
  {
    type: 'LiquidTag',
    name: 'echo',
    markup: 'var } error! {', // this is a string
  }
]

With { mode: 'strict' }, we'll throw errors instead of accepting that kind of crappy Liquid.

> toLiquidHTMLAST(`{% echo var } error! { %}`, { mode: 'strict' }) 
LiquidHTMLParsingError: 
  Expected "%}", "-%}", "}}", "-}}", a space, "|", ".", or "["

Consequences

Nothing in prettier-plugin-liquid; but, in theme-check-js, this should be enough to power the LiquidSyntaxError check.