discourse/app/assets/javascripts/discourse-plugins/transform-action-syntax.js
Jarek Radosz 06f841da08
DEV: Update Ember to 6.6.0 (#34460)
This upgrade does not include any breaking changes for Discourse
themes/plugins. Two of the three deprecations in Ember 6 (array
prototype extensions, component-template resolution) have already been
polyfilled in Discourse. The third (action helper/modifier) is
polyfilled in this commit.

Performance testing shows a 2-3% improvement in Discourse rendering
time, thanks to upstream performance fixes in the glimmer-vm since the
regressions in the Ember 5.x series.

---------

Co-authored-by: David Taylor <david@taylorhq.com>
2025-08-27 16:48:40 +01:00

67 lines
1.5 KiB
JavaScript

// backported from https://github.com/emberjs/ember.js/blob/v5.12.0/packages/ember-template-compiler/lib/plugins/transform-action-syntax.ts
/**
A Glimmer2 AST transformation that replaces all instances of
```handlebars
<button {{action 'foo'}}>
<button onblur={{action 'foo'}}>
<button onblur={{action (action 'foo') 'bar'}}>
```
with
```handlebars
<button {{action this 'foo'}}>
<button onblur={{action this 'foo'}}>
<button onblur={{action this (action this 'foo') 'bar'}}>
```
@private
@class TransformActionSyntax
*/
function transformActionSyntax({ syntax }) {
let { builders: b } = syntax;
return {
name: "transform-action-syntax",
visitor: {
ElementModifierStatement(node) {
if (isAction(node)) {
insertThisAsFirstParam(node, b);
}
},
MustacheStatement(node) {
if (isAction(node)) {
insertThisAsFirstParam(node, b);
}
},
SubExpression(node) {
if (isAction(node)) {
insertThisAsFirstParam(node, b);
}
},
},
};
}
transformActionSyntax.baseDir = () => __dirname;
transformActionSyntax.cacheKey = () => "transform-action-syntax";
module.exports = transformActionSyntax;
function isPath(node) {
return node.type === "PathExpression";
}
function isAction(node) {
return isPath(node.path) && node.path.original === "d-action";
}
function insertThisAsFirstParam(node, builders) {
node.params.unshift(builders.path("this"));
}