pugjs / babel-plugin-transform-react-pug

A plugin for transpiling pug templates to jsx
MIT License
811 stars 47 forks source link

Transform each into map #75

Closed ezhlobo closed 5 years ago

ezhlobo commented 5 years ago

Hey,

Now when I'm passing basic each it transpiles into a monster:

each item in [1, 2, 3]
  div(key=item)= item
((_pug_nodes, _pug_arr) => {
  if (!(_pug_arr == null || Array.isArray(_pug_arr)))
    throw new Error(
      'Expected "[1, 2, 3]" to be an array because it is passed to each.'
    );
  if (_pug_arr == null || _pug_arr.length === 0) return undefined;

  for (let _pug_index = 0; _pug_index < _pug_arr.length; _pug_index++) {
    const item = _pug_arr[_pug_index];
    _pug_nodes[_pug_nodes.length] = <div key={"pug" + item + ":0"}>{item}</div>;
  }

  return _pug_nodes;
})([], [1, 2, 3]);

What do you think if we eliminate all these validations and return simple .map:

[1, 2, 3].map((item) => (
  <div key={"pug" + item + ":0"}>{item}</div>
))

And then even into something with initial keys:

[1, 2, 3].map((item) => (
  <div key={item}>{item}</div>
))

Why?

  1. Because I wish to keep the code as simple as possible where we can.
  2. These validations are redundant because we anyway will catch the issue in js. No matter who throws an exception.

//CC @ForbesLindesay

jeremy-coleman commented 5 years ago

https://esbench.com/bench/5a295e6299634800a0349500

ezhlobo commented 5 years ago

Done in v7.0.0