@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,358 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.needsToBorrowPrevClosingTagEndMarker = needsToBorrowPrevClosingTagEndMarker;
4
+ exports.needsToBorrowLastChildClosingTagEndMarker = needsToBorrowLastChildClosingTagEndMarker;
5
+ exports.needsToBorrowParentClosingTagStartMarker = needsToBorrowParentClosingTagStartMarker;
6
+ exports.needsToBorrowNextOpeningTagStartMarker = needsToBorrowNextOpeningTagStartMarker;
7
+ exports.needsToBorrowParentOpeningTagEndMarker = needsToBorrowParentOpeningTagEndMarker;
8
+ exports.printOpeningTag = printOpeningTag;
9
+ exports.printOpeningTagStart = printOpeningTagStart;
10
+ exports.printOpeningTagPrefix = printOpeningTagPrefix;
11
+ exports.printOpeningTagStartMarker = printOpeningTagStartMarker;
12
+ exports.printClosingTag = printClosingTag;
13
+ exports.printClosingTagStart = printClosingTagStart;
14
+ exports.printClosingTagEnd = printClosingTagEnd;
15
+ exports.printClosingTagSuffix = printClosingTagSuffix;
16
+ exports.printClosingTagStartMarker = printClosingTagStartMarker;
17
+ exports.printClosingTagEndMarker = printClosingTagEndMarker;
18
+ exports.printOpeningTagEndMarker = printOpeningTagEndMarker;
19
+ exports.getNodeContent = getNodeContent;
20
+ const liquid_html_parser_1 = require("@platformos/liquid-html-parser");
21
+ const prettier_1 = require("prettier");
22
+ const utils_1 = require("../utils");
23
+ const { builders: { breakParent, indent, join, line, softline, hardline }, } = prettier_1.doc;
24
+ const { replaceEndOfLine } = prettier_1.doc.utils;
25
+ function shouldNotPrintClosingTag(node, _options) {
26
+ return (!(0, utils_1.hasNoCloseMarker)(node) && // has close marker
27
+ !node.blockEndPosition && // does not have blockEndPosition
28
+ ((0, utils_1.hasPrettierIgnore)(node) || (0, utils_1.shouldPreserveContent)(node.parentNode)));
29
+ }
30
+ function needsToBorrowPrevClosingTagEndMarker(node) {
31
+ /**
32
+ * <p></p
33
+ * >123
34
+ * ^
35
+ *
36
+ * <p></p
37
+ * ><a
38
+ * ^
39
+ */
40
+ return (!(0, utils_1.isLiquidNode)(node) &&
41
+ node.prev &&
42
+ // node.prev.type !== 'docType' &&
43
+ (0, utils_1.isHtmlNode)(node.prev) &&
44
+ (0, utils_1.hasMeaningfulLackOfLeadingWhitespace)(node));
45
+ }
46
+ function needsToBorrowLastChildClosingTagEndMarker(node) {
47
+ /**
48
+ * <p
49
+ * ><a></a
50
+ * ></p
51
+ * ^
52
+ * >
53
+ */
54
+ return ((0, utils_1.isHtmlNode)(node) &&
55
+ node.lastChild &&
56
+ (0, utils_1.hasMeaningfulLackOfTrailingWhitespace)(node.lastChild) &&
57
+ (0, utils_1.isHtmlNode)((0, utils_1.getLastDescendant)(node.lastChild)) &&
58
+ !(0, utils_1.isPreLikeNode)(node));
59
+ }
60
+ function needsToBorrowParentClosingTagStartMarker(node) {
61
+ /**
62
+ * <p>
63
+ * 123</p
64
+ * ^^^
65
+ * >
66
+ *
67
+ * 123</b
68
+ * ></a
69
+ * ^^^
70
+ * >
71
+ */
72
+ return ((0, utils_1.isHtmlNode)(node.parentNode) &&
73
+ !node.next &&
74
+ (0, utils_1.hasMeaningfulLackOfTrailingWhitespace)(node) &&
75
+ !(0, utils_1.isLiquidNode)(node) &&
76
+ ((0, utils_1.isTextLikeNode)((0, utils_1.getLastDescendant)(node)) || (0, utils_1.isLiquidNode)((0, utils_1.getLastDescendant)(node))));
77
+ }
78
+ function needsToBorrowNextOpeningTagStartMarker(node) {
79
+ /**
80
+ * 123<p
81
+ * ^^
82
+ * >
83
+ */
84
+ return (node.next &&
85
+ (0, utils_1.isHtmlNode)(node.next) &&
86
+ (0, utils_1.isTextLikeNode)(node) &&
87
+ (0, utils_1.hasMeaningfulLackOfTrailingWhitespace)(node));
88
+ }
89
+ function needsToBorrowParentOpeningTagEndMarker(node) {
90
+ /**
91
+ * <p
92
+ * >123
93
+ * ^
94
+ *
95
+ * <p
96
+ * ><a
97
+ * ^
98
+ */
99
+ return ((0, utils_1.isHtmlNode)(node.parentNode) &&
100
+ !node.prev &&
101
+ (0, utils_1.hasMeaningfulLackOfLeadingWhitespace)(node) &&
102
+ !(0, utils_1.isLiquidNode)(node));
103
+ }
104
+ /**
105
+ * This is so complicated :')
106
+ */
107
+ function printAttributes(path, options, print, attrGroupId) {
108
+ const node = path.getValue();
109
+ if ((0, utils_1.isHtmlComment)(node))
110
+ return '';
111
+ if (node.type === liquid_html_parser_1.NodeTypes.HtmlDanglingMarkerClose)
112
+ return '';
113
+ if (node.attributes.length === 0) {
114
+ return (0, utils_1.isSelfClosing)(node)
115
+ ? /**
116
+ * <br />
117
+ * ^
118
+ */
119
+ ' '
120
+ : '';
121
+ }
122
+ const prettierIgnoreAttributes = (0, utils_1.isPrettierIgnoreAttributeNode)(node.prev);
123
+ const printedAttributes = path.map((attr) => {
124
+ const attrNode = attr.getValue();
125
+ let extraNewline = '';
126
+ if (attrNode.prev &&
127
+ (0, utils_1.hasMoreThanOneNewLineBetweenNodes)(attrNode.source, attrNode.prev, attrNode)) {
128
+ extraNewline = hardline;
129
+ }
130
+ const printed = print(attr, { trailingSpaceGroupId: attrGroupId });
131
+ return [extraNewline, printed];
132
+ }, 'attributes');
133
+ const forceBreakAttrContent = node.source
134
+ .slice(node.blockStartPosition.start, (0, utils_1.last)(node.attributes).position.end)
135
+ .includes('\n');
136
+ const isSingleLineLinkTagException = options.singleLineLinkTags && typeof node.name === 'string' && node.name === 'link';
137
+ const shouldNotBreakAttributes = (((0, utils_1.isHtmlElement)(node) && node.children.length > 0) ||
138
+ (0, utils_1.isVoidElement)(node) ||
139
+ (0, utils_1.isSelfClosing)(node)) &&
140
+ !forceBreakAttrContent &&
141
+ node.attributes.length === 1 &&
142
+ !(0, utils_1.isLiquidNode)(node.attributes[0]);
143
+ const forceNotToBreakAttrContent = isSingleLineLinkTagException || shouldNotBreakAttributes;
144
+ const whitespaceBetweenAttributes = forceNotToBreakAttrContent
145
+ ? ' '
146
+ : options.singleAttributePerLine && node.attributes.length > 1
147
+ ? hardline
148
+ : line;
149
+ const attributes = prettierIgnoreAttributes
150
+ ? replaceEndOfLine(node.source.slice((0, utils_1.first)(node.attributes).position.start, (0, utils_1.last)(node.attributes).position.end))
151
+ : join(whitespaceBetweenAttributes, printedAttributes);
152
+ let trailingInnerWhitespace;
153
+ if (
154
+ /**
155
+ * 123<a
156
+ * attr
157
+ * ~
158
+ * >456
159
+ */
160
+ (node.firstChild && needsToBorrowParentOpeningTagEndMarker(node.firstChild)) ||
161
+ /**
162
+ * <span
163
+ * >123<meta
164
+ * ~
165
+ * /></span>
166
+ */
167
+ ((0, utils_1.hasNoCloseMarker)(node) && needsToBorrowLastChildClosingTagEndMarker(node.parentNode)) ||
168
+ forceNotToBreakAttrContent) {
169
+ trailingInnerWhitespace = (0, utils_1.isSelfClosing)(node) ? ' ' : '';
170
+ }
171
+ else {
172
+ trailingInnerWhitespace = options.bracketSameLine
173
+ ? (0, utils_1.isSelfClosing)(node)
174
+ ? ' '
175
+ : ''
176
+ : (0, utils_1.isSelfClosing)(node)
177
+ ? line
178
+ : softline;
179
+ }
180
+ return [
181
+ indent([
182
+ forceNotToBreakAttrContent ? ' ' : line,
183
+ forceBreakAttrContent ? breakParent : '',
184
+ attributes,
185
+ ]),
186
+ trailingInnerWhitespace,
187
+ ];
188
+ }
189
+ function printOpeningTag(path, options, print, attrGroupId) {
190
+ const node = path.getValue();
191
+ return [
192
+ printOpeningTagStart(node, options),
193
+ printAttributes(path, options, print, attrGroupId),
194
+ (0, utils_1.hasNoChildren)(node) ? '' : printOpeningTagEnd(node),
195
+ ];
196
+ }
197
+ // If the current node's `<` isn't borrowed by the previous node, will print the prefix and `<`
198
+ function printOpeningTagStart(node, options) {
199
+ return node.prev && needsToBorrowNextOpeningTagStartMarker(node.prev)
200
+ ? ''
201
+ : [printOpeningTagPrefix(node, options), printOpeningTagStartMarker(node)];
202
+ }
203
+ // The opening tag prefix is the mechanism we use to "borrow" closing tags to maintain lack of whitespace
204
+ // It will print the parent's or the previous node's `>` if we need to.
205
+ function printOpeningTagPrefix(node, options) {
206
+ return needsToBorrowParentOpeningTagEndMarker(node)
207
+ ? printOpeningTagEndMarker(node.parentNode) // opening tag '>' of parent
208
+ : needsToBorrowPrevClosingTagEndMarker(node)
209
+ ? printClosingTagEndMarker(node.prev, options) // closing '>' of previous
210
+ : '';
211
+ }
212
+ // Will maybe print the `>` of the node.
213
+ // If the first child needs to borrow the `>`, we won't print it
214
+ //
215
+ // <a
216
+ // ><img
217
+ // ^ this is the opening tag end. Might be borrowed by the first child.
218
+ // ></a>
219
+ function printOpeningTagEnd(node) {
220
+ return node.firstChild && needsToBorrowParentOpeningTagEndMarker(node.firstChild)
221
+ ? ''
222
+ : printOpeningTagEndMarker(node);
223
+ }
224
+ // Print the `<` equivalent for the node.
225
+ function printOpeningTagStartMarker(node) {
226
+ if (!node)
227
+ return '';
228
+ switch (node.type) {
229
+ case liquid_html_parser_1.NodeTypes.HtmlComment:
230
+ return '<!--';
231
+ case liquid_html_parser_1.NodeTypes.HtmlElement:
232
+ case liquid_html_parser_1.NodeTypes.HtmlSelfClosingElement:
233
+ return `<${getCompoundName(node)}`;
234
+ case liquid_html_parser_1.NodeTypes.HtmlDanglingMarkerClose:
235
+ return `</${getCompoundName(node)}`;
236
+ case liquid_html_parser_1.NodeTypes.HtmlVoidElement:
237
+ case liquid_html_parser_1.NodeTypes.HtmlRawNode:
238
+ return `<${node.name}`;
239
+ default:
240
+ return ''; // TODO
241
+ }
242
+ }
243
+ // this function's job is to print the closing part of the tag </a>
244
+ // curious: it also prints void elements `>` and self closing node's `/>`
245
+ // that's because we might want to borrow it
246
+ function printClosingTag(node, options) {
247
+ return [
248
+ (0, utils_1.hasNoCloseMarker)(node) ? '' : printClosingTagStart(node, options),
249
+ printClosingTagEnd(node, options),
250
+ ];
251
+ }
252
+ function printClosingTagStart(node, options) {
253
+ return node.lastChild && needsToBorrowParentClosingTagStartMarker(node.lastChild)
254
+ ? ''
255
+ : [printClosingTagPrefix(node, options), printClosingTagStartMarker(node, options)];
256
+ }
257
+ function printClosingTagEnd(node, options) {
258
+ return (node.next
259
+ ? needsToBorrowPrevClosingTagEndMarker(node.next)
260
+ : needsToBorrowLastChildClosingTagEndMarker(node.parentNode))
261
+ ? ''
262
+ : [printClosingTagEndMarker(node, options), printClosingTagSuffix(node, options)];
263
+ }
264
+ function printClosingTagPrefix(node, options) {
265
+ return needsToBorrowLastChildClosingTagEndMarker(node)
266
+ ? printClosingTagEndMarker(node.lastChild, options)
267
+ : '';
268
+ }
269
+ function printClosingTagSuffix(node, options) {
270
+ return needsToBorrowParentClosingTagStartMarker(node)
271
+ ? printClosingTagStartMarker(node.parentNode, options)
272
+ : needsToBorrowNextOpeningTagStartMarker(node)
273
+ ? printOpeningTagStartMarker(node.next)
274
+ : '';
275
+ }
276
+ function printClosingTagStartMarker(node, options) {
277
+ if (!node)
278
+ return '';
279
+ /* istanbul ignore next */
280
+ if (shouldNotPrintClosingTag(node, options)) {
281
+ return '';
282
+ }
283
+ switch (node.type) {
284
+ case liquid_html_parser_1.NodeTypes.HtmlElement:
285
+ return `</${getCompoundName(node)}`;
286
+ case liquid_html_parser_1.NodeTypes.HtmlRawNode:
287
+ return `</${node.name}`;
288
+ default:
289
+ return '';
290
+ }
291
+ }
292
+ function printClosingTagEndMarker(node, options) {
293
+ if (!node)
294
+ return '';
295
+ if (shouldNotPrintClosingTag(node, options) || (0, utils_1.isHtmlDanglingMarkerOpen)(node)) {
296
+ return '';
297
+ }
298
+ switch (node.type) {
299
+ case liquid_html_parser_1.NodeTypes.HtmlSelfClosingElement: {
300
+ // looks like it doesn't make sense because it should be part of
301
+ // the printOpeningTagEndMarker but this is handled somewhere else.
302
+ // This function is used to determine what to borrow so the "end" to
303
+ // borrow is actually the other end.
304
+ return '/>';
305
+ }
306
+ default:
307
+ return '>';
308
+ }
309
+ }
310
+ // Print the opening tag's `>`
311
+ function printOpeningTagEndMarker(node) {
312
+ if (!node)
313
+ return '';
314
+ switch (node.type) {
315
+ case liquid_html_parser_1.NodeTypes.HtmlComment:
316
+ return '-->';
317
+ case liquid_html_parser_1.NodeTypes.HtmlSelfClosingElement:
318
+ case liquid_html_parser_1.NodeTypes.HtmlVoidElement:
319
+ return ''; // the `>` is printed by the printClosingTagEndMarker for self closing things
320
+ case liquid_html_parser_1.NodeTypes.HtmlElement:
321
+ case liquid_html_parser_1.NodeTypes.HtmlDanglingMarkerClose:
322
+ // TODO why is this one not with the other group?
323
+ case liquid_html_parser_1.NodeTypes.HtmlRawNode:
324
+ return '>';
325
+ default:
326
+ return '>';
327
+ }
328
+ }
329
+ function getNodeContent(node, options) {
330
+ let start = node.blockStartPosition.end;
331
+ if (node.firstChild && needsToBorrowParentOpeningTagEndMarker(node.firstChild)) {
332
+ start -= printOpeningTagEndMarker(node).length;
333
+ }
334
+ let end = node.blockEndPosition.start;
335
+ if (node.lastChild && needsToBorrowParentClosingTagStartMarker(node.lastChild)) {
336
+ end += printClosingTagStartMarker(node, options).length;
337
+ }
338
+ else if (node.lastChild && needsToBorrowLastChildClosingTagEndMarker(node)) {
339
+ end -= printClosingTagEndMarker(node.lastChild, options).length;
340
+ }
341
+ return options.originalText.slice(start, end);
342
+ }
343
+ function getCompoundName(node) {
344
+ return node.name
345
+ .map((part) => {
346
+ if (part.type === liquid_html_parser_1.NodeTypes.TextNode) {
347
+ return part.value;
348
+ }
349
+ else if (typeof part.markup === 'string') {
350
+ return `{{ ${part.markup.trim()} }}`;
351
+ }
352
+ else {
353
+ return `{{ ${part.markup.rawSource} }}`;
354
+ }
355
+ })
356
+ .join('');
357
+ }
358
+ //# sourceMappingURL=tag.js.map
@@ -0,0 +1,3 @@
1
+ import * as AST from '@platformos/liquid-html-parser';
2
+ import { LiquidParserOptions, DocumentNode } from '../types';
3
+ export declare function preprocess(ast: AST.DocumentNode, options: LiquidParserOptions): DocumentNode;
@@ -0,0 +1,48 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.preprocess = preprocess;
37
+ const AST = __importStar(require("@platformos/liquid-html-parser"));
38
+ const preprocess_1 = require("./preprocess");
39
+ // This is super hard to type check so I'll just magically assume
40
+ // everything works.
41
+ function preprocess(ast, options) {
42
+ const augmentationPipeline = preprocess_1.AUGMENTATION_PIPELINE.map((fn) => fn.bind(null, options));
43
+ for (const augmentation of augmentationPipeline) {
44
+ AST.walk(ast, augmentation);
45
+ }
46
+ return ast;
47
+ }
48
+ //# sourceMappingURL=print-preprocess.js.map
@@ -0,0 +1,13 @@
1
+ import type { Printer as Printer2 } from 'prettier';
2
+ import type { Printer as Printer3 } from 'prettier3';
3
+ import { LiquidHtmlNode } from '../types';
4
+ export declare const printerLiquidHtml2: Printer2<LiquidHtmlNode> & {
5
+ preprocess: any;
6
+ } & {
7
+ getVisitorKeys: any;
8
+ };
9
+ export declare const printerLiquidHtml3: Printer3<LiquidHtmlNode> & {
10
+ preprocess: any;
11
+ } & {
12
+ getVisitorKeys: any;
13
+ };