Skip to content

[Bug]: jsx whitespace is not properly preserved when retainLines: true #16774

@hi-ogawa

Description

@hi-ogawa

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

Full code is here https://github.com/hi-ogawa/reproductions/tree/main/vite-17954-babel-jsx-whitespace and you can try it on stackblitz https://stackblitz.com/github/hi-ogawa/reproductions/tree/main/vite-17954-babel-jsx-whitespace?file=src%2Frepro.js

Input

export const test = <div>{
  "Lorem"
} ispum </div>;

Configuration file name

No response

Configuration

Using babel api

await babel.transformAsync(input, {
  parserOpts: {
    plugins: ["jsx"],
  },
  retainLines: true,
  configFile: false,
  babelrc: false,
})

Current and expected behavior

Current output:

export const test = <div>{
  "Lorem"}
   ispum </div>;

Expected output:

I'm not sure... maybe same as input.

Environment

(stackblitz)

System:
OS: Linux 5.0 undefined
Binaries:
Node: 18.20.3 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.2.3 - /usr/local/bin/npm
pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
@babel/core: ^7.25.2 => 7.25.2

Possible solution

No response

Additional context

Originally issue is reported on Vite vitejs/vite-plugin-react#360

Vite's babel react plugin uses babel transform for fast-refresh while keeping JSX syntax, then later runs esbuild to transpile JSX. For development, we use retainLines: true so that esbuild's jsxDEV transpilation can include accurate line (cf. vitejs/vite-plugin-react#246).

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: jsxoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: generator

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions