-
Notifications
You must be signed in to change notification settings - Fork 7
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
a0a4d05
commit 497a87d
Showing
8 changed files
with
196 additions
and
25 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/__tests__/fixtures/tag/__snapshots__/tag-with-state/node.render.expected.html
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,22 @@ | ||
<ul> | ||
<li> | ||
<input | ||
type="checkbox" | ||
/> | ||
Nested 1 | ||
</li> | ||
<li> | ||
<input | ||
type="checkbox" | ||
/> | ||
Nested 2 | ||
<ul> | ||
<li> | ||
<input | ||
type="checkbox" | ||
/> | ||
Nested 3 | ||
</li> | ||
</ul> | ||
</li> | ||
</ul> |
22 changes: 22 additions & 0 deletions
22
src/__tests__/fixtures/tag/__snapshots__/tag-with-state/web.render.expected.html
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,22 @@ | ||
<ul> | ||
<li> | ||
<input | ||
type="checkbox" | ||
/> | ||
Nested 1 | ||
</li> | ||
<li> | ||
<input | ||
type="checkbox" | ||
/> | ||
Nested 2 | ||
<ul> | ||
<li> | ||
<input | ||
type="checkbox" | ||
/> | ||
Nested 3 | ||
</li> | ||
</ul> | ||
</li> | ||
</ul> |
23 changes: 23 additions & 0 deletions
23
src/__tests__/fixtures/tag/__snapshots__/tag-with-state/web.step-0.expected.html
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 @@ | ||
<ul> | ||
<li> | ||
<input | ||
checked="" | ||
type="checkbox" | ||
/> | ||
Nested 1 | ||
</li> | ||
<li> | ||
<input | ||
type="checkbox" | ||
/> | ||
Nested 2 | ||
<ul> | ||
<li> | ||
<input | ||
type="checkbox" | ||
/> | ||
Nested 3 | ||
</li> | ||
</ul> | ||
</li> | ||
</ul> |
24 changes: 24 additions & 0 deletions
24
src/__tests__/fixtures/tag/__snapshots__/tag-with-state/web.step-1.expected.html
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,24 @@ | ||
<ul> | ||
<li> | ||
<input | ||
checked="" | ||
type="checkbox" | ||
/> | ||
Nested 1 | ||
</li> | ||
<li> | ||
<input | ||
checked="" | ||
type="checkbox" | ||
/> | ||
Nested 2 | ||
<ul> | ||
<li> | ||
<input | ||
type="checkbox" | ||
/> | ||
Nested 3 | ||
</li> | ||
</ul> | ||
</li> | ||
</ul> |
25 changes: 25 additions & 0 deletions
25
src/__tests__/fixtures/tag/__snapshots__/tag-with-state/web.step-2.expected.html
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,25 @@ | ||
<ul> | ||
<li> | ||
<input | ||
checked="" | ||
type="checkbox" | ||
/> | ||
Nested 1 | ||
</li> | ||
<li> | ||
<input | ||
checked="" | ||
type="checkbox" | ||
/> | ||
Nested 2 | ||
<ul> | ||
<li> | ||
<input | ||
checked="" | ||
type="checkbox" | ||
/> | ||
Nested 3 | ||
</li> | ||
</ul> | ||
</li> | ||
</ul> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<attrs/{ items }/> | ||
|
||
<tag/item|{ label, items }|> | ||
<let/isChecked = false/> | ||
<li> | ||
<input type="checkbox" checked:=isChecked> | ||
${label} | ||
|
||
<if=items> | ||
<ul> | ||
<for|value| of=items> | ||
<item ...value /> | ||
</for> | ||
</ul> | ||
</if> | ||
</li> | ||
</tag> | ||
|
||
<ul> | ||
<for|value| of=items> | ||
<item ...value /> | ||
</for> | ||
</ul> |
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 |
---|---|---|
@@ -1,32 +1,43 @@ | ||
import { types as t } from "@marko/compiler"; | ||
import assertNoAssignments from "../../util/assert-no-assignments"; | ||
const IDENTIFIERS = new WeakMap<t.NodePath<t.MarkoTag>, t.Identifier>(); | ||
|
||
export = function transform(tag: t.NodePath<t.MarkoTag>) { | ||
const tagVar = tag.node.var! as t.Identifier; | ||
const body = tag.node.body; | ||
const errorMessage = tag.node.attributes.length | ||
? "does not support attributes" | ||
: !tagVar | ||
? "requires a tag variable" | ||
: !t.isIdentifier(tagVar) | ||
? "cannot have a destructured tag variable" | ||
: tag.node.arguments | ||
? "does not support arguments" | ||
: !body.body.length | ||
? "requires body content" | ||
: undefined; | ||
export = { | ||
enter(tag: t.NodePath<t.MarkoTag>) { | ||
const tagVar = tag.node.var! as t.Identifier; | ||
const body = tag.node.body; | ||
const errorMessage = tag.node.attributes.length | ||
? "does not support attributes" | ||
: !tagVar | ||
? "requires a tag variable" | ||
: !t.isIdentifier(tagVar) | ||
? "cannot have a destructured tag variable" | ||
: tag.node.arguments | ||
? "does not support arguments" | ||
: !body.body.length | ||
? "requires body content" | ||
: undefined; | ||
|
||
if (errorMessage) { | ||
throw tag.get("name").buildCodeFrameError(`The <tag> tag ${errorMessage}.`); | ||
} | ||
if (errorMessage) { | ||
throw tag | ||
.get("name") | ||
.buildCodeFrameError(`The <tag> tag ${errorMessage}.`); | ||
} | ||
|
||
assertNoAssignments(tag.get("var") as t.NodePath<t.PatternLike>); | ||
assertNoAssignments(tag.get("var") as t.NodePath<t.PatternLike>); | ||
|
||
tag.replaceWith( | ||
t.functionDeclaration( | ||
tagVar, | ||
[t.identifier("out"), ...body.params], | ||
t.blockStatement(body.body) | ||
) | ||
); | ||
// We must clear the var to avoid the default translator complaining | ||
// but we store it in a weakmap so we can read on exit. | ||
IDENTIFIERS.set(tag, tagVar); | ||
tag.node.var = null; | ||
}, | ||
exit(tag: t.NodePath<t.MarkoTag>) { | ||
tag.replaceWith( | ||
t.functionDeclaration( | ||
IDENTIFIERS.get(tag)!, | ||
[t.identifier("out"), ...tag.node.body.params], | ||
t.blockStatement(tag.node.body.body) | ||
) | ||
); | ||
}, | ||
}; |