Skip to content

Commit

Permalink
fix #3818: preserve collapsed jsx whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 2, 2024
1 parent 7c2eb2e commit 626ac2c
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 217 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog

## Unreleased

* Fix preserving collapsed JSX whitespace ([#3818](https://github.com/evanw/esbuild/issues/3818))

When transformed, certain whitespace inside JSX elements is ignored completely if it collapses to an empty string. However, the whitespace should only be ignored if the JSX is being transformed, not if it's being preserved. This release fixes a bug where esbuild was previously incorrectly ignoring collapsed whitespace with `--jsx=preserve`. Here is an example:

```jsx
// Original code
<Foo>
<Bar />
</Foo>

// Old output (with --jsx=preserve)
<Foo><Bar /></Foo>;

// New output (with --jsx=preserve)
<Foo>
<Bar />
</Foo>;
```

## 0.22.0

**This release deliberately contains backwards-incompatible changes.** To avoid automatically picking up releases like this, you should either be pinning the exact version of `esbuild` in your `package.json` file (recommended) or be using a version range syntax that only accepts patch upgrades such as `^0.21.0` or `~0.21.0`. See npm's documentation about [semver](https://docs.npmjs.com/cli/v6/using-npm/semver/) for more information.
Expand Down
6 changes: 0 additions & 6 deletions internal/js_lexer/js_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,12 +794,6 @@ func (lexer *Lexer) NextJSXElementChild() {
if needsFixing {
// Slow path
lexer.decodedStringLiteralOrNil = fixWhitespaceAndDecodeJSXEntities(text)

// Skip this token if it turned out to be empty after trimming
if len(lexer.decodedStringLiteralOrNil) == 0 {
lexer.HasNewlineBefore = true
continue
}
} else {
// Fast path
n := len(text)
Expand Down
4 changes: 3 additions & 1 deletion internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5234,8 +5234,10 @@ func (p *parser) parseJSXElement(loc logger.Loc) js_ast.Expr {
case js_lexer.TStringLiteral:
if p.options.jsx.Preserve {
nullableChildren = append(nullableChildren, js_ast.Expr{Loc: p.lexer.Loc(), Data: &js_ast.EJSXText{Raw: p.lexer.Raw()}})
} else if str := p.lexer.StringLiteral(); len(str) > 0 {
nullableChildren = append(nullableChildren, js_ast.Expr{Loc: p.lexer.Loc(), Data: &js_ast.EString{Value: str}})
} else {
nullableChildren = append(nullableChildren, js_ast.Expr{Loc: p.lexer.Loc(), Data: &js_ast.EString{Value: p.lexer.StringLiteral()}})
// Skip this token if it turned out to be empty after trimming
}
p.lexer.NextJSXElementChild()

Expand Down
Loading

0 comments on commit 626ac2c

Please sign in to comment.