@zackad/prettier-plugin-twig 0.11.1 → 0.13.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
@@ -44,10 +44,6 @@ This Prettier plugin comes with some options that you can add to your Prettier c
44
44
 
45
45
  Values can be `true` or `false`. If `true`, single quotes will be used for string literals in Twig files.
46
46
 
47
- ### twigPrintWidth (default: `80`)
48
-
49
- Because Twig files might have a lot of nesting, it can be useful to define a separate print width for Twig files. This can be done with this option. If it is not set, the standard `printWidth` option is used.
50
-
51
47
  ### twigAlwaysBreakObjects (default: `true`)
52
48
 
53
49
  If set to `true`, objects will always be wrapped/broken, even if they would fit on one line:
@@ -60,7 +56,7 @@ If set to `true`, objects will always be wrapped/broken, even if they would fit
60
56
  ></section>
61
57
  ```
62
58
 
63
- If set to `false` (default value), this would be printed as:
59
+ If set to `false`, this would be printed as:
64
60
 
65
61
  ```html
66
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.11.1",
3
+ "version": "0.13.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",
package/src/index.js CHANGED
@@ -91,12 +91,6 @@ const options = {
91
91
  default: true,
92
92
  description: "Should objects always break in Twig files?"
93
93
  },
94
- twigPrintWidth: {
95
- type: "int",
96
- category: "Global",
97
- default: 80,
98
- description: "Print width for Twig files"
99
- },
100
94
  twigFollowOfficialCodingStandards: {
101
95
  type: "boolean",
102
96
  category: "Global",
@@ -38,6 +38,13 @@ export const binaryOperators = [];
38
38
  export const tests = [];
39
39
 
40
40
  //region Unary Expressions
41
+ export const SpreadExpression = createUnaryOperator(
42
+ "...",
43
+ "SpreadExpression",
44
+ // I can't find precedence reference for spread operator
45
+ // Ref: https://twig.symfony.com/doc/3.x/templates.html#operators
46
+ 20
47
+ );
41
48
  export const UnaryNotExpression = createUnaryOperator(
42
49
  "not",
43
50
  "UnaryNotExpression",
@@ -283,6 +290,14 @@ export const BinaryNullCoalesceExpression = createBinaryOperatorNode({
283
290
  precedence: 300,
284
291
  associativity: LEFT
285
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
+ });
286
301
  //endregion
287
302
 
288
303
  //region Test Expressions
@@ -764,6 +764,8 @@ export default class Parser {
764
764
  while (!tokens.test(Types.RBRACKET) && !tokens.test(Types.EOF)) {
765
765
  let computed = false;
766
766
  let key;
767
+
768
+ // find the key part of object property
767
769
  if (tokens.test(Types.STRING_START)) {
768
770
  key = this.matchStringExpression();
769
771
  if (!n.is(key, "StringLiteral")) {
@@ -780,14 +782,11 @@ export default class Parser {
780
782
  key = this.matchExpression();
781
783
  computed = true;
782
784
  } else {
783
- this.error({
784
- title: "Invalid map key",
785
- pos: tokens.la(0).pos,
786
- advice:
787
- "Key must be a string, symbol or a number but was " +
788
- tokens.next()
789
- });
785
+ // if none above check is matches, we can assume that key part is being omitted
786
+ // noop
790
787
  }
788
+
789
+ // find the value part of object property
791
790
  if (tokens.test(Types.COLON)) {
792
791
  tokens.expect(Types.COLON);
793
792
  const value = this.matchExpression();
@@ -796,12 +795,18 @@ export default class Parser {
796
795
  copyEnd(prop, value);
797
796
  obj.properties.push(prop);
798
797
  } else {
798
+ // when the part is missing, we can assume that it is being omitted
799
+ if (key === undefined) {
800
+ computed = true;
801
+ key = this.matchExpression();
802
+ }
799
803
  const value = key;
800
804
  const prop = new n.ObjectProperty(value, computed);
801
805
  copyStart(prop, key);
802
806
  copyEnd(prop, value);
803
807
  obj.properties.push(prop);
804
808
  }
809
+
805
810
  if (!tokens.test(Types.RBRACKET)) {
806
811
  tokens.expect(Types.COMMA);
807
812
  // support trailing comma
@@ -18,6 +18,11 @@ const p = (node, path, print, options) => {
18
18
  node.trimRightBlock ? " -%}" : " %}"
19
19
  ];
20
20
  const parts = [opener];
21
+
22
+ if (node.body.length === 0) {
23
+ parts.push(hardline);
24
+ }
25
+
21
26
  if (node.body.length > 0) {
22
27
  const indentedBody = printChildBlock(node, path, print, "body");
23
28
  parts.push(indentedBody);
@@ -30,10 +35,9 @@ const p = (node, path, print, options) => {
30
35
  node.trimRight ? " -%}" : " %}"
31
36
  );
32
37
 
33
- const result = group(parts);
34
- return result;
38
+ return group(parts);
35
39
  } else if (Node.isPrintExpressionStatement(node.body)) {
36
- const parts = [
40
+ return [
37
41
  node.trimLeft ? "{%-" : "{%",
38
42
  " block ",
39
43
  path.call(print, "name"),
@@ -41,7 +45,6 @@ const p = (node, path, print, options) => {
41
45
  path.call(print, "body", "value"),
42
46
  node.trimRight ? " -%}" : " %}"
43
47
  ];
44
- return parts;
45
48
  }
46
49
  };
47
50
 
@@ -16,7 +16,7 @@ const printOpeningTag = (node, path, print) => {
16
16
  const hasAttributes = node.attributes && node.attributes.length > 0;
17
17
 
18
18
  if (hasAttributes) {
19
- return [opener, indent([" ", printedAttributes]), openingTagEnd];
19
+ return [opener, indent([line, printedAttributes]), openingTagEnd];
20
20
  }
21
21
  return [opener, openingTagEnd];
22
22
  };
@@ -5,7 +5,9 @@ import {
5
5
  findParentNode,
6
6
  isMultipartExpression,
7
7
  IS_ROOT_LOGICAL_EXPRESSION,
8
- GROUP_TOP_LEVEL_LOGICAL
8
+ GROUP_TOP_LEVEL_LOGICAL,
9
+ wrapExpressionIfNeeded,
10
+ EXPRESSION_NEEDED
9
11
  } from "../util/index.js";
10
12
 
11
13
  const { softline, indent, group } = doc.builders;
@@ -41,6 +43,16 @@ const printLogicalExpression = (node, path, print) => {
41
43
  };
42
44
 
43
45
  const p = (node, path, print) => {
46
+ // We need this part to prevent argument being wrapped into expression separated from operator
47
+ // Example:
48
+ // with node[EXPRESSION_NEEDED] = true; // default value
49
+ // input: {{ ...vars }}
50
+ // output: ...{{ vars }}
51
+ // with node[EXPRESSION_NEEDED] = false;
52
+ // input: {{ ...vars }}
53
+ // output: {{ ...vars }}
54
+ node[EXPRESSION_NEEDED] = false;
55
+
44
56
  const parts = [];
45
57
  // Example: a is not same as ... => Here, the "not" is printed "inline"
46
58
  // Therefore, we do not output it here
@@ -48,10 +60,14 @@ const p = (node, path, print) => {
48
60
  if (isLogicalOperator(node.operator) && !hasTestExpressionArgument) {
49
61
  return printLogicalExpression(node, path, print);
50
62
  }
51
- if (!hasTestExpressionArgument) {
63
+ if (node.operator === "...") {
64
+ parts.push(node.operator);
65
+ } else if (!hasTestExpressionArgument) {
52
66
  parts.push(node.operator, " ");
53
67
  }
54
68
  parts.push(path.call(print, "argument"));
69
+ wrapExpressionIfNeeded(path, parts, node);
70
+
55
71
  return parts;
56
72
  };
57
73
 
package/src/printer.js CHANGED
@@ -97,10 +97,6 @@ const print = (path, options, print) => {
97
97
  originalSource = node[ORIGINAL_SOURCE];
98
98
  }
99
99
 
100
- if (options.twigPrintWidth) {
101
- options.printWidth = options.twigPrintWidth;
102
- }
103
-
104
100
  checkForIgnoreEnd(node);
105
101
  const useOriginalSource =
106
102
  (shouldApplyIgnoreNext(node) && ignoreNext) || ignoreRegion;