Skip to content

Commit 64f7dd8

Browse files
committed
Add liquidDocParam handling to stage2 AST
- Updated the `toLiquidDocAST` function to include handling for `@param` syntax and fallback text node
1 parent 740599d commit 64f7dd8

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -1306,14 +1306,27 @@ function toLiquidDocAST(source: string, matchingSource: string, offset: number)
13061306

13071307
const LiquidDocMappings: Mapping = {
13081308
Node: 0,
1309+
textNode: {
1310+
type: ConcreteNodeTypes.TextNode,
1311+
value: function () {
1312+
return (this as any).sourceString;
1313+
},
1314+
locStart,
1315+
locEnd,
1316+
source,
1317+
},
13091318
paramNode: {
13101319
type: ConcreteNodeTypes.LiquidDocParamNode,
1320+
name: 0,
13111321
locStart,
13121322
locEnd,
13131323
source,
13141324
},
13151325
fallbackNode: {
13161326
type: ConcreteNodeTypes.TextNode,
1327+
value: function () {
1328+
return (this as any).sourceString;
1329+
},
13171330
locStart,
13181331
locEnd,
13191332
source,

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

+11-12
Original file line numberDiff line numberDiff line change
@@ -1229,22 +1229,21 @@ describe('Unit: Stage 2 (AST)', () => {
12291229
expectPath(ast, 'children.0.body.type').toEqual('RawMarkup');
12301230
expectPath(ast, 'children.0.body.nodes').toEqual([]);
12311231

1232-
ast = toLiquidAST(`{% doc -%} single line doc {%- enddoc %}`);
1232+
ast = toLiquidAST(`
1233+
{% doc -%}
1234+
@param asdf
1235+
@unsupported this node falls back to a text node
1236+
{%- enddoc %}
1237+
`);
12331238
expectPath(ast, 'children.0.type').to.eql('LiquidRawTag');
12341239
expectPath(ast, 'children.0.name').to.eql('doc');
1235-
expectPath(ast, 'children.0.body.value').to.eql(' single line doc ');
1236-
expectPath(ast, 'children.0.body.nodes.0.type').toEqual('TextNode');
1240+
expectPath(ast, 'children.0.body.nodes.0.type').to.eql('LiquidDocParamNode');
1241+
expectPath(ast, 'children.0.body.nodes.0.name').to.eql('@param');
12371242

1238-
ast = toLiquidAST(`{% doc -%}
1239-
multi line doc
1240-
multi line doc
1241-
{%- enddoc %}`);
1242-
expectPath(ast, 'children.0.type').to.eql('LiquidRawTag');
1243-
expectPath(ast, 'children.0.name').to.eql('doc');
1244-
expectPath(ast, 'children.0.body.nodes.0.value').to.eql(
1245-
`multi line doc\n multi line doc`,
1243+
expectPath(ast, 'children.0.body.nodes.1.type').to.eql('TextNode');
1244+
expectPath(ast, 'children.0.body.nodes.1.value').to.eql(
1245+
'@unsupported this node falls back to a text node',
12461246
);
1247-
expectPath(ast, 'children.0.body.nodes.0.type').toEqual('TextNode');
12481247
});
12491248

12501249
it('should parse unclosed tables with assignments', () => {

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
LiquidHtmlConcreteNode,
7474
ConcreteLiquidTagBaseCase,
7575
ConcreteLiquidTagContentForMarkup,
76+
LiquidDocCST,
7677
} from './stage-1-cst';
7778
import { Comparators, NamedTags, NodeTypes, nonTraversableProperties, Position } from './types';
7879
import { assertNever, deepGet, dropLast } from './utils';
@@ -107,7 +108,8 @@ export type LiquidHtmlNode =
107108
| RenderVariableExpression
108109
| LiquidLogicalExpression
109110
| LiquidComparison
110-
| TextNode;
111+
| TextNode
112+
| LiquidDocParamNode;
111113

112114
/** The root node of all LiquidHTML ASTs. */
113115
export interface DocumentNode extends ASTNode<NodeTypes.Document> {
@@ -754,6 +756,10 @@ export interface TextNode extends ASTNode<NodeTypes.TextNode> {
754756
value: string;
755757
}
756758

759+
export interface LiquidDocParamNode extends ASTNode<NodeTypes.LiquidDocParamNode> {
760+
name: string;
761+
}
762+
757763
export interface ASTNode<T> {
758764
/**
759765
* The type of the node, as a string.
@@ -1103,7 +1109,7 @@ export function cstToAst(
11031109
}
11041110

11051111
function buildAst(
1106-
cst: LiquidHtmlCST | LiquidCST | ConcreteAttributeNode[],
1112+
cst: LiquidHtmlCST | LiquidCST | LiquidDocCST | ConcreteAttributeNode[],
11071113
options: ASTBuildOptions,
11081114
) {
11091115
const builder = new ASTBuilder(cst[0].source);
@@ -1268,6 +1274,16 @@ function buildAst(
12681274
break;
12691275
}
12701276

1277+
case ConcreteNodeTypes.LiquidDocParamNode: {
1278+
builder.push({
1279+
type: NodeTypes.LiquidDocParamNode,
1280+
name: node.name,
1281+
position: position(node),
1282+
source: node.source,
1283+
});
1284+
break;
1285+
}
1286+
12711287
default: {
12721288
assertNever(node);
12731289
}

packages/liquid-html-parser/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export enum NodeTypes {
4444
RawMarkup = 'RawMarkup',
4545
RenderMarkup = 'RenderMarkup',
4646
RenderVariableExpression = 'RenderVariableExpression',
47+
LiquidDocParamNode = 'LiquidDocParamNode',
4748
}
4849

4950
// These are officially supported with special node types

0 commit comments

Comments
 (0)