-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathnode.ts
More file actions
417 lines (362 loc) · 12.3 KB
/
Copy pathnode.ts
File metadata and controls
417 lines (362 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import { NodeTypes, LiquidNodeTypes, HtmlNodeTypes, Position } from '@shopify/liquid-html-parser';
import {
HtmlSelfClosingElement,
LiquidHtmlNode,
TextNode,
LiquidNode,
HtmlNode,
HtmlVoidElement,
HtmlComment,
HtmlElement,
LiquidTag,
AttributeNode,
LiquidVariableOutput,
HtmlDanglingMarkerClose,
} from '../../types';
import { isEmpty } from './array';
export function isScriptLikeTag(node: { type: NodeTypes }) {
return node.type === NodeTypes.HtmlRawNode;
}
export function isPreLikeNode(node: { cssWhitespace: string }) {
return node.cssWhitespace.startsWith('pre');
}
// A bit like self-closing except we distinguish between them.
// Comments are also considered self-closing.
export function hasNoCloseMarker(
node: LiquidHtmlNode,
): node is
| HtmlComment
| HtmlVoidElement
| HtmlSelfClosingElement
| HtmlDanglingMarkerClose
| HtmlElement {
return hasNoChildren(node) || isHtmlDanglingMarkerOpen(node);
}
export function hasNoChildren(
node: LiquidHtmlNode,
): node is HtmlComment | HtmlVoidElement | HtmlSelfClosingElement | HtmlDanglingMarkerClose {
return (
isSelfClosing(node) ||
isVoidElement(node) ||
isHtmlComment(node) ||
isHtmlDanglingMarkerClose(node)
);
}
export function isHtmlDanglingMarkerOpen(
node: LiquidHtmlNode,
): node is Omit<HtmlElement, 'blockEndPosition'> & { blockEndPosition: Position } {
return (
node.type === NodeTypes.HtmlElement && node.blockEndPosition.start === node.blockEndPosition.end
);
}
export function isHtmlDanglingMarkerClose(node: LiquidHtmlNode): node is HtmlDanglingMarkerClose {
return node.type === NodeTypes.HtmlDanglingMarkerClose;
}
export function isHtmlComment(node: LiquidHtmlNode): node is HtmlComment {
return node.type === NodeTypes.HtmlComment;
}
export function isSelfClosing(node: LiquidHtmlNode): node is HtmlSelfClosingElement {
return node.type === NodeTypes.HtmlSelfClosingElement;
}
export function isVoidElement(node: LiquidHtmlNode): node is HtmlVoidElement {
return node.type === NodeTypes.HtmlVoidElement;
}
export function isHtmlElement(node: LiquidHtmlNode): node is HtmlElement {
return node.type === NodeTypes.HtmlElement;
}
export function isTextLikeNode(node: LiquidHtmlNode | undefined): node is TextNode {
return !!node && node.type === NodeTypes.TextNode;
}
export function isLiquidNode(node: LiquidHtmlNode | undefined): node is LiquidNode {
return !!node && LiquidNodeTypes.includes(node.type as any);
}
export function isMultilineLiquidTag(node: LiquidHtmlNode | undefined): node is LiquidTag {
return !!node && node.type === NodeTypes.LiquidTag && !!node.children && !isEmpty(node.children);
}
export function isHtmlNode(node: LiquidHtmlNode | undefined): node is HtmlNode {
return !!node && HtmlNodeTypes.includes(node.type as any);
}
export function isAttributeNode(
node: LiquidHtmlNode,
): node is AttributeNode & { parentNode: HtmlNode } {
return (
isHtmlNode(node.parentNode) &&
'attributes' in node.parentNode &&
node.parentNode.attributes.indexOf(node as AttributeNode) !== -1
);
}
export function hasNonTextChild(node: LiquidHtmlNode) {
return (
(node as any).children &&
(node as any).children.some((child: LiquidHtmlNode) => child.type !== NodeTypes.TextNode)
);
}
export function shouldPreserveContent(node: LiquidHtmlNode) {
// // unterminated node in ie conditional comment
// // e.g. <!--[if lt IE 9]><html><![endif]-->
// if (
// node.type === "ieConditionalComment" &&
// node.lastChild &&
// !node.lastChild.isSelfClosing &&
// !node.lastChild.endSourceSpan
// ) {
// return true;
// }
// // incomplete html in ie conditional comment
// // e.g. <!--[if lt IE 9]></div><![endif]-->
// if (node.type === "ieConditionalComment" && !node.complete) {
// return true;
// }
// TODO: Handle pre correctly?
if (isPreLikeNode(node)) {
return true;
}
return false;
}
export function isPrettierIgnoreHtmlNode(node: LiquidHtmlNode | undefined): node is HtmlComment {
return (
!!node && node.type === NodeTypes.HtmlComment && /^\s*prettier-ignore(?=\s|$)/m.test(node.body)
);
}
export function isPrettierIgnoreLiquidNode(node: LiquidHtmlNode | undefined): node is LiquidTag {
return (
!!node &&
node.type === NodeTypes.LiquidTag &&
node.name === '#' &&
/^\s*prettier-ignore(?=\s|$)/m.test(node.markup)
);
}
export function isPrettierIgnoreNode(
node: LiquidHtmlNode | undefined,
): node is HtmlComment | LiquidTag {
return isPrettierIgnoreLiquidNode(node) || isPrettierIgnoreHtmlNode(node);
}
export function hasPrettierIgnore(node: LiquidHtmlNode) {
return (
isPrettierIgnoreNode(node) ||
isPrettierIgnoreNode(node.prev) ||
isPrettierIgnoreRangeStartNode(node) ||
isPrettierIgnoreRangeEndNode(node) ||
isInsidePrettierIgnoreRange(node)
);
}
export function isPrettierIgnoreRangeStartHtmlNode(
node: LiquidHtmlNode | undefined,
): node is HtmlComment {
return (
!!node &&
node.type === NodeTypes.HtmlComment &&
/^\s*prettier-ignore-start(?=\s|$)/m.test(node.body)
);
}
export function isPrettierIgnoreRangeStartLiquidNode(
node: LiquidHtmlNode | undefined,
): node is LiquidTag {
return (
!!node &&
node.type === NodeTypes.LiquidTag &&
node.name === '#' &&
/^\s*prettier-ignore-start(?=\s|$)/m.test(node.markup)
);
}
export function isPrettierIgnoreRangeStartNode(
node: LiquidHtmlNode | undefined,
): node is HtmlComment | LiquidTag {
return isPrettierIgnoreRangeStartHtmlNode(node) || isPrettierIgnoreRangeStartLiquidNode(node);
}
export function isPrettierIgnoreRangeEndHtmlNode(
node: LiquidHtmlNode | undefined,
): node is HtmlComment {
return (
!!node &&
node.type === NodeTypes.HtmlComment &&
/^\s*prettier-ignore-end(?=\s|$)/m.test(node.body)
);
}
export function isPrettierIgnoreRangeEndLiquidNode(
node: LiquidHtmlNode | undefined,
): node is LiquidTag {
return (
!!node &&
node.type === NodeTypes.LiquidTag &&
node.name === '#' &&
/^\s*prettier-ignore-end(?=\s|$)/m.test(node.markup)
);
}
export function isPrettierIgnoreRangeEndNode(
node: LiquidHtmlNode | undefined,
): node is HtmlComment | LiquidTag {
return isPrettierIgnoreRangeEndHtmlNode(node) || isPrettierIgnoreRangeEndLiquidNode(node);
}
/**
* Walk backward through siblings to determine if a node is inside an
* unmatched prettier-ignore-start / prettier-ignore-end range.
*/
export function isInsidePrettierIgnoreRange(node: LiquidHtmlNode): boolean {
let current = node.prev;
while (current) {
if (isPrettierIgnoreRangeEndNode(current)) {
return false;
}
if (isPrettierIgnoreRangeStartNode(current)) {
return true;
}
current = current.prev;
}
return false;
}
function getPrettierIgnoreAttributeCommentData(value: string): boolean {
const match = value.trim().match(/prettier-ignore-attribute(?:s?)(?:\s+(.+))?$/s);
if (!match) {
return false;
}
if (!match[1]) {
return true;
}
// TODO We should support 'prettier-ignore-attribute a,b,c' and allow users to not
// format the insides of some attributes.
//
// But since we don't reformat the insides of attributes yet (because of
// issue #4), that feature doesn't really make sense.
//
// For now, we'll only support `prettier-ignore-attribute`
//
// https://github.com/Shopify/prettier-plugin-liquid/issues/4
//
// return match[1].split(/\s+/);
return true;
}
export function isPrettierIgnoreAttributeNode(node: LiquidHtmlNode | undefined): boolean {
if (!node) return false;
if (node.type === NodeTypes.HtmlComment) {
return getPrettierIgnoreAttributeCommentData(node.body);
}
if (node.type === NodeTypes.LiquidTag && node.name === '#') {
return getPrettierIgnoreAttributeCommentData(node.markup);
}
return false;
}
export function forceNextEmptyLine(node: LiquidHtmlNode | undefined) {
if (!node) return false;
if (!node.next) return false;
const source = node.source;
// Current implementation: force next empty line when two consecutive
// lines exist between nodes.
let tmp: number;
tmp = source.indexOf('\n', node.position.end);
if (tmp === -1) return false;
tmp = source.indexOf('\n', tmp + 1);
if (tmp === -1) return false;
return tmp < node.next.position.start;
}
/** firstChild leadingSpaces and lastChild trailingSpaces */
export function forceBreakContent(node: LiquidHtmlNode) {
return (
forceBreakChildren(node) ||
(node.type === NodeTypes.HtmlElement &&
node.children.length > 0 &&
(isTagNameIncluded(['body', 'script', 'style'], node.name) ||
node.children.some((child) => hasNonTextChild(child)))) ||
(node.firstChild &&
node.firstChild === node.lastChild &&
node.firstChild.type !== NodeTypes.TextNode &&
hasLeadingLineBreak(node.firstChild) &&
(!node.lastChild.isTrailingWhitespaceSensitive || hasTrailingLineBreak(node.lastChild)))
);
}
/** spaces between children */
export function forceBreakChildren(node: LiquidHtmlNode) {
return (
node.type === NodeTypes.HtmlElement &&
node.children.length > 0 &&
(isTagNameIncluded(['html', 'head', 'ul', 'ol', 'select'], node.name) ||
(node.cssDisplay.startsWith('table') && node.cssDisplay !== 'table-cell'))
);
}
export function preferHardlineAsSurroundingSpaces(node: LiquidHtmlNode) {
switch (node.type) {
// case 'ieConditionalComment':
case NodeTypes.HtmlComment:
return true;
case NodeTypes.HtmlElement:
return isTagNameIncluded(['script', 'select'], node.name);
case NodeTypes.LiquidTag:
if ((node.prev && isTextLikeNode(node.prev)) || (node.next && isTextLikeNode(node.next))) {
return false;
}
return node.children && node.children.length > 0;
}
return false;
}
export function preferHardlineAsLeadingSpaces(node: LiquidHtmlNode) {
return (
preferHardlineAsSurroundingSpaces(node) ||
(isLiquidNode(node) && node.prev && isLiquidNode(node.prev)) ||
(node.prev && preferHardlineAsTrailingSpaces(node.prev)) ||
hasSurroundingLineBreak(node)
);
}
export function preferHardlineAsTrailingSpaces(node: LiquidHtmlNode) {
return (
preferHardlineAsSurroundingSpaces(node) ||
(isLiquidNode(node) && node.next && (isLiquidNode(node.next) || isHtmlNode(node.next))) ||
(node.type === NodeTypes.HtmlElement && isTagNameIncluded(['br'], node.name)) ||
hasSurroundingLineBreak(node)
);
}
export function hasMeaningfulLackOfLeadingWhitespace(node: LiquidHtmlNode): boolean {
return node.isLeadingWhitespaceSensitive && !node.hasLeadingWhitespace;
}
export function hasMeaningfulLackOfTrailingWhitespace(node: LiquidHtmlNode): boolean {
return node.isTrailingWhitespaceSensitive && !node.hasTrailingWhitespace;
}
export function hasMeaningfulLackOfDanglingWhitespace(node: LiquidHtmlNode): boolean {
return node.isDanglingWhitespaceSensitive && !node.hasDanglingWhitespace;
}
function hasSurroundingLineBreak(node: LiquidHtmlNode) {
return hasLeadingLineBreak(node) && hasTrailingLineBreak(node);
}
function hasLeadingLineBreak(node: LiquidHtmlNode) {
if (node.type === NodeTypes.Document) return false;
return (
node.hasLeadingWhitespace &&
hasLineBreakInRange(
node.source,
node.prev
? node.prev.position.end
: (node.parentNode as any).blockStartPosition
? (node.parentNode as any).blockStartPosition.end
: (node.parentNode as any).position.start,
node.position.start,
)
);
}
function hasTrailingLineBreak(node: LiquidHtmlNode) {
if (node.type === NodeTypes.Document) return false;
return (
node.hasTrailingWhitespace &&
hasLineBreakInRange(
node.source,
node.position.end,
node.next
? node.next.position.start
: (node.parentNode as any).blockEndPosition
? (node.parentNode as any).blockEndPosition.start
: (node.parentNode as any).position.end,
)
);
}
function hasLineBreakInRange(source: string, start: number, end: number) {
const index = source.indexOf('\n', start);
return index !== -1 && index < end;
}
export function getLastDescendant(node: LiquidHtmlNode): LiquidHtmlNode {
return node.lastChild ? getLastDescendant(node.lastChild) : node;
}
function isTagNameIncluded(
collection: string[],
name: (TextNode | LiquidVariableOutput)[],
): boolean {
if (name.length !== 1 || name[0].type !== NodeTypes.TextNode) return false;
return collection.includes(name[0].value);
}