-
-
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
4d0a17b
commit b375277
Showing
7 changed files
with
207 additions
and
109 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
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
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
|
||
/** | ||
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 function TransFormDotComponentInvocation() { | ||
// set later within Glimmer2 to the syntax package | ||
this.syntax = null; | ||
} | ||
|
||
TransFormDotComponentInvocation.prototype = { | ||
_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; | ||
} | ||
}; | ||
|
Oops, something went wrong.