-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: add support for arrow functions in filters #19
Comments
This is also pain point I face when using this plugin. It force me to create custom twig filter to circumvent this limitation. Definitely will be added. |
Same issue to me. I was formatting a full code base and the only place I needed to refactor something which made the code worse was with the |
Thought I could give it a try and added PR #42 to hopefully resolve this issue. Here are my notes. For anyone interested, and to maybe document how to add new features in the future. Step 1: Let lexer tokenize arrow functions
prettier-plugin-twig/src/melody/melody-parser/Lexer.js Lines 349 to 357 in c383441
Step 2: Let parser identify arrow function in addition to other filter arguments
prettier-plugin-twig/src/melody/melody-parser/Parser.js Lines 923 to 961 in c383441
Step 3: Add node type:Used to identify the arrow function as a whole prettier-plugin-twig/src/melody/melody-types/index.js Lines 326 to 339 in c383441
Step 4: Parse arrow function itselfArguments is everything between the filter’s opening parenthesis
Body is everything after the arrow
prettier-plugin-twig/src/melody/melody-parser/Parser.js Lines 963 to 1004 in c383441
Step 5: Add
|
import { doc } from "prettier"; | |
const { group, indent, join, line, softline } = doc.builders; | |
const p = (node, path, print) => { | |
const args = node.args; | |
const body = path.call(print, "body"); | |
let parts = []; | |
// Args | |
if (args.length > 1) { | |
// Multiple args | |
parts.push(group(["(", join(", ", args.map(arg => arg.name)), ")"])); | |
} else { | |
// Single arg | |
parts.push(args[0].name); | |
} | |
// Arrow | |
parts.push(" => "); | |
// Body | |
parts.push(body); | |
return group(parts); | |
}; | |
export { p as printArrowFunction }; |
Additional scenarios:
I have only tested arrow functions in tags {% %}
as opposed to expressions {{ }}
briefly, but they seem to work as well now (?)
Scope
As far as I know, arrow functions are only available in map, filter and reduce filters in Twig 3. While there is a Craft plugin to make it work everywhere else, and that plugin got a lot of thumbs up for being native Twig 4 functionality, I think making it work with the three above will improve this plugin a lot.
Thanks @jannisborgers and @zackad. Amazing work 🙏 |
Twig has some included filters that can take an arrow function
https://twig.symfony.com/doc/3.x/filters/map.html
Not sure if it would be best to build this out as a plugin or built into the core but the arrow completely breaks the parser.
The text was updated successfully, but these errors were encountered: