Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSX +- toggles like LiveScript object literals #135

Merged
merged 1 commit into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions civet.dev/cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ for item of iterable
## JSX

Enhancements, inspired by [solid-dsl discussions](https://github.com/solidjs-community/solid-dsl/discussions)
and [jsx spec issues](https://github.com/facebook/jsx/issues)

### Element id

Expand All @@ -375,6 +376,12 @@ Enhancements, inspired by [solid-dsl discussions](https://github.com/solidjs-com
<div .{expression}> Civet
</Playground>

### Boolean Toggles

<Playground>
<Component +draggable -disabled>
</Playground>

### Attributes

<Playground>
Expand Down
5 changes: 5 additions & 0 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -3967,6 +3967,11 @@ JSXAttribute
class: $2,
}

# NOTE: Matching LiveScript flagging shorthand +x -y -> x={true} y={false}
$[+-]:toggle JSXAttributeName:id ->
const value = toggle === "+" ? "true" : "false"
return [ " ", id, "={", value, "}" ]

JSXShorthandString
/(?:[\w\-:]+|\([^()]*\)|\[[^\[\]]*\])+/ ->
return module.quoteString($0)
Expand Down
17 changes: 17 additions & 0 deletions test/jsx/attr.civet
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,20 @@ describe "JSX class shorthand", ->
---
<div class={["class1 class2 t-[5px]", myClass(), anotherClass].filter(Boolean).join(" ")} id="id"/>
"""

describe "LiveScript-like toggles", ->
testCase """
single toggle
---
<div +draggable/>
---
<div draggable={true}/>
"""

testCase """
multiple toggles
---
<Component +draggable +enabled -visible/>
---
<Component draggable={true} enabled={true} visible={false}/>
"""