-
-
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.
- Loading branch information
1 parent
561e7a7
commit 586d038
Showing
4 changed files
with
121 additions
and
9 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
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
107 changes: 107 additions & 0 deletions
107
packages/ember-template-compiler/lib/plugins/transform-dot-component-invocation.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,107 @@ | ||
|
||
/** | ||
Transforms dot invocation of closure components to be wrapped | ||
with the component helper. This allows for a more static invocation | ||
of the component. | ||
```handlebars | ||
{{#my-component as |comps|}} | ||
{{comp.dropdown isOpen=false}} | ||
{{/my-component}} | ||
``` | ||
with | ||
```handlebars | ||
{{#my-component as |comps|}} | ||
{{component comp.dropdown isOpen=false}} | ||
{{/my-component}} | ||
``` | ||
and | ||
```handlebars | ||
{{#my-component as |comps|}} | ||
{{comp.dropdown isOpen}} | ||
{{/my-component}} | ||
``` | ||
with | ||
```handlebars | ||
{{#my-component as |comps|}} | ||
{{component comp.dropdown isOpen}} | ||
{{/my-component}} | ||
``` | ||
and | ||
```handlebars | ||
{{#my-component as |comps|}} | ||
{{#comp.dropdown}}Open{{/comp.dropdown}} | ||
{{/my-component}} | ||
``` | ||
with | ||
```handlebars | ||
{{#my-component as |comps|}} | ||
{{#component comp.dropdown}}Open{{/component}} | ||
{{/my-component}} | ||
``` | ||
@private | ||
@class TransFormDotComponentInvocation | ||
*/ | ||
export default class TransFormDotComponentInvocation { | ||
|
||
_isMulipartPath(path) { | ||
return path.parts.length > 1; | ||
} | ||
|
||
_isInlineInvocation(path, params, hash) { | ||
if (this._isMulipartPath(path)) { | ||
if (params.length > 0 || hash.pairs.length > 0) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
_wrapInComponent(node, builder) { | ||
let component = node.path; | ||
let componentHelper = builder.path('component'); | ||
node.path = componentHelper; | ||
node.params.unshift(component); | ||
} | ||
|
||
transform(ast) { | ||
let { traverse, builders: b } = this.syntax; | ||
|
||
traverse(ast, { | ||
BlockStatement: (node) => { | ||
node.program.body = node.program.body.map((_node) => { | ||
switch(_node.type) { | ||
case 'MustacheStatement': | ||
if (this._isInlineInvocation(_node.path, _node.params, _node.hash)) { | ||
this._wrapInComponent(_node, b); | ||
} | ||
break; | ||
case 'BlockStatement': | ||
if (this._isMulipartPath(_node.path)) { | ||
this._wrapInComponent(_node, b) | ||
} | ||
break; | ||
} | ||
|
||
return _node; | ||
}); | ||
|
||
return node; | ||
} | ||
}); | ||
|
||
return ast; | ||
} | ||
} | ||
|