@zackad/prettier-plugin-twig 0.7.0-alpha.1 → 0.7.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.
Files changed (63) hide show
  1. package/README.md +5 -7
  2. package/package.json +10 -8
  3. package/src/index.js +53 -53
  4. package/src/melody/melody-code-frame/index.js +44 -0
  5. package/src/melody/melody-code-frame/lineNumbers.js +45 -0
  6. package/src/melody/melody-extension-core/index.js +167 -0
  7. package/src/melody/melody-extension-core/operators.js +414 -0
  8. package/src/melody/melody-extension-core/parser/autoescape.js +92 -0
  9. package/src/melody/melody-extension-core/parser/block.js +93 -0
  10. package/src/melody/melody-extension-core/parser/do.js +32 -0
  11. package/src/melody/melody-extension-core/parser/embed.js +76 -0
  12. package/src/melody/melody-extension-core/parser/extends.js +35 -0
  13. package/src/melody/melody-extension-core/parser/filter.js +63 -0
  14. package/src/melody/melody-extension-core/parser/flush.js +33 -0
  15. package/src/melody/melody-extension-core/parser/for.js +110 -0
  16. package/src/melody/melody-extension-core/parser/from.js +63 -0
  17. package/src/melody/melody-extension-core/parser/if.js +100 -0
  18. package/src/melody/melody-extension-core/parser/import.js +44 -0
  19. package/src/melody/melody-extension-core/parser/include.js +48 -0
  20. package/src/melody/melody-extension-core/parser/macro.js +94 -0
  21. package/src/melody/melody-extension-core/parser/mount.js +147 -0
  22. package/src/melody/melody-extension-core/parser/set.js +103 -0
  23. package/src/melody/melody-extension-core/parser/spaceless.js +56 -0
  24. package/src/melody/melody-extension-core/parser/use.js +58 -0
  25. package/src/melody/melody-extension-core/types.js +402 -0
  26. package/src/melody/melody-extension-core/visitors/filters.js +146 -0
  27. package/src/melody/melody-extension-core/visitors/for.js +391 -0
  28. package/src/melody/melody-extension-core/visitors/functions.js +100 -0
  29. package/src/melody/melody-extension-core/visitors/tests.js +127 -0
  30. package/src/melody/melody-parser/Associativity.js +17 -0
  31. package/src/melody/melody-parser/CharStream.js +82 -0
  32. package/src/melody/melody-parser/GenericMultiTagParser.js +66 -0
  33. package/src/melody/melody-parser/GenericTagParser.js +53 -0
  34. package/src/melody/melody-parser/Lexer.js +708 -0
  35. package/src/melody/melody-parser/Parser.js +954 -0
  36. package/src/melody/melody-parser/TokenStream.js +183 -0
  37. package/src/melody/melody-parser/TokenTypes.js +74 -0
  38. package/src/melody/melody-parser/elementInfo.js +43 -0
  39. package/src/melody/melody-parser/index.js +94 -0
  40. package/src/melody/melody-parser/util.js +100 -0
  41. package/src/melody/melody-traverse/Binding.js +61 -0
  42. package/src/melody/melody-traverse/Path.js +427 -0
  43. package/src/melody/melody-traverse/Scope.js +218 -0
  44. package/src/melody/melody-traverse/TraversalContext.js +166 -0
  45. package/src/melody/melody-traverse/index.js +21 -0
  46. package/src/melody/melody-traverse/traverse.js +48 -0
  47. package/src/melody/melody-traverse/visitors.js +112 -0
  48. package/src/melody/melody-types/index.js +506 -0
  49. package/src/parser.js +7 -2
  50. package/src/print/Attribute.js +1 -1
  51. package/src/print/BinaryExpression.js +2 -2
  52. package/src/print/BlockStatement.js +1 -1
  53. package/src/print/CallExpression.js +1 -1
  54. package/src/print/ExpressionStatement.js +1 -1
  55. package/src/print/FilterExpression.js +1 -1
  56. package/src/print/GenericTwigTag.js +1 -1
  57. package/src/print/IfStatement.js +1 -1
  58. package/src/print/ObjectProperty.js +1 -1
  59. package/src/print/SetStatement.js +1 -1
  60. package/src/print/SwitchTag.js +1 -1
  61. package/src/print/UnarySubclass.js +1 -1
  62. package/src/util/printFunctions.js +1 -1
  63. package/src/util/publicFunctions.js +1 -1
package/README.md CHANGED
@@ -1,11 +1,9 @@
1
1
  # Prettier Plugin for Twig
2
2
 
3
- > [!WARNING]
4
- > Not ready. DON'T USE!
5
-
6
3
  Forked from [trivago/prettier-plugin-twig-melody](https://github.com/trivago/prettier-plugin-twig-melody) with focus on twig template only.
7
4
 
8
- ![Prettier Banner](https://raw.githubusercontent.com/prettier/prettier-logo/master/images/prettier-banner-light.png)
5
+ ![Prettier Twig Banner](./logo/wide-dark.svg#gh-dark-mode-only)
6
+ ![Prettier Twig Banner](./logo/wide-light.svg#gh-light-mode-only)
9
7
 
10
8
  ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/zackad/prettier-plugin-twig-melody/ci.yaml?branch=master&style=for-the-badge&logo=github)
11
9
  ![GitHub License](https://img.shields.io/github/license/zackad/prettier-plugin-twig-melody?style=for-the-badge)
@@ -57,8 +55,8 @@ If set to `true`, objects will always be wrapped/broken, even if they would fit
57
55
  ```html
58
56
  <section
59
57
  class="{{ {
60
- base: css.prices
61
- } | classes }}"
58
+ base: css.prices
59
+ } | classes }}"
62
60
  ></section>
63
61
  ```
64
62
 
@@ -138,4 +136,4 @@ You can also tell Prettier to leave entire regions as they are:
138
136
 
139
137
  ## Credit
140
138
 
141
- - Author: Tom Bartel <thomas.bartel@trivago.com>
139
+ - Author: Tom Bartel <thomas.bartel@trivago.com>
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@zackad/prettier-plugin-twig",
3
- "version": "0.7.0-alpha.1",
3
+ "version": "0.7.0",
4
4
  "description": "Prettier Plugin for Twig",
5
- "repository": "https://github.com/zackad/prettier-plugin-twig",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/zackad/prettier-plugin-twig.git"
8
+ },
6
9
  "license": "Apache-2.0",
7
10
  "author": "Tom Bartel <thomas.bartel@trivago.com>",
8
11
  "type": "module",
@@ -12,15 +15,14 @@
12
15
  ],
13
16
  "scripts": {
14
17
  "lint": "DEBUG=eslint:cli-engine eslint .",
15
- "prettier": "prettier --plugin=./src/index.js --parser=melody",
18
+ "prettier": "prettier --plugin=./src/index.js --parser=twig",
16
19
  "test": "vitest --run"
17
20
  },
18
21
  "dependencies": {
19
- "babel-types": "^6.26.0",
20
- "melody-extension-core": "^1.7.5",
21
- "melody-parser": "^1.7.5",
22
- "melody-traverse": "^1.7.5",
23
- "melody-types": "^1.7.5",
22
+ "@babel/template": "^7.24.7",
23
+ "@babel/types": "^7.24.9",
24
+ "he": "^1.2.0",
25
+ "lodash": "^4.17.21",
24
26
  "prettier": "^3.0.0",
25
27
  "resolve": "^1.12.0"
26
28
  },
package/src/index.js CHANGED
@@ -3,74 +3,74 @@ import { parse } from "./parser.js";
3
3
  import * as symbols from "./util/publicSymbols.js";
4
4
  import * as publicFunctions from "./util/publicFunctions.js";
5
5
 
6
+ /**
7
+ * @typedef {import('prettier').SupportLanguage} SupportLanguage
8
+ *
9
+ * @type {Partial<SupportLanguage>[]}
10
+ */
6
11
  const languages = [
7
12
  {
8
- name: "melody",
9
- parsers: ["melody"],
10
- group: "Melody",
11
- tmScope: "melody.twig",
12
- aceMode: "html",
13
- codemirrorMode: "clike",
14
- codemirrorMimeType: "text/melody-twig",
15
- extensions: [".melody.twig", ".html.twig", ".twig"],
16
- linguistLanguageId: 0,
17
- vscodeLanguageIds: ["twig"]
13
+ name: "twig",
14
+ parsers: ["twig"],
15
+ extensions: [".html.twig", ".twig"]
18
16
  }
19
17
  ];
20
18
 
21
- function hasPragma(/* text */) {
22
- return false;
23
- }
24
-
25
- function locStart(/* node */) {
26
- return -1;
27
- }
28
-
29
- function locEnd(/* node */) {
30
- return -1;
31
- }
32
-
19
+ /**
20
+ * @typedef {import('prettier').Parser} Parser
21
+ *
22
+ * @type Record<string, Parser>
23
+ */
33
24
  const parsers = {
34
- melody: {
25
+ twig: {
35
26
  parse,
36
- astFormat: "melody",
37
- hasPragma,
38
- locStart,
39
- locEnd
27
+ astFormat: "twig",
28
+ hasPragma() {
29
+ return false;
30
+ },
31
+ locStart() {
32
+ return -1;
33
+ },
34
+ locEnd() {
35
+ return -1;
36
+ }
40
37
  }
41
38
  };
42
39
 
43
- function canAttachComment(node) {
44
- return node.ast_type && node.ast_type !== "comment";
45
- }
46
-
47
- function printComment(commentPath) {
48
- const comment = commentPath.getValue();
49
-
50
- switch (comment.ast_type) {
51
- case "comment":
52
- return comment.value;
53
- default:
54
- throw new Error("Not a comment: " + JSON.stringify(comment));
55
- }
56
- }
57
-
58
- function clean(ast, newObj) {
59
- delete newObj.lineno;
60
- delete newObj.col_offset;
61
- }
62
-
40
+ /**
41
+ * @typedef {import('prettier').Printer} Printer
42
+ *
43
+ * @type Record<string, Printer>
44
+ */
63
45
  const printers = {
64
- melody: {
46
+ twig: {
65
47
  print,
66
- // hasPrettierIgnore,
67
- printComment,
68
- canAttachComment,
69
- massageAstNode: clean,
70
- willPrintOwnComments: () => true
48
+ printComment(commentPath) {
49
+ const comment = commentPath.getValue();
50
+
51
+ switch (comment.ast_type) {
52
+ case "comment":
53
+ return comment.value;
54
+ default:
55
+ throw new Error(
56
+ "Not a comment: " + JSON.stringify(comment)
57
+ );
58
+ }
59
+ },
60
+ canAttachComment(node) {
61
+ return node.ast_type && node.ast_type !== "comment";
62
+ },
63
+ massageAstNode(ast, newObj) {
64
+ delete newObj.lineno;
65
+ delete newObj.col_offset;
66
+ },
67
+ willPrintOwnComments() {
68
+ return true;
69
+ }
71
70
  }
72
71
  };
73
72
 
73
+ /** @type {import('prettier').Options} Options */
74
74
  const options = {
75
75
  twigMultiTags: {
76
76
  type: "path",
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Copyright 2017 trivago N.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS-IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import lineNumbers from "./lineNumbers.js";
17
+ import repeat from "lodash/repeat.js";
18
+
19
+ const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
20
+
21
+ export default function ({ rawLines, lineNumber, colNumber, length }) {
22
+ const lines = rawLines.split(NEWLINE);
23
+ const start = Math.max(lineNumber - 3, 0);
24
+ const end = Math.min(lineNumber + 3, lines.length);
25
+
26
+ return lineNumbers(lines.slice(start, end), {
27
+ start: start + 1,
28
+ before: " ",
29
+ after: " | ",
30
+ transform(params) {
31
+ if (params.number !== lineNumber) {
32
+ return;
33
+ }
34
+
35
+ if (typeof colNumber === "number") {
36
+ params.line += `\n${params.before}${repeat(" ", params.width)}${
37
+ params.after
38
+ }${repeat(" ", colNumber)}${repeat("^", length)}`;
39
+ }
40
+
41
+ params.before = params.before.replace(/^./, ">");
42
+ }
43
+ }).join("\n");
44
+ }
@@ -0,0 +1,45 @@
1
+ // Copyright 2014, 2015 Simon Lydell
2
+ // X11 (“MIT”) Licensed. (See LICENSE.)
3
+ /**
4
+ * Copyright 2017 trivago N.V.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS-IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ import padStart from "lodash/padStart.js";
19
+
20
+ const get = options => (key, defaultValue) =>
21
+ key in options ? options[key] : defaultValue;
22
+
23
+ function lineNumbers(lines, options) {
24
+ const getOption = get(options);
25
+ const transform = getOption("transform", Function.prototype);
26
+ const padding = getOption("padding", " ");
27
+ const before = getOption("before", " ");
28
+ const after = getOption("after", " | ");
29
+ const start = getOption("start", 1);
30
+ const end = start + lines.length - 1;
31
+ const width = String(end).length;
32
+ return lines.map((line, index) => {
33
+ const number = start + index;
34
+ const params = { before, number, width, after, line };
35
+ transform(params);
36
+ return (
37
+ params.before +
38
+ padStart(params.number, width, padding) +
39
+ params.after +
40
+ params.line
41
+ );
42
+ });
43
+ }
44
+
45
+ export default lineNumbers;
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Copyright 2017 trivago N.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS-IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { unaryOperators, binaryOperators, tests } from "./operators.js";
17
+ import { AutoescapeParser } from "./parser/autoescape.js";
18
+ import { BlockParser } from "./parser/block.js";
19
+ import { DoParser } from "./parser/do.js";
20
+ import { EmbedParser } from "./parser/embed.js";
21
+ import { ExtendsParser } from "./parser/extends.js";
22
+ import { FilterParser } from "./parser/filter.js";
23
+ import { FlushParser } from "./parser/flush.js";
24
+ import { ForParser } from "./parser/for.js";
25
+ import { FromParser } from "./parser/from.js";
26
+ import { IfParser } from "./parser/if.js";
27
+ import { ImportParser } from "./parser/import.js";
28
+ import { IncludeParser } from "./parser/include.js";
29
+ import { MacroParser } from "./parser/macro.js";
30
+ import { SetParser } from "./parser/set.js";
31
+ import { SpacelessParser } from "./parser/spaceless.js";
32
+ import { UseParser } from "./parser/use.js";
33
+ import { MountParser } from "./parser/mount.js";
34
+
35
+ import forVisitor from "./visitors/for.js";
36
+ import testVisitor from "./visitors/tests.js";
37
+ import filters from "./visitors/filters.js";
38
+ import functions from "./visitors/functions.js";
39
+
40
+ const filterMap = [
41
+ "attrs",
42
+ "classes",
43
+ "styles",
44
+ "batch",
45
+ "escape",
46
+ "format",
47
+ "merge",
48
+ "nl2br",
49
+ "number_format",
50
+ "raw",
51
+ "replace",
52
+ "reverse",
53
+ "round",
54
+ "striptags",
55
+ "title",
56
+ "url_encode",
57
+ "trim"
58
+ ].reduce((map, filterName) => {
59
+ map[filterName] = "melody-runtime";
60
+ return map;
61
+ }, Object.create(null));
62
+
63
+ Object.assign(filterMap, filters);
64
+
65
+ const functionMap = [
66
+ "attribute",
67
+ "constant",
68
+ "cycle",
69
+ "date",
70
+ "max",
71
+ "min",
72
+ "random",
73
+ "range",
74
+ "source",
75
+ "template_from_string"
76
+ ].reduce((map, functionName) => {
77
+ map[functionName] = "melody-runtime";
78
+ return map;
79
+ }, Object.create(null));
80
+ Object.assign(functionMap, functions);
81
+
82
+ export const extension = {
83
+ tags: [
84
+ AutoescapeParser,
85
+ BlockParser,
86
+ DoParser,
87
+ EmbedParser,
88
+ ExtendsParser,
89
+ FilterParser,
90
+ FlushParser,
91
+ ForParser,
92
+ FromParser,
93
+ IfParser,
94
+ ImportParser,
95
+ IncludeParser,
96
+ MacroParser,
97
+ SetParser,
98
+ SpacelessParser,
99
+ UseParser,
100
+ MountParser
101
+ ],
102
+ unaryOperators,
103
+ binaryOperators,
104
+ tests,
105
+ visitors: [forVisitor, testVisitor],
106
+ filterMap,
107
+ functionMap
108
+ };
109
+
110
+ export {
111
+ AutoescapeBlock,
112
+ BlockStatement,
113
+ BlockCallExpression,
114
+ MountStatement,
115
+ DoStatement,
116
+ EmbedStatement,
117
+ ExtendsStatement,
118
+ FilterBlockStatement,
119
+ FlushStatement,
120
+ ForStatement,
121
+ ImportDeclaration,
122
+ FromStatement,
123
+ IfStatement,
124
+ IncludeStatement,
125
+ MacroDeclarationStatement,
126
+ VariableDeclarationStatement,
127
+ SetStatement,
128
+ SpacelessBlock,
129
+ AliasExpression,
130
+ UseStatement,
131
+ UnaryNotExpression,
132
+ UnaryNeqExpression,
133
+ UnaryPosExpression,
134
+ BinaryOrExpression,
135
+ BinaryAndExpression,
136
+ BitwiseOrExpression,
137
+ BitwiseXorExpression,
138
+ BitwiseAndExpression,
139
+ BinaryEqualsExpression,
140
+ BinaryNotEqualsExpression,
141
+ BinaryLessThanExpression,
142
+ BinaryGreaterThanExpression,
143
+ BinaryLessThanOrEqualExpression,
144
+ BinaryGreaterThanOrEqualExpression,
145
+ BinaryNotInExpression,
146
+ BinaryInExpression,
147
+ BinaryMatchesExpression,
148
+ BinaryStartsWithExpression,
149
+ BinaryEndsWithExpression,
150
+ BinaryRangeExpression,
151
+ BinaryAddExpression,
152
+ BinaryMulExpression,
153
+ BinaryDivExpression,
154
+ BinaryFloorDivExpression,
155
+ BinaryModExpression,
156
+ BinaryPowerExpression,
157
+ BinaryNullCoalesceExpression,
158
+ TestEvenExpression,
159
+ TestOddExpression,
160
+ TestDefinedExpression,
161
+ TestSameAsExpression,
162
+ TestNullExpression,
163
+ TestDivisibleByExpression,
164
+ TestConstantExpression,
165
+ TestEmptyExpression,
166
+ TestIterableExpression
167
+ } from "./types.js";