Skip to content

Commit

Permalink
Add basic prettier support for doc tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Dec 11, 2024
1 parent 68227c4 commit 0f17fef
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/liquid-html-parser/src/stage-1-cst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export interface ConcreteBasicNode<T> {
export interface ConcreteLiquidDocParamNode
extends ConcreteBasicNode<ConcreteNodeTypes.LiquidDocParamNode> {
name: string;
value: string;
}

export interface ConcreteHtmlNodeBase<T> extends ConcreteBasicNode<T> {
Expand Down Expand Up @@ -1328,6 +1329,7 @@ function toLiquidDocAST(source: string, matchingSource: string, offset: number)
paramNode: {
type: ConcreteNodeTypes.LiquidDocParamNode,
name: 0,
value: 2,
locStart,
locEnd,
source,
Expand Down
2 changes: 2 additions & 0 deletions packages/liquid-html-parser/src/stage-2-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ export interface TextNode extends ASTNode<NodeTypes.TextNode> {

export interface LiquidDocParamNode extends ASTNode<NodeTypes.LiquidDocParamNode> {
name: string;
value: string;
}

export interface ASTNode<T> {
Expand Down Expand Up @@ -1279,6 +1280,7 @@ function buildAst(
name: node.name,
position: position(node),
source: node.source,
value: node.value,
});
break;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/prettier-plugin-liquid/src/printer/print/liquid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeTypes, NamedTags, isBranchedTag } from '@shopify/liquid-html-parser';
import { NodeTypes, NamedTags, isBranchedTag, RawMarkup } from '@shopify/liquid-html-parser';
import { Doc, doc } from 'prettier';

import {
Expand Down Expand Up @@ -490,6 +490,16 @@ export function printLiquidRawTag(
return [blockStart, ...body, blockEnd];
}

export function printLiquidDoc(
path: AstPath<RawMarkup>,
_options: LiquidParserOptions,
print: LiquidPrinter,
_args: LiquidPrinterArgs,
) {
const body = path.map((p: any) => print(p), 'nodes');
return [indent([hardline, body]), hardline];
}

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,
LiquidDocParamNode,
NodeTypes,
Position,
RawMarkupKinds,
Expand Down Expand Up @@ -30,6 +31,7 @@ import {
LiquidTag,
LiquidVariableOutput,
nonTraversableProperties,
RawMarkup,
TextNode,
} from '../types';
import { assertNever } from '../utils';
Expand All @@ -40,6 +42,7 @@ import { printChildren } from './print/children';
import { printElement } from './print/element';
import {
printLiquidBranch,
printLiquidDoc,
printLiquidRawTag,
printLiquidTag,
printLiquidVariableOutput,
Expand Down Expand Up @@ -210,6 +213,10 @@ function printNode(
}

case NodeTypes.RawMarkup: {
if (node.parentNode?.name === 'doc') {
return printLiquidDoc(path as AstPath<RawMarkup>, options, print, args);
}

const isRawMarkupIdentationSensitive = () => {
switch (node.kind) {
case RawMarkupKinds.typescript:
Expand Down Expand Up @@ -548,7 +555,7 @@ function printNode(
}

case NodeTypes.LiquidDocParamNode: {
return node.name;
return [node.name, ' ', node.value];
}

default: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
It should indent the body
{% doc %}
@param body
{% enddoc %}

It should not dedent to root
{% doc %}
@param body
{% enddoc %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
It should indent the body
{% doc %}
@param body
{% enddoc %}

It should not dedent to root
{% doc %}
@param body
{% enddoc %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'vitest';
import { assertFormattedEqualsFixed } from '../test-helpers';
import * as path from 'path';

test('Unit: liquid-doc', async () => {
await assertFormattedEqualsFixed(__dirname);
});

0 comments on commit 0f17fef

Please sign in to comment.