@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.
- package/README.md +158 -0
- package/ThirdPartyNotices.txt +17 -0
- package/dist/constants.evaluate.d.ts +7 -0
- package/dist/constants.evaluate.js +78 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -0
- package/dist/parser.d.ts +19 -0
- package/dist/parser.js +21 -0
- package/dist/plugin.d.ts +7 -0
- package/dist/plugin.js +88 -0
- package/dist/printer/embed.d.ts +9 -0
- package/dist/printer/embed.js +82 -0
- package/dist/printer/index.d.ts +14 -0
- package/dist/printer/index.js +12 -0
- package/dist/printer/preprocess/augment-with-css-properties.d.ts +2 -0
- package/dist/printer/preprocess/augment-with-css-properties.js +240 -0
- package/dist/printer/preprocess/augment-with-family.d.ts +2 -0
- package/dist/printer/preprocess/augment-with-family.js +13 -0
- package/dist/printer/preprocess/augment-with-parent.d.ts +2 -0
- package/dist/printer/preprocess/augment-with-parent.js +18 -0
- package/dist/printer/preprocess/augment-with-siblings.d.ts +1902 -0
- package/dist/printer/preprocess/augment-with-siblings.js +45 -0
- package/dist/printer/preprocess/augment-with-whitespace-helpers.d.ts +23 -0
- package/dist/printer/preprocess/augment-with-whitespace-helpers.js +460 -0
- package/dist/printer/preprocess/index.d.ts +1 -0
- package/dist/printer/preprocess/index.js +16 -0
- package/dist/printer/print/children.d.ts +6 -0
- package/dist/printer/print/children.js +283 -0
- package/dist/printer/print/element.d.ts +4 -0
- package/dist/printer/print/element.js +114 -0
- package/dist/printer/print/liquid.d.ts +14 -0
- package/dist/printer/print/liquid.js +637 -0
- package/dist/printer/print/tag.d.ts +23 -0
- package/dist/printer/print/tag.js +358 -0
- package/dist/printer/print-preprocess.d.ts +3 -0
- package/dist/printer/print-preprocess.js +48 -0
- package/dist/printer/printer-liquid-html.d.ts +13 -0
- package/dist/printer/printer-liquid-html.js +545 -0
- package/dist/printer/utils/array.d.ts +4 -0
- package/dist/printer/utils/array.js +19 -0
- package/dist/printer/utils/index.d.ts +14 -0
- package/dist/printer/utils/index.js +59 -0
- package/dist/printer/utils/node.d.ts +44 -0
- package/dist/printer/utils/node.js +270 -0
- package/dist/printer/utils/string.d.ts +15 -0
- package/dist/printer/utils/string.js +55 -0
- package/dist/types.d.ts +85 -0
- package/dist/types.js +53 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +35 -0
- package/package.json +65 -0
- package/standalone.js +19503 -0
|
@@ -0,0 +1,637 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.printLiquidVariableOutput = printLiquidVariableOutput;
|
|
4
|
+
exports.printLiquidBlockStart = printLiquidBlockStart;
|
|
5
|
+
exports.printLiquidBlockEnd = printLiquidBlockEnd;
|
|
6
|
+
exports.printLiquidTag = printLiquidTag;
|
|
7
|
+
exports.printLiquidRawTag = printLiquidRawTag;
|
|
8
|
+
exports.printLiquidDoc = printLiquidDoc;
|
|
9
|
+
exports.printLiquidDocParam = printLiquidDocParam;
|
|
10
|
+
exports.printLiquidDocExample = printLiquidDocExample;
|
|
11
|
+
exports.printLiquidDocDescription = printLiquidDocDescription;
|
|
12
|
+
exports.printLiquidDocPrompt = printLiquidDocPrompt;
|
|
13
|
+
exports.printLiquidBranch = printLiquidBranch;
|
|
14
|
+
const liquid_html_parser_1 = require("@platformos/liquid-html-parser");
|
|
15
|
+
const prettier_1 = require("prettier");
|
|
16
|
+
const utils_1 = require("../../utils");
|
|
17
|
+
const utils_2 = require("../utils");
|
|
18
|
+
const children_1 = require("./children");
|
|
19
|
+
const LIQUID_TAGS_THAT_ALWAYS_BREAK = ['for', 'case'];
|
|
20
|
+
const { builders, utils } = prettier_1.doc;
|
|
21
|
+
const { group, hardline, ifBreak, indent, join, line, softline, literalline } = builders;
|
|
22
|
+
const { replaceEndOfLine } = prettier_1.doc.utils;
|
|
23
|
+
function printLiquidVariableOutput(path, _options, print, { leadingSpaceGroupId, trailingSpaceGroupId }) {
|
|
24
|
+
const node = path.getValue();
|
|
25
|
+
const whitespaceStart = (0, utils_2.getWhitespaceTrim)(node.whitespaceStart, (0, utils_2.hasMeaningfulLackOfLeadingWhitespace)(node), leadingSpaceGroupId);
|
|
26
|
+
const whitespaceEnd = (0, utils_2.getWhitespaceTrim)(node.whitespaceEnd, (0, utils_2.hasMeaningfulLackOfTrailingWhitespace)(node), trailingSpaceGroupId);
|
|
27
|
+
if (typeof node.markup !== 'string') {
|
|
28
|
+
const whitespace = node.markup.filters.length > 0 ? line : ' ';
|
|
29
|
+
return group([
|
|
30
|
+
'{{',
|
|
31
|
+
whitespaceStart,
|
|
32
|
+
indent([whitespace, path.call((p) => print(p), 'markup')]),
|
|
33
|
+
whitespace,
|
|
34
|
+
whitespaceEnd,
|
|
35
|
+
'}}',
|
|
36
|
+
]);
|
|
37
|
+
}
|
|
38
|
+
// This should probably be better than this but it'll do for now.
|
|
39
|
+
const lines = (0, utils_2.markupLines)(node.markup);
|
|
40
|
+
if (lines.length > 1) {
|
|
41
|
+
return group([
|
|
42
|
+
'{{',
|
|
43
|
+
whitespaceStart,
|
|
44
|
+
indent([hardline, join(hardline, lines.map(utils_2.trim))]),
|
|
45
|
+
hardline,
|
|
46
|
+
whitespaceEnd,
|
|
47
|
+
'}}',
|
|
48
|
+
]);
|
|
49
|
+
}
|
|
50
|
+
return group(['{{', whitespaceStart, ' ', node.markup, ' ', whitespaceEnd, '}}']);
|
|
51
|
+
}
|
|
52
|
+
function printNamedLiquidBlockStart(path, _options, print, args, whitespaceStart, whitespaceEnd) {
|
|
53
|
+
const node = path.getValue();
|
|
54
|
+
const { isLiquidStatement } = args;
|
|
55
|
+
// This is slightly more verbose than 3 ternaries, but I feel like I
|
|
56
|
+
// should make it obvious that these three things work in tandem on the
|
|
57
|
+
// same conditional.
|
|
58
|
+
const { wrapper, prefix, suffix } = (() => {
|
|
59
|
+
if (isLiquidStatement) {
|
|
60
|
+
return {
|
|
61
|
+
wrapper: utils.removeLines,
|
|
62
|
+
prefix: '',
|
|
63
|
+
suffix: () => '',
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
return {
|
|
68
|
+
wrapper: group,
|
|
69
|
+
prefix: ['{%', whitespaceStart, ' '],
|
|
70
|
+
suffix: (trailingWhitespace) => [trailingWhitespace, whitespaceEnd, '%}'],
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
})();
|
|
74
|
+
const tag = (trailingWhitespace) => wrapper([
|
|
75
|
+
...prefix,
|
|
76
|
+
node.name,
|
|
77
|
+
' ',
|
|
78
|
+
indent(path.call((p) => print(p, args), 'markup')),
|
|
79
|
+
...suffix(trailingWhitespace),
|
|
80
|
+
]);
|
|
81
|
+
const tagWithArrayMarkup = (whitespace) => wrapper([
|
|
82
|
+
...prefix,
|
|
83
|
+
node.name,
|
|
84
|
+
' ',
|
|
85
|
+
indent([
|
|
86
|
+
join([',', line], path.map((p) => print(p, args), 'markup')),
|
|
87
|
+
]),
|
|
88
|
+
...suffix(whitespace),
|
|
89
|
+
]);
|
|
90
|
+
switch (node.name) {
|
|
91
|
+
case liquid_html_parser_1.NamedTags.echo: {
|
|
92
|
+
const trailingWhitespace = node.markup.filters.length > 0 ? line : ' ';
|
|
93
|
+
return tag(trailingWhitespace);
|
|
94
|
+
}
|
|
95
|
+
case liquid_html_parser_1.NamedTags.assign: {
|
|
96
|
+
const trailingWhitespace = node.markup.value.filters.length > 0 ? line : ' ';
|
|
97
|
+
return tag(trailingWhitespace);
|
|
98
|
+
}
|
|
99
|
+
case liquid_html_parser_1.NamedTags.hash_assign: {
|
|
100
|
+
const trailingWhitespace = node.markup.value.filters.length > 0 ? line : ' ';
|
|
101
|
+
return tag(trailingWhitespace);
|
|
102
|
+
}
|
|
103
|
+
case liquid_html_parser_1.NamedTags.cycle: {
|
|
104
|
+
const whitespace = node.markup.args.length > 1 ? line : ' ';
|
|
105
|
+
return wrapper([
|
|
106
|
+
...prefix,
|
|
107
|
+
node.name,
|
|
108
|
+
// We want to break after the groupName
|
|
109
|
+
node.markup.groupName ? ' ' : '',
|
|
110
|
+
indent(path.call((p) => print(p, args), 'markup')),
|
|
111
|
+
...suffix(whitespace),
|
|
112
|
+
]);
|
|
113
|
+
}
|
|
114
|
+
case liquid_html_parser_1.NamedTags.content_for: {
|
|
115
|
+
const markup = node.markup;
|
|
116
|
+
const trailingWhitespace = markup.args.length > 0 ? line : ' ';
|
|
117
|
+
return tag(trailingWhitespace);
|
|
118
|
+
}
|
|
119
|
+
case liquid_html_parser_1.NamedTags.include:
|
|
120
|
+
case liquid_html_parser_1.NamedTags.render: {
|
|
121
|
+
const markup = node.markup;
|
|
122
|
+
const trailingWhitespace = markup.args.length > 0 || (markup.variable && markup.alias) ? line : ' ';
|
|
123
|
+
return tag(trailingWhitespace);
|
|
124
|
+
}
|
|
125
|
+
case liquid_html_parser_1.NamedTags.function: {
|
|
126
|
+
const markup = node.markup;
|
|
127
|
+
const trailingWhitespace = markup.args.length > 0 ? line : ' ';
|
|
128
|
+
return tag(trailingWhitespace);
|
|
129
|
+
}
|
|
130
|
+
case liquid_html_parser_1.NamedTags.graphql: {
|
|
131
|
+
const markup = node.markup;
|
|
132
|
+
const trailingWhitespace = markup.args.length > 0 ? line : ' ';
|
|
133
|
+
return tag(trailingWhitespace);
|
|
134
|
+
}
|
|
135
|
+
case liquid_html_parser_1.NamedTags.capture:
|
|
136
|
+
case liquid_html_parser_1.NamedTags.increment:
|
|
137
|
+
case liquid_html_parser_1.NamedTags.decrement:
|
|
138
|
+
case liquid_html_parser_1.NamedTags.layout:
|
|
139
|
+
case liquid_html_parser_1.NamedTags.section: {
|
|
140
|
+
return tag(' ');
|
|
141
|
+
}
|
|
142
|
+
case liquid_html_parser_1.NamedTags.sections: {
|
|
143
|
+
return tag(' ');
|
|
144
|
+
}
|
|
145
|
+
case liquid_html_parser_1.NamedTags.form: {
|
|
146
|
+
const trailingWhitespace = node.markup.length > 1 ? line : ' ';
|
|
147
|
+
return tagWithArrayMarkup(trailingWhitespace);
|
|
148
|
+
}
|
|
149
|
+
case liquid_html_parser_1.NamedTags.tablerow:
|
|
150
|
+
case liquid_html_parser_1.NamedTags.for: {
|
|
151
|
+
const trailingWhitespace = node.markup.reversed || node.markup.args.length > 0 ? line : ' ';
|
|
152
|
+
return tag(trailingWhitespace);
|
|
153
|
+
}
|
|
154
|
+
case liquid_html_parser_1.NamedTags.paginate: {
|
|
155
|
+
return tag(line);
|
|
156
|
+
}
|
|
157
|
+
case liquid_html_parser_1.NamedTags.if:
|
|
158
|
+
case liquid_html_parser_1.NamedTags.elsif:
|
|
159
|
+
case liquid_html_parser_1.NamedTags.unless: {
|
|
160
|
+
const trailingWhitespace = [liquid_html_parser_1.NodeTypes.Comparison, liquid_html_parser_1.NodeTypes.LogicalExpression].includes(node.markup.type)
|
|
161
|
+
? line
|
|
162
|
+
: ' ';
|
|
163
|
+
return tag(trailingWhitespace);
|
|
164
|
+
}
|
|
165
|
+
case liquid_html_parser_1.NamedTags.case: {
|
|
166
|
+
return tag(' ');
|
|
167
|
+
}
|
|
168
|
+
case liquid_html_parser_1.NamedTags.when: {
|
|
169
|
+
const trailingWhitespace = node.markup.length > 1 ? line : ' ';
|
|
170
|
+
return tagWithArrayMarkup(trailingWhitespace);
|
|
171
|
+
}
|
|
172
|
+
case liquid_html_parser_1.NamedTags.liquid: {
|
|
173
|
+
return group([
|
|
174
|
+
...prefix,
|
|
175
|
+
node.name,
|
|
176
|
+
indent([
|
|
177
|
+
hardline,
|
|
178
|
+
join(hardline, path.map((p) => {
|
|
179
|
+
const curr = p.getValue();
|
|
180
|
+
return [
|
|
181
|
+
getSpaceBetweenLines(curr.prev, curr),
|
|
182
|
+
print(p, { ...args, isLiquidStatement: true }),
|
|
183
|
+
];
|
|
184
|
+
}, 'markup')),
|
|
185
|
+
]),
|
|
186
|
+
...suffix(hardline),
|
|
187
|
+
]);
|
|
188
|
+
}
|
|
189
|
+
// platformos tags
|
|
190
|
+
case liquid_html_parser_1.NamedTags.background: {
|
|
191
|
+
const markup = node.markup;
|
|
192
|
+
const trailingWhitespace = markup.args.length > 0 ? line : ' ';
|
|
193
|
+
return tag(trailingWhitespace);
|
|
194
|
+
}
|
|
195
|
+
case liquid_html_parser_1.NamedTags.cache: {
|
|
196
|
+
const trailingWhitespace = node.markup.args.length > 0 ? line : ' ';
|
|
197
|
+
return tag(trailingWhitespace);
|
|
198
|
+
}
|
|
199
|
+
case liquid_html_parser_1.NamedTags.context:
|
|
200
|
+
case liquid_html_parser_1.NamedTags.sign_in:
|
|
201
|
+
case liquid_html_parser_1.NamedTags.transaction: {
|
|
202
|
+
const trailingWhitespace = node.markup.length > 0 ? line : ' ';
|
|
203
|
+
return tagWithArrayMarkup(trailingWhitespace);
|
|
204
|
+
}
|
|
205
|
+
case liquid_html_parser_1.NamedTags.export: {
|
|
206
|
+
const trailingWhitespace = node.markup.variables.length > 1 || node.markup.namespace ? line : ' ';
|
|
207
|
+
return tag(trailingWhitespace);
|
|
208
|
+
}
|
|
209
|
+
case liquid_html_parser_1.NamedTags.include_form:
|
|
210
|
+
case liquid_html_parser_1.NamedTags.spam_protection:
|
|
211
|
+
case liquid_html_parser_1.NamedTags.theme_render_rc: {
|
|
212
|
+
const trailingWhitespace = node.markup.args.length > 0 ? line : ' ';
|
|
213
|
+
return tag(trailingWhitespace);
|
|
214
|
+
}
|
|
215
|
+
case liquid_html_parser_1.NamedTags.log:
|
|
216
|
+
case liquid_html_parser_1.NamedTags.redirect_to: {
|
|
217
|
+
const trailingWhitespace = node.markup.args.length > 0 ? line : ' ';
|
|
218
|
+
return tag(trailingWhitespace);
|
|
219
|
+
}
|
|
220
|
+
case liquid_html_parser_1.NamedTags.parse_json:
|
|
221
|
+
case liquid_html_parser_1.NamedTags.catch: {
|
|
222
|
+
return tag(' ');
|
|
223
|
+
}
|
|
224
|
+
case liquid_html_parser_1.NamedTags.print:
|
|
225
|
+
case liquid_html_parser_1.NamedTags.return:
|
|
226
|
+
case liquid_html_parser_1.NamedTags.yield:
|
|
227
|
+
case liquid_html_parser_1.NamedTags.response_headers:
|
|
228
|
+
case liquid_html_parser_1.NamedTags.response_status: {
|
|
229
|
+
return tag(' ');
|
|
230
|
+
}
|
|
231
|
+
case liquid_html_parser_1.NamedTags.session: {
|
|
232
|
+
return tag(' ');
|
|
233
|
+
}
|
|
234
|
+
case liquid_html_parser_1.NamedTags.rollback:
|
|
235
|
+
case liquid_html_parser_1.NamedTags.try: {
|
|
236
|
+
return tag('');
|
|
237
|
+
}
|
|
238
|
+
default: {
|
|
239
|
+
return (0, utils_1.assertNever)(node);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function printLiquidStatement(path, _options, _print, _args) {
|
|
244
|
+
const node = path.getValue();
|
|
245
|
+
const shouldSkipLeadingSpace = node.markup.trim() === '' || (node.name === '#' && node.markup.startsWith('#'));
|
|
246
|
+
return prettier_1.doc.utils.removeLines([node.name, shouldSkipLeadingSpace ? '' : ' ', node.markup]);
|
|
247
|
+
}
|
|
248
|
+
function printLiquidBlockStart(path, options, print, args = {}) {
|
|
249
|
+
const node = path.getValue();
|
|
250
|
+
const { leadingSpaceGroupId, trailingSpaceGroupId } = args;
|
|
251
|
+
if (!node.name)
|
|
252
|
+
return '';
|
|
253
|
+
const whitespaceStart = (0, utils_2.getWhitespaceTrim)(node.whitespaceStart, needsBlockStartLeadingWhitespaceStrippingOnBreak(node), leadingSpaceGroupId);
|
|
254
|
+
const whitespaceEnd = (0, utils_2.getWhitespaceTrim)(node.whitespaceEnd, needsBlockStartTrailingWhitespaceStrippingOnBreak(node), trailingSpaceGroupId);
|
|
255
|
+
if (typeof node.markup !== 'string') {
|
|
256
|
+
return printNamedLiquidBlockStart(path, options, print, args, whitespaceStart, whitespaceEnd);
|
|
257
|
+
}
|
|
258
|
+
if (args.isLiquidStatement) {
|
|
259
|
+
return printLiquidStatement(path, options, print, args);
|
|
260
|
+
}
|
|
261
|
+
const lines = (0, utils_2.markupLines)(node.markup);
|
|
262
|
+
if (node.name === 'liquid') {
|
|
263
|
+
return group([
|
|
264
|
+
'{%',
|
|
265
|
+
whitespaceStart,
|
|
266
|
+
' ',
|
|
267
|
+
node.name,
|
|
268
|
+
indent([hardline, join(hardline, (0, utils_2.reindent)(lines, true))]),
|
|
269
|
+
hardline,
|
|
270
|
+
whitespaceEnd,
|
|
271
|
+
'%}',
|
|
272
|
+
]);
|
|
273
|
+
}
|
|
274
|
+
if (lines.length > 1) {
|
|
275
|
+
return group([
|
|
276
|
+
'{%',
|
|
277
|
+
whitespaceStart,
|
|
278
|
+
indent([hardline, node.name, ' ', join(hardline, lines.map(utils_2.trim))]),
|
|
279
|
+
hardline,
|
|
280
|
+
whitespaceEnd,
|
|
281
|
+
'%}',
|
|
282
|
+
]);
|
|
283
|
+
}
|
|
284
|
+
const markup = node.markup;
|
|
285
|
+
return group([
|
|
286
|
+
'{%',
|
|
287
|
+
whitespaceStart,
|
|
288
|
+
' ',
|
|
289
|
+
node.name,
|
|
290
|
+
markup ? ` ${markup}` : '',
|
|
291
|
+
' ',
|
|
292
|
+
whitespaceEnd,
|
|
293
|
+
'%}',
|
|
294
|
+
]);
|
|
295
|
+
}
|
|
296
|
+
function printLiquidBlockEnd(path, _options, _print, args = {}) {
|
|
297
|
+
const node = path.getValue();
|
|
298
|
+
const { isLiquidStatement, leadingSpaceGroupId, trailingSpaceGroupId } = args;
|
|
299
|
+
if (!node.children || !node.blockEndPosition)
|
|
300
|
+
return '';
|
|
301
|
+
if (isLiquidStatement) {
|
|
302
|
+
return ['end', node.name];
|
|
303
|
+
}
|
|
304
|
+
const whitespaceStart = (0, utils_2.getWhitespaceTrim)(node.delimiterWhitespaceStart ?? '', needsBlockEndLeadingWhitespaceStrippingOnBreak(node), leadingSpaceGroupId);
|
|
305
|
+
const whitespaceEnd = (0, utils_2.getWhitespaceTrim)(node.delimiterWhitespaceEnd ?? '', (0, utils_2.hasMeaningfulLackOfTrailingWhitespace)(node), trailingSpaceGroupId);
|
|
306
|
+
return group(['{%', whitespaceStart, ` end${node.name} `, whitespaceEnd, '%}']);
|
|
307
|
+
}
|
|
308
|
+
function getNodeContent(node) {
|
|
309
|
+
if (!node.children || !node.blockEndPosition)
|
|
310
|
+
return '';
|
|
311
|
+
return node.source.slice(node.blockStartPosition.end, node.blockEndPosition.start);
|
|
312
|
+
}
|
|
313
|
+
function printLiquidTag(path, options, print, args) {
|
|
314
|
+
const { leadingSpaceGroupId, trailingSpaceGroupId } = args;
|
|
315
|
+
const node = path.getValue();
|
|
316
|
+
if (!node.children || !node.blockEndPosition) {
|
|
317
|
+
return printLiquidBlockStart(path, options, print, args);
|
|
318
|
+
}
|
|
319
|
+
if (!args.isLiquidStatement && (0, utils_2.shouldPreserveContent)(node)) {
|
|
320
|
+
return [
|
|
321
|
+
printLiquidBlockStart(path, options, print, {
|
|
322
|
+
...args,
|
|
323
|
+
leadingSpaceGroupId,
|
|
324
|
+
trailingSpaceGroupId: utils_2.FORCE_FLAT_GROUP_ID,
|
|
325
|
+
}),
|
|
326
|
+
...replaceEndOfLine(getNodeContent(node)),
|
|
327
|
+
printLiquidBlockEnd(path, options, print, {
|
|
328
|
+
...args,
|
|
329
|
+
leadingSpaceGroupId: utils_2.FORCE_FLAT_GROUP_ID,
|
|
330
|
+
trailingSpaceGroupId,
|
|
331
|
+
}),
|
|
332
|
+
];
|
|
333
|
+
}
|
|
334
|
+
const tagGroupId = Symbol('tag-group');
|
|
335
|
+
const blockStart = printLiquidBlockStart(path, options, print, {
|
|
336
|
+
...args,
|
|
337
|
+
leadingSpaceGroupId,
|
|
338
|
+
trailingSpaceGroupId: tagGroupId,
|
|
339
|
+
}); // {% if ... %}
|
|
340
|
+
const blockEnd = printLiquidBlockEnd(path, options, print, {
|
|
341
|
+
...args,
|
|
342
|
+
leadingSpaceGroupId: tagGroupId,
|
|
343
|
+
trailingSpaceGroupId,
|
|
344
|
+
}); // {% endif %}
|
|
345
|
+
let body = [];
|
|
346
|
+
if ((0, liquid_html_parser_1.isBranchedTag)(node)) {
|
|
347
|
+
body = cleanDoc(path.map((p) => print(p, {
|
|
348
|
+
...args,
|
|
349
|
+
leadingSpaceGroupId: tagGroupId,
|
|
350
|
+
trailingSpaceGroupId: tagGroupId,
|
|
351
|
+
}), 'children'));
|
|
352
|
+
if (node.name === 'case')
|
|
353
|
+
body = indent(body);
|
|
354
|
+
}
|
|
355
|
+
else if (node.children.length > 0) {
|
|
356
|
+
body = indent([
|
|
357
|
+
innerLeadingWhitespace(node),
|
|
358
|
+
(0, children_1.printChildren)(path, options, print, {
|
|
359
|
+
...args,
|
|
360
|
+
leadingSpaceGroupId: tagGroupId,
|
|
361
|
+
trailingSpaceGroupId: tagGroupId,
|
|
362
|
+
}),
|
|
363
|
+
]);
|
|
364
|
+
}
|
|
365
|
+
return group([blockStart, body, innerTrailingWhitespace(node, args), blockEnd], {
|
|
366
|
+
id: tagGroupId,
|
|
367
|
+
shouldBreak: LIQUID_TAGS_THAT_ALWAYS_BREAK.includes(node.name) ||
|
|
368
|
+
(0, utils_2.originallyHadLineBreaks)(path, options) ||
|
|
369
|
+
(0, utils_2.isAttributeNode)(node) ||
|
|
370
|
+
(0, utils_2.isDeeplyNested)(node),
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
function printLiquidRawTag(path, options, print, { isLiquidStatement }) {
|
|
374
|
+
let body = [];
|
|
375
|
+
const node = path.getValue();
|
|
376
|
+
const hasEmptyBody = node.body.value.trim() === '';
|
|
377
|
+
const shouldPrintAsIs = node.isIndentationSensitive ||
|
|
378
|
+
!(0, utils_2.hasLineBreakInRange)(node.source, node.body.position.start, node.body.position.end);
|
|
379
|
+
const blockStart = isLiquidStatement
|
|
380
|
+
? [node.name]
|
|
381
|
+
: group([
|
|
382
|
+
'{%',
|
|
383
|
+
node.whitespaceStart,
|
|
384
|
+
' ',
|
|
385
|
+
node.name,
|
|
386
|
+
' ',
|
|
387
|
+
node.markup ? `${node.markup} ` : '',
|
|
388
|
+
node.whitespaceEnd,
|
|
389
|
+
'%}',
|
|
390
|
+
]);
|
|
391
|
+
const blockEnd = isLiquidStatement
|
|
392
|
+
? ['end', node.name]
|
|
393
|
+
: ['{%', node.whitespaceStart, ' ', 'end', node.name, ' ', node.whitespaceEnd, '%}'];
|
|
394
|
+
if (shouldPrintAsIs) {
|
|
395
|
+
body = [node.source.slice(node.blockStartPosition.end, node.blockEndPosition.start)];
|
|
396
|
+
}
|
|
397
|
+
else if (hasEmptyBody) {
|
|
398
|
+
body = [hardline];
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
body = [path.call((p) => print(p), 'body')];
|
|
402
|
+
}
|
|
403
|
+
return [blockStart, ...body, blockEnd];
|
|
404
|
+
}
|
|
405
|
+
function printLiquidDoc(path, _options, print, _args) {
|
|
406
|
+
const nodes = path.map((p) => print(p), 'nodes');
|
|
407
|
+
if (nodes.length === 0)
|
|
408
|
+
return [];
|
|
409
|
+
const lines = [nodes[0]];
|
|
410
|
+
for (let i = 1; i < nodes.length; i++) {
|
|
411
|
+
lines.push(hardline);
|
|
412
|
+
// If the tag name is different from the previous one, add an extra line break
|
|
413
|
+
if (nodes[i - 1][0] !== nodes[i][0]) {
|
|
414
|
+
lines.push(hardline);
|
|
415
|
+
}
|
|
416
|
+
lines.push(nodes[i]);
|
|
417
|
+
}
|
|
418
|
+
return [indent([hardline, lines]), hardline];
|
|
419
|
+
}
|
|
420
|
+
function printLiquidDocParam(path, options, _print, _args) {
|
|
421
|
+
const node = path.getValue();
|
|
422
|
+
const parts = ['@param'];
|
|
423
|
+
if (node.paramType?.value) {
|
|
424
|
+
parts.push(' ', `{${node.paramType.value}}`);
|
|
425
|
+
}
|
|
426
|
+
if (node.required) {
|
|
427
|
+
parts.push(' ', node.paramName.value);
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
parts.push(' ', `[${node.paramName.value}]`);
|
|
431
|
+
}
|
|
432
|
+
if (node.paramDescription?.value) {
|
|
433
|
+
const normalizedDescription = node.paramDescription.value.replace(/\s+/g, ' ').trim();
|
|
434
|
+
if (options.liquidDocParamDash) {
|
|
435
|
+
parts.push(' - ', normalizedDescription);
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
parts.push(' ', normalizedDescription);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return parts;
|
|
442
|
+
}
|
|
443
|
+
function printLiquidDocExample(path, options, _print, _args) {
|
|
444
|
+
const node = path.getValue();
|
|
445
|
+
const parts = ['@example'];
|
|
446
|
+
const content = node.content.value;
|
|
447
|
+
if (content.trimEnd().includes('\n') || !node.isInline) {
|
|
448
|
+
parts.push(hardline);
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
parts.push(' ');
|
|
452
|
+
}
|
|
453
|
+
parts.push(content.trim());
|
|
454
|
+
return parts;
|
|
455
|
+
}
|
|
456
|
+
function printLiquidDocDescription(path, options, _print, _args) {
|
|
457
|
+
const node = path.getValue();
|
|
458
|
+
const parts = [];
|
|
459
|
+
const content = node.content.value;
|
|
460
|
+
if (node.isImplicit) {
|
|
461
|
+
parts.push(content.trim());
|
|
462
|
+
return parts;
|
|
463
|
+
}
|
|
464
|
+
parts.push('@description');
|
|
465
|
+
if (content.trimEnd().includes('\n') || !node.isInline) {
|
|
466
|
+
parts.push(hardline);
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
parts.push(' ');
|
|
470
|
+
}
|
|
471
|
+
parts.push(content.trim());
|
|
472
|
+
return parts;
|
|
473
|
+
}
|
|
474
|
+
// This is a platform controlled tag, so we don't really want to modify this at all to preserve the additional indent
|
|
475
|
+
// This DOES mean we won't fix the formatting if a developer were to manually modify the @prompt.
|
|
476
|
+
function printLiquidDocPrompt(path, options, _print, _args) {
|
|
477
|
+
const node = path.getValue();
|
|
478
|
+
return ['@prompt', node.content.value.trimEnd()];
|
|
479
|
+
}
|
|
480
|
+
function innerLeadingWhitespace(node) {
|
|
481
|
+
if (!node.firstChild) {
|
|
482
|
+
if (node.isDanglingWhitespaceSensitive && node.hasDanglingWhitespace) {
|
|
483
|
+
return line;
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
return '';
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
if (node.firstChild.hasLeadingWhitespace && node.firstChild.isLeadingWhitespaceSensitive) {
|
|
490
|
+
return line;
|
|
491
|
+
}
|
|
492
|
+
return softline;
|
|
493
|
+
}
|
|
494
|
+
function innerTrailingWhitespace(node, args) {
|
|
495
|
+
if ((!args.isLiquidStatement && (0, utils_2.shouldPreserveContent)(node)) ||
|
|
496
|
+
node.type === liquid_html_parser_1.NodeTypes.LiquidBranch ||
|
|
497
|
+
!node.blockEndPosition ||
|
|
498
|
+
!node.lastChild) {
|
|
499
|
+
return '';
|
|
500
|
+
}
|
|
501
|
+
if (node.lastChild.hasTrailingWhitespace && node.lastChild.isTrailingWhitespaceSensitive) {
|
|
502
|
+
return line;
|
|
503
|
+
}
|
|
504
|
+
return softline;
|
|
505
|
+
}
|
|
506
|
+
function printLiquidDefaultBranch(path, options, print, args) {
|
|
507
|
+
const branch = path.getValue();
|
|
508
|
+
const parentNode = path.getParentNode();
|
|
509
|
+
// When the node is empty and the parent is empty. The space will come
|
|
510
|
+
// from the trailingWhitespace of the parent. When this happens, we don't
|
|
511
|
+
// want the branch to print another one so we collapse it.
|
|
512
|
+
// e.g. {% if A %} {% endif %}
|
|
513
|
+
const shouldCollapseSpace = (0, utils_2.isEmpty)(branch.children) && parentNode.children.length === 1;
|
|
514
|
+
if (shouldCollapseSpace)
|
|
515
|
+
return '';
|
|
516
|
+
// When the branch is empty and doesn't have whitespace, we don't want
|
|
517
|
+
// anything so print nothing.
|
|
518
|
+
// e.g. {% if A %}{% endif %}
|
|
519
|
+
// e.g. {% if A %}{% else %}...{% endif %}
|
|
520
|
+
const isBranchEmptyWithoutSpace = (0, utils_2.isEmpty)(branch.children) && !branch.hasDanglingWhitespace;
|
|
521
|
+
if (isBranchEmptyWithoutSpace)
|
|
522
|
+
return '';
|
|
523
|
+
// If the branch does not break, is empty and had whitespace, we might
|
|
524
|
+
// want a space in there. We don't collapse those because the trailing
|
|
525
|
+
// whitespace does not come from the parent.
|
|
526
|
+
// {% if A %} {% else %}...{% endif %}
|
|
527
|
+
if (branch.hasDanglingWhitespace) {
|
|
528
|
+
return ifBreak('', ' ');
|
|
529
|
+
}
|
|
530
|
+
const shouldAddTrailingNewline = branch.next &&
|
|
531
|
+
branch.children.length > 0 &&
|
|
532
|
+
branch.source
|
|
533
|
+
.slice((0, utils_2.last)(branch.children).position.end, branch.next.position.start)
|
|
534
|
+
.replace(/ |\t/g, '').length >= 2;
|
|
535
|
+
// Otherwise print the branch as usual
|
|
536
|
+
// {% if A %} content...{% endif %}
|
|
537
|
+
return indent([
|
|
538
|
+
innerLeadingWhitespace(parentNode),
|
|
539
|
+
(0, children_1.printChildren)(path, options, print, args),
|
|
540
|
+
shouldAddTrailingNewline ? literalline : '',
|
|
541
|
+
]);
|
|
542
|
+
}
|
|
543
|
+
function printLiquidBranch(path, options, print, args) {
|
|
544
|
+
const branch = path.getValue();
|
|
545
|
+
const isDefaultBranch = !branch.name;
|
|
546
|
+
if (isDefaultBranch) {
|
|
547
|
+
return printLiquidDefaultBranch(path, options, print, args);
|
|
548
|
+
}
|
|
549
|
+
const leftSibling = branch.prev;
|
|
550
|
+
// When the left sibling is empty, its trailing whitespace is its leading
|
|
551
|
+
// whitespace. So we should collapse it here and ignore it.
|
|
552
|
+
const shouldCollapseSpace = leftSibling && (0, utils_2.isEmpty)(leftSibling.children);
|
|
553
|
+
const outerLeadingWhitespace = branch.hasLeadingWhitespace && !shouldCollapseSpace ? line : softline;
|
|
554
|
+
const shouldAddTrailingNewline = branch.next &&
|
|
555
|
+
branch.children.length > 0 &&
|
|
556
|
+
branch.source
|
|
557
|
+
.slice((0, utils_2.last)(branch.children).position.end, branch.next.position.start)
|
|
558
|
+
.replace(/ |\t/g, '').length >= 2;
|
|
559
|
+
return [
|
|
560
|
+
outerLeadingWhitespace,
|
|
561
|
+
printLiquidBlockStart(path, options, print, args),
|
|
562
|
+
indent([
|
|
563
|
+
innerLeadingWhitespace(branch),
|
|
564
|
+
(0, children_1.printChildren)(path, options, print, args),
|
|
565
|
+
shouldAddTrailingNewline ? literalline : '',
|
|
566
|
+
]),
|
|
567
|
+
];
|
|
568
|
+
}
|
|
569
|
+
function needsBlockStartLeadingWhitespaceStrippingOnBreak(node) {
|
|
570
|
+
switch (node.type) {
|
|
571
|
+
case liquid_html_parser_1.NodeTypes.LiquidTag: {
|
|
572
|
+
return !(0, utils_2.isAttributeNode)(node) && (0, utils_2.hasMeaningfulLackOfLeadingWhitespace)(node);
|
|
573
|
+
}
|
|
574
|
+
case liquid_html_parser_1.NodeTypes.LiquidBranch: {
|
|
575
|
+
return (!(0, utils_2.isAttributeNode)(node.parentNode) &&
|
|
576
|
+
(0, utils_2.hasMeaningfulLackOfLeadingWhitespace)(node));
|
|
577
|
+
}
|
|
578
|
+
default: {
|
|
579
|
+
return (0, utils_1.assertNever)(node);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
function needsBlockStartTrailingWhitespaceStrippingOnBreak(node) {
|
|
584
|
+
switch (node.type) {
|
|
585
|
+
case liquid_html_parser_1.NodeTypes.LiquidTag: {
|
|
586
|
+
if ((0, liquid_html_parser_1.isBranchedTag)(node)) {
|
|
587
|
+
return needsBlockStartLeadingWhitespaceStrippingOnBreak(node.firstChild);
|
|
588
|
+
}
|
|
589
|
+
if (!node.children) {
|
|
590
|
+
return (0, utils_2.hasMeaningfulLackOfTrailingWhitespace)(node);
|
|
591
|
+
}
|
|
592
|
+
return (0, utils_2.isEmpty)(node.children)
|
|
593
|
+
? (0, utils_2.hasMeaningfulLackOfDanglingWhitespace)(node)
|
|
594
|
+
: (0, utils_2.hasMeaningfulLackOfLeadingWhitespace)(node.firstChild);
|
|
595
|
+
}
|
|
596
|
+
case liquid_html_parser_1.NodeTypes.LiquidBranch: {
|
|
597
|
+
if ((0, utils_2.isAttributeNode)(node.parentNode)) {
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
600
|
+
return node.firstChild
|
|
601
|
+
? (0, utils_2.hasMeaningfulLackOfLeadingWhitespace)(node.firstChild)
|
|
602
|
+
: (0, utils_2.hasMeaningfulLackOfDanglingWhitespace)(node);
|
|
603
|
+
}
|
|
604
|
+
default: {
|
|
605
|
+
return (0, utils_1.assertNever)(node);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
function needsBlockEndLeadingWhitespaceStrippingOnBreak(node) {
|
|
610
|
+
if (!node.children) {
|
|
611
|
+
throw new Error('Should only call needsBlockEndLeadingWhitespaceStrippingOnBreak for tags that have closing tags');
|
|
612
|
+
}
|
|
613
|
+
else if ((0, utils_2.isAttributeNode)(node)) {
|
|
614
|
+
return false;
|
|
615
|
+
}
|
|
616
|
+
else if ((0, liquid_html_parser_1.isBranchedTag)(node)) {
|
|
617
|
+
return (0, utils_2.hasMeaningfulLackOfTrailingWhitespace)(node.lastChild);
|
|
618
|
+
}
|
|
619
|
+
else if ((0, utils_2.isEmpty)(node.children)) {
|
|
620
|
+
return (0, utils_2.hasMeaningfulLackOfDanglingWhitespace)(node);
|
|
621
|
+
}
|
|
622
|
+
else {
|
|
623
|
+
return (0, utils_2.hasMeaningfulLackOfTrailingWhitespace)(node.lastChild);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
function cleanDoc(doc) {
|
|
627
|
+
return doc.filter((x) => x !== '');
|
|
628
|
+
}
|
|
629
|
+
function getSpaceBetweenLines(prev, curr) {
|
|
630
|
+
if (!prev)
|
|
631
|
+
return '';
|
|
632
|
+
const source = curr.source;
|
|
633
|
+
const whitespaceBetweenNodes = source.slice(prev.position.end, curr.position.start);
|
|
634
|
+
const hasMoreThanOneNewLine = (whitespaceBetweenNodes.match(/\n/g) || []).length > 1;
|
|
635
|
+
return hasMoreThanOneNewLine ? hardline : '';
|
|
636
|
+
}
|
|
637
|
+
//# sourceMappingURL=liquid.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Position } from '@platformos/liquid-html-parser';
|
|
2
|
+
import { doc } from 'prettier';
|
|
3
|
+
import { AstPath, HtmlNode, LiquidHtmlNode, LiquidParserOptions, LiquidPrinter } from '../../types';
|
|
4
|
+
export declare function needsToBorrowPrevClosingTagEndMarker(node: LiquidHtmlNode): boolean | undefined;
|
|
5
|
+
export declare function needsToBorrowLastChildClosingTagEndMarker(node: LiquidHtmlNode): boolean | undefined;
|
|
6
|
+
export declare function needsToBorrowParentClosingTagStartMarker(node: LiquidHtmlNode): boolean;
|
|
7
|
+
export declare function needsToBorrowNextOpeningTagStartMarker(node: LiquidHtmlNode): boolean | undefined;
|
|
8
|
+
export declare function needsToBorrowParentOpeningTagEndMarker(node: LiquidHtmlNode): boolean;
|
|
9
|
+
export declare function printOpeningTag(path: AstPath<HtmlNode>, options: LiquidParserOptions, print: LiquidPrinter, attrGroupId: symbol): (string | (string | doc.builders.Line | doc.builders.Indent)[])[];
|
|
10
|
+
export declare function printOpeningTagStart(node: LiquidHtmlNode, options: LiquidParserOptions): string[] | "";
|
|
11
|
+
export declare function printOpeningTagPrefix(node: LiquidHtmlNode, options: LiquidParserOptions): "" | "-->" | ">" | "/>";
|
|
12
|
+
export declare function printOpeningTagStartMarker(node: LiquidHtmlNode | undefined): string;
|
|
13
|
+
export declare function printClosingTag(node: LiquidHtmlNode, options: LiquidParserOptions): (string | string[])[];
|
|
14
|
+
export declare function printClosingTagStart(node: LiquidHtmlNode, options: LiquidParserOptions): string[] | "";
|
|
15
|
+
export declare function printClosingTagEnd(node: LiquidHtmlNode, options: LiquidParserOptions): string[] | "";
|
|
16
|
+
export declare function printClosingTagSuffix(node: LiquidHtmlNode, options: LiquidParserOptions): string;
|
|
17
|
+
export declare function printClosingTagStartMarker(node: LiquidHtmlNode | undefined, options: LiquidParserOptions): string;
|
|
18
|
+
export declare function printClosingTagEndMarker(node: LiquidHtmlNode | undefined, options: LiquidParserOptions): "" | ">" | "/>";
|
|
19
|
+
export declare function printOpeningTagEndMarker(node: LiquidHtmlNode | undefined): "" | "-->" | ">";
|
|
20
|
+
export declare function getNodeContent(node: Extract<HtmlNode, {
|
|
21
|
+
blockStartPosition: Position;
|
|
22
|
+
blockEndPosition: Position;
|
|
23
|
+
}>, options: LiquidParserOptions): string;
|