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
2 changes: 2 additions & 0 deletions grammar/liquid-html.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Liquid <: Helpers {
| liquidTagDecrement
| liquidTagEcho
| liquidTagElse
| liquidTagElseif
| liquidTagElsif
| liquidTagInclude
| liquidTagIncrement
Expand Down Expand Up @@ -157,6 +158,7 @@ Liquid <: Helpers {
liquidTagOpenIf = liquidTagOpenRule<"if", liquidTagOpenConditionalMarkup>
liquidTagOpenUnless = liquidTagOpenRule<"unless", liquidTagOpenConditionalMarkup>
liquidTagElsif = liquidTagRule<"elsif", liquidTagOpenConditionalMarkup>
liquidTagElseif = liquidTagRule<"elseif", liquidTagOpenConditionalMarkup>

liquidTagBreak = liquidTagRule<"break", empty>
liquidTagContinue = liquidTagRule<"continue", empty>
Expand Down
1 change: 1 addition & 0 deletions src/constants.evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const CSS_DISPLAY_LIQUID_TAGS: Record<string, string> = {
if: 'inline',
unless: 'inline',
else: 'inline',
elseif: 'inline',
elsif: 'inline',
case: 'inline',
when: 'inline',
Expand Down
1 change: 0 additions & 1 deletion src/parser/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export const TAGS_WITHOUT_MARKUP = [
'style',
'schema',
'javascript',
'else',
'break',
'continue',
'comment',
Expand Down
4 changes: 4 additions & 0 deletions src/parser/stage-1-cst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ export interface ConcreteLiquidTagOpenUnless
NamedTags.unless,
ConcreteLiquidCondition[]
> {}
export interface ConcreteLiquidTagElseif
extends ConcreteLiquidTagNode<NamedTags.elseif, ConcreteLiquidCondition[]> {}
export interface ConcreteLiquidTagElsif
extends ConcreteLiquidTagNode<NamedTags.elsif, ConcreteLiquidCondition[]> {}

Expand Down Expand Up @@ -296,6 +298,7 @@ export type ConcreteLiquidTagNamed =
| ConcreteLiquidTagEcho
| ConcreteLiquidTagIncrement
| ConcreteLiquidTagDecrement
| ConcreteLiquidTagElseif
| ConcreteLiquidTagElsif
| ConcreteLiquidTagInclude
| ConcreteLiquidTagLayout
Expand Down Expand Up @@ -709,6 +712,7 @@ function toCST<T>(
liquidTagWhenMarkup: 0,
liquidTagOpenIf: 0,
liquidTagOpenUnless: 0,
liquidTagElseif: 0,
liquidTagElsif: 0,
liquidTagElse: 0,
liquidTagOpenConditionalMarkup: 0,
Expand Down
14 changes: 10 additions & 4 deletions src/parser/stage-2-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ export interface LiquidTagTablerow
export interface LiquidTagIf extends LiquidTagConditional<NamedTags.if> {}
export interface LiquidTagUnless
extends LiquidTagConditional<NamedTags.unless> {}
export interface LiquidBranchElseif
extends LiquidBranchNode<NamedTags.elseif, LiquidConditionalExpression> {}
export interface LiquidBranchElsif
extends LiquidBranchNode<NamedTags.elsif, LiquidConditionalExpression> {}
export interface LiquidTagConditional<Name>
Expand Down Expand Up @@ -335,7 +337,10 @@ export type LiquidBranch =
| LiquidBranchUnnamed
| LiquidBranchBaseCase
| LiquidBranchNamed;
export type LiquidBranchNamed = LiquidBranchElsif | LiquidBranchWhen;
export type LiquidBranchNamed =
| LiquidBranchElseif
| LiquidBranchElsif
| LiquidBranchWhen;

interface LiquidBranchNode<Name, Markup>
extends ASTNode<NodeTypes.LiquidBranch> {
Expand Down Expand Up @@ -561,16 +566,16 @@ function isLiquidBranchDisguisedAsTag(
): node is LiquidTagBaseCase {
return (
node.type === NodeTypes.LiquidTag &&
['else', 'elsif', 'when'].includes(node.name)
['else', 'elseif', 'elsif', 'when'].includes(node.name)
);
}

function isConcreteLiquidBranchDisguisedAsTag(
node: LiquidHtmlConcreteNode,
): node is ConcreteLiquidNode & { name: 'else' | 'eslif' | 'when' } {
): node is ConcreteLiquidNode & { name: 'else' | 'elseif' | 'eslif' | 'when' } {
return (
node.type === ConcreteNodeTypes.LiquidTag &&
['else', 'eslif', 'when'].includes(node.name)
['else', 'elseif', 'eslif', 'when'].includes(node.name)
);
}

Expand Down Expand Up @@ -1184,6 +1189,7 @@ function toNamedLiquidTag(
};
}

case NamedTags.elseif:
case NamedTags.elsif: {
return {
...liquidBranchBaseAttributes(node),
Expand Down
1 change: 1 addition & 0 deletions src/printer/print/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ function printNamedLiquidBlockStart(
}

case NamedTags.if:
case NamedTags.elseif:
case NamedTags.elsif:
case NamedTags.unless: {
const trailingWhitespace = [
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export enum NamedTags {
cycle = 'cycle',
decrement = 'decrement',
echo = 'echo',
elseif = 'elseif',
elsif = 'elsif',
for = 'for',
form = 'form',
Expand Down
15 changes: 15 additions & 0 deletions test/issue-6/fixed.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
It should not remove the "if" from "else if" tags
{% if condition1 %}
Condition1 is truthy
{% else if condition2 %}
Condition2 is truthy
{% endif %}

It should format elseif tags
{% if condition1 %}
Condition1 is truthy
{% elseif condition2 %}
Condition2 is truthy
{% else %}
Something else
{% endif %}
15 changes: 15 additions & 0 deletions test/issue-6/index.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
It should not remove the "if" from "else if" tags
{% if condition1 %}
Condition1 is truthy
{% else if condition2 %}
Condition2 is truthy
{% endif %}

It should format elseif tags
{% if condition1 %}
Condition1 is truthy
{% elseif condition2 %}
Condition2 is truthy
{% else %}
Something else
{% endif %}
6 changes: 6 additions & 0 deletions test/issue-6/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);
});
13 changes: 11 additions & 2 deletions test/liquid-tag-else/fixed.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ printWidth: 1
hello
{% endif %}

It should strip extraneous args
It should preserve extraneous args to avoid silent code changes
printWidth: 1
{% if false %}
{% else %}
{% else huh?? %}
hello
{% endif %}

It should preserve 'else if' syntax to avoid removing expressions
printWidth: 80
{% if condition1 %}
Condition1 is truthy
{% else if condition2 %}
Condition2 is truthy
{% endif %}

11 changes: 10 additions & 1 deletion test/liquid-tag-else/index.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ It should support missing whitespace
printWidth: 1
{% if false %} {%-else-%} hello {% endif %}

It should strip extraneous args
It should preserve extraneous args to avoid silent code changes
printWidth: 1
{% if false %} {% else huh?? %} hello {% endif %}

It should preserve 'else if' syntax to avoid removing expressions
printWidth: 80
{% if condition1 %}
Condition1 is truthy
{% else if condition2 %}
Condition2 is truthy
{% endif %}

31 changes: 31 additions & 0 deletions test/liquid-tag-elseif/fixed.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
It should correctly format elseif statements at same level as if/else
{% if condition1 %}
Condition1 is truthy
{% elseif condition2 %}
Condition2 is truthy
{% else %}
Something else
{% endif %}

It should handle multiple elseif branches
{% if condition1 %}
Result 1
{% elseif condition2 %}
Result 2
{% elseif condition3 %}
Result 3
{% else %}
Default result
{% endif %}

It should handle complex conditions in elseif
{% if x > 10 %}
Large
{% elseif x > 5 and x <= 10 %}
Medium
{% elseif x > 0 %}
Small
{% else %}
Zero or negative
{% endif %}

31 changes: 31 additions & 0 deletions test/liquid-tag-elseif/index.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
It should correctly format elseif statements at same level as if/else
{% if condition1 %}
Condition1 is truthy
{% elseif condition2 %}
Condition2 is truthy
{% else %}
Something else
{% endif %}

It should handle multiple elseif branches
{% if condition1 %}
Result 1
{% elseif condition2 %}
Result 2
{% elseif condition3 %}
Result 3
{% else %}
Default result
{% endif %}

It should handle complex conditions in elseif
{% if x > 10 %}
Large
{% elseif x > 5 and x <= 10 %}
Medium
{% elseif x > 0 %}
Small
{% else %}
Zero or negative
{% endif %}

6 changes: 6 additions & 0 deletions test/liquid-tag-elseif/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);
});
4 changes: 2 additions & 2 deletions test/liquid-tag-no-argument/fixed.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ It should strip arguments from javascript tags
{% javascript %}
{% endjavascript %}

It should strip arguments from else tags
{% if cond %}{% else %}{% endif %}
It should preserve arguments from else tags to avoid silent code changes
{% if cond %}{% else what %}{% endif %}

It should strip arguments from break tags
{% break %}
Expand Down
2 changes: 1 addition & 1 deletion test/liquid-tag-no-argument/index.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ It should strip arguments from javascript tags
{% javascript what %}
{% endjavascript %}

It should strip arguments from else tags
It should preserve arguments from else tags to avoid silent code changes
{% if cond %}{% else what %}{% endif %}

It should strip arguments from break tags
Expand Down