@zackad/prettier-plugin-twig 0.16.1 → 0.16.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zackad/prettier-plugin-twig",
3
- "version": "0.16.1",
3
+ "version": "0.16.3",
4
4
  "description": "Prettier Plugin for Twig",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,6 +31,7 @@ const State = {
31
31
  const STATE = Symbol("STATE");
32
32
  const OPERATORS = Symbol("OPERATORS");
33
33
  const STRING_START = Symbol("STRING_START");
34
+ const ATTR_QUOTE = Symbol("ATTR_QUOTE");
34
35
 
35
36
  const CHAR_TO_TOKEN = {
36
37
  "[": TokenTypes.LBRACE,
@@ -57,6 +58,7 @@ export default class Lexer {
57
58
  this[STATE] = [State.TEXT];
58
59
  this[OPERATORS] = [];
59
60
  this[STRING_START] = null;
61
+ this[ATTR_QUOTE] = null;
60
62
  this.options = {
61
63
  preserveSourceLiterally: preserveSourceLiterally === true
62
64
  };
@@ -74,6 +76,7 @@ export default class Lexer {
74
76
  reset() {
75
77
  this.input.reset();
76
78
  this[STATE] = [State.TEXT];
79
+ this[ATTR_QUOTE] = null;
77
80
  }
78
81
 
79
82
  get source() {
@@ -265,6 +268,8 @@ export default class Lexer {
265
268
  this.popState();
266
269
  return this.createToken(TokenTypes.ELEMENT_END, pos);
267
270
  case '"':
271
+ case "'":
272
+ this[ATTR_QUOTE] = c;
268
273
  input.next();
269
274
  this.pushState(State.ATTRIBUTE_VALUE);
270
275
  return this.createToken(TokenTypes.STRING_START, pos);
@@ -275,12 +280,14 @@ export default class Lexer {
275
280
  return this.matchSymbol(pos);
276
281
  }
277
282
  } else if (this.state === State.ATTRIBUTE_VALUE) {
278
- if (c === '"') {
283
+ const quoteChar = this[ATTR_QUOTE];
284
+ if (c === quoteChar) {
279
285
  input.next();
280
286
  this.popState();
287
+ this[ATTR_QUOTE] = null;
281
288
  return this.createToken(TokenTypes.STRING_END, pos);
282
289
  }
283
- return this.matchAttributeValue(pos);
290
+ return this.matchAttributeValue(pos, quoteChar);
284
291
  } else if (this.state === State.DECLARATION) {
285
292
  switch (c) {
286
293
  case ">":
@@ -569,9 +576,9 @@ export default class Lexer {
569
576
  return result;
570
577
  }
571
578
 
572
- matchAttributeValue(pos) {
579
+ matchAttributeValue(pos, quoteChar = '"') {
573
580
  const input = this.input;
574
- const start = this.state === State.STRING_SINGLE ? "'" : '"';
581
+ const start = quoteChar;
575
582
  let c = input.la(0);
576
583
  const c2 = input.la(1);
577
584
  if (c === "{" && (c2 === "{" || c2 === "#" || c2 === "%")) {
@@ -432,7 +432,7 @@ export default class Parser {
432
432
  if (nodes.length > 1) {
433
433
  expr.wasImplicitConcatenation = true;
434
434
  }
435
- const attr = new n.Attribute(keyNode, expr);
435
+ const attr = new n.Attribute(keyNode, expr, start.text);
436
436
  copyStart(attr, keyNode);
437
437
  copyEnd(attr, expr);
438
438
  element.attributes.push(attr);
@@ -448,11 +448,13 @@ export class Attribute extends Node {
448
448
  /**
449
449
  * @param {Node} name
450
450
  * @param {Node} value
451
+ * @param {string} quoteChar
451
452
  */
452
- constructor(name, value = null) {
453
+ constructor(name, value = null, quoteChar = '"') {
453
454
  super();
454
455
  this.name = name;
455
456
  this.value = value;
457
+ this.quoteChar = quoteChar;
456
458
  }
457
459
 
458
460
  isImmutable() {
@@ -25,7 +25,8 @@ const printAttribute = (node, path, print = print) => {
25
25
  node[EXPRESSION_NEEDED] = true;
26
26
  node[STRING_NEEDS_QUOTES] = false;
27
27
  if (node.value) {
28
- docs.push('="');
28
+ const quote = node.quoteChar || '"';
29
+ docs.push("=", quote);
29
30
  if (
30
31
  Node.isBinaryConcatExpression(node.value) &&
31
32
  node.value.wasImplicitConcatenation
@@ -41,7 +42,7 @@ const printAttribute = (node, path, print = print) => {
41
42
  }
42
43
  docs.push(path.call(print, "value"));
43
44
  }
44
- docs.push('"');
45
+ docs.push(quote);
45
46
  }
46
47
 
47
48
  return docs;
@@ -1,7 +1,7 @@
1
1
  import { doc } from "prettier";
2
2
  import { EXPRESSION_NEEDED, wrapExpressionIfNeeded } from "../util/index.js";
3
3
 
4
- const { group, hardline, softline, indent, join } = doc.builders;
4
+ const { group, line, hardline, indent, join } = doc.builders;
5
5
 
6
6
  const printObjectExpression = (node, path, print, options) => {
7
7
  if (node.properties.length === 0) {
@@ -9,8 +9,8 @@ const printObjectExpression = (node, path, print, options) => {
9
9
  }
10
10
  node[EXPRESSION_NEEDED] = false;
11
11
  const mappedElements = path.map(print, "properties");
12
- const separator = options.twigAlwaysBreakObjects ? hardline : softline;
13
- const indentedContent = [softline, join([", ", separator], mappedElements)];
12
+ const separator = options.twigAlwaysBreakObjects ? hardline : line;
13
+ const indentedContent = [line, join([",", separator], mappedElements)];
14
14
 
15
15
  const parts = ["{", indent(indentedContent), separator, "}"];
16
16
  wrapExpressionIfNeeded(path, parts, node);