From e5e9c5453b78490b5c2456e4a53c1cd579ac2b70 Mon Sep 17 00:00:00 2001 From: zackad Date: Wed, 14 Aug 2024 19:35:02 +0700 Subject: [PATCH] feat: add support for three-way-comparison (spaceship) operator Close GH-52 --- CHANGELOG.md | 3 +++ src/melody/melody-extension-core/operators.js | 6 ++++++ tests/Expressions/__snapshots__/arrowFunctions.snap.twig | 3 +++ tests/Expressions/__snapshots__/operators.snap.twig | 1 + tests/Expressions/arrowFunctions.twig | 3 +++ tests/Expressions/operators.twig | 1 + 6 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b288ee45..5fc37c78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## unreleased +### Features +- Add support for three-way-comparion operator (spaceship operator) + ### Bugfixes - Fix handling mapping that omit key part - Fix documentation about `twigAlwaysBreakObjects` option to reflect actual default value diff --git a/src/melody/melody-extension-core/operators.js b/src/melody/melody-extension-core/operators.js index 9e561939..77b61cb0 100644 --- a/src/melody/melody-extension-core/operators.js +++ b/src/melody/melody-extension-core/operators.js @@ -124,6 +124,12 @@ export const BinaryGreaterThanOrEqualExpression = createBinaryOperatorNode({ precedence: 20, associativity: LEFT }); +export const BinaryThreeWayComparisonExpression = createBinaryOperatorNode({ + text: "<=>", + type: "BinaryThreeWayComparisonExpression", + precedence: 20, + associativity: LEFT +}); export const BinaryNotInExpression = createBinaryOperatorNode({ text: "not in", diff --git a/tests/Expressions/__snapshots__/arrowFunctions.snap.twig b/tests/Expressions/__snapshots__/arrowFunctions.snap.twig index 697a7574..d786be00 100644 --- a/tests/Expressions/__snapshots__/arrowFunctions.snap.twig +++ b/tests/Expressions/__snapshots__/arrowFunctions.snap.twig @@ -6,3 +6,6 @@ Arrow function with multiple arguments: Arrow function with multiple arguments and second filter argument: {{ numbers|reduce((carry, v, k) => carry + v * k, 10) }} + +Arrow function with three-way-comparison (spaceship) operator +{{ array|sort((a, b) => a.value <=> b.value) }} diff --git a/tests/Expressions/__snapshots__/operators.snap.twig b/tests/Expressions/__snapshots__/operators.snap.twig index edbe8e3d..5f205851 100644 --- a/tests/Expressions/__snapshots__/operators.snap.twig +++ b/tests/Expressions/__snapshots__/operators.snap.twig @@ -26,6 +26,7 @@ {{ a ? b }} {{ a ? : b }} {{ a ?? b }} +{{ a <=> b }} {{ a is divisible by(b) }} {{ a is not divisible by(b) }} diff --git a/tests/Expressions/arrowFunctions.twig b/tests/Expressions/arrowFunctions.twig index 697a7574..d786be00 100644 --- a/tests/Expressions/arrowFunctions.twig +++ b/tests/Expressions/arrowFunctions.twig @@ -6,3 +6,6 @@ Arrow function with multiple arguments: Arrow function with multiple arguments and second filter argument: {{ numbers|reduce((carry, v, k) => carry + v * k, 10) }} + +Arrow function with three-way-comparison (spaceship) operator +{{ array|sort((a, b) => a.value <=> b.value) }} diff --git a/tests/Expressions/operators.twig b/tests/Expressions/operators.twig index df0f3e3e..142f3110 100644 --- a/tests/Expressions/operators.twig +++ b/tests/Expressions/operators.twig @@ -26,6 +26,7 @@ {{ a ? b }} {{ a ?: b }} {{ a ?? b }} +{{ a <=> b }} {{ a is divisible by(b) }} {{ a is not divisible by(b) }}