-
-
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 6df171a
Showing
8 changed files
with
220 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
101 changes: 101 additions & 0 deletions
101
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,101 @@ | ||
|
||
/** | ||
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, { | ||
MustacheStatement: (node) => { | ||
if (this._isInlineInvocation(node.path, node.params, node.hash)) { | ||
this._wrapInComponent(node, b); | ||
} | ||
}, | ||
BlockStatement: (node) => { | ||
if (this._isMulipartPath(node.path)) { | ||
this._wrapInComponent(node, b) | ||
} | ||
} | ||
}); | ||
|
||
return ast; | ||
} | ||
}; | ||
|
23 changes: 23 additions & 0 deletions
23
packages/ember-template-compiler/tests/plugins/transform-dot-component-invocation-test.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,23 @@ | ||
import { compile } from '../../index'; | ||
|
||
QUnit.module('ember-template-compiler: transforms dot component invocation'); | ||
|
||
QUnit.test('Does not throw a compiler error for path components', function(assert) { | ||
assert.expect(1); | ||
|
||
[ | ||
'{{this.modal open}}', | ||
'{{this.modal isOpen=true}}', | ||
'{{#this.modal}}Woot{{/this.modal}}', | ||
'{{c.modal open}}', | ||
'{{c.modal isOpen=true}}', | ||
'{{#c.modal}}Woot{{/c.modal}}', | ||
'{{#my-component as |c|}}{{c.a name="Chad"}}{{/my-component}}', | ||
'{{#my-component as |c|}}{{c.a "Chad"}}{{/my-component}}', | ||
'{{#my-component as |c|}}{{#c.a}}{{/c.a}}{{/my-component}}' | ||
].forEach((layout, i) => { | ||
compile(layout, { moduleName: `example-${i}` }); | ||
}); | ||
|
||
assert.ok(true); | ||
}); |
Oops, something went wrong.