@platformos/liquid-html-parser 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -79,6 +79,9 @@ export declare enum ConcreteNodeTypes {
79
79
  LiquidDocDescriptionNode = "LiquidDocDescriptionNode",
80
80
  LiquidDocExampleNode = "LiquidDocExampleNode",
81
81
  LiquidDocPromptNode = "LiquidDocPromptNode",
82
+ JsonHashLiteral = "JsonHashLiteral",
83
+ JsonArrayLiteral = "JsonArrayLiteral",
84
+ JsonKeyValuePair = "JsonKeyValuePair",
82
85
  BackgroundMarkup = "BackgroundMarkup",
83
86
  BackgroundInlineMarkup = "BackgroundInlineMarkup",
84
87
  CacheMarkup = "CacheMarkup",
@@ -273,6 +276,8 @@ export interface ConcreteLiquidTagAssign extends ConcreteLiquidTagNode<NamedTags
273
276
  }
274
277
  export interface ConcreteLiquidTagAssignMarkup extends ConcreteBasicNode<ConcreteNodeTypes.AssignMarkup> {
275
278
  name: string;
279
+ target: ConcreteLiquidVariableLookup;
280
+ operator: string;
276
281
  value: ConcreteLiquidVariable;
277
282
  }
278
283
  export interface ConcreteLiquidTagHashAssign extends ConcreteLiquidTagNode<NamedTags.hash_assign, ConcreteLiquidTagHashAssignMarkup> {
@@ -426,7 +431,7 @@ export interface ConcreteLiquidNamedArgument extends ConcreteBasicNode<ConcreteN
426
431
  name: string;
427
432
  value: ConcreteLiquidExpression;
428
433
  }
429
- export type ConcreteLiquidExpression = ConcreteStringLiteral | ConcreteNumberLiteral | ConcreteLiquidLiteral | ConcreteLiquidRange | ConcreteLiquidVariableLookup;
434
+ export type ConcreteLiquidExpression = ConcreteStringLiteral | ConcreteNumberLiteral | ConcreteLiquidLiteral | ConcreteLiquidRange | ConcreteLiquidVariableLookup | ConcreteJsonHashLiteral | ConcreteJsonArrayLiteral;
430
435
  export type ConcreteComplexLiquidExpression = ConcreteLiquidBooleanExpression | ConcreteLiquidExpression;
431
436
  export interface ConcreteLiquidBooleanExpression extends ConcreteBasicNode<ConcreteNodeTypes.BooleanExpression> {
432
437
  conditions: ConcreteLiquidCondition[];
@@ -450,6 +455,17 @@ export interface ConcreteLiquidVariableLookup extends ConcreteBasicNode<Concrete
450
455
  name: string | null;
451
456
  lookups: ConcreteLiquidExpression[];
452
457
  }
458
+ export interface ConcreteJsonHashLiteral extends ConcreteBasicNode<ConcreteNodeTypes.JsonHashLiteral> {
459
+ entries: ConcreteJsonKeyValuePair[];
460
+ }
461
+ export interface ConcreteJsonKeyValuePair extends ConcreteBasicNode<ConcreteNodeTypes.JsonKeyValuePair> {
462
+ key: ConcreteLiquidExpression;
463
+ value: ConcreteJsonValue;
464
+ }
465
+ export interface ConcreteJsonArrayLiteral extends ConcreteBasicNode<ConcreteNodeTypes.JsonArrayLiteral> {
466
+ elements: ConcreteJsonValue[];
467
+ }
468
+ export type ConcreteJsonValue = ConcreteJsonHashLiteral | ConcreteJsonArrayLiteral | ConcreteLiquidExpression;
453
469
  export type ConcreteHtmlNode = ConcreteHtmlDoctype | ConcreteHtmlComment | ConcreteHtmlRawTag | ConcreteHtmlVoidElement | ConcreteHtmlSelfClosingElement | ConcreteHtmlTagOpen | ConcreteHtmlTagClose;
454
470
  export interface ConcreteTextNode extends ConcreteBasicNode<ConcreteNodeTypes.TextNode> {
455
471
  value: string;
@@ -87,6 +87,9 @@ var ConcreteNodeTypes;
87
87
  ConcreteNodeTypes["LiquidDocDescriptionNode"] = "LiquidDocDescriptionNode";
88
88
  ConcreteNodeTypes["LiquidDocExampleNode"] = "LiquidDocExampleNode";
89
89
  ConcreteNodeTypes["LiquidDocPromptNode"] = "LiquidDocPromptNode";
90
+ ConcreteNodeTypes["JsonHashLiteral"] = "JsonHashLiteral";
91
+ ConcreteNodeTypes["JsonArrayLiteral"] = "JsonArrayLiteral";
92
+ ConcreteNodeTypes["JsonKeyValuePair"] = "JsonKeyValuePair";
90
93
  // platformos markup types
91
94
  ConcreteNodeTypes["BackgroundMarkup"] = "BackgroundMarkup";
92
95
  ConcreteNodeTypes["BackgroundInlineMarkup"] = "BackgroundInlineMarkup";
@@ -374,12 +377,66 @@ function toCST(source /* the original file */, grammars, grammar, cstMappings, m
374
377
  liquidTagLayoutMarkup: 0,
375
378
  liquidTagAssignMarkup: {
376
379
  type: ConcreteNodeTypes.AssignMarkup,
380
+ name: (tokens) => tokens[0].children[0].sourceString,
381
+ target: 0,
382
+ operator: 2,
383
+ value: 4,
384
+ locStart,
385
+ locEnd,
386
+ source,
387
+ },
388
+ assignTarget: {
389
+ type: ConcreteNodeTypes.VariableLookup,
377
390
  name: 0,
391
+ lookups: 1,
392
+ locStart,
393
+ locEnd,
394
+ source,
395
+ },
396
+ assignOperator: (node) => node.sourceString,
397
+ liquidAssignVariable: {
398
+ type: ConcreteNodeTypes.LiquidVariable,
399
+ expression: 0,
400
+ filters: 1,
401
+ rawSource: (tokens) => source.slice(locStart(tokens), tokens[tokens.length - 2].source.endIdx).trimEnd(),
402
+ locStart,
403
+ locEnd: locEndSecondToLast,
404
+ source,
405
+ },
406
+ liquidAssignExpression: 0,
407
+ liquidJsonHashLiteral: {
408
+ type: ConcreteNodeTypes.JsonHashLiteral,
409
+ entries: 2,
410
+ locStart,
411
+ locEnd,
412
+ source,
413
+ },
414
+ liquidJsonKeyValue: {
415
+ type: ConcreteNodeTypes.JsonKeyValuePair,
416
+ key: 0,
378
417
  value: 4,
379
418
  locStart,
380
419
  locEnd,
381
420
  source,
382
421
  },
422
+ liquidJsonKey: 0,
423
+ liquidJsonBareKey: {
424
+ type: ConcreteNodeTypes.VariableLookup,
425
+ name: 0,
426
+ lookups: () => [],
427
+ locStart,
428
+ locEnd,
429
+ source,
430
+ },
431
+ liquidJsonArrayLiteral: {
432
+ type: ConcreteNodeTypes.JsonArrayLiteral,
433
+ elements: 2,
434
+ locStart,
435
+ locEnd,
436
+ source,
437
+ },
438
+ liquidJsonValue: 0,
439
+ liquidJsonSep: 1,
383
440
  liquidTagHashAssignMarkup: {
384
441
  type: ConcreteNodeTypes.HashAssignMarkup,
385
442
  target: 0,
@@ -35,7 +35,7 @@
35
35
  import { ConcreteAttributeNode, ConcreteHtmlTagClose, ConcreteLiquidTagClose, LiquidCST, LiquidHtmlCST, ConcreteLiquidLiteral } from './stage-1-cst';
36
36
  import { Comparators, NamedTags, NodeTypes, Position } from './types';
37
37
  /** The union type of all possible node types inside a LiquidHTML AST. */
38
- export type LiquidHtmlNode = DocumentNode | YAMLFrontmatter | LiquidNode | HtmlDoctype | HtmlNode | AttributeNode | LiquidVariable | ComplexLiquidExpression | LiquidFilter | LiquidNamedArgument | AssignMarkup | HashAssignMarkup | ContentForMarkup | CycleMarkup | ForMarkup | RenderMarkup | FunctionMarkup | GraphQLMarkup | GraphQLInlineMarkup | PaginateMarkup | RawMarkup | RenderVariableExpression | RenderAliasExpression | LiquidLogicalExpression | LiquidComparison | TextNode | LiquidDocParamNode | LiquidDocExampleNode | LiquidDocPromptNode | LiquidDocDescriptionNode | BackgroundMarkup | BackgroundInlineMarkup | CacheMarkup | LogMarkup | SessionMarkup | ExportMarkup | RedirectToMarkup | IncludeFormMarkup | SpamProtectionMarkup;
38
+ export type LiquidHtmlNode = DocumentNode | YAMLFrontmatter | LiquidNode | HtmlDoctype | HtmlNode | AttributeNode | LiquidVariable | ComplexLiquidExpression | LiquidFilter | LiquidNamedArgument | AssignMarkup | HashAssignMarkup | ContentForMarkup | CycleMarkup | ForMarkup | RenderMarkup | FunctionMarkup | GraphQLMarkup | GraphQLInlineMarkup | PaginateMarkup | RawMarkup | RenderVariableExpression | RenderAliasExpression | LiquidLogicalExpression | LiquidComparison | TextNode | LiquidDocParamNode | LiquidDocExampleNode | LiquidDocPromptNode | LiquidDocDescriptionNode | JsonHashLiteral | JsonArrayLiteral | JsonKeyValuePair | BackgroundMarkup | BackgroundInlineMarkup | CacheMarkup | LogMarkup | SessionMarkup | ExportMarkup | RedirectToMarkup | IncludeFormMarkup | SpamProtectionMarkup;
39
39
  /** The root node of all LiquidHTML ASTs. */
40
40
  export interface DocumentNode extends ASTNode<NodeTypes.Document> {
41
41
  children: LiquidHtmlNode[];
@@ -137,8 +137,12 @@ export interface LiquidTagAssign extends LiquidTagNode<NamedTags.assign, AssignM
137
137
  }
138
138
  /** {% assign name = value %} */
139
139
  export interface AssignMarkup extends ASTNode<NodeTypes.AssignMarkup> {
140
- /** the name of the variable that is being assigned */
140
+ /** the base variable name (backward compatible) */
141
141
  name: string;
142
+ /** bracket/dot lookups on the target (empty for simple assignment) */
143
+ lookups: LiquidExpression[];
144
+ /** '=' for assignment, '<<' for array append */
145
+ operator: '=' | '<<';
142
146
  /** the value of the variable that is being assigned */
143
147
  value: LiquidVariable;
144
148
  }
@@ -506,7 +510,7 @@ export interface LiquidVariable extends ASTNode<NodeTypes.LiquidVariable> {
506
510
  rawSource: string;
507
511
  }
508
512
  /** The union type of all Liquid expression nodes */
509
- export type LiquidExpression = LiquidString | LiquidNumber | LiquidLiteral | LiquidRange | LiquidVariableLookup;
513
+ export type LiquidExpression = LiquidString | LiquidNumber | LiquidLiteral | LiquidRange | LiquidVariableLookup | JsonHashLiteral | JsonArrayLiteral;
510
514
  export type ComplexLiquidExpression = LiquidBooleanExpression | LiquidExpression;
511
515
  /** https://shopify.dev/docs/api/liquid/filters */
512
516
  export interface LiquidFilter extends ASTNode<NodeTypes.LiquidFilter> {
@@ -565,6 +569,19 @@ export interface LiquidVariableLookup extends ASTNode<NodeTypes.VariableLookup>
565
569
  /** name.lookup1[lookup2] */
566
570
  lookups: LiquidExpression[];
567
571
  }
572
+ /** Represents a JSON hash literal { key: value, ... } */
573
+ export interface JsonHashLiteral extends ASTNode<NodeTypes.JsonHashLiteral> {
574
+ entries: JsonKeyValuePair[];
575
+ }
576
+ /** Represents a key-value pair in a JSON hash literal */
577
+ export interface JsonKeyValuePair extends ASTNode<NodeTypes.JsonKeyValuePair> {
578
+ key: LiquidExpression;
579
+ value: LiquidExpression;
580
+ }
581
+ /** Represents a JSON array literal [el1, el2, ...] */
582
+ export interface JsonArrayLiteral extends ASTNode<NodeTypes.JsonArrayLiteral> {
583
+ elements: LiquidExpression[];
584
+ }
568
585
  /** The union type of all HTML nodes */
569
586
  export type HtmlNode = HtmlComment | HtmlElement | HtmlDanglingMarkerClose | HtmlVoidElement | HtmlSelfClosingElement | HtmlRawNode;
570
587
  /** The basic HTML node with an opening and closing tags. */
@@ -916,6 +916,8 @@ function toAssignMarkup(node) {
916
916
  return {
917
917
  type: types_1.NodeTypes.AssignMarkup,
918
918
  name: node.name,
919
+ lookups: node.target.lookups.map(toExpression),
920
+ operator: node.operator,
919
921
  value: toLiquidVariable(node.value),
920
922
  position: position(node),
921
923
  source: node.source,
@@ -1350,11 +1352,36 @@ function toExpression(node) {
1350
1352
  source: node.source,
1351
1353
  };
1352
1354
  }
1355
+ case stage_1_cst_1.ConcreteNodeTypes.JsonHashLiteral: {
1356
+ return {
1357
+ type: types_1.NodeTypes.JsonHashLiteral,
1358
+ entries: node.entries.map(toJsonKeyValuePair),
1359
+ position: position(node),
1360
+ source: node.source,
1361
+ };
1362
+ }
1363
+ case stage_1_cst_1.ConcreteNodeTypes.JsonArrayLiteral: {
1364
+ return {
1365
+ type: types_1.NodeTypes.JsonArrayLiteral,
1366
+ elements: node.elements.map(toExpression),
1367
+ position: position(node),
1368
+ source: node.source,
1369
+ };
1370
+ }
1353
1371
  default: {
1354
1372
  return (0, utils_1.assertNever)(node);
1355
1373
  }
1356
1374
  }
1357
1375
  }
1376
+ function toJsonKeyValuePair(node) {
1377
+ return {
1378
+ type: types_1.NodeTypes.JsonKeyValuePair,
1379
+ key: toExpression(node.key),
1380
+ value: toExpression(node.value),
1381
+ position: position(node),
1382
+ source: node.source,
1383
+ };
1384
+ }
1358
1385
  function toFilter(node) {
1359
1386
  return {
1360
1387
  type: types_1.NodeTypes.LiquidFilter,
package/dist/types.d.ts CHANGED
@@ -51,6 +51,9 @@ export declare enum NodeTypes {
51
51
  LiquidDocParamNode = "LiquidDocParamNode",
52
52
  LiquidDocExampleNode = "LiquidDocExampleNode",
53
53
  LiquidDocPromptNode = "LiquidDocPromptNode",
54
+ JsonHashLiteral = "JsonHashLiteral",
55
+ JsonArrayLiteral = "JsonArrayLiteral",
56
+ JsonKeyValuePair = "JsonKeyValuePair",
54
57
  BackgroundMarkup = "BackgroundMarkup",
55
58
  BackgroundInlineMarkup = "BackgroundInlineMarkup",
56
59
  CacheMarkup = "CacheMarkup",
package/dist/types.js CHANGED
@@ -49,6 +49,9 @@ var NodeTypes;
49
49
  NodeTypes["LiquidDocParamNode"] = "LiquidDocParamNode";
50
50
  NodeTypes["LiquidDocExampleNode"] = "LiquidDocExampleNode";
51
51
  NodeTypes["LiquidDocPromptNode"] = "LiquidDocPromptNode";
52
+ NodeTypes["JsonHashLiteral"] = "JsonHashLiteral";
53
+ NodeTypes["JsonArrayLiteral"] = "JsonArrayLiteral";
54
+ NodeTypes["JsonKeyValuePair"] = "JsonKeyValuePair";
52
55
  // platformos markup types
53
56
  NodeTypes["BackgroundMarkup"] = "BackgroundMarkup";
54
57
  NodeTypes["BackgroundInlineMarkup"] = "BackgroundInlineMarkup";
@@ -129,7 +129,33 @@ Liquid <: Helpers {
129
129
  liquidTagEchoMarkup = liquidVariable<delimTag>
130
130
 
131
131
  liquidTagAssign = liquidTagRule<"assign", liquidTagAssignMarkup>
132
- liquidTagAssignMarkup = variableSegment space* "=" space* liquidVariable<delimTag>
132
+ liquidTagAssignMarkup = assignTarget<delimTag> space* assignOperator space* liquidAssignVariable<delimTag>
133
+
134
+ // Assign-specific sub-rules
135
+ assignTarget<delim> = variableSegment lookup<delim>*
136
+ assignOperator = "<<" | "="
137
+
138
+ // Assign variable: like liquidVariable but expression allows JSON literals
139
+ liquidAssignVariable<delim> = liquidAssignExpression<delim> liquidFilter<delim>* space* &delim
140
+ liquidAssignExpression<delim> =
141
+ | liquidJsonHashLiteral<delim>
142
+ | liquidJsonArrayLiteral<delim>
143
+ | liquidComplexExpression<delim>
144
+
145
+ // JSON literal rules (assign-only, recursive)
146
+ liquidJsonHashLiteral<delim> =
147
+ "{" space* listOf<liquidJsonKeyValue<delim>, liquidJsonSep> space* "}"
148
+ liquidJsonKeyValue<delim> =
149
+ liquidJsonKey<delim> space* ":" space* liquidJsonValue<delim>
150
+ liquidJsonKey<delim> = liquidString<delim> | liquidJsonBareKey
151
+ liquidJsonBareKey = variableSegment
152
+ liquidJsonArrayLiteral<delim> =
153
+ "[" space* listOf<liquidJsonValue<delim>, liquidJsonSep> space* "]"
154
+ liquidJsonValue<delim> =
155
+ | liquidJsonHashLiteral<delim>
156
+ | liquidJsonArrayLiteral<delim>
157
+ | liquidExpression<delim>
158
+ liquidJsonSep = space* "," space*
133
159
 
134
160
  liquidTagHashAssign = liquidTagRule<"hash_assign", liquidTagHashAssignMarkup>
135
161
  liquidTagHashAssignMarkup = liquidVariableLookup<delimTag> space* "=" space* liquidVariable<delimTag>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformos/liquid-html-parser",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Liquid HTML parser for platformOS",
5
5
  "author": "platformOS",
6
6
  "homepage": "https://github.com/Platform-OS/platformos-tools/tree/master/packages/liquid-html-parser#readme",