@zackad/prettier-plugin-twig 0.12.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/README.md CHANGED
@@ -56,7 +56,7 @@ If set to `true`, objects will always be wrapped/broken, even if they would fit
56
56
  ></section>
57
57
  ```
58
58
 
59
- If set to `false` (default value), this would be printed as:
59
+ If set to `false`, this would be printed as:
60
60
 
61
61
  ```html
62
62
  <section class="{{ { base: css.prices } | classes }}"></section>
package/package.json CHANGED
@@ -1,15 +1,21 @@
1
1
  {
2
2
  "name": "@zackad/prettier-plugin-twig",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Prettier Plugin for Twig",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/zackad/prettier-plugin-twig.git"
8
8
  },
9
+ "funding": "https://github.com/zackad/prettier-plugin-twig?sponsor=1",
9
10
  "license": "Apache-2.0",
10
11
  "author": "Tom Bartel <thomas.bartel@trivago.com>",
11
12
  "type": "module",
12
- "exports": "./src/index.js",
13
+ "exports": {
14
+ ".": {
15
+ "default": "./src/index.js"
16
+ },
17
+ "./package.json": "./package.json"
18
+ },
13
19
  "files": [
14
20
  "src"
15
21
  ],
@@ -23,8 +29,7 @@
23
29
  "@babel/types": "^7.24.9",
24
30
  "he": "^1.2.0",
25
31
  "lodash": "^4.17.21",
26
- "prettier": "^3.0.0",
27
- "resolve": "^1.12.0"
32
+ "prettier": "^3.0.0"
28
33
  },
29
34
  "devDependencies": {
30
35
  "eslint": "^8.56.0",
@@ -290,6 +290,14 @@ export const BinaryNullCoalesceExpression = createBinaryOperatorNode({
290
290
  precedence: 300,
291
291
  associativity: LEFT
292
292
  });
293
+
294
+ // Ref: https://plugins.craftcms.com/empty-coalesce
295
+ export const BinaryEmptyCoalesceExpression = createBinaryOperatorNode({
296
+ text: "???",
297
+ type: "BinaryEmptyCoalesceExpression",
298
+ precedence: 300,
299
+ associativity: LEFT
300
+ });
293
301
  //endregion
294
302
 
295
303
  //region Test Expressions
@@ -1,6 +1,10 @@
1
1
  import { doc } from "prettier";
2
- import { Node } from "../melody/melody-types/index.js";
3
- import { EXPRESSION_NEEDED, printChildBlock } from "../util/index.js";
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 ",
@@ -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 [opener, indent([" ", printedAttributes]), openingTagEnd];
36
+ return group(
37
+ [opener, indent([line, printedAttributes]), openingTagEnd],
38
+ { id: groupId }
39
+ );
20
40
  }
21
- return [opener, openingTagEnd];
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 (!node.selfClosing) {
36
- node.children = removeSurroundingWhitespace(node.children);
56
+ if (node.selfClosing) {
57
+ return openingGroup;
58
+ }
37
59
 
38
- const childGroups = printChildGroups(node, path, print, "children");
39
- const closingTag = ["</", node.name, ">"];
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
- return isInlineElement(node) ? group(result) : result;
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 openingGroup;
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