Skip to content

Commit d4803fa

Browse files
committed
Add basic prettier support for doc tag
1 parent 1a1108a commit d4803fa

File tree

7 files changed

+48
-2
lines changed

7 files changed

+48
-2
lines changed

packages/liquid-html-parser/src/stage-1-cst.ts

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export interface ConcreteBasicNode<T> {
110110
export interface ConcreteLiquidDocParamNode
111111
extends ConcreteBasicNode<ConcreteNodeTypes.LiquidDocParamNode> {
112112
name: string;
113+
value: string;
113114
}
114115

115116
export interface ConcreteHtmlNodeBase<T> extends ConcreteBasicNode<T> {
@@ -1328,6 +1329,7 @@ function toLiquidDocAST(source: string, matchingSource: string, offset: number)
13281329
paramNode: {
13291330
type: ConcreteNodeTypes.LiquidDocParamNode,
13301331
name: 0,
1332+
value: 2,
13311333
locStart,
13321334
locEnd,
13331335
source,

packages/liquid-html-parser/src/stage-2-ast.ts

+2
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ export interface TextNode extends ASTNode<NodeTypes.TextNode> {
757757

758758
export interface LiquidDocParamNode extends ASTNode<NodeTypes.LiquidDocParamNode> {
759759
name: string;
760+
value: string;
760761
}
761762

762763
export interface ASTNode<T> {
@@ -1279,6 +1280,7 @@ function buildAst(
12791280
name: node.name,
12801281
position: position(node),
12811282
source: node.source,
1283+
value: node.value,
12821284
});
12831285
break;
12841286
}

packages/prettier-plugin-liquid/src/printer/print/liquid.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NodeTypes, NamedTags, isBranchedTag } from '@shopify/liquid-html-parser';
1+
import { NodeTypes, NamedTags, isBranchedTag, RawMarkup } from '@shopify/liquid-html-parser';
22
import { Doc, doc } from 'prettier';
33

44
import {
@@ -490,6 +490,16 @@ export function printLiquidRawTag(
490490
return [blockStart, ...body, blockEnd];
491491
}
492492

493+
export function printLiquidDoc(
494+
path: AstPath<RawMarkup>,
495+
_options: LiquidParserOptions,
496+
print: LiquidPrinter,
497+
_args: LiquidPrinterArgs,
498+
) {
499+
const body = path.map((p: any) => print(p), 'nodes');
500+
return [indent([hardline, body]), hardline];
501+
}
502+
493503
function innerLeadingWhitespace(node: LiquidTag | LiquidBranch) {
494504
if (!node.firstChild) {
495505
if (node.isDanglingWhitespaceSensitive && node.hasDanglingWhitespace) {

packages/prettier-plugin-liquid/src/printer/printer-liquid-html.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
getConditionalComment,
3+
LiquidDocParamNode,
34
NodeTypes,
45
Position,
56
RawMarkupKinds,
@@ -30,6 +31,7 @@ import {
3031
LiquidTag,
3132
LiquidVariableOutput,
3233
nonTraversableProperties,
34+
RawMarkup,
3335
TextNode,
3436
} from '../types';
3537
import { assertNever } from '../utils';
@@ -40,6 +42,7 @@ import { printChildren } from './print/children';
4042
import { printElement } from './print/element';
4143
import {
4244
printLiquidBranch,
45+
printLiquidDoc,
4346
printLiquidRawTag,
4447
printLiquidTag,
4548
printLiquidVariableOutput,
@@ -210,6 +213,10 @@ function printNode(
210213
}
211214

212215
case NodeTypes.RawMarkup: {
216+
if (node.parentNode?.name === 'doc') {
217+
return printLiquidDoc(path as AstPath<RawMarkup>, options, print, args);
218+
}
219+
213220
const isRawMarkupIdentationSensitive = () => {
214221
switch (node.kind) {
215222
case RawMarkupKinds.typescript:
@@ -548,7 +555,7 @@ function printNode(
548555
}
549556

550557
case NodeTypes.LiquidDocParamNode: {
551-
return node.name;
558+
return [node.name, ' ', node.value];
552559
}
553560

554561
default: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
It should indent the body
2+
{% doc %}
3+
@param body
4+
{% enddoc %}
5+
6+
It should not dedent to root
7+
{% doc %}
8+
@param body
9+
{% enddoc %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
It should indent the body
2+
{% doc %}
3+
@param body
4+
{% enddoc %}
5+
6+
It should not dedent to root
7+
{% doc %}
8+
@param body
9+
{% enddoc %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from 'vitest';
2+
import { assertFormattedEqualsFixed } from '../test-helpers';
3+
import * as path from 'path';
4+
5+
test('Unit: liquid-doc', async () => {
6+
await assertFormattedEqualsFixed(__dirname);
7+
});

0 commit comments

Comments
 (0)