Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/dull-jobs-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/prettier-plugin-liquid': minor
'@shopify/liquid-html-parser': minor
---

Added parsing support for the `snippet` tag by updating the ohm rules
3 changes: 3 additions & 0 deletions packages/liquid-html-parser/grammar/liquid-html.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Liquid <: Helpers {
| liquidTagOpenIf
| liquidTagOpenPaginate
| liquidTagOpenUnless
| liquidTagOpenSnippet

liquidTagOpen =
| liquidTagOpenStrict
Expand Down Expand Up @@ -105,6 +106,7 @@ Liquid <: Helpers {
liquidTagIncrement = liquidTagRule<"increment", variableSegmentAsLookupMarkup>
liquidTagDecrement = liquidTagRule<"decrement", variableSegmentAsLookupMarkup>
liquidTagOpenCapture = liquidTagOpenRule<"capture", variableSegmentAsLookupMarkup>
liquidTagOpenSnippet = liquidTagOpenRule<"snippet", variableSegmentAsLookupMarkup>
variableSegmentAsLookupMarkup = variableSegmentAsLookup space*

liquidTagSection = liquidTagRule<"section", liquidTagSectionMarkup>
Expand Down Expand Up @@ -322,6 +324,7 @@ Liquid <: Helpers {
| "if"
| "unless"
| "tablerow"
| "snippet"
) endOfIdentifier

delimTag = "-%}" | "%}"
Expand Down
11 changes: 11 additions & 0 deletions packages/liquid-html-parser/src/stage-1-cst.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,17 @@ describe('Unit: Stage 1 (CST)', () => {
});
});

it('should parse snippet arguments as a singular liquid variable lookup', () => {
const expression = `var`;
const type = 'VariableLookup';
for (const { toCST, expectPath } of testCases) {
cst = toCST(`{% snippet ${expression} -%}`);
expectPath(cst, '0.type').to.equal('LiquidTagOpen');
expectPath(cst, '0.name').to.equal('snippet');
expectPath(cst, '0.markup.type').to.equal(type);
}
});

it('should parse when arguments as an array of liquid expressions', () => {
[
{ expression: `"string"`, args: [{ type: 'String' }] },
Expand Down
4 changes: 4 additions & 0 deletions packages/liquid-html-parser/src/stage-1-cst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export type ConcreteLiquidTagOpen = ConcreteLiquidTagOpenBaseCase | ConcreteLiqu
export type ConcreteLiquidTagOpenNamed =
| ConcreteLiquidTagOpenCase
| ConcreteLiquidTagOpenCapture
| ConcreteLiquidTagOpenSnippet
| ConcreteLiquidTagOpenIf
| ConcreteLiquidTagOpenUnless
| ConcreteLiquidTagOpenForm
Expand All @@ -254,6 +255,8 @@ export interface ConcreteLiquidTagOpenBaseCase extends ConcreteLiquidTagOpenNode

export interface ConcreteLiquidTagOpenCapture
extends ConcreteLiquidTagOpenNode<NamedTags.capture, ConcreteLiquidVariableLookup> {}
export interface ConcreteLiquidTagOpenSnippet
extends ConcreteLiquidTagOpenNode<NamedTags.snippet, ConcreteLiquidVariableLookup> {}

export interface ConcreteLiquidTagOpenCase
extends ConcreteLiquidTagOpenNode<NamedTags.case, ConcreteLiquidExpression> {}
Expand Down Expand Up @@ -746,6 +749,7 @@ function toCST<T>(
},

liquidTagOpenCapture: 0,
liquidTagOpenSnippet: 0,
liquidTagOpenForm: 0,
liquidTagOpenFormMarkup: 0,
liquidTagOpenFor: 0,
Expand Down
38 changes: 38 additions & 0 deletions packages/liquid-html-parser/src/stage-2-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,25 @@ describe('Unit: Stage 2 (AST)', () => {
});
});

it('should parse snippet blocks', () => {
for (const { toAST, expectPath, expectPosition } of testCases) {
ast = toAST(`{% snippet hello_snippet %}{% echo "Hello content" %}{% endsnippet %}`);
expectPath(ast, 'children.0').to.exist;
expectPath(ast, 'children.0.type').to.eql('LiquidTag');
expectPath(ast, 'children.0.name').to.eql('snippet');
expectPath(ast, 'children.0.markup.type').to.eql('VariableLookup');
expectPath(ast, 'children.0.markup.name').to.eql('hello_snippet');

expectPath(ast, 'children.0.children.0.type').to.eql('LiquidTag');
expectPath(ast, 'children.0.children.0.name').to.eql('echo');
expectPath(ast, 'children.0.children.0.markup.type').to.eql('LiquidVariable');
expectPath(ast, 'children.0.children.0.markup.expression.value').to.eql('Hello content');

expectPosition(ast, 'children.0');
expectPosition(ast, 'children.0.markup');
}
});

describe('Case: content_for', () => {
it('should parse content_for tags with no arguments', () => {
for (const { toAST, expectPath, expectPosition } of testCases) {
Expand Down Expand Up @@ -1230,6 +1249,25 @@ describe('Unit: Stage 2 (AST)', () => {
expectPosition(ast, 'children.0.body.nodes.2').toEqual('}');
expectPosition(ast, 'children.0');
});

it('should parse snippet blocks with HTML content', () => {
ast = toLiquidHtmlAST(
`{% snippet hello_snippet %}<div class="component"><p>Hello</p></div>{% endsnippet %}`,
);
expectPath(ast, 'children.0.type').to.eql('LiquidTag');
expectPath(ast, 'children.0.name').to.eql('snippet');
expectPath(ast, 'children.0.markup.type').to.eql('VariableLookup');
expectPath(ast, 'children.0.markup.name').to.eql('hello_snippet');
expectPath(ast, 'children.0.children.0.type').to.eql('HtmlElement');
expectPath(ast, 'children.0.children.0.name.0.value').to.eql('div');
expectPath(ast, 'children.0.children.0.attributes.0.name.0.value').to.eql('class');
expectPath(ast, 'children.0.children.0.attributes.0.value.0.value').to.eql('component');
expectPath(ast, 'children.0.children.0.children.0.type').to.eql('HtmlElement');
expectPath(ast, 'children.0.children.0.children.0.name.0.value').to.eql('p');
expectPath(ast, 'children.0.children.0.children.0.children.0.type').to.eql('TextNode');
expectPath(ast, 'children.0.children.0.children.0.children.0.value').to.eql('Hello');
expectPosition(ast, 'children.0');
});
});

describe('Unit: toLiquidAST(text)', () => {
Expand Down
13 changes: 13 additions & 0 deletions packages/liquid-html-parser/src/stage-2-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export type LiquidTagNamed =
| LiquidTagRender
| LiquidTagSection
| LiquidTagSections
| LiquidTagSnippet
| LiquidTagTablerow
| LiquidTagUnless;

Expand Down Expand Up @@ -278,6 +279,9 @@ export interface LiquidTagDecrement
/** https://shopify.dev/docs/api/liquid/tags#capture */
export interface LiquidTagCapture extends LiquidTagNode<NamedTags.capture, LiquidVariableLookup> {}

/** https://shopify.dev/docs/api/liquid/tags#snippet */
export interface LiquidTagSnippet extends LiquidTagNode<NamedTags.snippet, LiquidVariableLookup> {}

/** https://shopify.dev/docs/api/liquid/tags#cycle */
export interface LiquidTagCycle extends LiquidTagNode<NamedTags.cycle, CycleMarkup> {}

Expand Down Expand Up @@ -1544,6 +1548,15 @@ function toNamedLiquidTag(
};
}

case NamedTags.snippet: {
return {
...liquidTagBaseAttributes(node),
name: node.name,
markup: toExpression(node.markup) as LiquidVariableLookup,
children: [],
};
}

case NamedTags.content_for: {
return {
...liquidTagBaseAttributes(node),
Expand Down
1 change: 1 addition & 0 deletions packages/liquid-html-parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export enum NamedTags {
layout = 'layout',
liquid = 'liquid',
paginate = 'paginate',
snippet = 'snippet',
render = 'render',
section = 'section',
sections = 'sections',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ function printNamedLiquidBlockStart(
}

case NamedTags.capture:
case NamedTags.snippet:
case NamedTags.increment:
case NamedTags.decrement:
case NamedTags.layout:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('Module: LiquidHTMLSyntaxError', () => {
const offenses = await runLiquidCheck(LiquidHTMLSyntaxError, sourceCode);
expect(offenses).to.have.length(1);
expect(offenses[0].message).to.equal(
`SyntaxError: expected "#", a letter, "when", "sections", "section", "render", "liquid", "layout", "increment", "include", "elsif", "else", "echo", "decrement", "content_for", "cycle", "continue", "break", "assign", "tablerow", "unless", "if", "ifchanged", "for", "case", "capture", "paginate", "form", "end", "style", "stylesheet", "schema", "javascript", "raw", "comment", or "doc"`,
`SyntaxError: expected "#", a letter, "when", "sections", "section", "render", "liquid", "layout", "increment", "include", "elsif", "else", "echo", "decrement", "content_for", "cycle", "continue", "break", "assign", "snippet", "tablerow", "unless", "if", "ifchanged", "for", "case", "capture", "paginate", "form", "end", "style", "stylesheet", "schema", "javascript", "raw", "comment", or "doc"`,
);
});

Expand Down