Skip to content

Commit 5c95930

Browse files
[JSX] Print Empty Elem - Self closing tags when elem has no children (#4037)
* Enhancement - EmptyElem JSX printer * Space before closing tag * Documentation regarding possible consequences for self closing tag in JSX rendering * Add test for self-closing tag * update changelog --------- Co-authored-by: Maxime Mangel <[email protected]>
1 parent 151db12 commit 5c95930

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

src/Fable.Cli/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
* [Python] `importSideEffects` shouldn't generate identifier (#3965) (by @alfonsogarciacaro)
2222
* [JS/TS] Fix #4031: Hoist vars locally in for and while loops (@alfonsogarciacaro)
2323

24+
### Changed
25+
26+
* [JS/TS] In `JSX`, generate self closing element when element has no children (#4037) (by @shayanhabibi)
27+
2428
## 5.0.0-alpha.9 - 2025-01-28
2529

2630
### Fixed

src/Fable.Compiler/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
* [Python] `importSideEffects` shouldn't generate identifier (#3965) (by @alfonsogarciacaro)
2222
* [JS/TS] Fix #4031: Hoist vars locally in for and while loops (@alfonsogarciacaro)
2323

24+
### Changed
25+
26+
* [JS/TS] In `JSX`, generate self closing element when element has no children (#4037) (by @shayanhabibi)
27+
2428
## 5.0.0-alpha.9 - 2025-01-28
2529

2630
### Fixed

src/Fable.Transforms/BabelPrinter.fs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,14 @@ module PrinterExtensions =
454454

455455
printer.PopIndentation()
456456

457-
printer.Print(">")
458-
459-
if not (List.isEmpty children) then
457+
if List.isEmpty children then
458+
// Self closing tag if el has no children. Matches expected behaviour of JSX with React/Solid etc
459+
// https://react-cn.github.io/react/tips/self-closing-tag.html
460+
// Self closing tag is invalid in HTML5 spec
461+
// https://stackoverflow.com/questions/3558119/are-non-void-self-closing-tags-valid-in-html5
462+
printer.Print(" />")
463+
else
464+
printer.Print(">")
460465
printer.PrintNewLine()
461466
printer.PushIndentation()
462467

@@ -482,9 +487,9 @@ module PrinterExtensions =
482487

483488
printer.PopIndentation()
484489

485-
printer.Print("</")
486-
printTag componentOrTag
487-
printer.Print(">")
490+
printer.Print("</")
491+
printTag componentOrTag
492+
printer.Print(">")
488493

489494
member printer.Print(expr: Expression) =
490495
match expr with

tests/React/__tests__/Tests.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ Jest.describe("React tests", (fun () ->
4646
Jest.expect(header).toHaveTextContent("6")
4747
))
4848

49+
Jest.test("JSX EmptyElem render self-closing tags", (fun () ->
50+
let elem = Counter.CounterJSX(5)
51+
Jest.expect(elem).toStrictEqual(Fable.Core.JSX.jsx "<CounterJSX init={5} />")
52+
))
53+
4954
Jest.test("SpreadSheet renders correctly", (fun () ->
5055
let elem = RTL.render(SpreadSheet.SpreadSheet() |> unbox)
5156
Jest.expect(elem.container).toMatchSnapshot()
@@ -74,4 +79,4 @@ Jest.describe("React tests", (fun () ->
7479
let text = elem.getByTestId "text"
7580
Jest.expect(text).toHaveTextContent("3")
7681
))
77-
))
82+
))

0 commit comments

Comments
 (0)