@zackad/prettier-plugin-twig 0.16.3 → 0.18.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.
@@ -23,13 +23,16 @@ import {
23
23
  copyStart,
24
24
  copyEnd,
25
25
  copyLoc,
26
- createNode
26
+ createNode,
27
+ attachLeadingComments,
28
+ attachListItemComments,
29
+ attachTrailingComments,
30
+ INLINE_COMMENT_ERROR
27
31
  } from "./util.js";
28
32
  import { GenericTagParser } from "./GenericTagParser.js";
29
33
  import { createMultiTagParser } from "./GenericMultiTagParser.js";
30
34
  import { voidElements } from "./elementInfo.js";
31
35
  import * as he from "he";
32
- import { Attribute } from "../melody-types/index.js";
33
36
 
34
37
  /**
35
38
  * @typedef {Object} UnaryOperator
@@ -158,99 +161,182 @@ export default class Parser {
158
161
  setEndFromToken(p, token);
159
162
  return p;
160
163
  }
161
- switch (token.type) {
162
- case Types.EXPRESSION_START: {
163
- const expression = this.matchExpression();
164
- const statement = new n.PrintExpressionStatement(
165
- expression
164
+ const statementCount = p.expressions.length;
165
+ try {
166
+ this.parseToken(token, p);
167
+ if (p.expressions.length > statementCount) {
168
+ const statement = p.expressions[p.expressions.length - 1];
169
+ const commentError = tokens.claimUnattachedComments(
170
+ token.pos.index,
171
+ tokens.la(-1).end
166
172
  );
167
- const endToken = tokens.expect(Types.EXPRESSION_END);
168
- setStartFromToken(statement, token);
169
- setEndFromToken(statement, endToken);
170
- setEndFromToken(p, endToken);
171
- statement.trimLeft = !!expression.trimLeft;
172
- statement.trimRight = !!expression.trimRight;
173
- p.add(statement);
173
+ if (commentError) {
174
+ statement[INLINE_COMMENT_ERROR] = commentError;
175
+ }
176
+ }
177
+ } catch (e) {
178
+ if (e.errorType === "UNEXPECTED_TOKEN") {
179
+ this.parseUnexpectedToken(e, token, p);
180
+ } else {
181
+ throw e;
182
+ }
183
+ }
184
+ }
185
+ return p;
186
+ }
174
187
 
175
- break;
188
+ /**
189
+ * @param {SequenceExpression} p
190
+ */
191
+ parseToken(token, p) {
192
+ const tokens = this.tokens;
193
+ switch (token.type) {
194
+ case Types.EXPRESSION_START: {
195
+ const expressionStartToken = tokens.la(0);
196
+ const expression = this.matchExpression();
197
+ attachLeadingComments(expression, tokens, expressionStartToken);
198
+ attachTrailingComments(expression, tokens, tokens.la(0));
199
+ const statement = new n.PrintExpressionStatement(expression);
200
+ const endToken = tokens.expect(Types.EXPRESSION_END);
201
+ setStartFromToken(statement, token);
202
+ setEndFromToken(statement, endToken);
203
+ setEndFromToken(p, endToken);
204
+ statement.trimLeft = !!expression.trimLeft;
205
+ statement.trimRight = !!expression.trimRight;
206
+ p.add(statement);
207
+
208
+ break;
209
+ }
210
+ case Types.TAG_START:
211
+ p.add(this.matchTag());
212
+ break;
213
+ case Types.TEXT: {
214
+ const textStringLiteral = createNode(
215
+ n.StringLiteral,
216
+ token,
217
+ token.text
218
+ );
219
+ const textTextStatement = createNode(
220
+ n.PrintTextStatement,
221
+ token,
222
+ textStringLiteral
223
+ );
224
+ p.add(textTextStatement);
225
+ break;
226
+ }
227
+ case Types.ENTITY: {
228
+ const entityStringLiteral = createNode(
229
+ n.StringLiteral,
230
+ token,
231
+ !this.options.decodeEntities ||
232
+ this.options.preserveSourceLiterally
233
+ ? token.text
234
+ : he.decode(token.text)
235
+ );
236
+ const entityTextStatement = createNode(
237
+ n.PrintTextStatement,
238
+ token,
239
+ entityStringLiteral
240
+ );
241
+ p.add(entityTextStatement);
242
+ break;
243
+ }
244
+ case Types.ELEMENT_START:
245
+ p.add(this.matchElement());
246
+ break;
247
+ case Types.DECLARATION_START: {
248
+ const declarationNode = this.matchDeclaration();
249
+ if (!this.options.ignoreDeclarations) {
250
+ p.add(declarationNode);
176
251
  }
177
- case Types.TAG_START:
178
- p.add(this.matchTag());
179
- break;
180
- case Types.TEXT: {
181
- const textStringLiteral = createNode(
252
+ break;
253
+ }
254
+ case Types.COMMENT:
255
+ if (!this.options.ignoreComments) {
256
+ const stringLiteral = createNode(
182
257
  n.StringLiteral,
183
258
  token,
184
259
  token.text
185
260
  );
186
- const textTextStatement = createNode(
187
- n.PrintTextStatement,
261
+ const twigComment = createNode(
262
+ n.TwigComment,
188
263
  token,
189
- textStringLiteral
264
+ stringLiteral
190
265
  );
191
- p.add(textTextStatement);
192
- break;
266
+ p.add(twigComment);
193
267
  }
194
- case Types.ENTITY: {
195
- const entityStringLiteral = createNode(
268
+ break;
269
+ case Types.HTML_COMMENT:
270
+ if (!this.options.ignoreHtmlComments) {
271
+ const stringLiteral = createNode(
196
272
  n.StringLiteral,
197
273
  token,
198
- !this.options.decodeEntities ||
199
- this.options.preserveSourceLiterally
200
- ? token.text
201
- : he.decode(token.text)
274
+ token.text
202
275
  );
203
- const entityTextStatement = createNode(
204
- n.PrintTextStatement,
276
+ const htmlComment = createNode(
277
+ n.HtmlComment,
205
278
  token,
206
- entityStringLiteral
279
+ stringLiteral
207
280
  );
208
- p.add(entityTextStatement);
209
- break;
281
+ p.add(htmlComment);
210
282
  }
211
- case Types.ELEMENT_START:
212
- p.add(this.matchElement());
213
- break;
214
- case Types.DECLARATION_START: {
215
- const declarationNode = this.matchDeclaration();
216
- if (!this.options.ignoreDeclarations) {
217
- p.add(declarationNode);
283
+ break;
284
+ }
285
+ }
286
+
287
+ /**
288
+ * @param {Error} cause
289
+ * @param {SequenceExpression} p
290
+ */
291
+ parseUnexpectedToken(cause, startToken, p) {
292
+ const tokens = this.tokens;
293
+ switch (startToken.type) {
294
+ case Types.DECLARATION_START:
295
+ case Types.EXPRESSION_START:
296
+ case Types.TAG_START: {
297
+ const END_DELIMITER = Types.DELIMITER_TABLE[startToken.type];
298
+ const unexpectedToken = cause.token;
299
+ let currentToken;
300
+ while ((currentToken = tokens.la(0))) {
301
+ if (
302
+ currentToken.type === Types.EOF ||
303
+ currentToken.type === END_DELIMITER
304
+ ) {
305
+ break;
306
+ } else {
307
+ tokens.next();
218
308
  }
219
- break;
220
309
  }
221
- case Types.COMMENT:
222
- if (!this.options.ignoreComments) {
223
- const stringLiteral = createNode(
224
- n.StringLiteral,
225
- token,
226
- token.text
227
- );
228
- const twigComment = createNode(
229
- n.TwigComment,
230
- token,
231
- stringLiteral
232
- );
233
- p.add(twigComment);
234
- }
235
- break;
236
- case Types.HTML_COMMENT:
237
- if (!this.options.ignoreHtmlComments) {
238
- const stringLiteral = createNode(
239
- n.StringLiteral,
240
- token,
241
- token.text
242
- );
243
- const htmlComment = createNode(
244
- n.HtmlComment,
245
- token,
246
- stringLiteral
247
- );
248
- p.add(htmlComment);
249
- }
250
- break;
310
+ const endToken = tokens.expect(END_DELIMITER);
311
+ // The statement is kept verbatim, including its comments
312
+ tokens.takeCommentsInRange(startToken.pos.index, endToken.end);
313
+
314
+ const start = Math.max(startToken.pos.index - 1, 0);
315
+ const end = Math.min(
316
+ endToken.endPos.index,
317
+ endToken.source.length
318
+ );
319
+ const slicedSource = unexpectedToken.source.slice(start, end);
320
+
321
+ const erroredSource = createNode(
322
+ n.StringLiteral,
323
+ unexpectedToken,
324
+ slicedSource
325
+ );
326
+ const erroredStatement = createNode(
327
+ n.PrintErroredStatement,
328
+ unexpectedToken,
329
+ erroredSource,
330
+ cause
331
+ );
332
+
333
+ setStartFromToken(erroredStatement, startToken);
334
+ setEndFromToken(erroredStatement, currentToken);
335
+
336
+ p.add(erroredStatement);
337
+ break;
251
338
  }
252
339
  }
253
- return p;
254
340
  }
255
341
 
256
342
  /**
@@ -649,8 +735,7 @@ export default class Parser {
649
735
  },
650
736
  {
651
737
  errorType: "UNEXPECTED_TOKEN",
652
- tokenText: token.text,
653
- tokenType: token.type
738
+ token
654
739
  }
655
740
  );
656
741
  }
@@ -770,6 +855,7 @@ export default class Parser {
770
855
  while (!tokens.test(Types.RBRACKET) && !tokens.test(Types.EOF)) {
771
856
  let computed = false;
772
857
  let key;
858
+ const propertyStartToken = tokens.la(0);
773
859
 
774
860
  // find the key part of object property
775
861
  if (tokens.test(Types.STRING_START)) {
@@ -799,6 +885,12 @@ export default class Parser {
799
885
  const prop = new n.ObjectProperty(value, computed, key);
800
886
  copyStart(prop, key);
801
887
  copyEnd(prop, value);
888
+ attachListItemComments(
889
+ prop,
890
+ obj.properties[obj.properties.length - 1],
891
+ tokens,
892
+ propertyStartToken
893
+ );
802
894
  obj.properties.push(prop);
803
895
  } else {
804
896
  // when the part is missing, we can assume that it is being omitted
@@ -810,6 +902,12 @@ export default class Parser {
810
902
  const prop = new n.ObjectProperty(value, computed);
811
903
  copyStart(prop, key);
812
904
  copyEnd(prop, value);
905
+ attachListItemComments(
906
+ prop,
907
+ obj.properties[obj.properties.length - 1],
908
+ tokens,
909
+ propertyStartToken
910
+ );
813
911
  obj.properties.push(prop);
814
912
  }
815
913
 
@@ -821,6 +919,11 @@ export default class Parser {
821
919
  }
822
920
  }
823
921
  }
922
+ attachTrailingComments(
923
+ obj.properties[obj.properties.length - 1],
924
+ tokens,
925
+ tokens.la(0)
926
+ );
824
927
  setEndFromToken(obj, tokens.expect(Types.RBRACKET));
825
928
  return obj;
826
929
  }
@@ -18,6 +18,7 @@ import {
18
18
  ERROR,
19
19
  ERROR_TABLE,
20
20
  COMMENT,
21
+ INLINE_COMMENT,
21
22
  WHITESPACE,
22
23
  HTML_COMMENT,
23
24
  TAG_START,
@@ -33,6 +34,7 @@ import codeFrame from "../melody-code-frame/index.js";
33
34
 
34
35
  const TOKENS = Symbol("TOKENS");
35
36
  const LENGTH = Symbol("LENGTH");
37
+ const INLINE_COMMENTS = Symbol("INLINE_COMMENTS");
36
38
 
37
39
  export default class TokenStream {
38
40
  constructor(lexer, options) {
@@ -48,23 +50,59 @@ export default class TokenStream {
48
50
  },
49
51
  options
50
52
  );
51
- this[TOKENS] = getAllTokens(lexer, mergedOptions);
53
+ this[INLINE_COMMENTS] = [];
54
+ this[TOKENS] = getAllTokens(
55
+ lexer,
56
+ mergedOptions,
57
+ this[INLINE_COMMENTS]
58
+ );
52
59
  this[LENGTH] = this[TOKENS].length;
60
+ }
53
61
 
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
- );
62
+ takeLeadingComments(token, filter = () => true) {
63
+ if (!token || !token.leadingComments) {
64
+ return [];
65
+ }
66
+ const comments = token.leadingComments.filter(filter);
67
+ comments.forEach(c => (c.handled = true));
68
+ return comments;
69
+ }
70
+
71
+ takeCommentsInRange(start, end) {
72
+ for (const comment of this[INLINE_COMMENTS]) {
73
+ if (comment.pos.index >= start && comment.end <= end) {
74
+ comment.handled = true;
75
+ }
76
+ }
77
+ }
78
+
79
+ // The innermost statement reports them, so prettier-ignore can keep it verbatim
80
+ claimUnattachedComments(start, end) {
81
+ const comments = this[INLINE_COMMENTS].filter(
82
+ c => !c.handled && c.pos.index >= start && c.end <= end
83
+ );
84
+ comments.forEach(c => (c.handled = true));
85
+ return comments.length > 0
86
+ ? this.createUnsupportedCommentError(comments[0])
87
+ : null;
88
+ }
89
+
90
+ assertInlineCommentsAttached() {
91
+ const comment = this[INLINE_COMMENTS].find(c => !c.handled);
92
+ if (comment) {
93
+ throw this.createUnsupportedCommentError(comment);
65
94
  }
66
95
  }
67
96
 
97
+ createUnsupportedCommentError(comment) {
98
+ return this.createError(
99
+ "Unsupported inline comment position",
100
+ comment.pos,
101
+ "Inline comments are only supported on their own line before a variable declaration in the props, types, set, for and macro tags, or before a key in a hash.",
102
+ comment.length
103
+ );
104
+ }
105
+
68
106
  la(offset) {
69
107
  const index = this.index + offset;
70
108
  return index < this[LENGTH] ? this[TOKENS][index] : EOF_TOKEN;
@@ -85,6 +123,18 @@ export default class TokenStream {
85
123
  }
86
124
  const token = this[TOKENS][this.index];
87
125
  this.index++;
126
+ if (token.type === ERROR) {
127
+ this.error(
128
+ token.message,
129
+ token.pos,
130
+ token.advice,
131
+ token.endPos.index - token.pos.index || 1,
132
+ {
133
+ errorType: "UNEXPECTED_TOKEN",
134
+ token
135
+ }
136
+ );
137
+ }
88
138
  return token;
89
139
  }
90
140
 
@@ -106,11 +156,19 @@ export default class TokenStream {
106
156
  `Expected ${ERROR_TABLE[type] || type || text} but found ${
107
157
  ERROR_TABLE[token.type] || token.type || token.text
108
158
  } instead.`,
109
- token.length
159
+ token.length,
160
+ {
161
+ errorType: "UNEXPECTED_TOKEN",
162
+ token
163
+ }
110
164
  );
111
165
  }
112
166
 
113
167
  error(message, pos, advice, length = 1, metadata = {}) {
168
+ throw this.createError(message, pos, advice, length, metadata);
169
+ }
170
+
171
+ createError(message, pos, advice, length = 1, metadata = {}) {
114
172
  let errorMessage = `ERROR: ${message}\n`;
115
173
  errorMessage += codeFrame({
116
174
  rawLines: this.input.source,
@@ -128,16 +186,32 @@ export default class TokenStream {
128
186
  }
129
187
  const result = new Error(errorMessage);
130
188
  Object.assign(result, metadata);
131
- throw result;
189
+ return result;
132
190
  }
133
191
  }
134
192
 
135
- function getAllTokens(lexer, options) {
193
+ function getAllTokens(lexer, options, inlineComments = []) {
136
194
  let token;
137
195
  const tokens = [];
138
196
  let acceptWhitespaceControl = false;
139
197
  let trimNext = false;
198
+ let pendingInlineComments = [];
199
+ let previousToken = null;
140
200
  while ((token = lexer.next()) !== EOF_TOKEN) {
201
+ if (token.type === INLINE_COMMENT) {
202
+ token.trailing =
203
+ !!previousToken && previousToken.endPos.line === token.pos.line;
204
+ inlineComments.push(token);
205
+ pendingInlineComments.push(token);
206
+ continue;
207
+ }
208
+ if (token.type !== WHITESPACE) {
209
+ previousToken = token;
210
+ if (pendingInlineComments.length > 0) {
211
+ token.leadingComments = pendingInlineComments;
212
+ pendingInlineComments = [];
213
+ }
214
+ }
141
215
  const shouldTrimNext = trimNext;
142
216
  trimNext = false;
143
217
  if (acceptWhitespaceControl) {
@@ -175,9 +249,6 @@ function getAllTokens(lexer, options) {
175
249
  tokens[tokens.length] = token;
176
250
  }
177
251
  acceptWhitespaceControl = options.applyWhitespaceTrimming;
178
- if (token.type === ERROR) {
179
- return tokens;
180
- }
181
252
  }
182
253
  return tokens;
183
254
  }
@@ -23,6 +23,7 @@ export const STRING_START = "stringStart";
23
23
  export const STRING_END = "stringEnd";
24
24
  export const DECLARATION_START = "declarationStart";
25
25
  export const COMMENT = "comment";
26
+ export const INLINE_COMMENT = "inlineComment";
26
27
  export const WHITESPACE = "whitespace";
27
28
  export const HTML_COMMENT = "htmlComment";
28
29
  export const TEXT = "text";
@@ -65,6 +66,15 @@ export const EOF_TOKEN = {
65
66
  text: ""
66
67
  };
67
68
 
69
+ export const DELIMITER_TABLE = {
70
+ [DECLARATION_START]: ELEMENT_END,
71
+ [ELEMENT_START]: ELEMENT_END,
72
+ [EXPRESSION_START]: EXPRESSION_END,
73
+ [INTERPOLATION_START]: INTERPOLATION_END,
74
+ [STRING_START]: STRING_END,
75
+ [TAG_START]: TAG_END
76
+ };
77
+
68
78
  export const ERROR_TABLE = {
69
79
  [EXPRESSION_END]: 'expression end "}}"',
70
80
  [EXPRESSION_START]: 'expression start "{{"',
@@ -27,6 +27,10 @@ import {
27
27
  copyLoc,
28
28
  getNodeSource,
29
29
  createNode,
30
+ attachLeadingComments,
31
+ attachListItemComments,
32
+ attachTrailingComments,
33
+ INLINE_COMMENT_ERROR,
30
34
  hasTagStartTokenTrimLeft,
31
35
  hasTagEndTokenTrimRight,
32
36
  isMelodyExtension
@@ -88,6 +92,10 @@ export {
88
92
  copyLoc,
89
93
  getNodeSource,
90
94
  createNode,
95
+ attachLeadingComments,
96
+ attachListItemComments,
97
+ attachTrailingComments,
98
+ INLINE_COMMENT_ERROR,
91
99
  hasTagStartTokenTrimLeft,
92
100
  hasTagEndTokenTrimRight,
93
101
  Types
@@ -13,6 +13,8 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
+ export const INLINE_COMMENT_ERROR = Symbol("INLINE_COMMENT_ERROR");
17
+
16
18
  export function setStartFromToken(node, { pos: { index, line, column } }) {
17
19
  node.loc.start = { line, column, index };
18
20
  return node;
@@ -74,6 +76,39 @@ export function createNode(Type, token, ...args) {
74
76
  return setEndFromToken(setStartFromToken(new Type(...args), token), token);
75
77
  }
76
78
 
79
+ function addComment(node, key, comment) {
80
+ node[key] = node[key] || [];
81
+ node[key].push(comment.text.trimEnd());
82
+ }
83
+
84
+ export function attachLeadingComments(node, tokens, token) {
85
+ for (const comment of tokens.takeLeadingComments(token)) {
86
+ addComment(node, "leadingComments", comment);
87
+ }
88
+ return node;
89
+ }
90
+
91
+ // Comments on the line of the previous list item stay with that item
92
+ export function attachListItemComments(node, previousNode, tokens, token) {
93
+ for (const comment of tokens.takeLeadingComments(token)) {
94
+ if (comment.trailing && previousNode) {
95
+ addComment(previousNode, "trailingComments", comment);
96
+ } else {
97
+ addComment(node, "leadingComments", comment);
98
+ }
99
+ }
100
+ return node;
101
+ }
102
+
103
+ export function attachTrailingComments(node, tokens, token) {
104
+ if (!node) {
105
+ return;
106
+ }
107
+ for (const comment of tokens.takeLeadingComments(token, c => c.trailing)) {
108
+ addComment(node, "trailingComments", comment);
109
+ }
110
+ }
111
+
77
112
  export function startNode(Type, token, ...args) {
78
113
  return setStartFromToken(new Type(...args), token);
79
114
  }
@@ -40,7 +40,9 @@ export class Node {
40
40
  }
41
41
  const value = this[name];
42
42
  if (Array.isArray(value)) {
43
- acc[name] = value.map(val => val.toJSON());
43
+ acc[name] = value.map(val =>
44
+ val && val.toJSON ? val.toJSON() : val
45
+ );
44
46
  } else {
45
47
  acc[name] = value && value.toJSON ? value.toJSON() : value;
46
48
  }
@@ -157,6 +159,21 @@ type(PrintTextStatement, "PrintTextStatement");
157
159
  alias(PrintTextStatement, "Statement", "PrintStatement");
158
160
  visitor(PrintTextStatement, "value");
159
161
 
162
+ export class PrintErroredStatement extends Node {
163
+ /**
164
+ * @param {StringLiteral} input
165
+ * @param {?Error} [cause]
166
+ */
167
+ constructor(input, cause = null) {
168
+ super();
169
+ this.value = input;
170
+ this.cause = cause;
171
+ }
172
+ }
173
+ type(PrintErroredStatement, "PrintErroredStatement");
174
+ alias(PrintErroredStatement, "Statement", "PrintStatement");
175
+ visitor(PrintErroredStatement, "value");
176
+
160
177
  export class ConstantValue extends Node {
161
178
  constructor(value) {
162
179
  super();
package/src/parser.js CHANGED
@@ -88,6 +88,7 @@ const parse = (text, parsers, options) => {
88
88
  const extensions = [coreExtension];
89
89
  const parser = createConfiguredParser(text, multiTagConfig, ...extensions);
90
90
  const ast = parser.parse();
91
+ parser.tokens.assertInlineCommentsAttached();
91
92
  ast[ORIGINAL_SOURCE] = text;
92
93
  return ast;
93
94
  };
@@ -0,0 +1,15 @@
1
+ import { doc } from "prettier";
2
+ import { PRETTIER_IGNORED_CODE } from "../util/publicSymbols.js";
3
+
4
+ /**
5
+ * @typedef {import('../../melody-types/index.js').PrintErroredStatement} node
6
+ */
7
+ const printErroredStatement = (node, path, print) => {
8
+ if (node[PRETTIER_IGNORED_CODE]) {
9
+ return path.call(print, "value");
10
+ }
11
+
12
+ throw node.cause;
13
+ };
14
+
15
+ export { printErroredStatement };
@@ -3,7 +3,8 @@ import { Node } from "../melody/melody-types/index.js";
3
3
  import {
4
4
  EXPRESSION_NEEDED,
5
5
  STRING_NEEDS_QUOTES,
6
- isContractableNodeType
6
+ isContractableNodeType,
7
+ hasComments
7
8
  } from "../util/index.js";
8
9
 
9
10
  const { group, indent, line } = doc.builders;
@@ -15,7 +16,8 @@ const printExpressionStatement = (node, path, print) => {
15
16
  const closing = node.trimRight ? "-}}" : "}}";
16
17
  const shouldContractValue =
17
18
  isContractableNodeType(node.value) &&
18
- !Node.isObjectExpression(node.value);
19
+ !Node.isObjectExpression(node.value) &&
20
+ !hasComments(node.value);
19
21
  const padding = shouldContractValue ? " " : line;
20
22
  const printedValue = [padding, path.call(print, "value")];
21
23
  const value = shouldContractValue ? printedValue : indent(printedValue);