Skip to content

Commit 7802081

Browse files
committed
Working on lexically-captured this support
1 parent d72607e commit 7802081

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

__tests__/tests.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,17 @@ describe('htmlbars-inline-precompile', function () {
18711871
`);
18721872
expect(spy.firstCall.lastArg).toHaveProperty('locals', ['bar']);
18731873
});
1874+
1875+
it('can pass lexically scoped "this"', function () {
1876+
let spy = sinon.spy(compiler, 'precompile');
1877+
transform(`
1878+
import { precompileTemplate } from '@ember/template-compilation';
1879+
export function example() {
1880+
return precompileTemplate('{{this.message}}', { scope: () => ({ "this": this }) });
1881+
}
1882+
`);
1883+
expect(spy.firstCall.lastArg).toHaveProperty('locals', ['this']);
1884+
});
18741885
});
18751886

18761887
describe('implicit-scope-form', function () {

pnpm-lock.yaml

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/expression-parser.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,18 @@ export class ExpressionParser {
101101

102102
let propName = name(key);
103103

104-
if (value.type !== 'Identifier') {
105-
throw path.buildCodeFrameError(
106-
`Scope objects for \`${invokedName}\` may only contain direct references to in-scope values, e.g. { ${propName} } or { ${propName}: ${propName} }`
107-
);
104+
switch (value.type) {
105+
case 'Identifier':
106+
res.add(propName, value.name);
107+
break;
108+
case 'ThisExpression':
109+
res.add(propName, 'this');
110+
break;
111+
default:
112+
throw path.buildCodeFrameError(
113+
`Scope objects for \`${invokedName}\` may only contain direct references to in-scope values, e.g. { ${propName} } or { ${propName}: ${propName} }. Found ${value.type}`
114+
);
108115
}
109-
110-
res.add(propName, value.name);
111116
return res;
112117
},
113118
new ScopeLocals({ mode: 'explicit' })

0 commit comments

Comments
 (0)