@zackad/prettier-plugin-twig 0.13.0 → 0.14.0
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/package.json +1 -1
- package/src/print/BlockStatement.js +9 -2
- package/src/print/Element.js +52 -29
- package/src/util/publicFunctions.js +22 -1
package/package.json
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { doc } from "prettier";
|
|
2
|
-
import { Node } from "../melody/melody-types/index.js";
|
|
3
|
-
import {
|
|
2
|
+
import { Node, StringLiteral } from "../melody/melody-types/index.js";
|
|
3
|
+
import {
|
|
4
|
+
printChildBlock,
|
|
5
|
+
EXPRESSION_NEEDED,
|
|
6
|
+
STRING_NEEDS_QUOTES
|
|
7
|
+
} from "../util/index.js";
|
|
4
8
|
|
|
5
9
|
const { hardline, group } = doc.builders;
|
|
6
10
|
|
|
@@ -37,6 +41,9 @@ const p = (node, path, print, options) => {
|
|
|
37
41
|
|
|
38
42
|
return group(parts);
|
|
39
43
|
} else if (Node.isPrintExpressionStatement(node.body)) {
|
|
44
|
+
if (node.body.value instanceof StringLiteral) {
|
|
45
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
46
|
+
}
|
|
40
47
|
return [
|
|
41
48
|
node.trimLeft ? "{%-" : "{%",
|
|
42
49
|
" block ",
|
package/src/print/Element.js
CHANGED
|
@@ -2,6 +2,7 @@ import { doc } from "prettier";
|
|
|
2
2
|
import {
|
|
3
3
|
removeSurroundingWhitespace,
|
|
4
4
|
isInlineElement,
|
|
5
|
+
isOwnlineElement,
|
|
5
6
|
printChildGroups,
|
|
6
7
|
EXPRESSION_NEEDED,
|
|
7
8
|
STRING_NEEDS_QUOTES
|
|
@@ -9,55 +10,77 @@ import {
|
|
|
9
10
|
|
|
10
11
|
const { group, line, hardline, softline, indent, join } = doc.builders;
|
|
11
12
|
|
|
12
|
-
const printOpeningTag = (node, path, print) => {
|
|
13
|
+
const printOpeningTag = (node, path, print, options) => {
|
|
14
|
+
const groupId = Symbol("opening-tag");
|
|
15
|
+
|
|
13
16
|
const opener = "<" + node.name;
|
|
14
17
|
const printedAttributes = printSeparatedList(path, print, "", "attributes");
|
|
15
|
-
const openingTagEnd = node.selfClosing ? " />" : ">";
|
|
16
|
-
const hasAttributes = node.attributes && node.attributes.length > 0;
|
|
17
18
|
|
|
19
|
+
const openingTagEnd = [];
|
|
20
|
+
if (node.selfClosing) {
|
|
21
|
+
if (!options.bracketSameLine) {
|
|
22
|
+
openingTagEnd.push(line);
|
|
23
|
+
} else {
|
|
24
|
+
openingTagEnd.push(" ");
|
|
25
|
+
}
|
|
26
|
+
openingTagEnd.push("/>");
|
|
27
|
+
} else {
|
|
28
|
+
if (!options.bracketSameLine) {
|
|
29
|
+
openingTagEnd.push(softline);
|
|
30
|
+
}
|
|
31
|
+
openingTagEnd.push(">");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const hasAttributes = node.attributes && node.attributes.length > 0;
|
|
18
35
|
if (hasAttributes) {
|
|
19
|
-
return
|
|
36
|
+
return group(
|
|
37
|
+
[opener, indent([line, printedAttributes]), openingTagEnd],
|
|
38
|
+
{ id: groupId }
|
|
39
|
+
);
|
|
20
40
|
}
|
|
21
|
-
|
|
41
|
+
|
|
42
|
+
return group([opener, openingTagEnd], { id: groupId });
|
|
22
43
|
};
|
|
23
44
|
|
|
24
45
|
const printSeparatedList = (path, print, separator, attrName) => {
|
|
25
46
|
return join([separator, line], path.map(print, attrName));
|
|
26
47
|
};
|
|
27
48
|
|
|
28
|
-
const p = (node, path, print) => {
|
|
49
|
+
const p = (node, path, print, options) => {
|
|
29
50
|
// Set a flag in case attributes contain, e.g., a FilterExpression
|
|
30
51
|
node[EXPRESSION_NEEDED] = true;
|
|
31
|
-
const openingGroup = group(printOpeningTag(node, path, print));
|
|
52
|
+
const openingGroup = group(printOpeningTag(node, path, print, options));
|
|
32
53
|
node[EXPRESSION_NEEDED] = false;
|
|
33
54
|
node[STRING_NEEDS_QUOTES] = false;
|
|
34
55
|
|
|
35
|
-
if (
|
|
36
|
-
|
|
56
|
+
if (node.selfClosing) {
|
|
57
|
+
return openingGroup;
|
|
58
|
+
}
|
|
37
59
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const result = [openingGroup];
|
|
41
|
-
const joinedChildren = childGroups;
|
|
42
|
-
if (isInlineElement(node)) {
|
|
43
|
-
result.push(indent([softline, joinedChildren]), softline);
|
|
44
|
-
} else {
|
|
45
|
-
const childBlock = [];
|
|
46
|
-
if (childGroups.length > 0) {
|
|
47
|
-
childBlock.push(hardline);
|
|
48
|
-
}
|
|
49
|
-
childBlock.push(joinedChildren);
|
|
50
|
-
result.push(indent(childBlock));
|
|
51
|
-
if (childGroups.length > 0) {
|
|
52
|
-
result.push(hardline);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
result.push(closingTag);
|
|
60
|
+
const groupElement = Symbol("element");
|
|
61
|
+
node.children = removeSurroundingWhitespace(node.children);
|
|
56
62
|
|
|
57
|
-
|
|
63
|
+
const childGroups = printChildGroups(node, path, print, "children");
|
|
64
|
+
const closingTag = ["</", node.name, ">"];
|
|
65
|
+
const result = [openingGroup];
|
|
66
|
+
const joinedChildren = childGroups;
|
|
67
|
+
if (isOwnlineElement(node) || isInlineElement(node)) {
|
|
68
|
+
const element = [indent([softline, joinedChildren]), softline];
|
|
69
|
+
result.push(element);
|
|
70
|
+
} else {
|
|
71
|
+
const childBlock = [];
|
|
72
|
+
if (childGroups.length > 0) {
|
|
73
|
+
childBlock.push(hardline);
|
|
74
|
+
}
|
|
75
|
+
childBlock.push(joinedChildren);
|
|
76
|
+
result.push(indent(childBlock));
|
|
77
|
+
if (childGroups.length > 0) {
|
|
78
|
+
result.push(hardline);
|
|
79
|
+
}
|
|
58
80
|
}
|
|
81
|
+
result.push(closingTag);
|
|
59
82
|
|
|
60
|
-
return
|
|
83
|
+
return group(result, { id: groupElement });
|
|
61
84
|
};
|
|
62
85
|
|
|
63
86
|
export { p as printElement };
|
|
@@ -28,7 +28,6 @@ const INLINE_HTML_ELEMENTS = [
|
|
|
28
28
|
"img",
|
|
29
29
|
"kbd",
|
|
30
30
|
"label",
|
|
31
|
-
"li",
|
|
32
31
|
"mark",
|
|
33
32
|
"q",
|
|
34
33
|
"s",
|
|
@@ -43,6 +42,22 @@ const INLINE_HTML_ELEMENTS = [
|
|
|
43
42
|
"var"
|
|
44
43
|
];
|
|
45
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Introducing new "type" html element that should be printed on their own line
|
|
47
|
+
* @type {string[]}
|
|
48
|
+
*/
|
|
49
|
+
const OWNLINE_HTML_ELEMENTS = [
|
|
50
|
+
"h1",
|
|
51
|
+
"h2",
|
|
52
|
+
"h3",
|
|
53
|
+
"h4",
|
|
54
|
+
"h5",
|
|
55
|
+
"h6",
|
|
56
|
+
"li",
|
|
57
|
+
"td",
|
|
58
|
+
"th"
|
|
59
|
+
];
|
|
60
|
+
|
|
46
61
|
/**
|
|
47
62
|
* Node types around which we avoid an extra line break.
|
|
48
63
|
* Example:
|
|
@@ -478,6 +493,12 @@ const isInlineElement = node => {
|
|
|
478
493
|
);
|
|
479
494
|
};
|
|
480
495
|
|
|
496
|
+
export const isOwnlineElement = node => {
|
|
497
|
+
return (
|
|
498
|
+
Node.isElement(node) && OWNLINE_HTML_ELEMENTS.indexOf(node.name) >= 0
|
|
499
|
+
);
|
|
500
|
+
};
|
|
501
|
+
|
|
481
502
|
const isCommentNode = node =>
|
|
482
503
|
Node.isTwigComment(node) || Node.isHtmlComment(node);
|
|
483
504
|
|