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

Commit

Permalink
Merge pull request #150 from Shopify/sections_tag_support
Browse files Browse the repository at this point in the history
Add support for sections tag
  • Loading branch information
jamesmengo authored Jan 4, 2023
2 parents e3318d8 + 05bf390 commit 43d6330
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions grammar/liquid-html.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Liquid <: Helpers {
| liquidTagLiquid
| liquidTagRender
| liquidTagSection
| liquidTagSections
| liquidTagWhen
| liquidTagBaseCase

Expand Down Expand Up @@ -97,6 +98,9 @@ Liquid <: Helpers {
liquidTagSection = liquidTagRule<"section", liquidTagSectionMarkup>
liquidTagSectionMarkup = liquidString space*

liquidTagSections = liquidTagRule<"sections", liquidTagSectionsMarkup>
liquidTagSectionsMarkup = liquidString space*

liquidTagLayout = liquidTagRule<"layout", liquidTagLayoutMarkup>
liquidTagLayoutMarkup = liquidExpression space*

Expand Down
5 changes: 5 additions & 0 deletions src/parser/stage-1-cst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export type ConcreteLiquidTagNamed =
| ConcreteLiquidTagLiquid
| ConcreteLiquidTagRender
| ConcreteLiquidTagSection
| ConcreteLiquidTagSections
| ConcreteLiquidTagWhen;

export interface ConcreteLiquidTagNode<Name, Markup>
Expand All @@ -319,6 +320,8 @@ export interface ConcreteLiquidTagDecrement
> {}
export interface ConcreteLiquidTagSection
extends ConcreteLiquidTagNode<NamedTags.section, ConcreteStringLiteral> {}
export interface ConcreteLiquidTagSections
extends ConcreteLiquidTagNode<NamedTags.sections, ConcreteStringLiteral> {}
export interface ConcreteLiquidTagLayout
extends ConcreteLiquidTagNode<NamedTags.layout, ConcreteLiquidExpression> {}

Expand Down Expand Up @@ -685,6 +688,7 @@ export function toLiquidHtmlCST(source: string): LiquidHtmlCST {
liquidTagRender: 0,
liquidTagInclude: 0,
liquidTagSection: 0,
liquidTagSections: 0,
liquidTagLayout: 0,
liquidTagRule: {
type: ConcreteNodeTypes.LiquidTag,
Expand Down Expand Up @@ -729,6 +733,7 @@ export function toLiquidHtmlCST(source: string): LiquidHtmlCST {

liquidTagEchoMarkup: 0,
liquidTagSectionMarkup: 0,
liquidTagSectionsMarkup: 0,
liquidTagLayoutMarkup: 0,
liquidTagAssignMarkup: {
type: ConcreteNodeTypes.AssignMarkup,
Expand Down
10 changes: 10 additions & 0 deletions src/parser/stage-2-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export type LiquidTagNamed =
| LiquidTagPaginate
| LiquidTagRender
| LiquidTagSection
| LiquidTagSections
| LiquidTagTablerow
| LiquidTagUnless;

Expand Down Expand Up @@ -285,6 +286,8 @@ export interface LiquidTagInclude

export interface LiquidTagSection
extends LiquidTagNode<NamedTags.section, LiquidString> {}
export interface LiquidTagSections
extends LiquidTagNode<NamedTags.sections, LiquidString> {}
export interface LiquidTagLayout
extends LiquidTagNode<NamedTags.layout, LiquidExpression> {}

Expand Down Expand Up @@ -1006,6 +1009,13 @@ function toNamedLiquidTag(
markup: toExpression(node.markup) as LiquidString,
};
}
case NamedTags.sections: {
return {
...liquidTagBaseAttributes(node),
name: node.name,
markup: toExpression(node.markup) as LiquidString,
};
}

case NamedTags.form: {
return {
Expand Down
3 changes: 3 additions & 0 deletions src/printer/print/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ function printNamedLiquidBlockStart(
case NamedTags.section: {
return tag(' ');
}
case NamedTags.sections: {
return tag(' ');
}

case NamedTags.form: {
const trailingWhitespace = node.markup.length > 1 ? line : ' ';
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export enum NamedTags {
paginate = 'paginate',
render = 'render',
section = 'section',
sections = 'sections',
tablerow = 'tablerow',
unless = 'unless',
when = 'when',
Expand Down
6 changes: 6 additions & 0 deletions test/liquid-tag-sections/fixed.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
It should never break a name
printWidth: 1
{% sections 'sectionName' %}
{% sections 'sectionName' %}
{% sections 'sectionName' %}
{%- sections 'sectionName' -%}
8 changes: 8 additions & 0 deletions test/liquid-tag-sections/index.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
It should never break a name
printWidth: 1
{% sections "sectionName" %}
{% sections "sectionName" %}
{% sections "sectionName"%}
{%- sections
"sectionName"
-%}
6 changes: 6 additions & 0 deletions test/liquid-tag-sections/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { assertFormattedEqualsFixed } from '../test-helpers';
import * as path from 'path';

describe(`Unit: ${path.basename(__dirname)}`, () => {
assertFormattedEqualsFixed(__dirname);
});

0 comments on commit 43d6330

Please sign in to comment.