Skip to content

Commit bb76291

Browse files
authored
Merge pull request #19472 from Windvis/bugfix/transform-attrs-into-args-block-params
2 parents 89c34a4 + 67da419 commit bb76291

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

packages/ember-template-compiler/lib/plugins/transform-attrs-into-args.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,27 @@ export default function transformAttrsIntoArgs(env: EmberASTPluginEnvironment):
3333

3434
let stack: string[][] = [[]];
3535

36+
function updateBlockParamsStack(blockParams: string[]) {
37+
let parent = stack[stack.length - 1];
38+
stack.push(parent.concat(blockParams));
39+
}
40+
3641
return {
3742
name: 'transform-attrs-into-args',
3843

3944
visitor: {
4045
Program: {
4146
enter(node: AST.Program) {
42-
let parent = stack[stack.length - 1];
43-
stack.push(parent.concat(node.blockParams));
47+
updateBlockParamsStack(node.blockParams);
48+
},
49+
exit() {
50+
stack.pop();
51+
},
52+
},
53+
54+
ElementNode: {
55+
enter(node: AST.ElementNode) {
56+
updateBlockParamsStack(node.blockParams);
4457
},
4558
exit() {
4659
stack.pop();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import TransformTestCase from '../utils/transform-test-case';
2+
import { moduleFor, RenderingTestCase } from 'internal-test-helpers';
3+
4+
moduleFor(
5+
'ember-template-compiler: transforming attrs into @args',
6+
class extends TransformTestCase {
7+
['@test it transforms attrs into @args']() {
8+
expectDeprecation(() => {
9+
this.assertTransformed(`{{attrs.foo}}`, `{{@foo}}`);
10+
}, /Using {{attrs}} to reference named arguments has been deprecated. {{attrs.foo}} should be updated to {{@foo}}./);
11+
12+
expectDeprecation(() => {
13+
this.assertTransformed(`{{attrs.foo.bar}}`, `{{@foo.bar}}`);
14+
}, /Using {{attrs}} to reference named arguments has been deprecated. {{attrs.foo.bar}} should be updated to {{@foo.bar}}./);
15+
16+
expectDeprecation(() => {
17+
this.assertTransformed(`{{if attrs.foo "foo"}}`, `{{if @foo "foo"}}`);
18+
}, /Using {{attrs}} to reference named arguments has been deprecated. {{attrs.foo}} should be updated to {{@foo}}./);
19+
20+
expectDeprecation(() => {
21+
this.assertTransformed(`{{#if attrs.foo}}{{/if}}`, `{{#if @foo}}{{/if}}`);
22+
}, /Using {{attrs}} to reference named arguments has been deprecated. {{attrs.foo}} should be updated to {{@foo}}./);
23+
24+
expectDeprecation(() => {
25+
this.assertTransformed(`{{deeply (nested attrs.foo.bar)}}`, `{{deeply (nested @foo.bar)}}`);
26+
}, /Using {{attrs}} to reference named arguments has been deprecated. {{attrs.foo.bar}} should be updated to {{@foo.bar}}./);
27+
28+
expectDeprecation(() => {
29+
this.assertTransformed(`{{this.attrs.foo}}`, `{{@foo}}`);
30+
}, /Using {{attrs}} to reference named arguments has been deprecated. {{attrs.foo}} should be updated to {{@foo}}./);
31+
}
32+
}
33+
);
34+
35+
moduleFor(
36+
'ember-template-compiler: not transforming block params named "attrs" into @args',
37+
class extends RenderingTestCase {
38+
["@test it doesn't transform block params"]() {
39+
this.registerComponent('foo', {
40+
template: '{{#let "foo" as |attrs|}}{{attrs}}{{/let}}',
41+
});
42+
this.render('<Foo />');
43+
this.assertComponentElement(this.firstChild, { content: 'foo' });
44+
}
45+
46+
["@test it doesn't transform component block params"]() {
47+
this.registerComponent('foo', {
48+
template: '{{yield "foo"}}',
49+
});
50+
this.render('<Foo as |attrs|>{{attrs}}</Foo>');
51+
this.assertComponentElement(this.firstChild, { content: 'foo' });
52+
}
53+
54+
["@test it doesn't transform block params with nested keys"]() {
55+
this.registerComponent('foo', {
56+
template: '{{yield (hash bar="baz")}}',
57+
});
58+
this.render('<Foo as |attrs|>{{attrs.bar}}</Foo>');
59+
this.assertComponentElement(this.firstChild, { content: 'baz' });
60+
}
61+
}
62+
);

0 commit comments

Comments
 (0)