@zackad/prettier-plugin-twig 0.8.0 → 0.9.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
@@ -48,7 +48,7 @@ Values can be `true` or `false`. If `true`, single quotes will be used for strin
48
48
 
49
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
50
 
51
- ### twigAlwaysBreakObjects (default: `false`)
51
+ ### twigAlwaysBreakObjects (default: `true`)
52
52
 
53
53
  If set to `true`, objects will always be wrapped/broken, even if they would fit on one line:
54
54
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zackad/prettier-plugin-twig",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "Prettier Plugin for Twig",
5
5
  "repository": {
6
6
  "type": "git",
@@ -124,6 +124,12 @@ export const BinaryGreaterThanOrEqualExpression = createBinaryOperatorNode({
124
124
  precedence: 20,
125
125
  associativity: LEFT
126
126
  });
127
+ export const BinaryThreeWayComparisonExpression = createBinaryOperatorNode({
128
+ text: "<=>",
129
+ type: "BinaryThreeWayComparisonExpression",
130
+ precedence: 20,
131
+ associativity: LEFT
132
+ });
127
133
 
128
134
  export const BinaryNotInExpression = createBinaryOperatorNode({
129
135
  text: "not in",
@@ -14,8 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import * as t from "@babel/types";
17
- import _template from "@babel/template";
18
- const template = _template.default;
17
+ import template from "@babel/template";
19
18
  // use default value if var is null, undefined or an empty string
20
19
  // but use var if value is 0, false, an empty array or an empty object
21
20
  const defaultFilter = template("VAR != null && VAR !== '' ? VAR : DEFAULT");
@@ -15,8 +15,7 @@
15
15
  */
16
16
  import { traverse } from "../../melody-traverse/index.js";
17
17
  import * as t from "@babel/types";
18
- import _template from "@babel/template";
19
- const babelTemplate = _template.default;
18
+ import babelTemplate from "@babel/template";
20
19
 
21
20
  // @param template
22
21
  // @returns function
@@ -52,6 +52,10 @@ const TAG = Symbol("TAG");
52
52
  const TEST = Symbol("TEST");
53
53
 
54
54
  export default class Parser {
55
+ /**
56
+ * @param {TokenStream} tokenStream
57
+ * @param {Object} options
58
+ */
55
59
  constructor(tokenStream, options) {
56
60
  this.tokens = tokenStream;
57
61
  this[UNARY] = {};
@@ -625,7 +629,7 @@ export default class Parser {
625
629
  if (token.type === Types.LBRACE) {
626
630
  node = this.matchArray();
627
631
  } else if (token.type === Types.LBRACKET) {
628
- node = this.matchMap();
632
+ node = this.matchMapping();
629
633
  } else {
630
634
  this.error(
631
635
  {
@@ -751,7 +755,7 @@ export default class Parser {
751
755
  return array;
752
756
  }
753
757
 
754
- matchMap() {
758
+ matchMapping() {
755
759
  const tokens = this.tokens;
756
760
  let token;
757
761
  const obj = new n.ObjectExpression();
@@ -765,6 +769,9 @@ export default class Parser {
765
769
  if (!n.is(key, "StringLiteral")) {
766
770
  computed = true;
767
771
  }
772
+ } else if ((token = tokens.nextIf(Types.EXPRESSION_START))) {
773
+ key = this.matchExpression();
774
+ computed = true;
768
775
  } else if ((token = tokens.nextIf(Types.SYMBOL))) {
769
776
  key = createNode(n.Identifier, token, token.text);
770
777
  } else if ((token = tokens.nextIf(Types.NUMBER))) {
@@ -781,12 +788,20 @@ export default class Parser {
781
788
  tokens.next()
782
789
  });
783
790
  }
784
- tokens.expect(Types.COLON);
785
- const value = this.matchExpression();
786
- const prop = new n.ObjectProperty(key, value, computed);
787
- copyStart(prop, key);
788
- copyEnd(prop, value);
789
- obj.properties.push(prop);
791
+ if (tokens.test(Types.COLON)) {
792
+ tokens.expect(Types.COLON);
793
+ const value = this.matchExpression();
794
+ const prop = new n.ObjectProperty(value, computed, key);
795
+ copyStart(prop, key);
796
+ copyEnd(prop, value);
797
+ obj.properties.push(prop);
798
+ } else {
799
+ const value = key;
800
+ const prop = new n.ObjectProperty(value, computed);
801
+ copyStart(prop, key);
802
+ copyEnd(prop, value);
803
+ obj.properties.push(prop);
804
+ }
790
805
  if (!tokens.test(Types.RBRACKET)) {
791
806
  tokens.expect(Types.COMMA);
792
807
  // support trailing comma
@@ -353,14 +353,14 @@ visitor(ObjectExpression, "properties");
353
353
 
354
354
  export class ObjectProperty extends Node {
355
355
  /**
356
- * @param {Node} key
357
- * @param {Node} value
358
- * @param {boolean} computed
356
+ * @param {Node} value Actual object property value
357
+ * @param {boolean} computed Whether or not the Node require additional processing
358
+ * @param {Node|null} [key] Optional that would allow omitting key part
359
359
  */
360
- constructor(key, value, computed) {
360
+ constructor(value, computed, key = null) {
361
361
  super();
362
- this.key = key;
363
362
  this.value = value;
363
+ this.key = key;
364
364
  this.computed = computed;
365
365
  }
366
366
  }
@@ -1,16 +1,26 @@
1
1
  import { doc } from "prettier";
2
+ import { isBoolean } from "lodash";
2
3
  import { printChildBlock, quoteChar } from "../util/index.js";
3
4
 
4
5
  const { hardline } = doc.builders;
5
6
 
7
+ const printEscapeType = (escapeType, options) => {
8
+ if (escapeType === null) {
9
+ return [];
10
+ }
11
+
12
+ if (isBoolean(escapeType)) {
13
+ return [escapeType ? "true" : "false", " "];
14
+ }
15
+
16
+ return [quoteChar(options), escapeType, quoteChar(options), " "];
17
+ };
18
+
6
19
  const createOpener = (node, options) => {
7
20
  return [
8
21
  node.trimLeft ? "{%-" : "{%",
9
22
  " autoescape ",
10
- quoteChar(options),
11
- node.escapeType || "html",
12
- quoteChar(options),
13
- " ",
23
+ ...printEscapeType(node.escapeType, options),
14
24
  node.trimRightAutoescape ? "-%}" : "%}"
15
25
  ];
16
26
  };
@@ -6,17 +6,23 @@ const p = (node, path, print, options) => {
6
6
  !node.computed &&
7
7
  Node.isStringLiteral(node.key) &&
8
8
  !isValidIdentifierName(node.key.value);
9
- const shouldPrintKeyAsString = node.key.wasImplicitConcatenation;
9
+ const shouldPrintKeyAsString = node.key?.wasImplicitConcatenation;
10
10
  const needsParentheses = node.computed && !shouldPrintKeyAsString;
11
11
  const parts = [];
12
- if (needsParentheses) {
13
- parts.push("(");
14
- }
15
- parts.push(path.call(print, "key"));
16
- if (needsParentheses) {
17
- parts.push(")");
12
+
13
+ // print "key" part if they exist
14
+ if (node.key !== null) {
15
+ if (needsParentheses) {
16
+ parts.push("(");
17
+ }
18
+ parts.push(path.call(print, "key"));
19
+ if (needsParentheses) {
20
+ parts.push(")");
21
+ }
22
+ parts.push(": ");
18
23
  }
19
- parts.push(": ");
24
+
25
+ // print "value" part
20
26
  node[STRING_NEEDS_QUOTES] = true;
21
27
  parts.push(path.call(print, "value"));
22
28
  return parts;