@zackad/prettier-plugin-twig 0.17.0 → 0.18.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zackad/prettier-plugin-twig",
3
- "version": "0.17.0",
3
+ "version": "0.18.1",
4
4
  "description": "Prettier Plugin for Twig",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,7 +30,8 @@ import {
30
30
  copyStart,
31
31
  copyEnd,
32
32
  copyLoc,
33
- LEFT
33
+ LEFT,
34
+ RIGHT
34
35
  } from "../melody-parser/index.js";
35
36
 
36
37
  export const unaryOperators = [];
@@ -290,7 +291,7 @@ export const BinaryPowerExpression = createBinaryOperatorNode({
290
291
  text: "**",
291
292
  type: "BinaryPowerExpression",
292
293
  precedence: 200,
293
- associativity: LEFT
294
+ associativity: RIGHT
294
295
  });
295
296
  export const BinaryNullCoalesceExpression = createBinaryOperatorNode({
296
297
  text: "??",
@@ -19,6 +19,7 @@ import {
19
19
  setStartFromToken,
20
20
  setEndFromToken,
21
21
  createNode,
22
+ attachLeadingComments,
22
23
  hasTagStartTokenTrimLeft,
23
24
  hasTagEndTokenTrimRight
24
25
  } from "../../melody-parser/index.js";
@@ -32,23 +33,23 @@ export const ForParser = {
32
33
 
33
34
  const keyTarget = tokens.expect(Types.SYMBOL);
34
35
  if (tokens.nextIf(Types.COMMA)) {
35
- forStatement.keyTarget = createNode(
36
- Identifier,
37
- keyTarget,
38
- keyTarget.text
36
+ forStatement.keyTarget = attachLeadingComments(
37
+ createNode(Identifier, keyTarget, keyTarget.text),
38
+ tokens,
39
+ keyTarget
39
40
  );
40
41
  const valueTarget = tokens.expect(Types.SYMBOL);
41
- forStatement.valueTarget = createNode(
42
- Identifier,
43
- valueTarget,
44
- valueTarget.text
42
+ forStatement.valueTarget = attachLeadingComments(
43
+ createNode(Identifier, valueTarget, valueTarget.text),
44
+ tokens,
45
+ valueTarget
45
46
  );
46
47
  } else {
47
48
  forStatement.keyTarget = null;
48
- forStatement.valueTarget = createNode(
49
- Identifier,
50
- keyTarget,
51
- keyTarget.text
49
+ forStatement.valueTarget = attachLeadingComments(
50
+ createNode(Identifier, keyTarget, keyTarget.text),
51
+ tokens,
52
+ keyTarget
52
53
  );
53
54
  }
54
55
 
@@ -25,6 +25,8 @@ import {
25
25
  setStartFromToken,
26
26
  setEndFromToken,
27
27
  createNode,
28
+ attachListItemComments,
29
+ attachTrailingComments,
28
30
  hasTagStartTokenTrimLeft,
29
31
  hasTagEndTokenTrimRight
30
32
  } from "../../melody-parser/index.js";
@@ -43,7 +45,14 @@ export const MacroParser = {
43
45
  const arg = tokens.expect(Types.SYMBOL);
44
46
  // if default value is defined
45
47
  if (!tokens.test(Types.ASSIGNMENT)) {
46
- args.push(createNode(Identifier, arg, arg.text));
48
+ args.push(
49
+ attachListItemComments(
50
+ createNode(Identifier, arg, arg.text),
51
+ args[args.length - 1],
52
+ tokens,
53
+ arg
54
+ )
55
+ );
47
56
  } else {
48
57
  tokens.expect(Types.ASSIGNMENT);
49
58
  let value;
@@ -76,7 +85,14 @@ export const MacroParser = {
76
85
  value = tokens.expect(Types.FALSE);
77
86
  node = createNode(BooleanLiteral, value, value.text);
78
87
  }
79
- args.push(createNode(Identifier, arg, arg.text, node));
88
+ args.push(
89
+ attachListItemComments(
90
+ createNode(Identifier, arg, arg.text, node),
91
+ args[args.length - 1],
92
+ tokens,
93
+ arg
94
+ )
95
+ );
80
96
  }
81
97
 
82
98
  if (!tokens.nextIf(Types.COMMA) && !tokens.test(Types.RPAREN)) {
@@ -88,6 +104,7 @@ export const MacroParser = {
88
104
  });
89
105
  }
90
106
  }
107
+ attachTrailingComments(args[args.length - 1], tokens, tokens.la(0));
91
108
  tokens.expect(Types.RPAREN);
92
109
 
93
110
  const openingTagEndToken = tokens.la(0);
@@ -1,5 +1,7 @@
1
1
  import { DoStatement, PropItem, PropsStatement } from "../types.js";
2
2
  import {
3
+ attachListItemComments,
4
+ attachTrailingComments,
3
5
  createNode,
4
6
  setEndFromToken,
5
7
  setStartFromToken,
@@ -25,9 +27,19 @@ export const PropsParser = {
25
27
  } else {
26
28
  valueNode = undefined;
27
29
  }
28
- const item = new PropItem(nameNode, valueNode);
30
+ const item = attachListItemComments(
31
+ new PropItem(nameNode, valueNode),
32
+ items[items.length - 1],
33
+ tokenStream,
34
+ name
35
+ );
29
36
  items.push(item);
30
37
  } while (tokenStream.nextIf(Types.COMMA));
38
+ attachTrailingComments(
39
+ items[items.length - 1],
40
+ tokenStream,
41
+ tokenStream.la(0)
42
+ );
31
43
 
32
44
  const propsStatement = new PropsStatement(items);
33
45
  setStartFromToken(propsStatement, token);
@@ -19,6 +19,7 @@ import {
19
19
  setStartFromToken,
20
20
  setEndFromToken,
21
21
  createNode,
22
+ attachLeadingComments,
22
23
  hasTagStartTokenTrimLeft,
23
24
  hasTagEndTokenTrimRight
24
25
  } from "../../melody-parser/index.js";
@@ -29,6 +30,7 @@ export const SetParser = {
29
30
  parse(parser, token) {
30
31
  const tokens = parser.tokens;
31
32
  const names = [];
33
+ const nameTokens = [];
32
34
  const values = [];
33
35
 
34
36
  let openingTagEndToken;
@@ -37,6 +39,7 @@ export const SetParser = {
37
39
  do {
38
40
  const name = tokens.expect(Types.SYMBOL);
39
41
  names.push(createNode(Identifier, name, name.text));
42
+ nameTokens.push(name);
40
43
  } while (tokens.nextIf(Types.COMMA));
41
44
 
42
45
  if (tokens.nextIf(Types.ASSIGNMENT)) {
@@ -79,9 +82,10 @@ ${names.length} variable names and ${values.length} values.`
79
82
  // now join names and values
80
83
  const assignments = [];
81
84
  for (let i = 0, len = names.length; i < len; i++) {
82
- assignments[i] = new VariableDeclarationStatement(
83
- names[i],
84
- values[i]
85
+ assignments[i] = attachLeadingComments(
86
+ new VariableDeclarationStatement(names[i], values[i]),
87
+ tokens,
88
+ nameTokens[i]
85
89
  );
86
90
  }
87
91
 
@@ -3,6 +3,8 @@ import {
3
3
  TypesVariableDeclaration
4
4
  } from "../types.js";
5
5
  import {
6
+ attachListItemComments,
7
+ attachTrailingComments,
6
8
  createNode,
7
9
  setEndFromToken,
8
10
  setStartFromToken,
@@ -60,12 +62,24 @@ export const TypesParser = {
60
62
  });
61
63
  }
62
64
 
63
- const varType = new TypesVariableDeclaration(nameNode, valueNode);
65
+ const varType = attachListItemComments(
66
+ new TypesVariableDeclaration(nameNode, valueNode),
67
+ typesStatement.declarations[
68
+ typesStatement.declarations.length - 1
69
+ ],
70
+ tokens,
71
+ nameToken
72
+ );
64
73
  varType.optional = isVariableOptional;
65
74
  typesStatement.declarations.push(varType);
66
75
 
67
76
  tokens.nextIf(Types.COMMA);
68
77
  }
78
+ attachTrailingComments(
79
+ typesStatement.declarations[typesStatement.declarations.length - 1],
80
+ tokens,
81
+ tokens.la(0)
82
+ );
69
83
 
70
84
  if (typesStatement.enclosed) {
71
85
  tokens.expect(Types.RBRACKET);
@@ -34,7 +34,7 @@ export const GenericTagParser = {
34
34
  } catch (e) {
35
35
  if (e.errorType === "UNEXPECTED_TOKEN") {
36
36
  twigTag.parts.push(
37
- new n.GenericToken(e.tokenType, e.tokenText)
37
+ new n.GenericToken(e.token.type, e.token.text)
38
38
  );
39
39
  tokens.next();
40
40
  } else {
@@ -186,6 +186,8 @@ export default class Lexer {
186
186
  input.next();
187
187
  input.next();
188
188
  if (!(c = input.next()) === ">") {
189
+ // Unused error; Prettier appears to
190
+ // fix malformed HTML comments.
189
191
  this.error(
190
192
  "Unexpected end for HTML comment",
191
193
  input.mark(),
@@ -304,6 +306,7 @@ export default class Lexer {
304
306
  return this.matchSymbol(pos);
305
307
  }
306
308
  } else {
309
+ // Unreachable error?
307
310
  return this.error(`Invalid state ${this.state}`, pos);
308
311
  }
309
312
  }
@@ -353,6 +356,8 @@ export default class Lexer {
353
356
  this.pushState(State.STRING_DOUBLE);
354
357
  input.next();
355
358
  return this.createToken(TokenTypes.STRING_START, pos);
359
+ case "#":
360
+ return this.matchInlineComment(pos);
356
361
  case "=":
357
362
  // Lookahead for '>'
358
363
  if (input.la(1) === ">") {
@@ -410,11 +415,13 @@ export default class Lexer {
410
415
  input.next();
411
416
  return this.createToken(CHAR_TO_TOKEN[c], pos);
412
417
  } else if (c === "\xa0") {
418
+ input.next();
413
419
  return this.error(
414
420
  "Unsupported token: Non-breaking space",
415
421
  pos
416
422
  );
417
423
  }
424
+ input.next();
418
425
  return this.error(`Unknown token ${c}`, pos);
419
426
  }
420
427
  }
@@ -521,6 +528,7 @@ export default class Lexer {
521
528
  }
522
529
  const end = input.mark();
523
530
  if (pos.index === end.index) {
531
+ // Unreachable error?
524
532
  return this.error(
525
533
  "Expected an Identifier",
526
534
  pos,
@@ -553,8 +561,9 @@ export default class Lexer {
553
561
  return this.createToken(TokenTypes.STRING_END, pos);
554
562
  }
555
563
  while ((c = input.la(0)) !== start && c !== EOF) {
556
- if (c === "\\" && input.la(1) === start) {
557
- // escape sequence for string start
564
+ if (c === "\\" && input.la(1) !== EOF) {
565
+ // escape sequence: the next character never ends the string
566
+ // or starts an interpolation (e.g. \', \\ or \#{)
558
567
  input.next();
559
568
  input.next();
560
569
  } else if (allowInterpolation && c === "#" && input.la(1) === "{") {
@@ -566,11 +575,11 @@ export default class Lexer {
566
575
  }
567
576
  }
568
577
  const result = this.createToken(TokenTypes.STRING, pos);
569
- // Replace double backslash before escaped quotes
578
+ // Unescape escaped quotes, but keep every other escape sequence
579
+ // (including an escaped backslash right before a quote) as is
570
580
  if (!this.options.preserveSourceLiterally) {
571
- result.text = result.text.replace(
572
- new RegExp("(?:\\\\)(" + start + ")", "g"),
573
- "$1"
581
+ result.text = result.text.replace(/\\([\s\S])/g, (match, char) =>
582
+ char === start ? char : match
574
583
  );
575
584
  }
576
585
  return result;
@@ -663,6 +672,16 @@ export default class Lexer {
663
672
  return this.createToken(TokenTypes.TEXT, pos);
664
673
  }
665
674
 
675
+ // Inline comment (Twig 3.15), runs to the end of the line
676
+ matchInlineComment(pos) {
677
+ const input = this.input;
678
+ let c;
679
+ while ((c = input.la(0)) !== EOF && c !== "\n") {
680
+ input.next();
681
+ }
682
+ return this.createToken(TokenTypes.INLINE_COMMENT, pos);
683
+ }
684
+
666
685
  matchComment(pos) {
667
686
  const input = this.input;
668
687
  let c;
@@ -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
  }