@zackad/prettier-plugin-twig 0.18.0 → 0.18.1
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/melody/melody-extension-core/operators.js +3 -2
- package/src/melody/melody-parser/Lexer.js +7 -6
- package/src/print/BinaryExpression.js +23 -2
- package/src/print/ConditionalExpression.js +8 -1
- package/src/print/StringLiteral.js +53 -18
- package/src/print/UnarySubclass.js +11 -1
package/package.json
CHANGED
|
@@ -30,7 +30,8 @@ import {
|
|
|
30
30
|
copyStart,
|
|
31
31
|
copyEnd,
|
|
32
32
|
copyLoc,
|
|
33
|
-
LEFT
|
|
33
|
+
LEFT,
|
|
34
|
+
RIGHT
|
|
34
35
|
} from "../melody-parser/index.js";
|
|
35
36
|
|
|
36
37
|
export const unaryOperators = [];
|
|
@@ -290,7 +291,7 @@ export const BinaryPowerExpression = createBinaryOperatorNode({
|
|
|
290
291
|
text: "**",
|
|
291
292
|
type: "BinaryPowerExpression",
|
|
292
293
|
precedence: 200,
|
|
293
|
-
associativity:
|
|
294
|
+
associativity: RIGHT
|
|
294
295
|
});
|
|
295
296
|
export const BinaryNullCoalesceExpression = createBinaryOperatorNode({
|
|
296
297
|
text: "??",
|
|
@@ -561,8 +561,9 @@ export default class Lexer {
|
|
|
561
561
|
return this.createToken(TokenTypes.STRING_END, pos);
|
|
562
562
|
}
|
|
563
563
|
while ((c = input.la(0)) !== start && c !== EOF) {
|
|
564
|
-
if (c === "\\" && input.la(1)
|
|
565
|
-
// escape sequence
|
|
564
|
+
if (c === "\\" && input.la(1) !== EOF) {
|
|
565
|
+
// escape sequence: the next character never ends the string
|
|
566
|
+
// or starts an interpolation (e.g. \', \\ or \#{)
|
|
566
567
|
input.next();
|
|
567
568
|
input.next();
|
|
568
569
|
} else if (allowInterpolation && c === "#" && input.la(1) === "{") {
|
|
@@ -574,11 +575,11 @@ export default class Lexer {
|
|
|
574
575
|
}
|
|
575
576
|
}
|
|
576
577
|
const result = this.createToken(TokenTypes.STRING, pos);
|
|
577
|
-
//
|
|
578
|
+
// Unescape escaped quotes, but keep every other escape sequence
|
|
579
|
+
// (including an escaped backslash right before a quote) as is
|
|
578
580
|
if (!this.options.preserveSourceLiterally) {
|
|
579
|
-
result.text = result.text.replace(
|
|
580
|
-
|
|
581
|
-
"$1"
|
|
581
|
+
result.text = result.text.replace(/\\([\s\S])/g, (match, char) =>
|
|
582
|
+
char === start ? char : match
|
|
582
583
|
);
|
|
583
584
|
}
|
|
584
585
|
return result;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { doc } from "prettier";
|
|
2
2
|
import { Node } from "../melody/melody-types/index.js";
|
|
3
3
|
import { extension as coreExtension } from "../melody/melody-extension-core/index.js";
|
|
4
|
+
import { LEFT } from "../melody/melody-parser/index.js";
|
|
4
5
|
import {
|
|
5
6
|
EXPRESSION_NEEDED,
|
|
6
7
|
STRING_NEEDS_QUOTES,
|
|
@@ -22,6 +23,14 @@ const operatorPrecedence = coreExtension.binaryOperators.reduce((acc, curr) => {
|
|
|
22
23
|
return acc;
|
|
23
24
|
}, {});
|
|
24
25
|
|
|
26
|
+
const operatorAssociativity = coreExtension.binaryOperators.reduce(
|
|
27
|
+
(acc, curr) => {
|
|
28
|
+
acc[curr.text] = curr.associativity;
|
|
29
|
+
return acc;
|
|
30
|
+
},
|
|
31
|
+
{}
|
|
32
|
+
);
|
|
33
|
+
|
|
25
34
|
const printInterpolatedString = (node, path, print, options) => {
|
|
26
35
|
node[STRING_NEEDS_QUOTES] = false;
|
|
27
36
|
node[INSIDE_OF_STRING] = true;
|
|
@@ -47,6 +56,8 @@ const hasLogicalOperator = node => {
|
|
|
47
56
|
return node.operator === "or" || node.operator === "and";
|
|
48
57
|
};
|
|
49
58
|
|
|
59
|
+
const isSignOperator = operator => operator === "-" || operator === "+";
|
|
60
|
+
|
|
50
61
|
const otherNeedsParentheses = (node, otherProp) => {
|
|
51
62
|
const other = node[otherProp];
|
|
52
63
|
const isBinaryOther = Node.isBinaryExpression(other);
|
|
@@ -54,14 +65,24 @@ const otherNeedsParentheses = (node, otherProp) => {
|
|
|
54
65
|
const otherPrecedence = isBinaryOther
|
|
55
66
|
? operatorPrecedence[node[otherProp].operator]
|
|
56
67
|
: Number.MAX_SAFE_INTEGER;
|
|
68
|
+
const sameSideAsAssociativity =
|
|
69
|
+
(operatorAssociativity[node.operator] === LEFT) ===
|
|
70
|
+
(otherProp === "left");
|
|
57
71
|
return (
|
|
58
72
|
otherPrecedence < ownPrecedence ||
|
|
73
|
+
(otherPrecedence === ownPrecedence &&
|
|
74
|
+
isBinaryOther &&
|
|
75
|
+
!other.wasImplicitConcatenation &&
|
|
76
|
+
!sameSideAsAssociativity) ||
|
|
59
77
|
(otherPrecedence > ownPrecedence &&
|
|
60
78
|
isBinaryOther &&
|
|
61
79
|
hasLogicalOperator(other)) ||
|
|
62
80
|
Node.isFilterExpression(other) ||
|
|
63
|
-
|
|
64
|
-
|
|
81
|
+
Node.isConditionalExpression(other) ||
|
|
82
|
+
(node.operator === "**" &&
|
|
83
|
+
otherProp === "left" &&
|
|
84
|
+
Node.isUnaryLike(other) &&
|
|
85
|
+
isSignOperator(other.operator))
|
|
65
86
|
);
|
|
66
87
|
};
|
|
67
88
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { doc } from "prettier";
|
|
2
|
+
import { Node } from "../melody/melody-types/index.js";
|
|
2
3
|
import {
|
|
3
4
|
EXPRESSION_NEEDED,
|
|
4
5
|
STRING_NEEDS_QUOTES,
|
|
@@ -18,7 +19,13 @@ const printConditionalExpression = (node, path, print) => {
|
|
|
18
19
|
if (node.alternate) {
|
|
19
20
|
rest.push(line, ": ", path.call(print, "alternate"));
|
|
20
21
|
}
|
|
21
|
-
const
|
|
22
|
+
const printedTest = path.call(print, "test");
|
|
23
|
+
const parts = [
|
|
24
|
+
Node.isConditionalExpression(node.test)
|
|
25
|
+
? ["(", printedTest, ")"]
|
|
26
|
+
: printedTest,
|
|
27
|
+
indent(rest)
|
|
28
|
+
];
|
|
22
29
|
wrapExpressionIfNeeded(path, parts, node);
|
|
23
30
|
|
|
24
31
|
return group(parts);
|
|
@@ -2,35 +2,66 @@ import {
|
|
|
2
2
|
firstValueInAncestorChain,
|
|
3
3
|
quoteChar,
|
|
4
4
|
STRING_NEEDS_QUOTES,
|
|
5
|
-
OVERRIDE_QUOTE_CHAR
|
|
5
|
+
OVERRIDE_QUOTE_CHAR,
|
|
6
|
+
INSIDE_OF_STRING
|
|
6
7
|
} from "../util/index.js";
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Lists the quote characters of a string literal value that are not
|
|
11
|
+
* part of an escape sequence (a backslash escapes the next character)
|
|
12
|
+
*/
|
|
13
|
+
const unescapedQuotes = s => {
|
|
14
|
+
const quotes = new Set();
|
|
15
|
+
for (let i = 0; i < s.length; i++) {
|
|
16
|
+
if (s[i] === "\\") {
|
|
17
|
+
i++;
|
|
18
|
+
} else if (s[i] === "'" || s[i] === '"') {
|
|
19
|
+
quotes.add(s[i]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return quotes;
|
|
10
23
|
};
|
|
11
24
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Escapes the occurrences of the given quote character that are not
|
|
27
|
+
* already part of an escape sequence
|
|
28
|
+
*/
|
|
29
|
+
const escapeQuotes = (s, quote) => {
|
|
30
|
+
let result = "";
|
|
31
|
+
for (let i = 0; i < s.length; i++) {
|
|
32
|
+
if (s[i] === "\\") {
|
|
33
|
+
result += s.slice(i, i + 2);
|
|
34
|
+
i++;
|
|
35
|
+
} else {
|
|
36
|
+
result += s[i] === quote ? "\\" + quote : s[i];
|
|
17
37
|
}
|
|
18
|
-
pos = s.indexOf(char, pos + 1);
|
|
19
38
|
}
|
|
20
|
-
return
|
|
39
|
+
return result;
|
|
21
40
|
};
|
|
22
41
|
|
|
23
|
-
const
|
|
24
|
-
const containsUnmaskedDoubleQuote = containsUnmasked('"');
|
|
42
|
+
const alternate = quote => (quote === '"' ? "'" : '"');
|
|
25
43
|
|
|
26
44
|
const getQuoteChar = (s, options) => {
|
|
27
|
-
|
|
28
|
-
|
|
45
|
+
const preferred = quoteChar(options);
|
|
46
|
+
const quotes = unescapedQuotes(s);
|
|
47
|
+
if (quotes.has(preferred) && !quotes.has(alternate(preferred))) {
|
|
48
|
+
return alternate(preferred);
|
|
29
49
|
}
|
|
30
|
-
|
|
31
|
-
|
|
50
|
+
return preferred;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Whether the string literal is a fragment of an interpolated
|
|
55
|
+
* string ("... #{ expr } ..."), which is always double quoted
|
|
56
|
+
*/
|
|
57
|
+
const isInterpolatedStringFragment = path => {
|
|
58
|
+
for (let i = path.stack.length - 2; i >= 0; i--) {
|
|
59
|
+
const element = path.stack[i];
|
|
60
|
+
if (element && element[STRING_NEEDS_QUOTES] !== undefined) {
|
|
61
|
+
return element[INSIDE_OF_STRING] === true;
|
|
62
|
+
}
|
|
32
63
|
}
|
|
33
|
-
return
|
|
64
|
+
return false;
|
|
34
65
|
};
|
|
35
66
|
|
|
36
67
|
const printStringLiteral = (node, path, print, options) => {
|
|
@@ -54,7 +85,11 @@ const printStringLiteral = (node, path, print, options) => {
|
|
|
54
85
|
const quote = overridingQuoteChar
|
|
55
86
|
? overridingQuoteChar
|
|
56
87
|
: getQuoteChar(node.value, options);
|
|
57
|
-
return quote + node.value + quote;
|
|
88
|
+
return quote + escapeQuotes(node.value, quote) + quote;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (isInterpolatedStringFragment(path)) {
|
|
92
|
+
return escapeQuotes(node.value, '"');
|
|
58
93
|
}
|
|
59
94
|
|
|
60
95
|
return node.value;
|
|
@@ -16,6 +16,8 @@ const argumentNeedsParentheses = node => isMultipartExpression(node);
|
|
|
16
16
|
|
|
17
17
|
const isLogicalOperator = operator => operator === "not";
|
|
18
18
|
|
|
19
|
+
const isSignOperator = operator => operator === "-" || operator === "+";
|
|
20
|
+
|
|
19
21
|
const printLogicalExpression = (node, path, print) => {
|
|
20
22
|
const foundRootAbove = firstValueInAncestorChain(
|
|
21
23
|
path,
|
|
@@ -65,7 +67,15 @@ const printUnarySubclass = (node, path, print) => {
|
|
|
65
67
|
} else if (!hasTestExpressionArgument) {
|
|
66
68
|
parts.push(node.operator, " ");
|
|
67
69
|
}
|
|
68
|
-
|
|
70
|
+
if (
|
|
71
|
+
isSignOperator(node.operator) &&
|
|
72
|
+
!hasTestExpressionArgument &&
|
|
73
|
+
argumentNeedsParentheses(node.argument)
|
|
74
|
+
) {
|
|
75
|
+
parts.push("(", path.call(print, "argument"), ")");
|
|
76
|
+
} else {
|
|
77
|
+
parts.push(path.call(print, "argument"));
|
|
78
|
+
}
|
|
69
79
|
wrapExpressionIfNeeded(path, parts, node);
|
|
70
80
|
|
|
71
81
|
return parts;
|