@zackad/prettier-plugin-twig 0.8.0 → 0.9.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/README.md +1 -1
- package/package.json +1 -1
- package/src/melody/melody-extension-core/operators.js +6 -0
- package/src/melody/melody-extension-core/visitors/filters.js +15 -1
- package/src/melody/melody-extension-core/visitors/for.js +1 -2
- package/src/melody/melody-parser/Parser.js +23 -8
- package/src/melody/melody-types/index.js +5 -5
- package/src/print/AutoescapeBlock.js +14 -4
- package/src/print/ObjectProperty.js +14 -8
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: `
|
|
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
|
@@ -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,22 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
import * as t from "@babel/types";
|
|
17
|
+
import isFunction from "lodash/isFunction.js";
|
|
17
18
|
import _template from "@babel/template";
|
|
18
|
-
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* For some reason there's inconsistency when importing "@babel/template" on
|
|
22
|
+
* vitest environment and loading this plugin directly on cli. I have no idea why
|
|
23
|
+
* this happens but this workaround seems to be fixed it.
|
|
24
|
+
*
|
|
25
|
+
* Without this workaround, an error will occur either on test or when loading
|
|
26
|
+
* plugin on cli. This error doesn't happen on `src/melody/melody-extension-core/visitors/for.js`.
|
|
27
|
+
* ```
|
|
28
|
+
* [error] template is not a function
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
const template = isFunction(_template) ? _template : _template.default;
|
|
32
|
+
|
|
19
33
|
// use default value if var is null, undefined or an empty string
|
|
20
34
|
// but use var if value is 0, false, an empty array or an empty object
|
|
21
35
|
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
|
|
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.
|
|
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
|
-
|
|
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.
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
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}
|
|
357
|
-
* @param {
|
|
358
|
-
* @param {
|
|
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(
|
|
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/isBoolean.js";
|
|
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
|
-
|
|
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
|
|
9
|
+
const shouldPrintKeyAsString = node.key?.wasImplicitConcatenation;
|
|
10
10
|
const needsParentheses = node.computed && !shouldPrintKeyAsString;
|
|
11
11
|
const parts = [];
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
24
|
+
|
|
25
|
+
// print "value" part
|
|
20
26
|
node[STRING_NEEDS_QUOTES] = true;
|
|
21
27
|
parts.push(path.call(print, "value"));
|
|
22
28
|
return parts;
|