From a6cc8829243564a129de28d5db8fcc7ba5a1a300 Mon Sep 17 00:00:00 2001 From: Tony Quetano Date: Mon, 18 Dec 2023 13:04:31 -0500 Subject: [PATCH] reuse member expressions as the local collection (#35) --- __tests__/__fixtures__/complex/jsx/output.js | 33 ++++++++------------ src/handlers.ts | 2 +- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/__tests__/__fixtures__/complex/jsx/output.js b/__tests__/__fixtures__/complex/jsx/output.js index fe1a8a7..130632c 100644 --- a/__tests__/__fixtures__/complex/jsx/output.js +++ b/__tests__/__fixtures__/complex/jsx/output.js @@ -1,23 +1,16 @@ import React from 'react'; function List(props) { - return /*#__PURE__*/ React.createElement( - 'ul', - null, - (() => { - const _collection = props.items; - const _length = _collection.length; - const _results = Array(_length); - for (let _key = 0, _item; _key < _length; ++_key) { - _item = _collection[_key]; - _results[_key] = /*#__PURE__*/ React.createElement( - 'li', - { - key: _item.id, - }, - _item.value, - ); - } - return _results; - })(), - ); + const _length = props.items.length; + const _results = Array(_length); + for (let _key = 0, _item; _key < _length; ++_key) { + _item = props.items[_key]; + _results[_key] = /*#__PURE__*/ React.createElement( + 'li', + { + key: _item.id, + }, + _item.value, + ); + } + return /*#__PURE__*/ React.createElement('ul', null, _results); } \ No newline at end of file diff --git a/src/handlers.ts b/src/handlers.ts index 9f1a79f..8acecdc 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -178,7 +178,7 @@ export function createHandlers(babel: MacroParams['babel']) { let localCollection = collection.node; - if (!collection.isIdentifier()) { + if (!collection.isIdentifier() && !collection.isMemberExpression()) { localCollection = getLocalName(path, 'collection'); const localVariable = templates.localVariable({