Skip to content

Commit 9498833

Browse files
committed
glhf
1 parent e2d2bf0 commit 9498833

8 files changed

Lines changed: 28 additions & 49 deletions

File tree

packages/@glimmer-workspace/integration-tests/lib/compile.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export function createTemplate(
2424
): TemplateFactory {
2525
options.locals = options.locals ?? Object.keys(scopeValues ?? {});
2626
let [block, usedLocals] = precompileJSON(templateSource, options);
27-
let reifiedScopeValues = usedLocals.map((key) => scopeValues[key]);
27+
let reifiedScope: Record<string, unknown> = {};
28+
for (let key of usedLocals) {
29+
reifiedScope[key] = scopeValues[key];
30+
}
2831

2932
if ('emit' in options && options.emit?.debugSymbols) {
3033
block.push(usedLocals);
@@ -34,7 +37,7 @@ export function createTemplate(
3437
id: String(templateId++),
3538
block: JSON.stringify(block),
3639
moduleName: options.meta?.moduleName ?? '(unknown template module)',
37-
scope: reifiedScopeValues.length > 0 ? () => reifiedScopeValues : null,
40+
scope: usedLocals.length > 0 ? () => reifiedScope : null,
3841
isStrictMode: options.strictMode ?? false,
3942
};
4043

packages/@glimmer-workspace/integration-tests/test/compiler/compile-options-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module('[glimmer-compiler] precompile', ({ test }) => {
5757
...WireFormat.Statement[],
5858
];
5959

60-
assert.deepEqual(wire.scope?.(), [hello]);
60+
assert.deepEqual(wire.scope?.(), { hello });
6161

6262
assert.deepEqual(
6363
componentNameExpr,
@@ -84,7 +84,7 @@ module('[glimmer-compiler] precompile', ({ test }) => {
8484
...WireFormat.Statement[],
8585
];
8686

87-
assert.deepEqual(wire.scope?.(), [f]);
87+
assert.deepEqual(wire.scope?.(), { f });
8888
assert.deepEqual(
8989
componentNameExpr,
9090
[SexpOpcodes.GetLexicalSymbol, 0, ['hello']],
@@ -218,7 +218,7 @@ module('[glimmer-compiler] precompile', ({ test }) => {
218218
_wire = compile(`{{this.message}}`, ['this'], (source) => eval(source));
219219
}).call(target);
220220
let wire = _wire!;
221-
assert.deepEqual(wire.scope?.(), [target]);
221+
assert.deepEqual(wire.scope?.(), { this: target });
222222
assert.deepEqual(wire.block[0], [
223223
[SexpOpcodes.Append, [SexpOpcodes.GetLexicalSymbol, 0, ['message']]],
224224
]);

packages/@glimmer-workspace/integration-tests/test/debug-render-tree-test.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,10 @@ class DebugRenderTreeTest extends RenderTest {
111111
}
112112

113113
@test 'strict-mode components without debug symbols preserve names from scope'() {
114-
const state = trackedObj({ showSecond: false });
115-
116114
const HelloWorld = defComponent('{{@arg}}');
117115
const Root = defComponent(
118-
`<HelloWorld @arg="first"/>{{#if state.showSecond}}<HelloWorld @arg="second"/>{{/if}}`,
119-
{ scope: { HelloWorld, state }, emit: { moduleName: 'root.hbs', debugSymbols: false } }
116+
`<HelloWorld @arg="first"/>`,
117+
{ scope: { HelloWorld }, emit: { moduleName: 'root.hbs', debugSymbols: false } }
120118
);
121119

122120
this.renderComponent(Root);
@@ -142,39 +140,6 @@ class DebugRenderTreeTest extends RenderTest {
142140
],
143141
},
144142
]);
145-
146-
state['showSecond'] = true;
147-
148-
this.assertRenderTree([
149-
{
150-
type: 'component',
151-
name: '{ROOT}',
152-
args: { positional: [], named: {} },
153-
instance: null,
154-
template: 'root.hbs',
155-
bounds: this.elementBounds(this.delegate.getInitialElement()),
156-
children: [
157-
{
158-
type: 'component',
159-
name: 'HelloWorld',
160-
args: { positional: [], named: { arg: 'first' } },
161-
instance: null,
162-
template: '(unknown template module)',
163-
bounds: this.nodeBounds(this.delegate.getInitialElement().firstChild),
164-
children: [],
165-
},
166-
{
167-
type: 'component',
168-
name: 'HelloWorld',
169-
args: { positional: [], named: { arg: 'second' } },
170-
instance: null,
171-
template: '(unknown template module)',
172-
bounds: this.nodeBounds(this.delegate.getInitialElement().lastChild),
173-
children: [],
174-
},
175-
],
176-
},
177-
]);
178143
}
179144

180145
@test 'strict-mode modifiers'() {

packages/@glimmer/compiler/lib/compiler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,14 @@ export function precompile(
148148
let stringified = JSON.stringify(templateJSONObject);
149149

150150
if (usedLocals.length > 0) {
151-
const scopeFn = `()=>[${usedLocals.join(',')}]`;
151+
const scopeEntries = usedLocals.map((name) => {
152+
// Reserved words like "this" can't use shorthand property syntax
153+
if (name === 'this') {
154+
return `"this":this`;
155+
}
156+
return name;
157+
});
158+
const scopeFn = `()=>({${scopeEntries.join(',')}})`;
152159

153160
stringified = stringified.replace(`"${SCOPE_PLACEHOLDER}"`, scopeFn);
154161
}

packages/@glimmer/interfaces/lib/compile/wire-format/api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ export interface SerializedTemplateWithLazyBlock {
397397
id?: Nullable<string>;
398398
block: SerializedTemplateBlockJSON;
399399
moduleName: string;
400-
scope?: (() => unknown[]) | undefined | null;
400+
scope?: (() => Record<string, unknown>) | undefined | null;
401401
isStrictMode: boolean;
402402
}
403403

packages/@glimmer/interfaces/lib/template.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface LayoutWithContext {
1818
readonly block: SerializedTemplateBlock;
1919
readonly moduleName: string;
2020
readonly owner: Owner | null;
21-
readonly scope: (() => unknown[]) | undefined | null;
21+
readonly scope: (() => Record<string, unknown>) | undefined | null;
2222
readonly isStrictMode: boolean;
2323
}
2424

packages/@glimmer/opcode-compiler/lib/opcode-builder/helpers/shared.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ export function CompilePositional(
107107

108108
export function meta(layout: LayoutWithContext): BlockMetadata {
109109
let [, locals, upvars, lexicalSymbols] = layout.block;
110+
let scopeRecord = layout.scope?.() ?? null;
110111

111112
return {
112113
symbols: {
113114
locals,
114115
upvars,
115-
lexical: lexicalSymbols,
116+
lexical: scopeRecord ? Object.keys(scopeRecord) : lexicalSymbols,
116117
},
117-
scopeValues: layout.scope?.() ?? null,
118+
scopeValues: scopeRecord ? Object.values(scopeRecord) : null,
118119
isStrictMode: layout.isStrictMode,
119120
moduleName: layout.moduleName,
120121
owner: layout.owner,

packages/internal-test-helpers/lib/compile.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ export default function compile(
2424
): TemplateFactory {
2525
options.locals = options.locals ?? Object.keys(scopeValues ?? {});
2626
let [block, usedLocals] = precompileJSON(templateSource, compileOptions(options));
27-
let reifiedScopeValues = usedLocals.map((key) => scopeValues[key]);
27+
let reifiedScope: Record<string, unknown> = {};
28+
for (let key of usedLocals) {
29+
reifiedScope[key] = scopeValues[key];
30+
}
2831

2932
let templateBlock: SerializedTemplateWithLazyBlock = {
3033
block: JSON.stringify(block),
3134
moduleName: options.moduleName ?? options.meta?.moduleName ?? '(unknown template module)',
32-
scope: reifiedScopeValues.length > 0 ? () => reifiedScopeValues : null,
35+
scope: usedLocals.length > 0 ? () => reifiedScope : null,
3336
isStrictMode: options.strictMode ?? false,
3437
};
3538

0 commit comments

Comments
 (0)