denoland / deno_ast

Source text parsing, lexing, and AST related functionality for Deno
https://crates.io/crates/deno_ast
MIT License
144 stars 43 forks source link

fix: precompile double escape with Fragments #238

Closed marvinhagemeister closed 3 months ago

marvinhagemeister commented 3 months ago

This PR fixes an issue which would lead to content being escaped twice when Fragments with single children were used.

function Foo(props) {
  return <>{props.children}</>
}

const nodes = <>{'"test"'}</>
const final = <Foo>{nodes}</Foo>

// Would be transpiled to something like this
function Foo(props) {
  return jsxEscape(props.children)
}

const nodes = jsxEscape('"test"')
const final = jsx(Foo, { children: nodes })

The issue occurred because of an incorrect assumption that we could optimize single string or expression child Fragments away to a string. But when the string is passed around we don't know whether the string has already been escaped or not, so the string will be escaped again.

The solution is to drop that faulty optimization and always materialize a container element.

Fixes https://github.com/denoland/deno/issues/22965#issuecomment-2024730199