-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathtransform-each-track-array.ts
62 lines (51 loc) · 1.48 KB
/
transform-each-track-array.ts
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
import type { AST, ASTPlugin } from '@glimmer/syntax';
import { assert } from '@ember/debug';
import type { EmberASTPluginEnvironment } from '../types';
import { isPath, trackLocals } from './utils';
/**
@module ember
*/
/**
A Glimmer2 AST transformation that replaces all instances of
```handlebars
{{#each iterableThing as |key value|}}
```
with
```handlebars
{{#each (-track-array iterableThing) as |key value|}}
```
@private
@class TransformHasBlockSyntax
*/
export default function transformEachTrackArray(env: EmberASTPluginEnvironment): ASTPlugin {
let { builders: b } = env.syntax;
let { hasLocal, visitor } = trackLocals(env);
return {
name: 'transform-each-track-array',
visitor: {
...visitor,
BlockStatement(node: AST.BlockStatement): AST.Node | void {
if (isPath(node.path) && node.path.original === 'each' && !hasLocal('each')) {
let firstParam = node.params[0];
assert('has firstParam', firstParam);
if (
firstParam.type === 'SubExpression' &&
firstParam.path.type === 'PathExpression' &&
firstParam.path.original === '-each-in'
) {
return;
}
node.params[0] = b.sexpr(b.path('-track-array'), [firstParam]);
return b.block(
b.path('each'),
node.params,
node.hash,
node.program,
node.inverse,
node.loc
);
}
},
},
};
}