trivago / prettier-plugin-twig-melody

Code formatting plugin for Prettier which can handle Twig/Melody templates
Apache License 2.0
154 stars 31 forks source link

Arrow function in twig filter or function returns an error #51

Open JeanLucEsser opened 4 years ago

JeanLucEsser commented 4 years ago

Hi there,

In the same way you added custom twig tags for Craft CMS and others, it would be nice to add custom twig filters (and functions). For example, Craft CMS allows for a few of twig filters in top of twig's defaults as well as some twig functions.

Thanx for everything you bring to the Twig community ;)

[Edit] After some tinkering, this is not the issue. The issue is related to the arrow function (see below).

JeanLucEsser commented 4 years ago

Looks like the issue is not because of an unknown filter but because of the arrow function inside it.

This works:

{% set excludeIds = alerts |group('id') %}

But this won't work:

{% set excludeIds = alerts |group(e => e.alertRef[0].id ?? 0) %}

The culprit is that arrow function e => e.alertRef[0].id.

To be clear, this is not Craft related. Let's take a default twig filter like sort:

{% set alerts = alertsRef|sort((a,b) => a.alertColor <=> b.alertColor) %}

This will return an error.

rubas commented 3 years ago

Upstream Reference: https://github.com/trivago/melody/issues/163