Skip to content

[JSX] Print Empty Elem - Self closing tags when elem has no children #4037

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

Merged
merged 5 commits into from
Feb 9, 2025
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
4 changes: 4 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [JS/TS] - Fix anonymous record printing (#4029) (by @alfonsogarciacaro)
* [Python] - Fix #3998: PhysicalEquality (by @alfonsogarciacaro)

### Changed

* [JS/TS] In `JSX`, generate self closing element when element has no children (#4037) (by @shayanhabibi)

## 5.0.0-alpha.9 - 2025-01-28

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [JS/TS] - Fix anonymous record printing (#4029) (by @alfonsogarciacaro)
* [Python] - Fix #3998: PhysicalEquality (by @alfonsogarciacaro)

### Changed

* [JS/TS] In `JSX`, generate self closing element when element has no children (#4037) (by @shayanhabibi)

## 5.0.0-alpha.9 - 2025-01-28

### Fixed
Expand Down
17 changes: 11 additions & 6 deletions src/Fable.Transforms/BabelPrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,14 @@ module PrinterExtensions =

printer.PopIndentation()

printer.Print(">")

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

Expand All @@ -482,9 +487,9 @@ module PrinterExtensions =

printer.PopIndentation()

printer.Print("</")
printTag componentOrTag
printer.Print(">")
printer.Print("</")
printTag componentOrTag
printer.Print(">")

member printer.Print(expr: Expression) =
match expr with
Expand Down
7 changes: 6 additions & 1 deletion tests/React/__tests__/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Jest.describe("React tests", (fun () ->
Jest.expect(header).toHaveTextContent("6")
))

Jest.test("JSX EmptyElem render self-closing tags", (fun () ->
let elem = Counter.CounterJSX(5)
Jest.expect(elem).toStrictEqual(Fable.Core.JSX.jsx "<CounterJSX init={5} />")
))

Jest.test("SpreadSheet renders correctly", (fun () ->
let elem = RTL.render(SpreadSheet.SpreadSheet() |> unbox)
Jest.expect(elem.container).toMatchSnapshot()
Expand Down Expand Up @@ -74,4 +79,4 @@ Jest.describe("React tests", (fun () ->
let text = elem.getByTestId "text"
Jest.expect(text).toHaveTextContent("3")
))
))
))
Loading