-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
transform-action-syntax.js
67 lines (56 loc) · 1.33 KB
/
transform-action-syntax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
@module ember
@submodule ember-glimmer
*/
/**
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
*/
export default function TransformActionSyntax() {
// set later within Glimmer2 to the syntax package
this.syntax = null;
}
/**
@private
@method transform
@param {AST} ast The AST to be transformed.
*/
TransformActionSyntax.prototype.transform = function TransformActionSyntax_transform(ast) {
let { traverse, builders: b } = this.syntax;
traverse(ast, {
ElementModifierStatement(node) {
if (isAction(node)) {
insertThisAsFirstParam(node, b);
}
},
MustacheStatement(node) {
if (isAction(node)) {
insertThisAsFirstParam(node, b);
}
},
SubExpression(node) {
if (isAction(node)) {
insertThisAsFirstParam(node, b);
}
}
});
return ast;
};
function isAction(node) {
return node.path.original === 'action';
}
function insertThisAsFirstParam(node, builders) {
node.params.unshift(builders.path('this'));
}