Skip to content

Commit

Permalink
JSX +- toggles like LiveScript object literals (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine authored Jan 7, 2023
1 parent 680beed commit 5d81e00
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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}/>
"""

0 comments on commit 5d81e00

Please sign in to comment.