Skip to content

Commit

Permalink
add prettier support and test coverage for example in liquid doc
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilGenius13 committed Jan 20, 2025
1 parent e44887c commit fcc693e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function getCssDisplay(node: AugmentedNode<WithSiblings>, options: LiquidParserO
case NodeTypes.LogicalExpression:
case NodeTypes.Comparison:
case NodeTypes.LiquidDocParamNode:
case NodeTypes.LiquidDocExampleNode:
return 'should not be relevant';

default:
Expand Down Expand Up @@ -235,6 +236,7 @@ function getNodeCssStyleWhiteSpace(
case NodeTypes.LogicalExpression:
case NodeTypes.Comparison:
case NodeTypes.LiquidDocParamNode:
case NodeTypes.LiquidDocExampleNode:
return 'should not be relevant';

default:
Expand Down
25 changes: 25 additions & 0 deletions packages/prettier-plugin-liquid/src/printer/print/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isBranchedTag,
RawMarkup,
LiquidDocParamNode,
LiquidDocExampleNode,
} from '@shopify/liquid-html-parser';
import { Doc, doc } from 'prettier';

Expand Down Expand Up @@ -536,6 +537,30 @@ export function printLiquidDocParam(
return parts;
}

export function printLiquidDocExample(
path: AstPath<LiquidDocExampleNode>,
options: LiquidParserOptions,
_print: LiquidPrinter,
_args: LiquidPrinterArgs,
): Doc {
const node = path.getValue();
const parts: Doc[] = ['@example'];

if (node.exampleContent?.value) {
const content = node.exampleContent.value.trim();
if (content) {
parts.push(hardline);
const lines = content
.split('\n')
.map((line) => line.trim())
.filter(Boolean);
parts.push(join(hardline, lines));
}
}

return parts;
}

function innerLeadingWhitespace(node: LiquidTag | LiquidBranch) {
if (!node.firstChild) {
if (node.isDanglingWhitespaceSensitive && node.hasDanglingWhitespace) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
getConditionalComment,
LiquidDocExampleNode,
LiquidDocParamNode,
NodeTypes,
Position,
Expand Down Expand Up @@ -47,6 +48,7 @@ import {
printLiquidTag,
printLiquidVariableOutput,
printLiquidDocParam,
printLiquidDocExample,
} from './print/liquid';
import { printClosingTagSuffix, printOpeningTagPrefix } from './print/tag';
import { bodyLines, hasLineBreakInRange, isEmpty, isTextLikeNode, reindent } from './utils';
Expand Down Expand Up @@ -559,6 +561,10 @@ function printNode(
return printLiquidDocParam(path as AstPath<LiquidDocParamNode>, options, print, args);
}

case NodeTypes.LiquidDocExampleNode: {
return printLiquidDocExample(path as AstPath<LiquidDocExampleNode>, options, print, args);
}

default: {
return assertNever(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ It should normalize the param description
{% doc %}
@param paramName - param with description
{% enddoc %}

It should push example content to the next line
{% doc %}
@example
This is a valid example
{% enddoc %}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ It should normalize the param description
{% doc %}
@param paramName - param with description
{% enddoc %}

It should push example content to the next line
{% doc %}
@example This is a valid example
{% enddoc %}
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ function findCurrentNode(
case NodeTypes.LiquidDocParamNode: {
break;
}
case NodeTypes.LiquidDocExampleNode: {
break;
}

default: {
return assertNever(current);
Expand Down

0 comments on commit fcc693e

Please sign in to comment.