-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathtransform-resolutions.ts
198 lines (161 loc) · 4.15 KB
/
transform-resolutions.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { AST, ASTPlugin, print } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';
import { EmberASTPluginEnvironment } from '../types';
import { isPath, isStringLiteral, trackLocals } from './utils';
/**
@module ember
*/
/**
A Glimmer2 AST transformation that replaces all instances of
```handlebars
{{helper "..." ...}}
```
with
```handlebars
{{helper (-resolve "helper:...") ...}}
```
and
```handlebars
{{helper ... ...}}
```
with
```handlebars
{{helper (-disallow-dynamic-resolution ...) ...}}
```
and
```handlebars
{{modifier "..." ...}}
```
with
```handlebars
{{modifier (-resolve "modifier:...") ...}}
```
and
```handlebars
{{modifier ... ...}}
```
with
```handlebars
{{modifier (-disallow-dynamic-resolution ...) ...}}
```
@private
@class TransformResolutions
*/
const TARGETS = Object.freeze(['helper', 'modifier']);
export default function transformResolutions(env: EmberASTPluginEnvironment): ASTPlugin {
let { builders: b } = env.syntax;
let moduleName = env.meta?.moduleName;
let { hasLocal, node: tracker } = trackLocals();
let seen: Set<AST.Node> | undefined;
return {
name: 'transform-resolutions',
visitor: {
Template: {
enter() {
seen = new Set();
},
exit() {
seen = undefined;
},
},
Block: tracker,
ElementNode: {
keys: {
children: tracker,
},
},
MustacheStatement(node: AST.MustacheStatement): AST.Node | void {
assert('[BUG] seen set should be available', seen);
if (seen.has(node)) {
return;
}
if (
isPath(node.path) &&
!isLocalVariable(node.path, hasLocal) &&
TARGETS.indexOf(node.path.original) !== -1
) {
let result = b.mustache(
node.path,
transformParams(b, node.params, node.path.original, moduleName, node.loc),
node.hash,
node.trusting,
node.loc,
node.strip
);
// Avoid double/infinite-processing
seen.add(result);
return result;
}
},
SubExpression(node: AST.SubExpression): AST.Node | void {
assert('[BUG] seen set should be available', seen);
if (seen.has(node)) {
return;
}
if (
isPath(node.path) &&
!isLocalVariable(node.path, hasLocal) &&
TARGETS.indexOf(node.path.original) !== -1
) {
let result = b.sexpr(
node.path,
transformParams(b, node.params, node.path.original, moduleName, node.loc),
node.hash,
node.loc
);
// Avoid double/infinite-processing
seen.add(result);
return result;
}
},
},
};
}
function isLocalVariable(node: AST.PathExpression, hasLocal: (k: string) => boolean): boolean {
return !node.this && node.parts.length === 1 && hasLocal(node.parts[0]!);
}
function transformParams(
b: EmberASTPluginEnvironment['syntax']['builders'],
params: AST.Expression[],
type: string,
moduleName: string | undefined,
loc: AST.SourceLocation | undefined
): AST.Expression[] {
let [first, ...rest] = params;
assert(
`The ${type} keyword requires at least one positional arguments ${calculateLocationDisplay(
moduleName,
loc
)}`,
first
);
if (isStringLiteral(first)) {
return [
b.sexpr(
b.path('-resolve', first.loc),
[b.string(`${type}:${first.value}`)],
undefined,
first.loc
),
...rest,
];
} else if (DEBUG) {
return [
b.sexpr(
b.path('-disallow-dynamic-resolution', first.loc),
[first],
b.hash([
b.pair('type', b.string(type), first.loc),
b.pair('loc', b.string(calculateLocationDisplay(moduleName, loc)), first.loc),
b.pair('original', b.string(print(first))),
]),
first.loc
),
...rest,
];
} else {
return params;
}
}