forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.ts
317 lines (265 loc) · 9.16 KB
/
resolver.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
import type { InternalFactory, InternalOwner, RegisterOptions } from '@ember/-internals/owner';
import { isFactory } from '@ember/-internals/owner';
import { assert } from '@ember/debug';
import { _instrumentStart } from '@ember/instrumentation';
import { DEBUG } from '@glimmer/env';
import type {
CompileTimeResolver,
HelperDefinitionState,
ModifierDefinitionState,
ResolvedComponentDefinition,
RuntimeResolver,
Template,
TemplateFactory,
} from '@glimmer/interfaces';
import type { Nullable } from '@ember/-internals/utility-types';
import {
getComponentTemplate,
getInternalComponentManager,
setInternalHelperManager,
} from '@glimmer/manager';
import {
array,
concat,
fn,
get,
hash,
on,
templateOnlyComponent,
TEMPLATE_ONLY_COMPONENT_MANAGER,
} from '@glimmer/runtime';
import { isCurlyManager } from './component-managers/curly';
import { CLASSIC_HELPER_MANAGER, isClassicHelper } from './helper';
import { default as disallowDynamicResolution } from './helpers/-disallow-dynamic-resolution';
import { default as inElementNullCheckHelper } from './helpers/-in-element-null-check';
import { default as normalizeClassHelper } from './helpers/-normalize-class';
import { default as resolve } from './helpers/-resolve';
import { default as trackArray } from './helpers/-track-array';
import { default as action } from './helpers/action';
import { default as eachIn } from './helpers/each-in';
import { default as mut } from './helpers/mut';
import { default as readonly } from './helpers/readonly';
import { default as unbound } from './helpers/unbound';
import { default as uniqueId } from './helpers/unique-id';
import actionModifier from './modifiers/action';
import { mountHelper } from './syntax/mount';
import { outletHelper } from './syntax/outlet';
import { DEPRECATIONS, deprecateUntil } from '@ember/-internals/deprecations';
function instrumentationPayload(name: string) {
return { object: `component:${name}` };
}
function componentFor(
name: string,
owner: InternalOwner
): Nullable<InternalFactory<object> | object> {
let fullName = `component:${name}` as const;
return owner.factoryFor(fullName) || null;
}
function layoutFor(
name: string,
owner: InternalOwner,
options?: RegisterOptions
): Nullable<Template> {
if (DEPRECATIONS.DEPRECATE_COMPONENT_TEMPLATE_RESOLVING.isRemoved) {
return null;
}
let templateFullName = `template:components/${name}` as const;
let result = (owner.lookup(templateFullName, options) as Template) || null;
if (result) {
deprecateUntil(
`Components with separately resolved templates are deprecated. Migrate to either co-located js/ts + hbs files or to gjs/gts. Tried to lookup '${templateFullName}'.`,
DEPRECATIONS.DEPRECATE_COMPONENT_TEMPLATE_RESOLVING
);
}
return result;
}
type LookupResult =
| {
component: InternalFactory<object>;
layout: TemplateFactory;
}
| {
component: InternalFactory<object>;
layout: null;
}
| {
component: null;
layout: TemplateFactory;
};
function lookupComponentPair(
owner: InternalOwner,
name: string,
options?: RegisterOptions
): Nullable<LookupResult> {
let component = componentFor(name, owner);
if (isFactory(component) && component.class) {
let layout = getComponentTemplate(component.class);
if (layout !== undefined) {
return { component, layout };
}
}
let layout = layoutFor(name, owner, options);
if (component === null && layout === null) {
return null;
} else {
return { component, layout } as LookupResult;
}
}
const BUILTIN_KEYWORD_HELPERS: Record<string, object> = {
action,
mut,
readonly,
unbound,
'-hash': hash,
'-each-in': eachIn,
'-normalize-class': normalizeClassHelper,
'-resolve': resolve,
'-track-array': trackArray,
'-mount': mountHelper,
'-outlet': outletHelper,
'-in-el-null': inElementNullCheckHelper,
};
const BUILTIN_HELPERS: Record<string, object> = {
...BUILTIN_KEYWORD_HELPERS,
array,
concat,
fn,
get,
hash,
'unique-id': uniqueId,
};
if (DEBUG) {
BUILTIN_HELPERS['-disallow-dynamic-resolution'] = disallowDynamicResolution;
} else {
// Bug: this may be a quirk of our test setup?
// In prod builds, this is a no-op helper and is unused in practice. We shouldn't need
// to add it at all, but the current test build doesn't produce a "prod compiler", so
// we ended up running the debug-build for the template compliler in prod tests. Once
// that is fixed, this can be removed. For now, this allows the test to work and does
// not really harm anything, since it's just a no-op pass-through helper and the bytes
// has to be included anyway. In the future, perhaps we can avoid the latter by using
// `import(...)`?
BUILTIN_HELPERS['-disallow-dynamic-resolution'] = disallowDynamicResolution;
}
const BUILTIN_KEYWORD_MODIFIERS: Record<string, ModifierDefinitionState> = {
action: actionModifier,
};
const BUILTIN_MODIFIERS: Record<string, object> = {
...BUILTIN_KEYWORD_MODIFIERS,
on,
};
const CLASSIC_HELPER_MANAGER_ASSOCIATED = new WeakSet();
export default class ResolverImpl
implements RuntimeResolver<InternalOwner>, CompileTimeResolver<InternalOwner>
{
private componentDefinitionCache: Map<object, ResolvedComponentDefinition | null> = new Map();
lookupPartial(): null {
return null;
}
lookupHelper(name: string, owner: InternalOwner): Nullable<HelperDefinitionState> {
assert(
`You attempted to overwrite the built-in helper "${name}" which is not allowed. Please rename the helper.`,
!(BUILTIN_HELPERS[name] && owner.hasRegistration(`helper:${name}`))
);
let helper = BUILTIN_HELPERS[name];
if (helper !== undefined) {
return helper;
}
let factory = owner.factoryFor(`helper:${name}`);
if (factory === undefined) {
return null;
}
let definition = factory.class;
if (definition === undefined) {
return null;
}
if (typeof definition === 'function' && isClassicHelper(definition)) {
// For classic class based helpers, we need to pass the factoryFor result itself rather
// than the raw value (`factoryFor(...).class`). This is because injections are already
// bound in the factoryFor result, including type-based injections
if (DEBUG) {
// In DEBUG we need to only set the associated value once, otherwise
// we'll trigger an assertion
if (!CLASSIC_HELPER_MANAGER_ASSOCIATED.has(factory)) {
CLASSIC_HELPER_MANAGER_ASSOCIATED.add(factory);
setInternalHelperManager(CLASSIC_HELPER_MANAGER, factory);
}
} else {
setInternalHelperManager(CLASSIC_HELPER_MANAGER, factory);
}
return factory;
}
return definition;
}
lookupBuiltInHelper(name: string): HelperDefinitionState | null {
return BUILTIN_KEYWORD_HELPERS[name] ?? null;
}
lookupModifier(name: string, owner: InternalOwner): Nullable<ModifierDefinitionState> {
let builtin = BUILTIN_MODIFIERS[name];
if (builtin !== undefined) {
return builtin;
}
let modifier = owner.factoryFor(`modifier:${name}`);
if (modifier === undefined) {
return null;
}
return modifier.class || null;
}
lookupBuiltInModifier<K extends keyof typeof BUILTIN_KEYWORD_MODIFIERS>(
name: K
): (typeof BUILTIN_KEYWORD_MODIFIERS)[K];
lookupBuiltInModifier(name: string): null;
lookupBuiltInModifier(name: string): ModifierDefinitionState | null {
return BUILTIN_KEYWORD_MODIFIERS[name] ?? null;
}
lookupComponent(name: string, owner: InternalOwner): ResolvedComponentDefinition | null {
let pair = lookupComponentPair(owner, name);
if (pair === null) {
assert(
'Could not find component `<TextArea />` (did you mean `<Textarea />`?)',
name !== 'text-area'
);
return null;
}
let template: Template | null = null;
let key: object;
if (pair.component === null) {
key = template = pair.layout(owner);
} else {
key = pair.component;
}
let cachedComponentDefinition = this.componentDefinitionCache.get(key);
if (cachedComponentDefinition !== undefined) {
return cachedComponentDefinition;
}
if (template === null && pair.layout !== null) {
template = pair.layout(owner);
}
let finalizer = _instrumentStart('render.getComponentDefinition', instrumentationPayload, name);
let definition: Nullable<ResolvedComponentDefinition> = null;
if (pair.component === null) {
definition = {
state: templateOnlyComponent(undefined, name),
manager: TEMPLATE_ONLY_COMPONENT_MANAGER,
template,
};
} else {
let factory = pair.component;
assert(`missing component class ${name}`, factory.class !== undefined);
let ComponentClass = factory.class;
let manager = getInternalComponentManager(ComponentClass);
definition = {
state: isCurlyManager(manager) ? factory : ComponentClass,
manager,
template,
};
}
finalizer();
this.componentDefinitionCache.set(key, definition);
assert(
'Could not find component `<TextArea />` (did you mean `<Textarea />`?)',
!(definition === null && name === 'text-area')
);
return definition;
}
}