@platformos/prettier-plugin-liquid 0.0.2

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.
Files changed (52) hide show
  1. package/README.md +158 -0
  2. package/ThirdPartyNotices.txt +17 -0
  3. package/dist/constants.evaluate.d.ts +7 -0
  4. package/dist/constants.evaluate.js +78 -0
  5. package/dist/index.d.ts +2 -0
  6. package/dist/index.js +7 -0
  7. package/dist/parser.d.ts +19 -0
  8. package/dist/parser.js +21 -0
  9. package/dist/plugin.d.ts +7 -0
  10. package/dist/plugin.js +88 -0
  11. package/dist/printer/embed.d.ts +9 -0
  12. package/dist/printer/embed.js +82 -0
  13. package/dist/printer/index.d.ts +14 -0
  14. package/dist/printer/index.js +12 -0
  15. package/dist/printer/preprocess/augment-with-css-properties.d.ts +2 -0
  16. package/dist/printer/preprocess/augment-with-css-properties.js +240 -0
  17. package/dist/printer/preprocess/augment-with-family.d.ts +2 -0
  18. package/dist/printer/preprocess/augment-with-family.js +13 -0
  19. package/dist/printer/preprocess/augment-with-parent.d.ts +2 -0
  20. package/dist/printer/preprocess/augment-with-parent.js +18 -0
  21. package/dist/printer/preprocess/augment-with-siblings.d.ts +1902 -0
  22. package/dist/printer/preprocess/augment-with-siblings.js +45 -0
  23. package/dist/printer/preprocess/augment-with-whitespace-helpers.d.ts +23 -0
  24. package/dist/printer/preprocess/augment-with-whitespace-helpers.js +460 -0
  25. package/dist/printer/preprocess/index.d.ts +1 -0
  26. package/dist/printer/preprocess/index.js +16 -0
  27. package/dist/printer/print/children.d.ts +6 -0
  28. package/dist/printer/print/children.js +283 -0
  29. package/dist/printer/print/element.d.ts +4 -0
  30. package/dist/printer/print/element.js +114 -0
  31. package/dist/printer/print/liquid.d.ts +14 -0
  32. package/dist/printer/print/liquid.js +637 -0
  33. package/dist/printer/print/tag.d.ts +23 -0
  34. package/dist/printer/print/tag.js +358 -0
  35. package/dist/printer/print-preprocess.d.ts +3 -0
  36. package/dist/printer/print-preprocess.js +48 -0
  37. package/dist/printer/printer-liquid-html.d.ts +13 -0
  38. package/dist/printer/printer-liquid-html.js +545 -0
  39. package/dist/printer/utils/array.d.ts +4 -0
  40. package/dist/printer/utils/array.js +19 -0
  41. package/dist/printer/utils/index.d.ts +14 -0
  42. package/dist/printer/utils/index.js +59 -0
  43. package/dist/printer/utils/node.d.ts +44 -0
  44. package/dist/printer/utils/node.js +270 -0
  45. package/dist/printer/utils/string.d.ts +15 -0
  46. package/dist/printer/utils/string.js +55 -0
  47. package/dist/types.d.ts +85 -0
  48. package/dist/types.js +53 -0
  49. package/dist/utils.d.ts +6 -0
  50. package/dist/utils.js +35 -0
  51. package/package.json +65 -0
  52. package/standalone.js +19503 -0
@@ -0,0 +1,545 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.printerLiquidHtml3 = exports.printerLiquidHtml2 = void 0;
4
+ const liquid_html_parser_1 = require("@platformos/liquid-html-parser");
5
+ const prettier_1 = require("prettier");
6
+ const types_1 = require("../types");
7
+ const utils_1 = require("../utils");
8
+ const embed_1 = require("./embed");
9
+ const print_preprocess_1 = require("./print-preprocess");
10
+ const children_1 = require("./print/children");
11
+ const element_1 = require("./print/element");
12
+ const liquid_1 = require("./print/liquid");
13
+ const tag_1 = require("./print/tag");
14
+ const utils_2 = require("./utils");
15
+ const { builders, utils } = prettier_1.doc;
16
+ const { fill, group, hardline, dedentToRoot, indent, join, line, softline } = builders;
17
+ const oppositeQuotes = {
18
+ '"': "'",
19
+ "'": '"',
20
+ };
21
+ function printAttributeName(path, _options, print) {
22
+ const node = path.getValue();
23
+ node.name;
24
+ return join('', path.map((part) => {
25
+ const value = part.getValue();
26
+ if (typeof value === 'string') {
27
+ return value;
28
+ }
29
+ else {
30
+ // We want to force the LiquidVariableOutput to be on one line to avoid weird
31
+ // shenanigans
32
+ return utils.removeLines(print(part));
33
+ }
34
+ }, 'name'));
35
+ }
36
+ function printAttribute(path, options, print) {
37
+ const node = path.getValue();
38
+ const attrGroupId = Symbol('attr-group-id');
39
+ // What should be the rule here? Should it really be "paragraph"?
40
+ // ideally... if the thing is and the line is too long
41
+ // use cases:
42
+ // - attr-{{ section.id }}--something.
43
+ // * We should try to put that "block" on one line
44
+ //
45
+ // - attr {{ classname }} foo
46
+ // * we should try to put on one line?
47
+ //
48
+ // - attr hello world ok fellow friends what do
49
+ // * if the line becomes too long do we want to break one per line?
50
+ // - for alt, would be paragraph
51
+ // - for classes, yeah maybe
52
+ // - for srcset?, it should be "split on comma"
53
+ // - for sizes?, it should be "split on comma"
54
+ // - for href?, it should be no space url
55
+ // - for others?, it should be keywords
56
+ // - for style, should be break on ;
57
+ // - for other?, should be...
58
+ // - how the fuck am I going to do that?
59
+ // - same way we do this? with a big ass switch case?
60
+ // - or we... don't and leave it as is?
61
+ //
62
+ // Anyway, for that reason ^, for now I'll just paste in what we have in
63
+ // the source. It's too hard to get right.
64
+ const value = node.source.slice(node.attributePosition.start, node.attributePosition.end);
65
+ const preferredQuote = options.singleQuote ? `'` : `"`;
66
+ const attributeValueContainsQuote = !!node.value.find((valueNode) => (0, utils_2.isTextLikeNode)(valueNode) && valueNode.value.includes(preferredQuote));
67
+ const quote = attributeValueContainsQuote ? oppositeQuotes[preferredQuote] : preferredQuote;
68
+ return [
69
+ printAttributeName(path, options, print),
70
+ '=',
71
+ quote,
72
+ (0, utils_2.hasLineBreakInRange)(node.source, node.attributePosition.start, node.attributePosition.end)
73
+ ? group([indent([softline, join(hardline, (0, utils_2.reindent)((0, utils_2.bodyLines)(value), true))]), softline], {
74
+ id: attrGroupId,
75
+ })
76
+ : value,
77
+ quote,
78
+ ];
79
+ }
80
+ function isYamlFrontMatter(node) {
81
+ return (node.parentNode &&
82
+ node.parentNode.type === liquid_html_parser_1.NodeTypes.Document &&
83
+ !node.prev &&
84
+ /^---\r?\n/.test(node.value));
85
+ }
86
+ function printTextNode(path, options, _print) {
87
+ const node = path.getValue();
88
+ if (isYamlFrontMatter(node))
89
+ return node.value;
90
+ if (node.value.match(/^\s*$/))
91
+ return '';
92
+ const text = node.value;
93
+ const paragraphs = text
94
+ .split(/(\r?\n){2,}/)
95
+ .filter(Boolean) // removes empty paragraphs (trailingWhitespace)
96
+ .map((curr) => {
97
+ let doc = [];
98
+ const words = curr.trim().split(/\s+/g);
99
+ let isFirst = true;
100
+ for (let j = 0; j < words.length; j++) {
101
+ const word = words[j];
102
+ if (isFirst) {
103
+ isFirst = false;
104
+ }
105
+ else {
106
+ doc.push(line);
107
+ }
108
+ doc.push(word);
109
+ }
110
+ return fill(doc);
111
+ });
112
+ return [
113
+ (0, tag_1.printOpeningTagPrefix)(node, options),
114
+ join(hardline, paragraphs),
115
+ (0, tag_1.printClosingTagSuffix)(node, options),
116
+ ];
117
+ }
118
+ function printNode(path, options, print, args = {}) {
119
+ const node = path.getValue();
120
+ switch (node.type) {
121
+ case liquid_html_parser_1.NodeTypes.Document: {
122
+ return [(0, children_1.printChildren)(path, options, print, args), hardline];
123
+ }
124
+ case liquid_html_parser_1.NodeTypes.HtmlElement: {
125
+ return (0, element_1.printElement)(path, options, print, args);
126
+ }
127
+ case liquid_html_parser_1.NodeTypes.HtmlDanglingMarkerClose: {
128
+ return (0, element_1.printElement)(path, options, print, args);
129
+ }
130
+ case liquid_html_parser_1.NodeTypes.HtmlVoidElement: {
131
+ return (0, element_1.printElement)(path, options, print, args);
132
+ }
133
+ case liquid_html_parser_1.NodeTypes.HtmlSelfClosingElement: {
134
+ return (0, element_1.printElement)(path, options, print, args);
135
+ }
136
+ case liquid_html_parser_1.NodeTypes.HtmlRawNode: {
137
+ return (0, element_1.printElement)(path, options, print, args);
138
+ }
139
+ case liquid_html_parser_1.NodeTypes.RawMarkup: {
140
+ if (node.parentNode?.name === 'doc') {
141
+ return (0, liquid_1.printLiquidDoc)(path, options, print, args);
142
+ }
143
+ const isRawMarkupIdentationSensitive = () => {
144
+ switch (node.kind) {
145
+ case liquid_html_parser_1.RawMarkupKinds.typescript:
146
+ case liquid_html_parser_1.RawMarkupKinds.javascript: {
147
+ return node.value.includes('`');
148
+ }
149
+ default: {
150
+ return false;
151
+ }
152
+ }
153
+ };
154
+ if (isRawMarkupIdentationSensitive()) {
155
+ return [node.value.trimEnd(), hardline];
156
+ }
157
+ const parentNode = node.parentNode;
158
+ const shouldNotIndentBody = parentNode &&
159
+ parentNode.type === liquid_html_parser_1.NodeTypes.LiquidRawTag &&
160
+ parentNode.name === 'schema' &&
161
+ !options.indentSchema;
162
+ const shouldIndentBody = node.kind !== liquid_html_parser_1.RawMarkupKinds.markdown && !shouldNotIndentBody;
163
+ const lines = (0, utils_2.bodyLines)(node.value);
164
+ const rawFirstLineIsntIndented = !!node.value.split(/\r?\n/)[0]?.match(/\S/);
165
+ const shouldSkipFirstLine = rawFirstLineIsntIndented;
166
+ const body = lines.length > 0 && lines.find((line) => line.trim() !== '')
167
+ ? join(hardline, (0, utils_2.reindent)(lines, shouldSkipFirstLine))
168
+ : softline;
169
+ if (shouldIndentBody) {
170
+ return [indent([hardline, body]), hardline];
171
+ }
172
+ else {
173
+ return [dedentToRoot([hardline, body]), hardline];
174
+ }
175
+ }
176
+ case liquid_html_parser_1.NodeTypes.LiquidVariableOutput: {
177
+ return (0, liquid_1.printLiquidVariableOutput)(path, options, print, args);
178
+ }
179
+ case liquid_html_parser_1.NodeTypes.LiquidRawTag: {
180
+ return (0, liquid_1.printLiquidRawTag)(path, options, print, args);
181
+ }
182
+ case liquid_html_parser_1.NodeTypes.LiquidTag: {
183
+ return (0, liquid_1.printLiquidTag)(path, options, print, args);
184
+ }
185
+ case liquid_html_parser_1.NodeTypes.LiquidBranch: {
186
+ return (0, liquid_1.printLiquidBranch)(path, options, print, args);
187
+ }
188
+ case liquid_html_parser_1.NodeTypes.AttrEmpty: {
189
+ return printAttributeName(path, options, print);
190
+ }
191
+ case liquid_html_parser_1.NodeTypes.AttrUnquoted:
192
+ case liquid_html_parser_1.NodeTypes.AttrSingleQuoted:
193
+ case liquid_html_parser_1.NodeTypes.AttrDoubleQuoted: {
194
+ return printAttribute(path, options, print);
195
+ }
196
+ case liquid_html_parser_1.NodeTypes.HtmlDoctype: {
197
+ if (!node.legacyDoctypeString)
198
+ return '<!doctype html>';
199
+ return node.source.slice(node.position.start, node.position.end);
200
+ }
201
+ case liquid_html_parser_1.NodeTypes.HtmlComment: {
202
+ const conditionalComment = (0, liquid_html_parser_1.getConditionalComment)(node.source.slice(node.position.start, node.position.end));
203
+ if (conditionalComment) {
204
+ const { startTag, body, endTag } = conditionalComment;
205
+ return [
206
+ startTag,
207
+ group([indent([line, join(hardline, (0, utils_2.reindent)((0, utils_2.bodyLines)(body), true))]), line]),
208
+ endTag,
209
+ ];
210
+ }
211
+ if (node.body.includes('prettier-ignore') ||
212
+ node.body.startsWith('display:') ||
213
+ node.body.startsWith('white-space:')) {
214
+ return node.source.slice(node.position.start, node.position.end);
215
+ }
216
+ return [
217
+ '<!--',
218
+ group([indent([line, join(hardline, (0, utils_2.reindent)((0, utils_2.bodyLines)(node.body), true))]), line]),
219
+ '-->',
220
+ ];
221
+ }
222
+ case liquid_html_parser_1.NodeTypes.AssignMarkup: {
223
+ return [node.name, ' = ', path.call((p) => print(p), 'value')];
224
+ }
225
+ case liquid_html_parser_1.NodeTypes.HashAssignMarkup: {
226
+ return [
227
+ path.call((p) => print(p), 'target'),
228
+ ' = ',
229
+ path.call((p) => print(p), 'value'),
230
+ ];
231
+ }
232
+ case liquid_html_parser_1.NodeTypes.CycleMarkup: {
233
+ const doc = [];
234
+ if (node.groupName) {
235
+ doc.push(path.call((p) => print(p), 'groupName'), ':');
236
+ }
237
+ const whitespace = node.args.length > 1 ? line : ' ';
238
+ doc.push(whitespace, join([',', whitespace], path.map((p) => print(p), 'args')));
239
+ return doc;
240
+ }
241
+ case liquid_html_parser_1.NodeTypes.ForMarkup: {
242
+ const doc = [node.variableName, ' in ', path.call((p) => print(p), 'collection')];
243
+ if (node.reversed) {
244
+ doc.push(line, 'reversed');
245
+ }
246
+ if (node.args.length > 0) {
247
+ doc.push([
248
+ line,
249
+ join(line, path.map((p) => print(p), 'args')),
250
+ ]);
251
+ }
252
+ return doc;
253
+ }
254
+ case liquid_html_parser_1.NodeTypes.PaginateMarkup: {
255
+ const doc = [
256
+ path.call((p) => print(p), 'collection'),
257
+ line,
258
+ 'by ',
259
+ path.call((p) => print(p), 'pageSize'),
260
+ ];
261
+ if (node.args.length > 0) {
262
+ doc.push([
263
+ ',',
264
+ line,
265
+ join([',', line], path.map((p) => print(p), 'args')),
266
+ ]);
267
+ }
268
+ return doc;
269
+ }
270
+ case liquid_html_parser_1.NodeTypes.ContentForMarkup: {
271
+ const contentForType = path.call((p) => print(p), 'contentForType');
272
+ const doc = [contentForType];
273
+ if (node.args.length > 0) {
274
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
275
+ }
276
+ return doc;
277
+ }
278
+ case liquid_html_parser_1.NodeTypes.RenderMarkup: {
279
+ const snippet = path.call((p) => print(p), 'snippet');
280
+ const doc = [snippet];
281
+ if (node.variable) {
282
+ const whitespace = node.alias?.value ? line : ' ';
283
+ doc.push(whitespace, path.call((p) => print(p), 'variable'));
284
+ }
285
+ if (node.alias?.value) {
286
+ doc.push(' ', 'as', ' ', node.alias.value);
287
+ }
288
+ if (node.args.length > 0) {
289
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
290
+ }
291
+ return doc;
292
+ }
293
+ case liquid_html_parser_1.NodeTypes.FunctionMarkup: {
294
+ const partial = path.call((p) => print(p), 'partial');
295
+ const doc = [node.name, ' = ', partial];
296
+ if (node.args.length > 0) {
297
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
298
+ }
299
+ return doc;
300
+ }
301
+ case liquid_html_parser_1.NodeTypes.GraphQLMarkup: {
302
+ const partial = path.call((p) => print(p), 'graphql');
303
+ const doc = [node.name, ' = ', partial];
304
+ if (node.args.length > 0) {
305
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
306
+ }
307
+ return doc;
308
+ }
309
+ case liquid_html_parser_1.NodeTypes.GraphQLInlineMarkup: {
310
+ const doc = [node.name];
311
+ if (node.args.length > 0) {
312
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
313
+ }
314
+ return doc;
315
+ }
316
+ case liquid_html_parser_1.NodeTypes.BackgroundMarkup: {
317
+ const partial = path.call((p) => print(p), 'partial');
318
+ const doc = [node.jobId, ' = ', partial];
319
+ if (node.args.length > 0) {
320
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
321
+ }
322
+ return doc;
323
+ }
324
+ case liquid_html_parser_1.NodeTypes.BackgroundInlineMarkup: {
325
+ const doc = [];
326
+ if (node.args.length > 0) {
327
+ doc.push(join([',', line], path.map((p) => print(p), 'args')));
328
+ }
329
+ return doc;
330
+ }
331
+ case liquid_html_parser_1.NodeTypes.CacheMarkup: {
332
+ const key = path.call((p) => print(p), 'key');
333
+ const doc = [key];
334
+ if (node.args.length > 0) {
335
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
336
+ }
337
+ return doc;
338
+ }
339
+ case liquid_html_parser_1.NodeTypes.LogMarkup: {
340
+ const value = path.call((p) => print(p), 'value');
341
+ const doc = [value];
342
+ if (node.args.length > 0) {
343
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
344
+ }
345
+ return doc;
346
+ }
347
+ case liquid_html_parser_1.NodeTypes.SessionMarkup: {
348
+ const value = path.call((p) => print(p), 'value');
349
+ return [node.name, ' = ', value];
350
+ }
351
+ case liquid_html_parser_1.NodeTypes.ExportMarkup: {
352
+ const variables = path.map((p) => print(p), 'variables');
353
+ const doc = [join(', ', variables)];
354
+ if (node.namespace) {
355
+ const ns = path.call((p) => print(p), 'namespace');
356
+ doc.push(',', line, 'namespace: ', ns);
357
+ }
358
+ return doc;
359
+ }
360
+ case liquid_html_parser_1.NodeTypes.RedirectToMarkup: {
361
+ const url = path.call((p) => print(p), 'url');
362
+ const doc = [url];
363
+ if (node.args.length > 0) {
364
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
365
+ }
366
+ return doc;
367
+ }
368
+ case liquid_html_parser_1.NodeTypes.IncludeFormMarkup: {
369
+ const form = path.call((p) => print(p), 'form');
370
+ const doc = [form];
371
+ if (node.args.length > 0) {
372
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
373
+ }
374
+ return doc;
375
+ }
376
+ case liquid_html_parser_1.NodeTypes.SpamProtectionMarkup: {
377
+ const value = path.call((p) => print(p), 'value');
378
+ const doc = [value];
379
+ if (node.args.length > 0) {
380
+ doc.push(',', line, join([',', line], path.map((p) => print(p), 'args')));
381
+ }
382
+ return doc;
383
+ }
384
+ case liquid_html_parser_1.NodeTypes.RenderVariableExpression: {
385
+ return [node.kind, ' ', path.call((p) => print(p), 'name')];
386
+ }
387
+ case liquid_html_parser_1.NodeTypes.RenderAliasExpression: {
388
+ return node.value;
389
+ }
390
+ case liquid_html_parser_1.NodeTypes.LogicalExpression: {
391
+ return [
392
+ path.call((p) => print(p), 'left'),
393
+ line,
394
+ node.relation,
395
+ ' ',
396
+ path.call((p) => print(p), 'right'),
397
+ ];
398
+ }
399
+ case liquid_html_parser_1.NodeTypes.Comparison: {
400
+ return group([
401
+ path.call((p) => print(p), 'left'),
402
+ indent([line, node.comparator, ' ', path.call((p) => print(p), 'right')]),
403
+ ]);
404
+ }
405
+ case liquid_html_parser_1.NodeTypes.LiquidVariable: {
406
+ const name = path.call((p) => print(p), 'expression');
407
+ let filters = '';
408
+ if (node.filters.length > 0) {
409
+ filters = [
410
+ line,
411
+ join(line, path.map((p) => print(p), 'filters')),
412
+ ];
413
+ }
414
+ return [name, filters];
415
+ }
416
+ case liquid_html_parser_1.NodeTypes.LiquidFilter: {
417
+ let args = [];
418
+ if (node.args.length > 0) {
419
+ const printed = path.map((p) => print(p), 'args');
420
+ const shouldPrintFirstArgumentSameLine = node.args[0].type !== liquid_html_parser_1.NodeTypes.NamedArgument;
421
+ if (shouldPrintFirstArgumentSameLine) {
422
+ const [firstDoc, ...rest] = printed;
423
+ const restDoc = (0, utils_2.isEmpty)(rest) ? '' : indent([',', line, join([',', line], rest)]);
424
+ args = [': ', firstDoc, restDoc];
425
+ }
426
+ else {
427
+ args = [':', indent([line, join([',', line], printed)])];
428
+ }
429
+ }
430
+ return group(['| ', node.name, ...args]);
431
+ }
432
+ case liquid_html_parser_1.NodeTypes.NamedArgument: {
433
+ return [node.name, ': ', path.call((p) => print(p), 'value')];
434
+ }
435
+ case liquid_html_parser_1.NodeTypes.TextNode: {
436
+ return printTextNode(path, options, print);
437
+ }
438
+ case liquid_html_parser_1.NodeTypes.YAMLFrontmatter: {
439
+ return ['---', hardline, node.body, '---'];
440
+ }
441
+ case liquid_html_parser_1.NodeTypes.BooleanExpression: {
442
+ return path.call((p) => print(p), 'condition');
443
+ }
444
+ case liquid_html_parser_1.NodeTypes.String: {
445
+ const preferredQuote = options.liquidSingleQuote ? `'` : `"`;
446
+ const valueHasQuotes = node.value.includes(preferredQuote);
447
+ const quote = valueHasQuotes ? oppositeQuotes[preferredQuote] : preferredQuote;
448
+ return [quote, node.value, quote];
449
+ }
450
+ case liquid_html_parser_1.NodeTypes.Number: {
451
+ if (args.truncate) {
452
+ return node.value.replace(/\.\d+$/, '');
453
+ }
454
+ else {
455
+ return node.value;
456
+ }
457
+ }
458
+ case liquid_html_parser_1.NodeTypes.Range: {
459
+ return [
460
+ '(',
461
+ path.call((p) => print(p, { truncate: true }), 'start'),
462
+ '..',
463
+ path.call((p) => print(p, { truncate: true }), 'end'),
464
+ ')',
465
+ ];
466
+ }
467
+ case liquid_html_parser_1.NodeTypes.LiquidLiteral: {
468
+ // We prefer null over nil.
469
+ if (node.keyword === 'nil') {
470
+ return 'null';
471
+ }
472
+ return node.keyword;
473
+ }
474
+ case liquid_html_parser_1.NodeTypes.VariableLookup: {
475
+ const doc = [];
476
+ if (node.name) {
477
+ doc.push(node.name);
478
+ }
479
+ const lookups = path.map((lookupPath, index) => {
480
+ const lookup = lookupPath.getValue();
481
+ switch (lookup.type) {
482
+ case liquid_html_parser_1.NodeTypes.String: {
483
+ const value = lookup.value;
484
+ // We prefer direct access
485
+ // (for everything but stuff with dashes and stuff that starts with a number)
486
+ const isGlobalStringLookup = index === 0 && !node.name;
487
+ if (!isGlobalStringLookup && /^\D/.test(value) && /^[a-z0-9_]+\??$/i.test(value)) {
488
+ return ['.', value];
489
+ }
490
+ return ['[', print(lookupPath), ']'];
491
+ }
492
+ default: {
493
+ return ['[', print(lookupPath), ']'];
494
+ }
495
+ }
496
+ }, 'lookups');
497
+ return [...doc, ...lookups];
498
+ }
499
+ case liquid_html_parser_1.NodeTypes.LiquidDocParamNode: {
500
+ return (0, liquid_1.printLiquidDocParam)(path, options, print, args);
501
+ }
502
+ case liquid_html_parser_1.NodeTypes.LiquidDocExampleNode: {
503
+ return (0, liquid_1.printLiquidDocExample)(path, options, print, args);
504
+ }
505
+ case liquid_html_parser_1.NodeTypes.LiquidDocDescriptionNode: {
506
+ return (0, liquid_1.printLiquidDocDescription)(path, options, print, args);
507
+ }
508
+ case liquid_html_parser_1.NodeTypes.LiquidDocPromptNode: {
509
+ return (0, liquid_1.printLiquidDocPrompt)(path, options, print, args);
510
+ }
511
+ default: {
512
+ return (0, utils_1.assertNever)(node);
513
+ }
514
+ }
515
+ }
516
+ exports.printerLiquidHtml2 = {
517
+ print: printNode,
518
+ embed: embed_1.embed2,
519
+ preprocess: print_preprocess_1.preprocess,
520
+ getVisitorKeys(node, nonTraversableKeys) {
521
+ return Object.keys(node).filter((key) => !nonTraversableKeys.has(key) &&
522
+ !types_1.nonTraversableProperties.has(key) &&
523
+ hasOrIsNode(node, key));
524
+ },
525
+ };
526
+ exports.printerLiquidHtml3 = {
527
+ print: printNode,
528
+ embed: embed_1.embed3,
529
+ preprocess: print_preprocess_1.preprocess,
530
+ getVisitorKeys(node, nonTraversableKeys) {
531
+ return Object.keys(node).filter((key) => !nonTraversableKeys.has(key) &&
532
+ !types_1.nonTraversableProperties.has(key) &&
533
+ hasOrIsNode(node, key));
534
+ },
535
+ };
536
+ function hasOrIsNode(node, key) {
537
+ const v = node[key];
538
+ // this works because there's no ()[] type that is string | Node, it only
539
+ // happens for singular nodes such as name: string | LiquidDrop, etc.
540
+ return Array.isArray(v) || isNode(v);
541
+ }
542
+ function isNode(x) {
543
+ return x !== null && typeof x === 'object' && 'type' in x && typeof x.type === 'string';
544
+ }
545
+ //# sourceMappingURL=printer-liquid-html.js.map
@@ -0,0 +1,4 @@
1
+ export declare function last<T>(x: T[]): T;
2
+ export declare function first<T>(x: T[]): T;
3
+ export declare function intersperse<T>(array: T[], delim: T): T[];
4
+ export declare function isEmpty(col: any[]): boolean;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.last = last;
4
+ exports.first = first;
5
+ exports.intersperse = intersperse;
6
+ exports.isEmpty = isEmpty;
7
+ function last(x) {
8
+ return x[x.length - 1];
9
+ }
10
+ function first(x) {
11
+ return x[0];
12
+ }
13
+ function intersperse(array, delim) {
14
+ return array.flatMap((val) => [delim, val]).slice(1);
15
+ }
16
+ function isEmpty(col) {
17
+ return col.length === 0;
18
+ }
19
+ //# sourceMappingURL=array.js.map
@@ -0,0 +1,14 @@
1
+ import { Doc, doc } from 'prettier';
2
+ import { LiquidAstPath, LiquidHtmlNode } from '../../types';
3
+ export * from './array';
4
+ export * from './string';
5
+ export * from './node';
6
+ export declare function getSource(path: LiquidAstPath): string;
7
+ export declare function isDeeplyNested(node: Extract<LiquidHtmlNode, {
8
+ children?: LiquidHtmlNode[];
9
+ }>): boolean;
10
+ export declare function getWhitespaceTrim(currWhitespaceTrim: string, needsWhitespaceStrippingOnBreak: boolean | undefined, groupIds?: symbol | symbol[]): Doc;
11
+ export declare const FORCE_FLAT_GROUP_ID: unique symbol;
12
+ export declare const FORCE_BREAK_GROUP_ID: unique symbol;
13
+ export declare function ifBreakChain(breaksContent: Doc, flatContent: Doc, groupIds: (symbol | undefined)[]): doc.builders.Doc;
14
+ export declare function isNonEmptyArray(object: any): object is any[];
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.FORCE_BREAK_GROUP_ID = exports.FORCE_FLAT_GROUP_ID = void 0;
18
+ exports.getSource = getSource;
19
+ exports.isDeeplyNested = isDeeplyNested;
20
+ exports.getWhitespaceTrim = getWhitespaceTrim;
21
+ exports.ifBreakChain = ifBreakChain;
22
+ exports.isNonEmptyArray = isNonEmptyArray;
23
+ const prettier_1 = require("prettier");
24
+ const liquid_html_parser_1 = require("@platformos/liquid-html-parser");
25
+ const array_1 = require("./array");
26
+ __exportStar(require("./array"), exports);
27
+ __exportStar(require("./string"), exports);
28
+ __exportStar(require("./node"), exports);
29
+ const { builders } = prettier_1.doc;
30
+ const { ifBreak } = builders;
31
+ function getSource(path) {
32
+ return path.getValue().source;
33
+ }
34
+ function isDeeplyNested(node) {
35
+ if (!node.children)
36
+ return false;
37
+ if ((0, liquid_html_parser_1.isBranchedTag)(node)) {
38
+ return !!node.children.find((child) => isDeeplyNested(child));
39
+ }
40
+ return !!node.children.find((child) => !(0, array_1.isEmpty)(child.children || []));
41
+ }
42
+ // Optionally converts a '' into '-' if any of the parent group breaks and source[loc] is non space.
43
+ function getWhitespaceTrim(currWhitespaceTrim, needsWhitespaceStrippingOnBreak, groupIds) {
44
+ return ifBreakChain(needsWhitespaceStrippingOnBreak ? '-' : currWhitespaceTrim, currWhitespaceTrim, Array.isArray(groupIds) ? groupIds : [groupIds]);
45
+ }
46
+ // Threads ifBreak into multiple sources of breakage (paragraph or self, etc.)
47
+ exports.FORCE_FLAT_GROUP_ID = Symbol('force-no-break');
48
+ exports.FORCE_BREAK_GROUP_ID = Symbol('force-break');
49
+ function ifBreakChain(breaksContent, flatContent, groupIds) {
50
+ if (groupIds.includes(exports.FORCE_BREAK_GROUP_ID))
51
+ return breaksContent;
52
+ if (groupIds.includes(exports.FORCE_FLAT_GROUP_ID))
53
+ return flatContent;
54
+ return groupIds.reduce((currFlatContent, groupId) => ifBreak(breaksContent, currFlatContent, { groupId }), flatContent);
55
+ }
56
+ function isNonEmptyArray(object) {
57
+ return Array.isArray(object) && object.length > 0;
58
+ }
59
+ //# sourceMappingURL=index.js.map