@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
@@ -0,0 +1,82 @@
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
+
17
+ export const EOF = Symbol("EOF");
18
+
19
+ export class CharStream {
20
+ constructor(input) {
21
+ this.input = String(input);
22
+ this.length = this.input.length;
23
+ this.index = 0;
24
+ this.position = { line: 1, column: 0 };
25
+ }
26
+
27
+ get source() {
28
+ return this.input;
29
+ }
30
+
31
+ reset() {
32
+ this.rewind({ line: 1, column: 0, index: 0 });
33
+ }
34
+
35
+ mark() {
36
+ const { line, column } = this.position;
37
+ const index = this.index;
38
+ return { line, column, index };
39
+ }
40
+
41
+ rewind(marker) {
42
+ this.position.line = marker.line;
43
+ this.position.column = marker.column;
44
+ this.index = marker.index;
45
+ }
46
+
47
+ la(offset) {
48
+ const index = this.index + offset;
49
+ return index < this.length ? this.input.charAt(index) : EOF;
50
+ }
51
+
52
+ lac(offset) {
53
+ const index = this.index + offset;
54
+ return index < this.length ? this.input.codePointAt(index) : EOF;
55
+ }
56
+
57
+ next() {
58
+ if (this.index === this.length) {
59
+ return EOF;
60
+ }
61
+ const ch = this.input.charAt(this.index);
62
+ this.index++;
63
+ this.position.column++;
64
+ if (ch === "\n") {
65
+ this.position.line += 1;
66
+ this.position.column = 0;
67
+ }
68
+ return ch;
69
+ }
70
+
71
+ match(str) {
72
+ const start = this.mark();
73
+ for (let i = 0, len = str.length; i < len; i++) {
74
+ const ch = this.next();
75
+ if (ch !== str.charAt(i) || ch === EOF) {
76
+ this.rewind(start);
77
+ return false;
78
+ }
79
+ }
80
+ return true;
81
+ }
82
+ }
@@ -0,0 +1,66 @@
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 { setStartFromToken, setEndFromToken } from "./util.js";
17
+ import { GenericTagParser } from "./GenericTagParser.js";
18
+ import * as Types from "./TokenTypes.js";
19
+
20
+ const tagMatchesOneOf = (tokenStream, tagNames) => {
21
+ for (let i = 0; i < tagNames.length; i++) {
22
+ if (tokenStream.test(Types.SYMBOL, tagNames[i])) {
23
+ return true;
24
+ }
25
+ }
26
+ return false;
27
+ };
28
+
29
+ export const createMultiTagParser = (tagName, subTags = []) => ({
30
+ name: "genericTwigMultiTag",
31
+ parse(parser, token) {
32
+ const tokens = parser.tokens;
33
+ const tagStartToken = tokens.la(-1);
34
+
35
+ if (subTags.length === 0) {
36
+ subTags.push("end" + tagName);
37
+ }
38
+
39
+ const twigTag = GenericTagParser.parse(parser, token);
40
+ let currentTagName = tagName;
41
+ const endTagName = subTags[subTags.length - 1];
42
+
43
+ while (currentTagName !== endTagName) {
44
+ // Parse next section
45
+ twigTag.sections.push(
46
+ parser.parse((tokenText, token, tokens) => {
47
+ const hasReachedNextTag =
48
+ token.type === Types.TAG_START &&
49
+ tagMatchesOneOf(tokens, subTags);
50
+ return hasReachedNextTag;
51
+ })
52
+ );
53
+ tokens.next(); // Get past "{%"
54
+
55
+ // Parse next tag
56
+ const childTag = GenericTagParser.parse(parser);
57
+ twigTag.sections.push(childTag);
58
+ currentTagName = childTag.tagName;
59
+ }
60
+
61
+ setStartFromToken(twigTag, tagStartToken);
62
+ setEndFromToken(twigTag, tokens.la(0));
63
+
64
+ return twigTag;
65
+ }
66
+ });
@@ -0,0 +1,53 @@
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 { hasTagStartTokenTrimLeft, hasTagEndTokenTrimRight } from "./util.js";
17
+ import * as Types from "./TokenTypes.js";
18
+ import * as n from "../melody-types/index.js";
19
+
20
+ export const GenericTagParser = {
21
+ name: "genericTwigTag",
22
+ parse(parser) {
23
+ const tokens = parser.tokens;
24
+ const tagStartToken = tokens.la(-2);
25
+ let currentToken;
26
+
27
+ const twigTag = new n.GenericTwigTag(tokens.la(-1).text);
28
+ while ((currentToken = tokens.la(0))) {
29
+ if (currentToken.type === Types.TAG_END) {
30
+ break;
31
+ } else {
32
+ try {
33
+ twigTag.parts.push(parser.matchExpression());
34
+ } catch (e) {
35
+ if (e.errorType === "UNEXPECTED_TOKEN") {
36
+ twigTag.parts.push(
37
+ new n.GenericToken(e.tokenType, e.tokenText)
38
+ );
39
+ tokens.next();
40
+ } else {
41
+ throw e;
42
+ }
43
+ }
44
+ }
45
+ }
46
+ tokens.expect(Types.TAG_END);
47
+
48
+ twigTag.trimLeft = hasTagStartTokenTrimLeft(tagStartToken);
49
+ twigTag.trimRight = hasTagEndTokenTrimRight(currentToken);
50
+
51
+ return twigTag;
52
+ }
53
+ };