@zackad/prettier-plugin-twig 0.14.1 → 0.15.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.
package/README.md CHANGED
@@ -105,6 +105,25 @@ If we did not list the `"nav,endnav"` entry in `twigMultiTags`, this code exampl
105
105
 
106
106
  Note that the order matters: It has to be `"nav,endnav"`, and it must not be `"endnav,nav"`. In general, the first and the last tag name matter. In the case of `"switch,case,default,endswitch"`, the order of `case` and `default` does not matter. However, `switch` has to come first, and `endswitch` has to come last.
107
107
 
108
+ ### twigTestExpressions (default: `[]`)
109
+
110
+ Make custom Twig tests known to the parser.
111
+
112
+ ```json
113
+ twigTestExpressions: [
114
+ "snake_case_test",
115
+ "camelCaseTest"
116
+ ]
117
+ ```
118
+
119
+ __Example__
120
+ ```twig
121
+ {{ a is snake_case_test }}
122
+ {{ a is not snake_case_test }}
123
+ {{ a is camelCaseTest }}
124
+ {{ a is not camelCaseTest }}
125
+ ```
126
+
108
127
  ## Features
109
128
 
110
129
  ### `prettier-ignore` and `prettier-ignore-start`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zackad/prettier-plugin-twig",
3
- "version": "0.14.1",
3
+ "version": "0.15.0",
4
4
  "description": "Prettier Plugin for Twig",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.js CHANGED
@@ -72,6 +72,13 @@ const printers = {
72
72
 
73
73
  /** @type {import('prettier').Options} Options */
74
74
  const options = {
75
+ twigTestExpressions: {
76
+ type: "path",
77
+ category: "Global",
78
+ array: true,
79
+ default: [{ value: [] }],
80
+ description: "Make custom Twig tests known to the parser."
81
+ },
75
82
  twigMultiTags: {
76
83
  type: "path",
77
84
  category: "Global",
@@ -301,13 +301,19 @@ export const BinaryEmptyCoalesceExpression = createBinaryOperatorNode({
301
301
  //endregion
302
302
 
303
303
  //region Test Expressions
304
- export const TestEvenExpression = createTest("even", "TestEvenExpression");
305
- export const TestOddExpression = createTest("odd", "TestOddExpression");
306
- export const TestDefinedExpression = createTest(
304
+ export const TestEvenExpression = createTestExpression(
305
+ "even",
306
+ "TestEvenExpression"
307
+ );
308
+ export const TestOddExpression = createTestExpression(
309
+ "odd",
310
+ "TestOddExpression"
311
+ );
312
+ export const TestDefinedExpression = createTestExpression(
307
313
  "defined",
308
314
  "TestDefinedExpression"
309
315
  );
310
- export const TestSameAsExpression = createTest(
316
+ export const TestSameAsExpression = createTestExpression(
311
317
  "same as",
312
318
  "TestSameAsExpression"
313
319
  );
@@ -318,14 +324,17 @@ tests.push({
318
324
  return new TestSameAsExpression(expr, args);
319
325
  }
320
326
  });
321
- export const TestNullExpression = createTest("null", "TestNullExpression");
327
+ export const TestNullExpression = createTestExpression(
328
+ "null",
329
+ "TestNullExpression"
330
+ );
322
331
  tests.push({
323
332
  text: "none",
324
333
  createNode(expr, args) {
325
334
  return new TestNullExpression(expr, args);
326
335
  }
327
336
  });
328
- export const TestDivisibleByExpression = createTest(
337
+ export const TestDivisibleByExpression = createTestExpression(
329
338
  "divisible by",
330
339
  "TestDivisibleByExpression"
331
340
  );
@@ -336,24 +345,26 @@ tests.push({
336
345
  return new TestDivisibleByExpression(expr, args);
337
346
  }
338
347
  });
339
- export const TestConstantExpression = createTest(
348
+ export const TestConstantExpression = createTestExpression(
340
349
  "constant",
341
350
  "TestConstantExpression"
342
351
  );
343
- export const TestEmptyExpression = createTest("empty", "TestEmptyExpression");
344
- export const TestIterableExpression = createTest(
352
+ export const TestEmptyExpression = createTestExpression(
353
+ "empty",
354
+ "TestEmptyExpression"
355
+ );
356
+ export const TestIterableExpression = createTestExpression(
345
357
  "iterable",
346
358
  "TestIterableExpression"
347
359
  );
348
-
349
- export const TestInstanceOfExpression = createTest(
360
+ export const TestInstanceOfExpression = createTestExpression(
350
361
  "instance of",
351
362
  "TestInstanceOfExpression"
352
363
  );
353
364
  //endregion
354
365
 
355
366
  //region Utilities
356
- function createTest(text, typeName) {
367
+ export function createTestExpression(text, typeName) {
357
368
  const TestExpression = class extends Node {
358
369
  /**
359
370
  * @param {Node} expr
@@ -29,6 +29,7 @@ import { GenericTagParser } from "./GenericTagParser.js";
29
29
  import { createMultiTagParser } from "./GenericMultiTagParser.js";
30
30
  import { voidElements } from "./elementInfo.js";
31
31
  import * as he from "he";
32
+ import { Attribute } from "../melody-types/index.js";
32
33
 
33
34
  /**
34
35
  * @typedef {Object} UnaryOperator
@@ -378,6 +379,7 @@ export default class Parser {
378
379
  tokens.lat(0) !== Types.ELEMENT_END
379
380
  ) {
380
381
  const key = tokens.nextIf(Types.SYMBOL);
382
+ let twigComment;
381
383
  if (key) {
382
384
  const keyNode = new n.Identifier(key.text);
383
385
  setStartFromToken(keyNode, key);
@@ -442,6 +444,10 @@ export default class Parser {
442
444
  } else if (tokens.nextIf(Types.EXPRESSION_START)) {
443
445
  element.attributes.push(this.matchExpression());
444
446
  tokens.expect(Types.EXPRESSION_END);
447
+ } else if ((twigComment = tokens.nextIf(Types.COMMENT))) {
448
+ const twigCommentValue = new n.StringLiteral(twigComment.text);
449
+ const twigCommentNode = new n.TwigComment(twigCommentValue);
450
+ element.attributes.push(twigCommentNode);
445
451
  } else {
446
452
  this.error({
447
453
  title: "Invalid token",
package/src/parser.js CHANGED
@@ -5,6 +5,7 @@ import {
5
5
  Parser
6
6
  } from "./melody/melody-parser/index.js";
7
7
  import { extension as coreExtension } from "./melody/melody-extension-core/index.js";
8
+ import { createTestExpression } from "./melody/melody-extension-core/operators.js";
8
9
 
9
10
  const ORIGINAL_SOURCE = Symbol("ORIGINAL_SOURCE");
10
11
 
@@ -74,8 +75,15 @@ const getMultiTagConfig = (tagsCsvs = []) =>
74
75
  return acc;
75
76
  }, {});
76
77
 
78
+ const registerTestExtensions = (testExpressions = []) => {
79
+ testExpressions.forEach(test =>
80
+ createTestExpression(test, `Test[${test}]Expression`)
81
+ );
82
+ };
83
+
77
84
  const parse = (text, parsers, options) => {
78
85
  const multiTagConfig = getMultiTagConfig(options.twigMultiTags || []);
86
+ registerTestExtensions(options.twigTestExpressions || []);
79
87
 
80
88
  const extensions = [coreExtension];
81
89
  const parser = createConfiguredParser(text, multiTagConfig, ...extensions);
@@ -18,6 +18,9 @@ const printOpener = (node, path, print) => {
18
18
  if (node.argument) {
19
19
  parts.push(indent([line, "with ", path.call(print, "argument")]));
20
20
  }
21
+ if (node.contextFree) {
22
+ parts.push(" only");
23
+ }
21
24
  parts.push([line, node.trimRightEmbed ? "-%}" : "%}"]);
22
25
  return group(parts);
23
26
  };
@@ -28,7 +28,15 @@ const printTestExpression = (node, path, print) => {
28
28
  if (isNegator(parent)) {
29
29
  parts.push("not ");
30
30
  }
31
- if (!textMap[expressionType]) {
31
+
32
+ if (expressionType.includes("[") && expressionType.includes("]")) {
33
+ const testText = expressionType.substring(
34
+ expressionType.indexOf("[") + 1,
35
+ expressionType.lastIndexOf("]")
36
+ );
37
+
38
+ parts.push(testText);
39
+ } else if (!textMap[expressionType]) {
32
40
  console.error(
33
41
  "TestExpression: No text for " + expressionType + " defined"
34
42
  );