Skip to content

Commit

Permalink
Merge pull request #14717 from emberjs/fix-attrs
Browse files Browse the repository at this point in the history
[BUGFIX release] Fix block params named `attrs`
  • Loading branch information
chancancode authored Dec 14, 2016
2 parents 338817f + 5d86c30 commit ae0f5f5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
20 changes: 20 additions & 0 deletions packages/ember-glimmer/tests/integration/syntax/with-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ moduleFor('Syntax test: {{#with as}}', class extends IfUnlessWithSyntaxTest {
this.assertText('Hello world');
}

['@test `attrs` can be used as a block param [GH#14678]']() {
this.render('{{#with hash as |attrs|}}[{{hash.foo}}-{{attrs.foo}}]{{/with}}', {
hash: { foo: 'foo' }
});

this.assertText('[foo-foo]');

this.runTask(() => this.rerender());

this.assertText('[foo-foo]');

this.runTask(() => this.context.set('hash.foo', 'FOO'));

this.assertText('[FOO-FOO]');

this.runTask(() => this.context.set('hash.foo', 'foo'));

this.assertText('[foo-foo]');
}

});

moduleFor('Syntax test: Multiple {{#with as}} helpers', class extends RenderingTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,24 @@ export default function TransformAttrsToProps() {
this.syntax = null;
}

function isAttrs(node) {
if (node.parts[0] === 'attrs') {
return true;
function isAttrs(node, symbols) {
let name = node.parts[0];

if (symbols.indexOf(name) !== -1) {
return false;
}

let _this = node.parts[0];
let attrs = node.parts[1];
if (name === 'attrs') {
return true;
}

if (_this === null && attrs === 'attrs') {
if (name === null && node.parts[1] === 'attrs') {
node.parts.shift();
node.original = node.original.slice(5);
return true;
}

return false;
}

/**
Expand All @@ -50,9 +55,21 @@ function isAttrs(node) {
TransformAttrsToProps.prototype.transform = function TransformAttrsToProps_transform(ast) {
let { traverse, builders: b } = this.syntax;

let stack = [[]];

traverse(ast, {
Program: {
enter(node) {
let parent = stack[stack.length - 1];
stack.push(parent.concat(node.blockParams));
},
exit(node) {
stack.pop();
}
},

PathExpression(node) {
if (isAttrs(node)) {
if (isAttrs(node, stack[stack.length - 1])) {
let path = b.path(node.original.substr(6));
path.original = `@${path.original}`;
path.data = true;
Expand Down

0 comments on commit ae0f5f5

Please sign in to comment.