@zackad/prettier-plugin-twig 0.7.0-alpha.1
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/LICENSE +201 -0
- package/README.md +141 -0
- package/package.json +39 -0
- package/src/index.js +130 -0
- package/src/parser.js +82 -0
- package/src/print/AliasExpression.js +5 -0
- package/src/print/ArrayExpression.js +14 -0
- package/src/print/Attribute.js +50 -0
- package/src/print/AutoescapeBlock.js +31 -0
- package/src/print/BinaryExpression.js +155 -0
- package/src/print/BlockStatement.js +48 -0
- package/src/print/CallExpression.js +38 -0
- package/src/print/ConditionalExpression.js +27 -0
- package/src/print/Declaration.js +15 -0
- package/src/print/DoStatement.js +10 -0
- package/src/print/Element.js +63 -0
- package/src/print/EmbedStatement.js +38 -0
- package/src/print/ExpressionStatement.js +25 -0
- package/src/print/ExtendsStatement.js +13 -0
- package/src/print/FilterBlockStatement.js +27 -0
- package/src/print/FilterExpression.js +131 -0
- package/src/print/FlushStatement.js +7 -0
- package/src/print/ForStatement.js +58 -0
- package/src/print/FromStatement.js +32 -0
- package/src/print/GenericToken.js +5 -0
- package/src/print/GenericTwigTag.js +30 -0
- package/src/print/HtmlComment.js +22 -0
- package/src/print/Identifier.js +15 -0
- package/src/print/IfStatement.js +81 -0
- package/src/print/ImportDeclaration.js +18 -0
- package/src/print/IncludeStatement.js +26 -0
- package/src/print/MacroDeclarationStatement.js +31 -0
- package/src/print/MemberExpression.js +23 -0
- package/src/print/MountStatement.js +84 -0
- package/src/print/NamedArgumentExpression.js +10 -0
- package/src/print/ObjectExpression.js +21 -0
- package/src/print/ObjectProperty.js +25 -0
- package/src/print/SequenceExpression.js +21 -0
- package/src/print/SetStatement.js +91 -0
- package/src/print/SliceExpression.js +8 -0
- package/src/print/SpacelessBlock.js +23 -0
- package/src/print/StringLiteral.js +63 -0
- package/src/print/SwitchTag.js +35 -0
- package/src/print/TestExpression.js +47 -0
- package/src/print/TextStatement.js +45 -0
- package/src/print/TwigComment.js +23 -0
- package/src/print/UnaryExpression.js +18 -0
- package/src/print/UnarySubclass.js +58 -0
- package/src/print/UseStatement.js +25 -0
- package/src/print/VariableDeclarationStatement.js +21 -0
- package/src/printer.js +252 -0
- package/src/util/index.js +3 -0
- package/src/util/printFunctions.js +33 -0
- package/src/util/publicFunctions.js +593 -0
- package/src/util/publicSymbols.js +88 -0
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "melody-types";
|
|
3
|
+
import {
|
|
4
|
+
EXPRESSION_NEEDED,
|
|
5
|
+
INSIDE_OF_STRING,
|
|
6
|
+
PRESERVE_LEADING_WHITESPACE,
|
|
7
|
+
PRESERVE_TRAILING_WHITESPACE,
|
|
8
|
+
NEWLINES_ONLY
|
|
9
|
+
} from "./publicSymbols.js";
|
|
10
|
+
|
|
11
|
+
const { line, indent, fill, group, hardline } = doc.builders;
|
|
12
|
+
|
|
13
|
+
const INLINE_HTML_ELEMENTS = [
|
|
14
|
+
"a",
|
|
15
|
+
"abbr",
|
|
16
|
+
"acronym",
|
|
17
|
+
"b",
|
|
18
|
+
"bdo",
|
|
19
|
+
"big",
|
|
20
|
+
"br",
|
|
21
|
+
"button",
|
|
22
|
+
"cite",
|
|
23
|
+
"code",
|
|
24
|
+
"dd",
|
|
25
|
+
"dfn",
|
|
26
|
+
"em",
|
|
27
|
+
"i",
|
|
28
|
+
"img",
|
|
29
|
+
"kbd",
|
|
30
|
+
"label",
|
|
31
|
+
"li",
|
|
32
|
+
"mark",
|
|
33
|
+
"q",
|
|
34
|
+
"s",
|
|
35
|
+
"samp",
|
|
36
|
+
"strong",
|
|
37
|
+
"sup",
|
|
38
|
+
"sub",
|
|
39
|
+
"small",
|
|
40
|
+
"span",
|
|
41
|
+
"time",
|
|
42
|
+
"tt",
|
|
43
|
+
"var"
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Node types around which we avoid an extra line break.
|
|
48
|
+
* Example:
|
|
49
|
+
* {{ {
|
|
50
|
+
* animal: "dog",
|
|
51
|
+
* owner: "Jim"
|
|
52
|
+
* } }}
|
|
53
|
+
*
|
|
54
|
+
* instead of
|
|
55
|
+
* {{
|
|
56
|
+
* {
|
|
57
|
+
* animal: "dog",
|
|
58
|
+
* owner: "Jim"
|
|
59
|
+
* }
|
|
60
|
+
* }}
|
|
61
|
+
*/
|
|
62
|
+
const CONTRACTABLE_NODE_TYPES = [
|
|
63
|
+
"ObjectExpression",
|
|
64
|
+
"BinaryExpression",
|
|
65
|
+
"ConditionalExpression",
|
|
66
|
+
"ArrayExpression"
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
const registerContractableNodeType = nodeType => {
|
|
70
|
+
CONTRACTABLE_NODE_TYPES.push(nodeType);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const isContractableNodeType = node => {
|
|
74
|
+
for (let i = 0; i < CONTRACTABLE_NODE_TYPES.length; i++) {
|
|
75
|
+
const contractableNodeType = CONTRACTABLE_NODE_TYPES[i];
|
|
76
|
+
const methodName = "is" + contractableNodeType;
|
|
77
|
+
if (Node[methodName] && Node[methodName].call(null, node)) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (Node.isUnaryLike(node)) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const isNotExpression = node =>
|
|
88
|
+
Node.isUnaryLike(node) && node.operator === "not";
|
|
89
|
+
|
|
90
|
+
const isMultipartExpression = node => {
|
|
91
|
+
return (
|
|
92
|
+
Node.isBinaryExpression(node) ||
|
|
93
|
+
Node.isConditionalExpression(node) ||
|
|
94
|
+
Node.isUnaryLike(node)
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Calls the callback for each parent
|
|
100
|
+
*
|
|
101
|
+
* Return false from the callback if you want the iteration
|
|
102
|
+
* to end.
|
|
103
|
+
*
|
|
104
|
+
* @param {FastPath} path A standard Prettier FastPath object
|
|
105
|
+
* representing the current AST traversal state
|
|
106
|
+
* @param {function} callback Gets called with each ancestor node
|
|
107
|
+
*/
|
|
108
|
+
const walkParents = (path, callback, startWithSelf = false) => {
|
|
109
|
+
let currentIndex = path.stack.length - 1;
|
|
110
|
+
if (!startWithSelf) {
|
|
111
|
+
currentIndex -= 1;
|
|
112
|
+
}
|
|
113
|
+
while (currentIndex >= 0) {
|
|
114
|
+
const currentElement = path.stack[currentIndex];
|
|
115
|
+
if (isMelodyNode(currentElement)) {
|
|
116
|
+
const callbackResult = callback(currentElement);
|
|
117
|
+
if (callbackResult === false) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
currentIndex--;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const firstValueInAncestorChain = (path, property, defaultValue) => {
|
|
126
|
+
let currentIndex = path.stack.length - 2; // Don't start with self
|
|
127
|
+
while (currentIndex >= 0) {
|
|
128
|
+
const currentElement = path.stack[currentIndex];
|
|
129
|
+
if (
|
|
130
|
+
isMelodyNode(currentElement) &&
|
|
131
|
+
currentElement[property] !== undefined
|
|
132
|
+
) {
|
|
133
|
+
return currentElement[property];
|
|
134
|
+
}
|
|
135
|
+
currentIndex--;
|
|
136
|
+
}
|
|
137
|
+
return defaultValue;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const quoteChar = options => {
|
|
141
|
+
// Might change depending on configuration options
|
|
142
|
+
return options && options.twigSingleQuote ? "'" : '"';
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const isValidIdentifierName = s => {
|
|
146
|
+
const identifierRegex = /^[A-Z][0-9A-Z_$]*$/i;
|
|
147
|
+
return typeof s === "string" && identifierRegex.test(s);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const isMelodyNode = n => {
|
|
151
|
+
const proto = n.__proto__;
|
|
152
|
+
return (
|
|
153
|
+
typeof n === "object" &&
|
|
154
|
+
proto.type &&
|
|
155
|
+
typeof Node["is" + proto.type] === "function"
|
|
156
|
+
);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const findParentNode = path => {
|
|
160
|
+
let currentIndex = path.stack.length - 2;
|
|
161
|
+
while (currentIndex >= 0) {
|
|
162
|
+
const currentElement = path.stack[currentIndex];
|
|
163
|
+
if (isMelodyNode(currentElement)) {
|
|
164
|
+
return currentElement;
|
|
165
|
+
}
|
|
166
|
+
currentIndex--;
|
|
167
|
+
}
|
|
168
|
+
return null;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const isRootNode = path => {
|
|
172
|
+
return findParentNode(path) === null;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const testCurrentAndParentNodes = (path, predicate) =>
|
|
176
|
+
testCurrentNode(path, predicate) || someParentNode(path, predicate);
|
|
177
|
+
|
|
178
|
+
const testCurrentNode = (path, predicate) => {
|
|
179
|
+
const index = path.stack.length - 1;
|
|
180
|
+
if (index >= 0) {
|
|
181
|
+
const node = path.stack[index];
|
|
182
|
+
return isMelodyNode(node) && predicate(node);
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const someParentNode = (path, predicate) => {
|
|
188
|
+
let currentIndex = path.stack.length - 2;
|
|
189
|
+
while (currentIndex >= 0) {
|
|
190
|
+
const currentElement = path.stack[currentIndex];
|
|
191
|
+
if (isMelodyNode(currentElement) && predicate(currentElement)) {
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
currentIndex--;
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Returns EXPRESSION_NEEDED or INSIDE_OF_STRING, depending
|
|
201
|
+
* on what kind of wrapping is needed around expressions:
|
|
202
|
+
* EXPRESSION_NEEDED => {{ ... }}
|
|
203
|
+
* INSIDE_OF_STRING => #{ ... }
|
|
204
|
+
*
|
|
205
|
+
* @param {FastPath} path The representation of the current AST traversal state
|
|
206
|
+
*/
|
|
207
|
+
const shouldExpressionsBeWrapped = path => {
|
|
208
|
+
let result = false;
|
|
209
|
+
walkParents(path, node => {
|
|
210
|
+
if (node[INSIDE_OF_STRING] === true) {
|
|
211
|
+
result = INSIDE_OF_STRING;
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
if (node[EXPRESSION_NEEDED] === true) {
|
|
215
|
+
result = EXPRESSION_NEEDED;
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
if (
|
|
219
|
+
node[EXPRESSION_NEEDED] === false ||
|
|
220
|
+
node[INSIDE_OF_STRING] === false
|
|
221
|
+
) {
|
|
222
|
+
// Abort walking up the ancestor chain
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
return result;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const wrapExpressionIfNeeded = (path, fragments, node = {}) => {
|
|
230
|
+
const wrapType = shouldExpressionsBeWrapped(path);
|
|
231
|
+
if (wrapType === EXPRESSION_NEEDED) {
|
|
232
|
+
wrapInEnvironment(fragments, node.trimLeft, node.trimRight);
|
|
233
|
+
} else if (wrapType === INSIDE_OF_STRING) {
|
|
234
|
+
wrapInStringInterpolation(fragments);
|
|
235
|
+
}
|
|
236
|
+
return fragments;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Puts environment braces {{ ... }} around an element
|
|
241
|
+
*
|
|
242
|
+
* @param {array} parts The finished, printed element,
|
|
243
|
+
* except for concatenation and grouping
|
|
244
|
+
*/
|
|
245
|
+
const wrapInEnvironment = (parts, trimLeft = false, trimRight = false) => {
|
|
246
|
+
const leftBraces = trimLeft ? "{{-" : "{{";
|
|
247
|
+
const rightBraces = trimRight ? "-}}" : "}}";
|
|
248
|
+
parts.unshift(leftBraces, line);
|
|
249
|
+
parts.push(line, rightBraces);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Puts string interpolation braces #{ ... } around an element
|
|
254
|
+
*
|
|
255
|
+
* @param {array} parts The finished, printed element,
|
|
256
|
+
* except for concatenation and grouping
|
|
257
|
+
*/
|
|
258
|
+
const wrapInStringInterpolation = parts => {
|
|
259
|
+
parts.unshift("#{");
|
|
260
|
+
parts.push("}");
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const isWhitespaceOnly = s => typeof s === "string" && s.trim() === "";
|
|
264
|
+
|
|
265
|
+
const countNewlines = s => {
|
|
266
|
+
return (s.match(/\n/g) || "").length;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const hasNoNewlines = s => {
|
|
270
|
+
return countNewlines(s) === 0;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const hasAtLeastTwoNewlines = s => countNewlines(s) >= 2;
|
|
274
|
+
|
|
275
|
+
// Split string by whitespace, but preserving the whitespace
|
|
276
|
+
// "\n Next\n" => ["", "\n ", "Next", "\n", ""]
|
|
277
|
+
const splitByWhitespace = s => s.split(/([\s\n]+)/gm);
|
|
278
|
+
|
|
279
|
+
const unifyWhitespace = (s, replacement = " ") =>
|
|
280
|
+
splitByWhitespace(s)
|
|
281
|
+
.filter(s => !isWhitespaceOnly(s))
|
|
282
|
+
.join(replacement);
|
|
283
|
+
|
|
284
|
+
const normalizeWhitespace = whitespace => {
|
|
285
|
+
const numNewlines = countNewlines(whitespace);
|
|
286
|
+
if (numNewlines > 0) {
|
|
287
|
+
// Normalize to one/two newline(s)
|
|
288
|
+
return numNewlines > 1 ? [hardline, hardline] : [hardline];
|
|
289
|
+
}
|
|
290
|
+
// Normalize to one single space
|
|
291
|
+
return [line];
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const createTextGroups = (
|
|
295
|
+
s,
|
|
296
|
+
preserveLeadingWhitespace,
|
|
297
|
+
preserveTrailingWhitespace
|
|
298
|
+
) => {
|
|
299
|
+
const parts = splitByWhitespace(s);
|
|
300
|
+
const groups = [];
|
|
301
|
+
let currentGroup = [];
|
|
302
|
+
const len = parts.length;
|
|
303
|
+
parts.forEach((curr, index) => {
|
|
304
|
+
if (curr !== "") {
|
|
305
|
+
if (isWhitespaceOnly(curr)) {
|
|
306
|
+
const isFirst =
|
|
307
|
+
groups.length === 0 && currentGroup.length === 0;
|
|
308
|
+
const isLast =
|
|
309
|
+
index === len - 1 ||
|
|
310
|
+
(index === len - 2 && parts[len - 1] === "");
|
|
311
|
+
// Remove leading whitespace if allowed
|
|
312
|
+
if (
|
|
313
|
+
(isFirst && preserveLeadingWhitespace) ||
|
|
314
|
+
(isLast && preserveTrailingWhitespace)
|
|
315
|
+
) {
|
|
316
|
+
currentGroup.push(...normalizeWhitespace(curr));
|
|
317
|
+
} else if (!isFirst && !isLast) {
|
|
318
|
+
const numNewlines = countNewlines(curr);
|
|
319
|
+
if (numNewlines <= 1) {
|
|
320
|
+
currentGroup.push(line);
|
|
321
|
+
} else {
|
|
322
|
+
groups.push(currentGroup);
|
|
323
|
+
currentGroup = [];
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
currentGroup.push(curr);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
if (currentGroup.length > 0) {
|
|
333
|
+
groups.push(currentGroup);
|
|
334
|
+
}
|
|
335
|
+
return groups.map(elem => fill(elem));
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const isWhitespaceNode = node => {
|
|
339
|
+
return (
|
|
340
|
+
(Node.isPrintTextStatement(node) &&
|
|
341
|
+
isWhitespaceOnly(node.value.value)) ||
|
|
342
|
+
(Node.isStringLiteral(node) && isWhitespaceOnly(node.value))
|
|
343
|
+
);
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const isEmptySequence = node =>
|
|
347
|
+
Node.isSequenceExpression(node) && node.expressions.length === 0;
|
|
348
|
+
|
|
349
|
+
const removeSurroundingWhitespace = children => {
|
|
350
|
+
if (!Array.isArray(children)) {
|
|
351
|
+
return children;
|
|
352
|
+
}
|
|
353
|
+
const result = [];
|
|
354
|
+
children.forEach((child, index) => {
|
|
355
|
+
const isFirstOrLast = index === 0 || index === children.length - 1;
|
|
356
|
+
// Remove initial whitespace
|
|
357
|
+
if (isFirstOrLast && isWhitespaceNode(child)) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
result.push(child);
|
|
362
|
+
});
|
|
363
|
+
return result;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const getDeepProperty = (obj, ...properties) => {
|
|
367
|
+
let result = obj;
|
|
368
|
+
properties.forEach(p => {
|
|
369
|
+
result = result[p];
|
|
370
|
+
});
|
|
371
|
+
return result;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const setDeepProperty = (obj, value, ...properties) => {
|
|
375
|
+
let containingObject = obj;
|
|
376
|
+
const len = properties.length;
|
|
377
|
+
for (let i = 0; i < len - 1; i++) {
|
|
378
|
+
containingObject = containingObject[properties[i]];
|
|
379
|
+
}
|
|
380
|
+
containingObject[properties[len - 1]] = value;
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
const printChildBlock = (node, path, print, ...childPath) => {
|
|
384
|
+
const originalChildren = getDeepProperty(node, ...childPath);
|
|
385
|
+
setDeepProperty(
|
|
386
|
+
node,
|
|
387
|
+
removeSurroundingWhitespace(originalChildren),
|
|
388
|
+
...childPath
|
|
389
|
+
);
|
|
390
|
+
const childGroups = printChildGroups(node, path, print, ...childPath);
|
|
391
|
+
return indent(group([hardline, ...childGroups]));
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
const addNewlineIfNotEmpty = items => {
|
|
395
|
+
if (items.length > 0) {
|
|
396
|
+
items.push(hardline);
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const endsWithHtmlComment = s => s.endsWith("-->");
|
|
401
|
+
|
|
402
|
+
const stripCommentChars = (start, end) => s => {
|
|
403
|
+
let result = s;
|
|
404
|
+
if (result.startsWith(start)) {
|
|
405
|
+
result = result.slice(start.length);
|
|
406
|
+
}
|
|
407
|
+
if (result.endsWith(end)) {
|
|
408
|
+
result = result.slice(0, 0 - end.length);
|
|
409
|
+
}
|
|
410
|
+
return result;
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const stripHtmlCommentChars = stripCommentChars("<!--", "-->");
|
|
414
|
+
|
|
415
|
+
const stripTwigCommentChars = s => {
|
|
416
|
+
let result = s;
|
|
417
|
+
if (result.startsWith("{#")) {
|
|
418
|
+
result = result.slice(2);
|
|
419
|
+
}
|
|
420
|
+
if (result.startsWith("-")) {
|
|
421
|
+
result = result.slice(1);
|
|
422
|
+
}
|
|
423
|
+
if (result.endsWith("#}")) {
|
|
424
|
+
result = result.slice(0, -2);
|
|
425
|
+
}
|
|
426
|
+
if (result.endsWith("-")) {
|
|
427
|
+
result = result.slice(0, -1);
|
|
428
|
+
}
|
|
429
|
+
return result;
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
const normalizeHtmlComment = s => {
|
|
433
|
+
const commentText = stripHtmlCommentChars(s);
|
|
434
|
+
return "<!-- " + unifyWhitespace(commentText) + " -->";
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
const normalizeTwigComment = (s, trimLeft, trimRight) => {
|
|
438
|
+
const commentText = stripTwigCommentChars(s);
|
|
439
|
+
const open = trimLeft ? "{#-" : "{#";
|
|
440
|
+
const close = trimRight ? "-#}" : "#}";
|
|
441
|
+
return open + " " + unifyWhitespace(commentText) + " " + close;
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
const isHtmlCommentEqualTo = substr => node => {
|
|
445
|
+
return (
|
|
446
|
+
node.constructor.name === "HtmlComment" &&
|
|
447
|
+
node.value.value &&
|
|
448
|
+
normalizeHtmlComment(node.value.value) === "<!-- " + substr + " -->"
|
|
449
|
+
);
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
const isTwigCommentEqualTo = substr => node => {
|
|
453
|
+
return (
|
|
454
|
+
node.constructor.name === "TwigComment" &&
|
|
455
|
+
node.value.value &&
|
|
456
|
+
normalizeTwigComment(node.value.value) === "{# " + substr + " #}"
|
|
457
|
+
);
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
const isInlineTextStatement = node => {
|
|
461
|
+
if (!Node.isPrintTextStatement(node)) {
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
// If the statement ends with an HTML comment
|
|
465
|
+
const trimmedValue =
|
|
466
|
+
typeof node.value.value === "string" && node.value.value.trim();
|
|
467
|
+
return !endsWithHtmlComment(trimmedValue);
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
const isInlineElement = node => {
|
|
471
|
+
const isInlineHtmlElement =
|
|
472
|
+
Node.isElement(node) && INLINE_HTML_ELEMENTS.indexOf(node.name) >= 0;
|
|
473
|
+
|
|
474
|
+
return (
|
|
475
|
+
isInlineHtmlElement ||
|
|
476
|
+
Node.isPrintExpressionStatement(node) ||
|
|
477
|
+
isInlineTextStatement(node)
|
|
478
|
+
);
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
const isCommentNode = node =>
|
|
482
|
+
Node.isTwigComment(node) || Node.isHtmlComment(node);
|
|
483
|
+
|
|
484
|
+
const createInlineMap = nodes => nodes.map(node => isInlineElement(node));
|
|
485
|
+
|
|
486
|
+
const textStatementsOnlyNewlines = nodes => {
|
|
487
|
+
nodes.forEach(node => {
|
|
488
|
+
if (Node.isPrintTextStatement(node)) {
|
|
489
|
+
node[NEWLINES_ONLY] = true;
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
const addPreserveWhitespaceInfo = (inlineMap, nodes) => {
|
|
495
|
+
nodes.forEach((node, index) => {
|
|
496
|
+
const previousNodeIsComment =
|
|
497
|
+
index > 0 && isCommentNode(nodes[index - 1]);
|
|
498
|
+
const followingNodeIsComment =
|
|
499
|
+
index < nodes.length - 1 && isCommentNode(nodes[index + 1]);
|
|
500
|
+
if (Node.isPrintTextStatement(node)) {
|
|
501
|
+
const hasPreviousInlineElement = index > 0 && inlineMap[index - 1];
|
|
502
|
+
if (hasPreviousInlineElement || previousNodeIsComment) {
|
|
503
|
+
node[PRESERVE_LEADING_WHITESPACE] = true;
|
|
504
|
+
}
|
|
505
|
+
const hasFollowingInlineElement =
|
|
506
|
+
index < inlineMap.length - 1 && inlineMap[index + 1];
|
|
507
|
+
if (hasFollowingInlineElement || followingNodeIsComment) {
|
|
508
|
+
node[PRESERVE_TRAILING_WHITESPACE] = true;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
const indentWithHardline = contents => indent([hardline, contents]);
|
|
515
|
+
|
|
516
|
+
const printChildGroups = (node, path, print, ...childPath) => {
|
|
517
|
+
// For the preprocessed children, get a map showing which elements can
|
|
518
|
+
// be printed inline
|
|
519
|
+
const children = getDeepProperty(node, ...childPath);
|
|
520
|
+
const inlineMap = createInlineMap(children);
|
|
521
|
+
addPreserveWhitespaceInfo(inlineMap, children);
|
|
522
|
+
textStatementsOnlyNewlines(children);
|
|
523
|
+
const printedChildren = path.map(print, ...childPath);
|
|
524
|
+
// Go over the children, while carrying along a group to be filled
|
|
525
|
+
// - If the element is inline, add it to the group
|
|
526
|
+
// - If the element is not inline, and the group is not empty
|
|
527
|
+
// => print the group as fill()
|
|
528
|
+
let inlineGroup = [];
|
|
529
|
+
const finishedGroups = [];
|
|
530
|
+
printedChildren.forEach((child, index) => {
|
|
531
|
+
if (inlineMap[index]) {
|
|
532
|
+
// Maybe a PrintTextStatement should not be
|
|
533
|
+
// considered "inline" if it contains more than
|
|
534
|
+
// one \n character
|
|
535
|
+
inlineGroup.push(child);
|
|
536
|
+
} else {
|
|
537
|
+
if (inlineGroup.length > 0) {
|
|
538
|
+
finishedGroups.push(fill(inlineGroup));
|
|
539
|
+
inlineGroup = [];
|
|
540
|
+
}
|
|
541
|
+
// Ensure line break between two block elements
|
|
542
|
+
if (finishedGroups.length > 0 && !inlineMap[index - 1]) {
|
|
543
|
+
addNewlineIfNotEmpty(finishedGroups);
|
|
544
|
+
}
|
|
545
|
+
finishedGroups.push(child);
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
if (inlineGroup.length > 0) {
|
|
549
|
+
finishedGroups.push(fill(inlineGroup));
|
|
550
|
+
}
|
|
551
|
+
return finishedGroups;
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
export {
|
|
555
|
+
shouldExpressionsBeWrapped,
|
|
556
|
+
wrapExpressionIfNeeded,
|
|
557
|
+
wrapInStringInterpolation,
|
|
558
|
+
wrapInEnvironment,
|
|
559
|
+
findParentNode,
|
|
560
|
+
isRootNode,
|
|
561
|
+
isMelodyNode,
|
|
562
|
+
someParentNode,
|
|
563
|
+
walkParents,
|
|
564
|
+
firstValueInAncestorChain,
|
|
565
|
+
isContractableNodeType,
|
|
566
|
+
isNotExpression,
|
|
567
|
+
isMultipartExpression,
|
|
568
|
+
registerContractableNodeType,
|
|
569
|
+
quoteChar,
|
|
570
|
+
isValidIdentifierName,
|
|
571
|
+
testCurrentNode,
|
|
572
|
+
testCurrentAndParentNodes,
|
|
573
|
+
isWhitespaceOnly,
|
|
574
|
+
isWhitespaceNode,
|
|
575
|
+
isEmptySequence,
|
|
576
|
+
hasNoNewlines,
|
|
577
|
+
countNewlines,
|
|
578
|
+
hasAtLeastTwoNewlines,
|
|
579
|
+
stripHtmlCommentChars,
|
|
580
|
+
stripTwigCommentChars,
|
|
581
|
+
normalizeHtmlComment,
|
|
582
|
+
normalizeTwigComment,
|
|
583
|
+
isHtmlCommentEqualTo,
|
|
584
|
+
isTwigCommentEqualTo,
|
|
585
|
+
createTextGroups,
|
|
586
|
+
removeSurroundingWhitespace,
|
|
587
|
+
getDeepProperty,
|
|
588
|
+
setDeepProperty,
|
|
589
|
+
isInlineElement,
|
|
590
|
+
printChildBlock,
|
|
591
|
+
printChildGroups,
|
|
592
|
+
indentWithHardline
|
|
593
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* These symbols are visible to outside users of
|
|
3
|
+
* the package. For example, they might be useful
|
|
4
|
+
* for plugins.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Set this to true on an AST node that might be the
|
|
9
|
+
* parent of a StringLiteral node. The StringLiteral
|
|
10
|
+
* will be enclosed in quotes when this attribute is
|
|
11
|
+
* set to true on the parent.
|
|
12
|
+
*/
|
|
13
|
+
const STRING_NEEDS_QUOTES = Symbol("STRING_NEEDS_QUOTES");
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Set to " or '
|
|
17
|
+
* Allows a node type to determine the quote char string
|
|
18
|
+
* literals must use.
|
|
19
|
+
*/
|
|
20
|
+
const OVERRIDE_QUOTE_CHAR = Symbol("OVERRIDE_QUOTE_CHAR");
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* This signals to child nodes that an expression environment
|
|
24
|
+
* {{ ... }} has not yet been opened, so they might have
|
|
25
|
+
* to open one. Example: An Element node, in its attributes
|
|
26
|
+
* array, can directly contain a FilterExpression. Usually,
|
|
27
|
+
* a FilterExpression does not open an {{...}} environment,
|
|
28
|
+
* but here, it has to.
|
|
29
|
+
*/
|
|
30
|
+
const EXPRESSION_NEEDED = Symbol("EXPRESSION_NEEDED");
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Signals to child nodes that they are part of a string,
|
|
34
|
+
* which means that expressions have to be interpolated.
|
|
35
|
+
* Example:
|
|
36
|
+
* "Part #{ partNr } of #{ partCount }"
|
|
37
|
+
*/
|
|
38
|
+
const INSIDE_OF_STRING = Symbol("INSIDE_OF_STRING");
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Signals to FilterStatement nodes that they are part of
|
|
42
|
+
* a filter block
|
|
43
|
+
*/
|
|
44
|
+
const FILTER_BLOCK = Symbol("FILTER_BLOCK");
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Signals to text nodes that they should preserve leading
|
|
48
|
+
* whitespace (whitespace at the beginning)
|
|
49
|
+
*/
|
|
50
|
+
const PRESERVE_LEADING_WHITESPACE = Symbol("PRESERVE_LEADING_WHITESPACE");
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Signals to text nodes that they should preserve trailing
|
|
54
|
+
* whitespace (whitespace at the end)
|
|
55
|
+
*/
|
|
56
|
+
const PRESERVE_TRAILING_WHITESPACE = Symbol("PRESERVE_TRAILING_WHITESPACE");
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Signals to text statements that only newlines should be
|
|
60
|
+
* preserved when hitting a whitespace-only node
|
|
61
|
+
*/
|
|
62
|
+
const NEWLINES_ONLY = Symbol("NEWLINES_ONLY");
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* This defaults to TRUE. Only if it is explicitly set to FALSE,
|
|
66
|
+
* a logical expression will not create a wrapping group on the
|
|
67
|
+
* top level
|
|
68
|
+
*/
|
|
69
|
+
const GROUP_TOP_LEVEL_LOGICAL = Symbol("GROUP_TOP_LEVEL_LOGICAL");
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Used to mark the root of a logical expression. Can be important
|
|
73
|
+
* for grouping and parenthesis placement.
|
|
74
|
+
*/
|
|
75
|
+
const IS_ROOT_LOGICAL_EXPRESSION = Symbol("IS_ROOT_LOGICAL_EXPRESSION");
|
|
76
|
+
|
|
77
|
+
export {
|
|
78
|
+
STRING_NEEDS_QUOTES,
|
|
79
|
+
OVERRIDE_QUOTE_CHAR,
|
|
80
|
+
INSIDE_OF_STRING,
|
|
81
|
+
EXPRESSION_NEEDED,
|
|
82
|
+
FILTER_BLOCK,
|
|
83
|
+
PRESERVE_TRAILING_WHITESPACE,
|
|
84
|
+
PRESERVE_LEADING_WHITESPACE,
|
|
85
|
+
NEWLINES_ONLY,
|
|
86
|
+
GROUP_TOP_LEVEL_LOGICAL,
|
|
87
|
+
IS_ROOT_LOGICAL_EXPRESSION
|
|
88
|
+
};
|