Skip to content

Commit

Permalink
feat: add support for test expression instance of
Browse files Browse the repository at this point in the history
  • Loading branch information
zackad committed Nov 10, 2024
1 parent 39fcfb6 commit ea8d92e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features
- Add support for default value on macros
- Add support for test expression `instance of`, feature of [Craft CMS](https://craftcms.com/docs/5.x/reference/twig/tests.html#instance-of)

---
## 0.9.1 (2024-10-10)
Expand Down
5 changes: 5 additions & 0 deletions src/melody/melody-extension-core/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ export const TestIterableExpression = createTest(
"iterable",
"TestIterableExpression"
);

export const TestInstanceOfExpression = createTest(
"instance of",
"TestInstanceOfExpression"
);
//endregion

//region Utilities
Expand Down
6 changes: 4 additions & 2 deletions src/print/TestExpression.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { doc } from "prettier";
import { findParentNode } from "../util/index.js";
import { findParentNode, STRING_NEEDS_QUOTES } from "../util/index.js";

const { softline, line, group, join, indent } = doc.builders;

Expand All @@ -11,13 +11,15 @@ const textMap = {
TestEvenExpression: "even",
TestOddExpression: "odd",
TestIterableExpression: "iterable",
TestSameAsExpression: "same as"
TestSameAsExpression: "same as",
TestInstanceOfExpression: "instance of"
};

const isNegator = node =>
node.constructor.name === "UnarySubclass" && node.operator === "not";

const p = (node, path, print) => {
node[STRING_NEEDS_QUOTES] = true;
const expressionType = node.__proto__.type;
const parts = [path.call(print, "expression"), " is "];
const parent = findParentNode(path);
Expand Down
12 changes: 12 additions & 0 deletions tests/Expressions/__snapshots__/operators.snap.twig
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@
pomegrenade
) }}

{# Craft CMS specific feature #}
{# Ref: https://craftcms.com/docs/5.x/reference/twig/tests.html#instance-of #}
{% if
element is instance of('craft\\elements\\Entry')
or element is instance of(
constant('Namespace\\Classname::CONSTANT_NAME')
) %}
<h1>
{{ entry.title }}
</h1>
{% endif %}

{{ dump(test) }}
{{ range(2, 3)|sort|join(',') }}
{{ range(3)|sort|join(',') }}
Expand Down
6 changes: 6 additions & 0 deletions tests/Expressions/operators.twig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
{{ a is not same as(b) }}
{{ a is not same as(banana, apple, orange, lemonade, kiwi, coconut, pineapple, pomegrenade) }}

{# Craft CMS specific feature #}
{# Ref: https://craftcms.com/docs/5.x/reference/twig/tests.html#instance-of #}
{% if element is instance of('craft\\elements\\Entry') or element is instance of(constant('Namespace\\Classname::CONSTANT_NAME')) %}
<h1>{{ entry.title }}</h1>
{% endif %}

{{ dump(test) }}
{{ range(2, 3) | sort | join(',') }}
{{ range(3) | sort | join(',') }}
Expand Down

0 comments on commit ea8d92e

Please sign in to comment.