@zackad/prettier-plugin-twig 0.15.3 → 0.16.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 +24 -21
- package/package.json +1 -1
- package/src/melody/melody-extension-core/index.js +3 -1
- package/src/melody/melody-extension-core/parser/props.js +37 -0
- package/src/melody/melody-extension-core/types.js +30 -0
- package/src/print/PropItem.js +18 -0
- package/src/print/PropsStatement.js +24 -0
- package/src/printer.js +4 -0
package/README.md
CHANGED
|
@@ -7,35 +7,34 @@ Forked from [trivago/prettier-plugin-twig-melody](https://github.com/trivago/pre
|
|
|
7
7
|
|
|
8
8
|

|
|
9
9
|

|
|
10
|
-

|
|
11
|
-

|
|
10
|
+
[](https://www.npmjs.com/package/@zackad/prettier-plugin-twig)
|
|
12
11
|
|
|
13
12
|
---
|
|
14
13
|
|
|
15
14
|
This Plugin enables Prettier to format `.twig` files, as well as `.html.twig`.
|
|
16
15
|
|
|
17
|
-
##
|
|
16
|
+
## Installation
|
|
18
17
|
|
|
19
18
|
```bash
|
|
20
19
|
yarn add --dev @zackad/prettier-plugin-twig
|
|
21
20
|
```
|
|
22
21
|
|
|
23
|
-
##
|
|
22
|
+
## Configuration
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
prettier --write "**/*.twig"
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
In your editor, if the plugin is not automatically picked up and invoked (e.g., if you are using format on save, but no formatting is happening when you save), try adding the plugin explicitly in your Prettier configuration (e.g., `.prettierrc.json`) using the `plugins` key:
|
|
24
|
+
Add the plugin explicitly into your Prettier configuration (e.g., `.prettierrc.json`) using the `plugins` key:
|
|
30
25
|
|
|
31
26
|
```json
|
|
32
27
|
{
|
|
33
|
-
"printWidth": 80,
|
|
34
|
-
"tabWidth": 4,
|
|
35
28
|
"plugins": ["@zackad/prettier-plugin-twig"]
|
|
36
29
|
}
|
|
37
30
|
```
|
|
38
31
|
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
./node_modules/.bin/prettier --write "**/*.twig"
|
|
36
|
+
```
|
|
37
|
+
|
|
39
38
|
## Options
|
|
40
39
|
|
|
41
40
|
This Prettier plugin comes with some options that you can add to your Prettier configuration (e.g., `prettierrc.json`).
|
|
@@ -75,12 +74,14 @@ Choose whether to output the block name in `{% endblock %}` tags (e.g., `{% endb
|
|
|
75
74
|
An array of coherent sequences of non-standard Twig tags that should be treated as belonging together. Example (inspired by [Craft CMS](https://docs.craftcms.com/v2/templating/nav.html)):
|
|
76
75
|
|
|
77
76
|
```json
|
|
78
|
-
|
|
79
|
-
"
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
77
|
+
{
|
|
78
|
+
"twigMultiTags": [
|
|
79
|
+
"nav,endnav",
|
|
80
|
+
"switch,case,default,endswitch",
|
|
81
|
+
"ifchildren,endifchildren",
|
|
82
|
+
"cache,endcache"
|
|
83
|
+
]
|
|
84
|
+
}
|
|
84
85
|
```
|
|
85
86
|
|
|
86
87
|
Looking at the case of `nav,endnav`, this means that the Twig tags `{% nav %}` and `{% endnav %}` will be treated as a pair, and everything in between will be indented:
|
|
@@ -110,10 +111,12 @@ Note that the order matters: It has to be `"nav,endnav"`, and it must not be `"e
|
|
|
110
111
|
Make custom Twig tests known to the parser.
|
|
111
112
|
|
|
112
113
|
```json
|
|
113
|
-
|
|
114
|
-
"
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
{
|
|
115
|
+
"twigTestExpressions": [
|
|
116
|
+
"snake_case_test",
|
|
117
|
+
"camelCaseTest"
|
|
118
|
+
]
|
|
119
|
+
}
|
|
117
120
|
```
|
|
118
121
|
|
|
119
122
|
__Example__
|
package/package.json
CHANGED
|
@@ -31,6 +31,7 @@ import { SetParser } from "./parser/set.js";
|
|
|
31
31
|
import { SpacelessParser } from "./parser/spaceless.js";
|
|
32
32
|
import { UseParser } from "./parser/use.js";
|
|
33
33
|
import { MountParser } from "./parser/mount.js";
|
|
34
|
+
import { PropsParser } from "./parser/props.js";
|
|
34
35
|
|
|
35
36
|
import forVisitor from "./visitors/for.js";
|
|
36
37
|
import testVisitor from "./visitors/tests.js";
|
|
@@ -97,7 +98,8 @@ export const extension = {
|
|
|
97
98
|
SetParser,
|
|
98
99
|
SpacelessParser,
|
|
99
100
|
UseParser,
|
|
100
|
-
MountParser
|
|
101
|
+
MountParser,
|
|
102
|
+
PropsParser
|
|
101
103
|
],
|
|
102
104
|
unaryOperators,
|
|
103
105
|
binaryOperators,
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { DoStatement, PropItem, PropsStatement } from "../types.js";
|
|
2
|
+
import {
|
|
3
|
+
createNode,
|
|
4
|
+
setEndFromToken,
|
|
5
|
+
setStartFromToken,
|
|
6
|
+
Types
|
|
7
|
+
} from "../../melody-parser/index.js";
|
|
8
|
+
import { Identifier } from "../../melody-types/index.js";
|
|
9
|
+
|
|
10
|
+
export const PropsParser = {
|
|
11
|
+
name: "props",
|
|
12
|
+
parse(parser, token) {
|
|
13
|
+
const tokenStream = parser.tokens;
|
|
14
|
+
const items = [];
|
|
15
|
+
let name;
|
|
16
|
+
let nameNode;
|
|
17
|
+
let valueNode;
|
|
18
|
+
|
|
19
|
+
do {
|
|
20
|
+
name = tokenStream.expect(Types.SYMBOL);
|
|
21
|
+
nameNode = createNode(Identifier, name, name.text);
|
|
22
|
+
if (tokenStream.test(Types.ASSIGNMENT)) {
|
|
23
|
+
tokenStream.expect(Types.ASSIGNMENT);
|
|
24
|
+
valueNode = parser.matchExpression();
|
|
25
|
+
} else {
|
|
26
|
+
valueNode = undefined;
|
|
27
|
+
}
|
|
28
|
+
const item = new PropItem(nameNode, valueNode);
|
|
29
|
+
items.push(item);
|
|
30
|
+
} while (tokenStream.nextIf(Types.COMMA));
|
|
31
|
+
|
|
32
|
+
const propsStatement = new PropsStatement(items);
|
|
33
|
+
setStartFromToken(propsStatement, token);
|
|
34
|
+
setEndFromToken(propsStatement, tokenStream.expect(Types.TAG_END));
|
|
35
|
+
return propsStatement;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
@@ -319,6 +319,36 @@ type(SetStatement, "SetStatement");
|
|
|
319
319
|
alias(SetStatement, "Statement", "ContextMutation");
|
|
320
320
|
visitor(SetStatement, "assignments");
|
|
321
321
|
|
|
322
|
+
export class PropsStatement extends Node {
|
|
323
|
+
/**
|
|
324
|
+
*
|
|
325
|
+
* @param {Array<PropItem>} items
|
|
326
|
+
*/
|
|
327
|
+
constructor(items) {
|
|
328
|
+
super();
|
|
329
|
+
this.items = items;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
type(PropsStatement, "PropsStatement");
|
|
333
|
+
alias(PropsStatement, "Statement");
|
|
334
|
+
visitor(PropsStatement, "items");
|
|
335
|
+
|
|
336
|
+
export class PropItem extends Node {
|
|
337
|
+
/**
|
|
338
|
+
*
|
|
339
|
+
* @param {Identifier} name
|
|
340
|
+
* @param {Node|undefined} value
|
|
341
|
+
*/
|
|
342
|
+
constructor(name, value = undefined) {
|
|
343
|
+
super();
|
|
344
|
+
this.name = name;
|
|
345
|
+
this.value = value;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
type(PropItem, "PropItem");
|
|
349
|
+
alias(PropItem, "Expression");
|
|
350
|
+
visitor(PropItem, "value");
|
|
351
|
+
|
|
322
352
|
export class SpacelessBlock extends Node {
|
|
323
353
|
/**
|
|
324
354
|
* @param {Node} [body]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
import { EXPRESSION_NEEDED, STRING_NEEDS_QUOTES } from "../util/index.js";
|
|
3
|
+
|
|
4
|
+
const { group, indent, join, line } = doc.builders;
|
|
5
|
+
|
|
6
|
+
const printPropItem = (node, path, print) => {
|
|
7
|
+
node[EXPRESSION_NEEDED] = false;
|
|
8
|
+
node[STRING_NEEDS_QUOTES] = true;
|
|
9
|
+
|
|
10
|
+
const docs = [path.call(print, "name")];
|
|
11
|
+
if (node.value !== undefined) {
|
|
12
|
+
docs.push([" = ", path.call(print, "value")]);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return group(docs);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export { printPropItem };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { doc } from "prettier";
|
|
2
|
+
|
|
3
|
+
const { group, indent, join, line, indentIfBreak, ifBreak, softline } =
|
|
4
|
+
doc.builders;
|
|
5
|
+
|
|
6
|
+
const printPropsStatement = (node, path, print) => {
|
|
7
|
+
const itemsGroupId = Symbol("prop-items");
|
|
8
|
+
|
|
9
|
+
const mappedItems = path.map(print, "items");
|
|
10
|
+
const joinedItems = join([",", line], mappedItems);
|
|
11
|
+
|
|
12
|
+
return group(
|
|
13
|
+
[
|
|
14
|
+
node.trimLeft ? "{%-" : "{%",
|
|
15
|
+
" props ",
|
|
16
|
+
indentIfBreak([softline, joinedItems], { groupId: itemsGroupId }),
|
|
17
|
+
line,
|
|
18
|
+
node.trimRight ? "-%}" : "%}"
|
|
19
|
+
],
|
|
20
|
+
{ id: itemsGroupId }
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { printPropsStatement };
|
package/src/printer.js
CHANGED
|
@@ -29,6 +29,8 @@ import { printIfStatement } from "./print/IfStatement.js";
|
|
|
29
29
|
import { printMountStatement } from "./print/MountStatement.js";
|
|
30
30
|
import { printForStatement } from "./print/ForStatement.js";
|
|
31
31
|
import { printSetStatement } from "./print/SetStatement.js";
|
|
32
|
+
import { printPropsStatement } from "./print/PropsStatement.js";
|
|
33
|
+
import { printPropItem } from "./print/PropItem.js";
|
|
32
34
|
import { printDoStatement } from "./print/DoStatement.js";
|
|
33
35
|
import { printExtendsStatement } from "./print/ExtendsStatement.js";
|
|
34
36
|
import { printEmbedStatement } from "./print/EmbedStatement.js";
|
|
@@ -221,6 +223,8 @@ printFunctions["MountStatement"] = printMountStatement;
|
|
|
221
223
|
printFunctions["ForStatement"] = printForStatement;
|
|
222
224
|
printFunctions["BinaryConcatExpression"] = printBinaryExpression;
|
|
223
225
|
printFunctions["SetStatement"] = printSetStatement;
|
|
226
|
+
printFunctions["PropsStatement"] = printPropsStatement;
|
|
227
|
+
printFunctions["PropItem"] = printPropItem;
|
|
224
228
|
printFunctions["VariableDeclarationStatement"] =
|
|
225
229
|
printVariableDeclarationStatement;
|
|
226
230
|
printFunctions["DoStatement"] = printDoStatement;
|