-
Notifications
You must be signed in to change notification settings - Fork 142
/
resolver-transform.ts
328 lines (306 loc) · 11.5 KB
/
resolver-transform.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { default as Resolver, ComponentResolution, ComponentLocator } from './resolver';
import type { ASTv1 } from '@glimmer/syntax';
// This is the AST transform that resolves components and helpers at build time
// and puts them into `dependencies`.
export function makeResolverTransform(resolver: Resolver) {
function resolverTransform({ filename }: { filename: string }) {
resolver.enter(filename);
let scopeStack = new ScopeStack();
return {
name: 'embroider-build-time-resolver',
visitor: {
Program: {
enter(node: ASTv1.Program) {
scopeStack.push(node.blockParams);
},
exit() {
scopeStack.pop();
},
},
BlockStatement(node: ASTv1.BlockStatement) {
if (node.path.type !== 'PathExpression') {
return;
}
if (scopeStack.inScope(node.path.parts[0])) {
return;
}
if (node.path.this === true) {
return;
}
if (node.path.parts.length > 1) {
// paths with a dot in them (which therefore split into more than
// one "part") are classically understood by ember to be contextual
// components, which means there's nothing to resolve at this
// location.
return;
}
if (node.path.original === 'component' && node.params.length > 0) {
handleComponentHelper(node.params[0], resolver, filename, scopeStack);
return;
}
// a block counts as args from our perpsective (it's enough to prove
// this thing must be a component, not content)
let hasArgs = true;
const resolution = resolver.resolveMustache(node.path.original, hasArgs, filename, node.path.loc);
if (resolution && resolution.type === 'component') {
scopeStack.enteringComponentBlock(resolution, ({ argumentsAreComponents }) => {
for (let name of argumentsAreComponents) {
let pair = node.hash.pairs.find((pair: ASTv1.HashPair) => pair.key === name);
if (pair) {
handleComponentHelper(pair.value, resolver, filename, scopeStack, {
componentName: (node.path as ASTv1.PathExpression).original,
argumentName: name,
});
}
}
});
}
},
SubExpression(node: ASTv1.SubExpression) {
if (node.path.type !== 'PathExpression') {
return;
}
if (node.path.this === true) {
return;
}
if (scopeStack.inScope(node.path.parts[0])) {
return;
}
if (node.path.original === 'component' && node.params.length > 0) {
handleComponentHelper(node.params[0], resolver, filename, scopeStack);
return;
}
resolver.resolveSubExpression(node.path.original, filename, node.path.loc);
},
MustacheStatement(node: ASTv1.MustacheStatement) {
if (node.path.type !== 'PathExpression') {
return;
}
if (scopeStack.inScope(node.path.parts[0])) {
return;
}
if (node.path.this === true) {
return;
}
if (node.path.parts.length > 1) {
// paths with a dot in them (which therefore split into more than
// one "part") are classically understood by ember to be contextual
// components, which means there's nothing to resolve at this
// location.
return;
}
if (node.path.original === 'component' && node.params.length > 0) {
handleComponentHelper(node.params[0], resolver, filename, scopeStack);
return;
}
let hasArgs = node.params.length > 0 || node.hash.pairs.length > 0;
let resolution = resolver.resolveMustache(node.path.original, hasArgs, filename, node.path.loc);
if (resolution && resolution.type === 'component') {
for (let name of resolution.argumentsAreComponents) {
let pair = node.hash.pairs.find((pair: ASTv1.HashPair) => pair.key === name);
if (pair) {
handleComponentHelper(pair.value, resolver, filename, scopeStack, {
componentName: node.path.original,
argumentName: name,
});
}
}
}
},
ElementNode: {
enter(node: ASTv1.ElementNode) {
if (!scopeStack.inScope(node.tag.split('.')[0])) {
const resolution = resolver.resolveElement(node.tag, filename, node.loc);
if (resolution && resolution.type === 'component') {
scopeStack.enteringComponentBlock(resolution, ({ argumentsAreComponents }) => {
for (let name of argumentsAreComponents) {
let attr = node.attributes.find((attr: ASTv1.AttrNode) => attr.name === '@' + name);
if (attr) {
handleComponentHelper(attr.value, resolver, filename, scopeStack, {
componentName: node.tag,
argumentName: name,
});
}
}
});
}
}
scopeStack.push(node.blockParams);
},
exit() {
scopeStack.pop();
},
},
},
};
}
resolverTransform.parallelBabel = {
requireFile: __filename,
buildUsing: 'makeResolverTransform',
params: Resolver,
};
return resolverTransform;
}
interface ComponentBlockMarker {
type: 'componentBlockMarker';
resolution: ComponentResolution;
argumentsAreComponents: string[];
exit: (marker: ComponentBlockMarker) => void;
}
type ScopeEntry = { type: 'blockParams'; blockParams: string[] } | ComponentBlockMarker;
class ScopeStack {
private stack: ScopeEntry[] = [];
// as we enter a block, we push the block params onto here to mark them as
// being in scope
push(blockParams: string[]) {
this.stack.unshift({ type: 'blockParams', blockParams });
}
// and when we leave the block they go out of scope. If this block was tagged
// by a safe component marker, we also clear that.
pop() {
this.stack.shift();
let next = this.stack[0];
if (next && next.type === 'componentBlockMarker') {
next.exit(next);
this.stack.shift();
}
}
// right before we enter a block, we might determine that some of the values
// that will be yielded as marked (by a rule) as safe to be used with the
// {{component}} helper.
enteringComponentBlock(resolution: ComponentResolution, exit: ComponentBlockMarker['exit']) {
this.stack.unshift({
type: 'componentBlockMarker',
resolution,
argumentsAreComponents: resolution.argumentsAreComponents.slice(),
exit,
});
}
inScope(name: string) {
for (let scope of this.stack) {
if (scope.type === 'blockParams' && scope.blockParams.includes(name)) {
return true;
}
}
return false;
}
safeComponentInScope(name: string): boolean {
let parts = name.split('.');
if (parts.length > 2) {
// we let component rules specify that they yield components or objects
// containing components. But not deeper than that. So the max path length
// that can refer to a marked-safe component is two segments.
return false;
}
for (let i = 0; i < this.stack.length - 1; i++) {
let here = this.stack[i];
let next = this.stack[i + 1];
if (here.type === 'blockParams' && next.type === 'componentBlockMarker') {
let positionalIndex = here.blockParams.indexOf(parts[0]);
if (positionalIndex === -1) {
continue;
}
if (parts.length === 1) {
if (next.resolution.yieldsComponents[positionalIndex] === true) {
return true;
}
let sourceArg = next.resolution.yieldsArguments[positionalIndex];
if (typeof sourceArg === 'string') {
next.argumentsAreComponents.push(sourceArg);
return true;
}
} else {
let entry = next.resolution.yieldsComponents[positionalIndex];
if (entry && typeof entry === 'object') {
return entry[parts[1]] === true;
}
let argsEntry = next.resolution.yieldsArguments[positionalIndex];
if (argsEntry && typeof argsEntry === 'object') {
let sourceArg = argsEntry[parts[1]];
if (typeof sourceArg === 'string') {
next.argumentsAreComponents.push(sourceArg);
return true;
}
}
}
// we found the source of the name, but there were no rules to cover it.
// Don't keep searching higher, those are different names.
return false;
}
}
return false;
}
}
function handleComponentHelper(
param: ASTv1.Node,
resolver: Resolver,
moduleName: string,
scopeStack: ScopeStack,
impliedBecause?: { componentName: string; argumentName: string }
): void {
let locator: ComponentLocator;
switch (param.type) {
case 'StringLiteral':
locator = { type: 'literal', path: param.value };
break;
case 'PathExpression':
locator = { type: 'path', path: param.original };
break;
case 'MustacheStatement':
if (param.hash.pairs.length === 0 && param.params.length === 0) {
handleComponentHelper(param.path, resolver, moduleName, scopeStack, impliedBecause);
return;
} else if (param.path.type === 'PathExpression' && param.path.original === 'component') {
// safe because we will handle this inner `{{component ...}}` mustache on its own
return;
} else {
locator = { type: 'other' };
}
break;
case 'TextNode':
locator = { type: 'literal', path: param.chars };
break;
case 'SubExpression':
if (param.path.type === 'PathExpression' && param.path.original === 'component') {
// safe because we will handle this inner `(component ...)` subexpression on its own
return;
}
if (param.path.type === 'PathExpression' && param.path.original === 'ensure-safe-component') {
// safe because we trust ensure-safe-component
return;
}
locator = { type: 'other' };
break;
default:
locator = { type: 'other' };
}
if (locator.type === 'path' && scopeStack.safeComponentInScope(locator.path)) {
return;
}
resolver.resolveComponentHelper(locator, moduleName, param.loc, impliedBecause);
// if (
// param.type === 'MustacheStatement' &&
// param.hash.pairs.length === 0 &&
// param.params.length === 0 &&
// handleComponentHelper(param.path, resolver, moduleName, scopeStack)
// ) {
// return;
// }
// if (
// param.type === 'MustacheStatement' &&
// param.path.type === 'PathExpression' &&
// param.path.original === 'component'
// ) {
// // safe because we will handle this inner `{{component ...}}` mustache on its own
// return;
// }
// if (param.type === 'TextNode') {
// resolver.resolveComponentHelper({ type: 'literal', path: param.chars }, moduleName, param.loc);
// return;
// }
// if (param.type === 'SubExpression' && param.path.type === 'PathExpression' && param.path.original === 'component') {
// // safe because we will handle this inner `(component ...)` subexpression on its own
// return;
// }
// resolver.unresolvableComponentArgument(componentName, argumentName, moduleName, param.loc);
}