-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX release] Add mut to attributes in contextual components
As explained in issue #13061, given a component `change-button` with template: ```hbs <button {{action (action (mut val) 10)}}>Change to 10</button> ``` If it is invoked as in the following template: ```hbs {{change-button val=value}} <span class="value"> {{value}} </span> ``` Clicking on the button will cause `.value` to contain `10`. On the other hand, invoking it via a closure component: ```hbs {{component (component "change-button" val=value)}} <span class="value"> {{value}} </span> ``` Clicking on the button will not change the text in `.value`. This PR adds a transform that adds `mut` to params and hash values. (cherry picked from commit 31bb7a2)
- Loading branch information
Showing
3 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/ember-template-compiler/lib/plugins/transform-closure-component-attrs-into-mut.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
function TransformClosureComponentAttrsIntoMut() { | ||
// set later within HTMLBars to the syntax package | ||
this.syntax = null; | ||
} | ||
|
||
/** | ||
@private | ||
@method transform | ||
@param {AST} ast The AST to be transformed. | ||
*/ | ||
TransformClosureComponentAttrsIntoMut.prototype.transform = function TransformClosureComponentAttrsIntoMut_transform(ast) { | ||
let b = this.syntax.builders; | ||
let walker = new this.syntax.Walker(); | ||
|
||
walker.visit(ast, function(node) { | ||
if (validate(node)) { | ||
processExpression(b, node); | ||
} | ||
}); | ||
|
||
return ast; | ||
}; | ||
|
||
function processExpression(builder, node) { | ||
processSubExpressionsInNode(builder, node); | ||
|
||
if (isComponentClosure(node)) { | ||
mutParameters(builder, node); | ||
} | ||
} | ||
|
||
function processSubExpressionsInNode(builder, node) { | ||
for (let i = 0; i < node.params.length; i++) { | ||
if (node.params[i].type === 'SubExpression') { | ||
processExpression(builder, node.params[i]); | ||
} | ||
} | ||
|
||
each(node.hash.pairs, function(pair) { | ||
let { value } = pair; | ||
|
||
if (value.type === 'SubExpression') { | ||
processExpression(builder, value); | ||
} | ||
}); | ||
} | ||
|
||
function isComponentClosure(node) { | ||
return node.type === 'SubExpression' && node.path.original === 'component'; | ||
} | ||
|
||
function mutParameters(builder, node) { | ||
for (let i = 1; i < node.params.length; i++) { | ||
if (node.params[i].type === 'PathExpression') { | ||
node.params[i] = builder.sexpr(builder.path('@mut'), [node.params[i]]); | ||
} | ||
} | ||
|
||
each(node.hash.pairs, function(pair) { | ||
let { value } = pair; | ||
|
||
if (value.type === 'PathExpression') { | ||
pair.value = builder.sexpr(builder.path('@mut'), [pair.value]); | ||
} | ||
}); | ||
} | ||
|
||
function validate(node) { | ||
return node.type === 'BlockStatement' || node.type === 'MustacheStatement'; | ||
} | ||
|
||
function each(list, callback) { | ||
for (var i = 0, l = list.length; i < l; i++) { | ||
callback(list[i]); | ||
} | ||
} | ||
|
||
export default TransformClosureComponentAttrsIntoMut; |