@zackad/prettier-plugin-twig 0.7.0-alpha.1 → 0.7.0-alpha.2

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 -4
  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 +665 -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,183 @@
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 {
17
+ EOF_TOKEN,
18
+ ERROR,
19
+ ERROR_TABLE,
20
+ COMMENT,
21
+ WHITESPACE,
22
+ HTML_COMMENT,
23
+ TAG_START,
24
+ TAG_END,
25
+ EXPRESSION_START,
26
+ EXPRESSION_END,
27
+ TEXT,
28
+ STRING
29
+ } from "./TokenTypes.js";
30
+ import trimEnd from "lodash/trimEnd.js";
31
+ import trimStart from "lodash/trimStart.js";
32
+ import codeFrame from "../melody-code-frame/index.js";
33
+
34
+ const TOKENS = Symbol("TOKENS");
35
+ const LENGTH = Symbol("LENGTH");
36
+
37
+ export default class TokenStream {
38
+ constructor(lexer, options) {
39
+ this.input = lexer;
40
+ this.index = 0;
41
+ const mergedOptions = Object.assign(
42
+ {},
43
+ {
44
+ ignoreComments: true,
45
+ ignoreHtmlComments: true,
46
+ ignoreWhitespace: true,
47
+ applyWhitespaceTrimming: true
48
+ },
49
+ options
50
+ );
51
+ this[TOKENS] = getAllTokens(lexer, mergedOptions);
52
+ this[LENGTH] = this[TOKENS].length;
53
+
54
+ if (
55
+ this[TOKENS].length &&
56
+ this[TOKENS][this[TOKENS].length - 1].type === ERROR
57
+ ) {
58
+ const errorToken = this[TOKENS][this[TOKENS].length - 1];
59
+ this.error(
60
+ errorToken.message,
61
+ errorToken.pos,
62
+ errorToken.advice,
63
+ errorToken.endPos.index - errorToken.pos.index || 1
64
+ );
65
+ }
66
+ }
67
+
68
+ la(offset) {
69
+ const index = this.index + offset;
70
+ return index < this[LENGTH] ? this[TOKENS][index] : EOF_TOKEN;
71
+ }
72
+
73
+ lat(offset) {
74
+ return this.la(offset).type;
75
+ }
76
+
77
+ test(type, text) {
78
+ const token = this.la(0);
79
+ return token.type === type && (!text || token.text === text);
80
+ }
81
+
82
+ next() {
83
+ if (this.index === this[LENGTH]) {
84
+ return EOF_TOKEN;
85
+ }
86
+ const token = this[TOKENS][this.index];
87
+ this.index++;
88
+ return token;
89
+ }
90
+
91
+ nextIf(type, text) {
92
+ if (this.test(type, text)) {
93
+ return this.next();
94
+ }
95
+ return false;
96
+ }
97
+
98
+ expect(type, text) {
99
+ const token = this.la(0);
100
+ if (token.type === type && (!text || token.text === text)) {
101
+ return this.next();
102
+ }
103
+ this.error(
104
+ "Invalid Token",
105
+ token.pos,
106
+ `Expected ${ERROR_TABLE[type] || type || text} but found ${
107
+ ERROR_TABLE[token.type] || token.type || token.text
108
+ } instead.`,
109
+ token.length
110
+ );
111
+ }
112
+
113
+ error(message, pos, advice, length = 1, metadata = {}) {
114
+ let errorMessage = `ERROR: ${message}\n`;
115
+ errorMessage += codeFrame({
116
+ rawLines: this.input.source,
117
+ lineNumber: pos.line,
118
+ colNumber: pos.column,
119
+ length,
120
+ tokens: getAllTokens(this.input, {
121
+ ignoreWhitespace: false,
122
+ ignoreComments: false,
123
+ ignoreHtmlComments: false
124
+ })
125
+ });
126
+ if (advice) {
127
+ errorMessage += "\n\n" + advice;
128
+ }
129
+ const result = new Error(errorMessage);
130
+ Object.assign(result, metadata);
131
+ throw result;
132
+ }
133
+ }
134
+
135
+ function getAllTokens(lexer, options) {
136
+ let token;
137
+ const tokens = [];
138
+ let acceptWhitespaceControl = false;
139
+ let trimNext = false;
140
+ while ((token = lexer.next()) !== EOF_TOKEN) {
141
+ const shouldTrimNext = trimNext;
142
+ trimNext = false;
143
+ if (acceptWhitespaceControl) {
144
+ switch (token.type) {
145
+ case EXPRESSION_START:
146
+ case TAG_START:
147
+ if (token.text[token.text.length - 1] === "-") {
148
+ tokens[tokens.length - 1].text = trimEnd(
149
+ tokens[tokens.length - 1].text
150
+ );
151
+ }
152
+ break;
153
+ case EXPRESSION_END:
154
+ case TAG_END:
155
+ if (token.text[0] === "-") {
156
+ trimNext = true;
157
+ }
158
+ break;
159
+ case COMMENT:
160
+ if (tokens[tokens.length - 1].type === TEXT) {
161
+ tokens[tokens.length - 1].text = trimEnd(tokens.text);
162
+ }
163
+ trimNext = true;
164
+ break;
165
+ }
166
+ }
167
+ if (shouldTrimNext && (token.type === TEXT || token.type === STRING)) {
168
+ token.text = trimStart(token.text);
169
+ }
170
+ if (
171
+ (token.type !== COMMENT || !options.ignoreComments) &&
172
+ (token.type !== WHITESPACE || !options.ignoreWhitespace) &&
173
+ (token.type !== HTML_COMMENT || !options.ignoreHtmlComments)
174
+ ) {
175
+ tokens[tokens.length] = token;
176
+ }
177
+ acceptWhitespaceControl = options.applyWhitespaceTrimming;
178
+ if (token.type === ERROR) {
179
+ return tokens;
180
+ }
181
+ }
182
+ return tokens;
183
+ }
@@ -0,0 +1,74 @@
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
+ export const EXPRESSION_START = "expressionStart";
17
+ export const EXPRESSION_END = "expressionEnd";
18
+ export const TAG_START = "tagStart";
19
+ export const TAG_END = "tagEnd";
20
+ export const INTERPOLATION_START = "interpolationStart";
21
+ export const INTERPOLATION_END = "interpolationEnd";
22
+ export const STRING_START = "stringStart";
23
+ export const STRING_END = "stringEnd";
24
+ export const DECLARATION_START = "declarationStart";
25
+ export const COMMENT = "comment";
26
+ export const WHITESPACE = "whitespace";
27
+ export const HTML_COMMENT = "htmlComment";
28
+ export const TEXT = "text";
29
+ export const ENTITY = "entity";
30
+ export const SYMBOL = "symbol";
31
+ export const STRING = "string";
32
+ export const OPERATOR = "operator";
33
+ export const TRUE = "true";
34
+ export const FALSE = "false";
35
+ export const NULL = "null";
36
+ export const LBRACE = "[";
37
+ export const RBRACE = "]";
38
+ export const LPAREN = "(";
39
+ export const RPAREN = ")";
40
+ export const LBRACKET = "{";
41
+ export const RBRACKET = "}";
42
+ export const COLON = ":";
43
+ export const COMMA = ",";
44
+ export const DOT = ".";
45
+ export const PIPE = "|";
46
+ export const QUESTION_MARK = "?";
47
+ export const ASSIGNMENT = "=";
48
+ export const ELEMENT_START = "<";
49
+ export const SLASH = "/";
50
+ export const ELEMENT_END = ">";
51
+ export const NUMBER = "number";
52
+ export const EOF = "EOF";
53
+ export const ERROR = "ERROR";
54
+ export const EOF_TOKEN = {
55
+ type: EOF,
56
+ pos: {
57
+ index: -1,
58
+ line: -1,
59
+ pos: -1
60
+ },
61
+ end: -1,
62
+ length: 0,
63
+ source: null,
64
+ text: ""
65
+ };
66
+
67
+ export const ERROR_TABLE = {
68
+ [EXPRESSION_END]: 'expression end "}}"',
69
+ [EXPRESSION_START]: 'expression start "{{"',
70
+ [TAG_START]: 'tag start "{%"',
71
+ [TAG_END]: 'tag end "%}"',
72
+ [INTERPOLATION_START]: 'interpolation start "#{"',
73
+ [INTERPOLATION_END]: 'interpolation end "}"'
74
+ };
@@ -0,0 +1,43 @@
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
+ // https://www.w3.org/TR/html5/syntax.html#void-elements
17
+ export const voidElements = {
18
+ area: true,
19
+ base: true,
20
+ br: true,
21
+ col: true,
22
+ embed: true,
23
+ hr: true,
24
+ img: true,
25
+ input: true,
26
+ keygen: true,
27
+ link: true,
28
+ meta: true,
29
+ param: true,
30
+ source: true,
31
+ track: true,
32
+ wbr: true
33
+ };
34
+
35
+ export const rawTextElements = {
36
+ script: true,
37
+ style: true
38
+ };
39
+
40
+ export const escapableRawTextElements = {
41
+ textarea: true,
42
+ title: true
43
+ };
@@ -0,0 +1,94 @@
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 Parser from "./Parser.js";
17
+ import TokenStream from "./TokenStream.js";
18
+ import * as Types from "./TokenTypes.js";
19
+ import Lexer from "./Lexer.js";
20
+ import { EOF, CharStream } from "./CharStream.js";
21
+ import { LEFT, RIGHT } from "./Associativity.js";
22
+ import {
23
+ setStartFromToken,
24
+ setEndFromToken,
25
+ copyStart,
26
+ copyEnd,
27
+ copyLoc,
28
+ getNodeSource,
29
+ createNode,
30
+ hasTagStartTokenTrimLeft,
31
+ hasTagEndTokenTrimRight,
32
+ isMelodyExtension
33
+ } from "./util.js";
34
+
35
+ function parse(code, options, ...extensions) {
36
+ return createExtendedParser(code, options, ...extensions).parse();
37
+ }
38
+
39
+ function createExtendedParser(code, options, ...extensions) {
40
+ let passedOptions = options;
41
+ const passedExtensions = extensions;
42
+ if (isMelodyExtension(options)) {
43
+ // Variant without options parameter: createExtendedParser(code, ...extensions)
44
+ passedOptions = undefined;
45
+ passedExtensions.unshift(options);
46
+ }
47
+ const lexer = createExtendedLexer(code, options, ...passedExtensions);
48
+ const parser = new Parser(
49
+ new TokenStream(lexer, passedOptions),
50
+ passedOptions
51
+ );
52
+ for (const ext of passedExtensions) {
53
+ parser.applyExtension(ext);
54
+ }
55
+ return parser;
56
+ }
57
+
58
+ function createExtendedLexer(code, options, ...extensions) {
59
+ let passedOptions = options;
60
+ const passedExtensions = extensions;
61
+ if (isMelodyExtension(options)) {
62
+ // Variant without options parameter: createExtendedLexer(code, ...extensions)
63
+ passedOptions = undefined;
64
+ passedExtensions.unshift(options);
65
+ }
66
+ const lexer = new Lexer(new CharStream(code), passedOptions);
67
+ for (const ext of passedExtensions) {
68
+ lexer.applyExtension(ext);
69
+ }
70
+ return lexer;
71
+ }
72
+
73
+ export {
74
+ Parser,
75
+ TokenStream,
76
+ Lexer,
77
+ EOF,
78
+ CharStream,
79
+ LEFT,
80
+ RIGHT,
81
+ parse,
82
+ createExtendedLexer,
83
+ createExtendedParser,
84
+ setStartFromToken,
85
+ setEndFromToken,
86
+ copyStart,
87
+ copyEnd,
88
+ copyLoc,
89
+ getNodeSource,
90
+ createNode,
91
+ hasTagStartTokenTrimLeft,
92
+ hasTagEndTokenTrimRight,
93
+ Types
94
+ };
@@ -0,0 +1,100 @@
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
+ export function setStartFromToken(node, { pos: { index, line, column } }) {
17
+ node.loc.start = { line, column, index };
18
+ return node;
19
+ }
20
+
21
+ export function setEndFromToken(node, { pos: { line, column }, end }) {
22
+ node.loc.end = { line, column, index: end };
23
+ return node;
24
+ }
25
+
26
+ export function setMarkFromToken(
27
+ node,
28
+ propertyName,
29
+ { pos: { index, line, column } }
30
+ ) {
31
+ node[propertyName] = { line, column, index };
32
+ return node;
33
+ }
34
+
35
+ export function copyStart(
36
+ node,
37
+ {
38
+ loc: {
39
+ start: { line, column, index }
40
+ }
41
+ }
42
+ ) {
43
+ node.loc.start.line = line;
44
+ node.loc.start.column = column;
45
+ node.loc.start.index = index;
46
+ return node;
47
+ }
48
+
49
+ export function copyEnd(node, end) {
50
+ node.loc.end.line = end.loc.end.line;
51
+ node.loc.end.column = end.loc.end.column;
52
+ node.loc.end.index = end.loc.end.index;
53
+ return node;
54
+ }
55
+
56
+ export function getNodeSource(node, entireSource) {
57
+ if (entireSource && node.loc.start && node.loc.end) {
58
+ return entireSource.substring(node.loc.start.index, node.loc.end.index);
59
+ }
60
+ return "";
61
+ }
62
+
63
+ export function copyLoc(node, { loc: { start, end } }) {
64
+ node.loc.start.line = start.line;
65
+ node.loc.start.column = start.column;
66
+ node.loc.start.index = start.index;
67
+ node.loc.end.line = end.line;
68
+ node.loc.end.column = end.column;
69
+ node.loc.end.index = end.index;
70
+ return node;
71
+ }
72
+
73
+ export function createNode(Type, token, ...args) {
74
+ return setEndFromToken(setStartFromToken(new Type(...args), token), token);
75
+ }
76
+
77
+ export function startNode(Type, token, ...args) {
78
+ return setStartFromToken(new Type(...args), token);
79
+ }
80
+
81
+ export function hasTagStartTokenTrimLeft(token) {
82
+ return token.text.endsWith("-");
83
+ }
84
+
85
+ export function hasTagEndTokenTrimRight(token) {
86
+ return token.text.startsWith("-");
87
+ }
88
+
89
+ export function isMelodyExtension(obj) {
90
+ return (
91
+ obj &&
92
+ (Array.isArray(obj.binaryOperators) ||
93
+ typeof obj.filterMap === "object" ||
94
+ typeof obj.functionMap === "object" ||
95
+ Array.isArray(obj.tags) ||
96
+ Array.isArray(obj.tests) ||
97
+ Array.isArray(obj.unaryOperators) ||
98
+ Array.isArray(obj.visitors))
99
+ );
100
+ }
@@ -0,0 +1,61 @@
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
+ export class Binding {
17
+ constructor(identifier, scope, path, kind = "global") {
18
+ this.identifier = identifier;
19
+ this.scope = scope;
20
+ this.path = path;
21
+ this.kind = kind;
22
+
23
+ this.referenced = false;
24
+ this.references = 0;
25
+ this.referencePaths = [];
26
+ this.definitionPaths = [];
27
+ this.shadowedBinding = null;
28
+ this.contextual = false;
29
+
30
+ this.data = Object.create(null);
31
+ }
32
+
33
+ getData(key) {
34
+ return this.data[key];
35
+ }
36
+
37
+ setData(key, value) {
38
+ this.data[key] = value;
39
+ }
40
+
41
+ reference(path) {
42
+ this.referenced = true;
43
+ this.references++;
44
+ this.referencePaths.push(path);
45
+ }
46
+
47
+ // dereference(path) {
48
+ // if (path) {
49
+ // this.referencePaths.splice(this.referencePaths.indexOf(path), 1);
50
+ // }
51
+ // this.references--;
52
+ // this.referenced = !!this.references;
53
+ // }
54
+
55
+ getRootDefinition() {
56
+ if (this.shadowedBinding) {
57
+ return this.shadowedBinding.getRootDefinition();
58
+ }
59
+ return this;
60
+ }
61
+ }