@zackad/prettier-plugin-twig 0.9.0 → 0.10.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
@@ -7,7 +7,7 @@ Forked from [trivago/prettier-plugin-twig-melody](https://github.com/trivago/pre
7
7
 
8
8
  ![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/zackad/prettier-plugin-twig-melody/ci.yaml?branch=master&style=for-the-badge&logo=github)
9
9
  ![GitHub License](https://img.shields.io/github/license/zackad/prettier-plugin-twig-melody?style=for-the-badge)
10
- ![GitHub Release](https://img.shields.io/github/v/release/zackad/prettier-plugin-twig-melody?display_name=release&style=for-the-badge&logo=github)
10
+ ![NPM Version](https://img.shields.io/npm/v/%40zackad%2Fprettier-plugin-twig?style=for-the-badge&logo=npm)
11
11
  ![GitHub Tag](https://img.shields.io/github/v/tag/zackad/prettier-plugin-twig-melody?style=for-the-badge)
12
12
 
13
13
  ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zackad/prettier-plugin-twig",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Prettier Plugin for Twig",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,8 @@
15
15
  ],
16
16
  "scripts": {
17
17
  "lint": "DEBUG=eslint:cli-engine eslint .",
18
- "test": "vitest --run"
18
+ "test": "vitest --run",
19
+ "test-smoke": "prettier --check --parser=twig --plugin=./src/index.js tests/smoke-test.twig"
19
20
  },
20
21
  "dependencies": {
21
22
  "@babel/template": "^7.24.7",
@@ -330,6 +330,11 @@ export const TestIterableExpression = createTest(
330
330
  "iterable",
331
331
  "TestIterableExpression"
332
332
  );
333
+
334
+ export const TestInstanceOfExpression = createTest(
335
+ "instance of",
336
+ "TestInstanceOfExpression"
337
+ );
333
338
  //endregion
334
339
 
335
340
  //region Utilities
@@ -13,7 +13,13 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import { Identifier } from "../../melody-types/index.js";
16
+ import {
17
+ BooleanLiteral,
18
+ Identifier,
19
+ NullLiteral,
20
+ NumericLiteral,
21
+ StringLiteral
22
+ } from "../../melody-types/index.js";
17
23
  import {
18
24
  Types,
19
25
  setStartFromToken,
@@ -35,7 +41,36 @@ export const MacroParser = {
35
41
  tokens.expect(Types.LPAREN);
36
42
  while (!tokens.test(Types.RPAREN) && !tokens.test(Types.EOF)) {
37
43
  const arg = tokens.expect(Types.SYMBOL);
38
- args.push(createNode(Identifier, arg, arg.text));
44
+ // if default value is defined
45
+ if (!tokens.test(Types.ASSIGNMENT)) {
46
+ args.push(createNode(Identifier, arg, arg.text));
47
+ } else {
48
+ tokens.expect(Types.ASSIGNMENT);
49
+ let value;
50
+ let node;
51
+ if (tokens.nextIf(Types.STRING_START)) {
52
+ value = tokens.expect(Types.STRING);
53
+ tokens.expect(Types.STRING_END);
54
+ node = createNode(StringLiteral, value, value.text);
55
+ }
56
+ if (tokens.test(Types.NUMBER)) {
57
+ value = tokens.expect(Types.NUMBER);
58
+ node = createNode(NumericLiteral, value, value.text);
59
+ }
60
+ if (tokens.test(Types.NULL)) {
61
+ value = tokens.expect(Types.NULL);
62
+ node = createNode(NullLiteral, value, value.text);
63
+ }
64
+ if (tokens.test(Types.TRUE)) {
65
+ value = tokens.expect(Types.TRUE);
66
+ node = createNode(BooleanLiteral, value, value.text);
67
+ }
68
+ if (tokens.test(Types.FALSE)) {
69
+ value = tokens.expect(Types.FALSE);
70
+ node = createNode(BooleanLiteral, value, value.text);
71
+ }
72
+ args.push(createNode(Identifier, arg, arg.text, node));
73
+ }
39
74
 
40
75
  if (!tokens.nextIf(Types.COMMA) && !tokens.test(Types.RPAREN)) {
41
76
  // not followed by comma or rparen
@@ -14,7 +14,22 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import * as t from "@babel/types";
17
- import template from "@babel/template";
17
+ import isFunction from "lodash/isFunction.js";
18
+ import _template from "@babel/template";
19
+
20
+ /**
21
+ * For some reason there's inconsistency when importing "@babel/template" on
22
+ * vitest environment and loading this plugin directly on cli. I have no idea why
23
+ * this happens but this workaround seems to be fixed it.
24
+ *
25
+ * Without this workaround, an error will occur either on test or when loading
26
+ * plugin on cli. This error doesn't happen on `src/melody/melody-extension-core/visitors/for.js`.
27
+ * ```
28
+ * [error] template is not a function
29
+ * ```
30
+ */
31
+ const template = isFunction(_template) ? _template : _template.default;
32
+
18
33
  // use default value if var is null, undefined or an empty string
19
34
  // but use var if value is 0, false, an empty array or an empty object
20
35
  const defaultFilter = template("VAR != null && VAR !== '' ? VAR : DEFAULT");
@@ -950,7 +950,8 @@ export default class Parser {
950
950
  args.push(arrowFunction);
951
951
  } else if (
952
952
  tokens.test(Types.SYMBOL) &&
953
- tokens.lat(1) === Types.ASSIGNMENT
953
+ (tokens.lat(1) === Types.ASSIGNMENT ||
954
+ tokens.lat(1) === Types.COLON)
954
955
  ) {
955
956
  // OPTION 2: named filter argument(s)
956
957
  const name = tokens.next();
@@ -195,9 +195,14 @@ type(NullLiteral, "NullLiteral");
195
195
  alias(NullLiteral, "Expression", "Literal", "Immutable");
196
196
 
197
197
  export class Identifier extends Node {
198
- constructor(name) {
198
+ /**
199
+ * @param name
200
+ * @param {ConstantValue|undefined} value
201
+ */
202
+ constructor(name, value = undefined) {
199
203
  super();
200
204
  this.name = name;
205
+ this.value = value;
201
206
  }
202
207
  }
203
208
  type(Identifier, "Identifier");
@@ -1,5 +1,5 @@
1
1
  import { doc } from "prettier";
2
- import { isBoolean } from "lodash";
2
+ import isBoolean from "lodash/isBoolean.js";
3
3
  import { printChildBlock, quoteChar } from "../util/index.js";
4
4
 
5
5
  const { hardline } = doc.builders;
@@ -1,12 +1,21 @@
1
1
  import { doc } from "prettier";
2
- import { EXPRESSION_NEEDED, wrapExpressionIfNeeded } from "../util/index.js";
2
+ import {
3
+ EXPRESSION_NEEDED,
4
+ STRING_NEEDS_QUOTES,
5
+ wrapExpressionIfNeeded
6
+ } from "../util/index.js";
3
7
 
4
8
  const { group } = doc.builders;
5
9
 
6
- const p = (node, path) => {
10
+ const p = (node, path, print) => {
7
11
  node[EXPRESSION_NEEDED] = false;
12
+ node[STRING_NEEDS_QUOTES] = true;
8
13
 
9
14
  const parts = [node.name];
15
+ if (node.value !== undefined) {
16
+ parts.push([" = ", path.call(print, "value")]);
17
+ }
18
+
10
19
  wrapExpressionIfNeeded(path, parts, node);
11
20
  const result = parts;
12
21
  return parts.length === 1 ? result : group(result);
@@ -1,5 +1,5 @@
1
1
  import { doc } from "prettier";
2
- import { findParentNode } from "../util/index.js";
2
+ import { findParentNode, STRING_NEEDS_QUOTES } from "../util/index.js";
3
3
 
4
4
  const { softline, line, group, join, indent } = doc.builders;
5
5
 
@@ -11,13 +11,15 @@ const textMap = {
11
11
  TestEvenExpression: "even",
12
12
  TestOddExpression: "odd",
13
13
  TestIterableExpression: "iterable",
14
- TestSameAsExpression: "same as"
14
+ TestSameAsExpression: "same as",
15
+ TestInstanceOfExpression: "instance of"
15
16
  };
16
17
 
17
18
  const isNegator = node =>
18
19
  node.constructor.name === "UnarySubclass" && node.operator === "not";
19
20
 
20
21
  const p = (node, path, print) => {
22
+ node[STRING_NEEDS_QUOTES] = true;
21
23
  const expressionType = node.__proto__.type;
22
24
  const parts = [path.call(print, "expression"), " is "];
23
25
  const parent = findParentNode(path);