denoland / deno_ast

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

fix: drop unnecessary whitespace #169

Closed marvinhagemeister closed 10 months ago

marvinhagemeister commented 10 months ago

Similar to text nodes in HTML, JSXText nodes that have surrounding whitespace can be trimmed in most scenarios. This makes the compilation output cleaner and allows us to flatten children arrays if only a single child remained after dropping unnecessary nodes.

Before:

 _jsxssr($$_tpl_1, _jsx(Head, {
    children: [
      "\n\n        ",
      _jsxssr($$_tpl_2),
      "\n\n        ",
      _jsxssr($$_tpl_3),
      "\n\n        ",
      _jsxssr($$_tpl_4),
      "\n\n        ",
      _jsxssr($$_tpl_5),
      "\n\n\n\n        ",
      "\n\n        ",
      _jsxssr($$_tpl_6),
      "\n\n\n\n        ",
      _jsxssr($$_tpl_7),
      "\n\n        ",
      _jsxssr($$_tpl_8),
      "\n\n      "
    ]
  })

After

 _jsxssr($$_tpl_1, _jsx(Head, {
    children: [
      _jsxssr($$_tpl_2),
      _jsxssr($$_tpl_3),
      _jsxssr($$_tpl_4),
      _jsxssr($$_tpl_5),
      _jsxssr($$_tpl_6),
      _jsxssr($$_tpl_7),
      _jsxssr($$_tpl_8)
    ]
  })