diff --git a/__tests__/__fixtures__/bailout/early-return/output.js b/__tests__/__fixtures__/bailout/early-return/output.js index ef75d7d4..222c045b 100644 --- a/__tests__/__fixtures__/bailout/early-return/output.js +++ b/__tests__/__fixtures__/bailout/early-return/output.js @@ -1,12 +1,19 @@ -const _iterable = [1, 2, 3]; -const _fn = (value) => { - if (value === 2) { +const _collection = [1, 2, 3]; +const _fn = (_value) => { + if (_value === 2) { return true; } }; -let _result = []; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; - if (_fn(_value, _key, _iterable)) _result.push(_value); +const _results = []; +for ( + let _key = 0, _length = _collection.length, _value, _result; + _key < _length; + ++_key +) { + _value = _collection[_key]; + _result = _fn(_value, _key, _collection); + if (_result) { + _results.push(_value); + } } -const result = _result; \ No newline at end of file +const result = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/bailout/multiple-returns/output.js b/__tests__/__fixtures__/bailout/multiple-returns/output.js index c6c159e9..46adadcc 100644 --- a/__tests__/__fixtures__/bailout/multiple-returns/output.js +++ b/__tests__/__fixtures__/bailout/multiple-returns/output.js @@ -1,13 +1,14 @@ -const _iterable = [1, 2, 3]; -const _fn = (value) => { - if (value === 2) { +const _collection = [1, 2, 3]; +const _fn = (_value) => { + if (_value === 2) { return 82; } - return value; + return _value; }; -let _result = []; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; - _result[_key] = _fn(_value, _key, _iterable); +const _length = _collection.length; +const _results = Array(_length); +for (let _key = 0, _value; _key < _length; ++_key) { + _value = _collection[_key]; + _results[_key] = _fn(_value, _key, _collection); } -const result = _result; \ No newline at end of file +const result = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/every/output.js b/__tests__/__fixtures__/cached/every/output.js index d4188205..8ffe17f4 100644 --- a/__tests__/__fixtures__/cached/every/output.js +++ b/__tests__/__fixtures__/cached/every/output.js @@ -1,9 +1,14 @@ -let _result = true; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = true; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (!fn(_value, _key, array)) { - _result = false; + _result = fn(_value, _key, array); + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/everyObject/output.js b/__tests__/__fixtures__/cached/everyObject/output.js index beefda47..c1f04247 100644 --- a/__tests__/__fixtures__/cached/everyObject/output.js +++ b/__tests__/__fixtures__/cached/everyObject/output.js @@ -1,10 +1,12 @@ -let _result = true; -let _value; -for (let _key in object) { +let _determination = true, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (!fn(_value, _key, object)) { - _result = false; + _result = fn(_value, _key, object); + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/everyRight/output.js b/__tests__/__fixtures__/cached/everyRight/output.js index 8a65f0ca..73c990c0 100644 --- a/__tests__/__fixtures__/cached/everyRight/output.js +++ b/__tests__/__fixtures__/cached/everyRight/output.js @@ -1,9 +1,10 @@ -let _result = true; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +let _determination = true; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (!fn(_value, _key, array)) { - _result = false; + _result = fn(_value, _key, array); + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/filter/output.js b/__tests__/__fixtures__/cached/filter/output.js index 5b378426..d71d9d31 100644 --- a/__tests__/__fixtures__/cached/filter/output.js +++ b/__tests__/__fixtures__/cached/filter/output.js @@ -1,6 +1,13 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _results = []; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (fn(_value, _key, array)) _result.push(_value); + _result = fn(_value, _key, array); + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/filterObject/output.js b/__tests__/__fixtures__/cached/filterObject/output.js index 9283c8e0..16b853cf 100644 --- a/__tests__/__fixtures__/cached/filterObject/output.js +++ b/__tests__/__fixtures__/cached/filterObject/output.js @@ -1,7 +1,10 @@ -let _result = {}; -let _value; -for (let _key in object) { +const _results = {}; +let _result, _value; +for (const _key in object) { _value = object[_key]; - if (fn(_value, _key, object)) _result[_key] = _value; + _result = fn(_value, _key, object); + if (_result) { + _results[_key] = _value; + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/filterRight/output.js b/__tests__/__fixtures__/cached/filterRight/output.js index 4f90a449..6b235a7c 100644 --- a/__tests__/__fixtures__/cached/filterRight/output.js +++ b/__tests__/__fixtures__/cached/filterRight/output.js @@ -1,6 +1,10 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _results = []; +let _result, _value; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (fn(_value, _key, array)) _result.push(_value); + _result = fn(_value, _key, array); + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/find/output.js b/__tests__/__fixtures__/cached/find/output.js index ad80dbac..7457d123 100644 --- a/__tests__/__fixtures__/cached/find/output.js +++ b/__tests__/__fixtures__/cached/find/output.js @@ -1,9 +1,14 @@ -let _result; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _match; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (fn(_value, _key, array)) { - _result = _value; + _result = fn(_value, _key, array); + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/findIndex/output.js b/__tests__/__fixtures__/cached/findIndex/output.js index 2ecc7809..87598e06 100644 --- a/__tests__/__fixtures__/cached/findIndex/output.js +++ b/__tests__/__fixtures__/cached/findIndex/output.js @@ -1,9 +1,14 @@ -let _result = -1; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _match = -1; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (fn(_value, _key, array)) { - _result = _key; + _result = fn(_value, _key, array); + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/findIndexRight/output.js b/__tests__/__fixtures__/cached/findIndexRight/output.js index 1caf8853..44764e12 100644 --- a/__tests__/__fixtures__/cached/findIndexRight/output.js +++ b/__tests__/__fixtures__/cached/findIndexRight/output.js @@ -1,9 +1 @@ -let _result = -1; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - if (fn(_value, _key, array)) { - _result = _key; - break; - } -} -const firstEven = _result; \ No newline at end of file +const firstEven = findIndexRight(array, fn); \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/findKey/output.js b/__tests__/__fixtures__/cached/findKey/output.js index 9ac7b658..e5736822 100644 --- a/__tests__/__fixtures__/cached/findKey/output.js +++ b/__tests__/__fixtures__/cached/findKey/output.js @@ -1,10 +1,12 @@ -let _result; -let _value; -for (let _key in object) { +let _match = -1, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (fn(_value, _key, object)) { - _result = _key; + _result = fn(_value, _key, object); + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/findObject/output.js b/__tests__/__fixtures__/cached/findObject/output.js index aef71d4f..b43f1460 100644 --- a/__tests__/__fixtures__/cached/findObject/output.js +++ b/__tests__/__fixtures__/cached/findObject/output.js @@ -1,10 +1,10 @@ -let _result; -let _value; -for (let _key in object) { +let _match, _value, _result; +for (const _key in object) { _value = object[_key]; - if (fn(_value, _key, object)) { - _result = _value; + _result = fn(_value, _key, object); + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/findRight/output.js b/__tests__/__fixtures__/cached/findRight/output.js index ef00542b..e70142c5 100644 --- a/__tests__/__fixtures__/cached/findRight/output.js +++ b/__tests__/__fixtures__/cached/findRight/output.js @@ -1,9 +1 @@ -let _result; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - if (fn(_value, _key, array)) { - _result = _value; - break; - } -} -const lastEven = _result; \ No newline at end of file +const lastEven = findRight(array, fn); \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/flatMap/output.js b/__tests__/__fixtures__/cached/flatMap/output.js index c77b4210..3b9bff2f 100644 --- a/__tests__/__fixtures__/cached/flatMap/output.js +++ b/__tests__/__fixtures__/cached/flatMap/output.js @@ -1,6 +1,11 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _results = []; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - _result.push.apply(_result, fn(_value, _key, array)); + _result = fn(_value, _key, array); + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/flatMapRight/output.js b/__tests__/__fixtures__/cached/flatMapRight/output.js index 1b4172c5..53f746df 100644 --- a/__tests__/__fixtures__/cached/flatMapRight/output.js +++ b/__tests__/__fixtures__/cached/flatMapRight/output.js @@ -1,6 +1,7 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +let _results = []; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - _result.push.apply(_result, fn(_value, _key, array)); + _result = fn(_value, _key, array); + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/forEachObject/output.js b/__tests__/__fixtures__/cached/forEachObject/output.js index cebb56fc..b4e63d88 100644 --- a/__tests__/__fixtures__/cached/forEachObject/output.js +++ b/__tests__/__fixtures__/cached/forEachObject/output.js @@ -1,5 +1,5 @@ let _value; -for (let _key in object) { +for (const _key in object) { _value = object[_key]; fn(_value, _key, object); } \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/forEachRight/output.js b/__tests__/__fixtures__/cached/forEachRight/output.js index 7e1cb7d9..8db68b40 100644 --- a/__tests__/__fixtures__/cached/forEachRight/output.js +++ b/__tests__/__fixtures__/cached/forEachRight/output.js @@ -1,4 +1,4 @@ -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +for (let _key = array.length, _value; --_key >= 0; ) { _value = array[_key]; fn(_value, _key, array); } \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/map/output.js b/__tests__/__fixtures__/cached/map/output.js index a3f84a12..f84c82cf 100644 --- a/__tests__/__fixtures__/cached/map/output.js +++ b/__tests__/__fixtures__/cached/map/output.js @@ -1,6 +1,7 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _length = array.length; +const _results = Array(_length); +for (let _key = 0, _value; _key < _length; ++_key) { _value = array[_key]; - _result[_key] = fn(_value, _key, array); + _results[_key] = fn(_value, _key, array); } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/mapObject/output.js b/__tests__/__fixtures__/cached/mapObject/output.js index c73d980f..0b0bf4ef 100644 --- a/__tests__/__fixtures__/cached/mapObject/output.js +++ b/__tests__/__fixtures__/cached/mapObject/output.js @@ -1,7 +1,7 @@ -let _result = {}; -let _value; -for (let _key in object) { +const _results = {}; +let _value, _result; +for (const _key in object) { _value = object[_key]; - _result[_key] = fn(_value, _key, object); + _results[_key] = fn(_value, _key, object); } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/mapRight/output.js b/__tests__/__fixtures__/cached/mapRight/output.js index 29bb3943..92b0140b 100644 --- a/__tests__/__fixtures__/cached/mapRight/output.js +++ b/__tests__/__fixtures__/cached/mapRight/output.js @@ -1,6 +1,8 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _length = array.length; +let _key = _length; +const _results = Array(_length); +for (let _value; --_key >= 0; ) { _value = array[_key]; - _result[_result.length] = fn(_value, _key, array); + _results[_length - _key - 1] = fn(_value, _key, array); } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/reduce-no-initialValue/output.js b/__tests__/__fixtures__/cached/reduce-no-initialValue/output.js index 034dda7d..e829fbc3 100644 --- a/__tests__/__fixtures__/cached/reduce-no-initialValue/output.js +++ b/__tests__/__fixtures__/cached/reduce-no-initialValue/output.js @@ -1,6 +1,6 @@ -let _result = array[0]; +let _accumulated = array[0]; for (let _key = 1, _length = array.length, _value; _key < _length; ++_key) { _value = array[_key]; - _result = fn(_result, _value, _key, array); + _accumulated = fn(_accumulated, _value, _key, array); } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _accumulated; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/reduce/output.js b/__tests__/__fixtures__/cached/reduce/output.js index f12e7dc5..b38f75db 100644 --- a/__tests__/__fixtures__/cached/reduce/output.js +++ b/__tests__/__fixtures__/cached/reduce/output.js @@ -1,6 +1,6 @@ -let _result = {}; +let _accumulated = {}; for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { _value = array[_key]; - _result = fn(_result, _value, _key, array); + _accumulated = fn(_accumulated, _value, _key, array); } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _accumulated; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/reduceObject-no-initialValue/output.js b/__tests__/__fixtures__/cached/reduceObject-no-initialValue/output.js index e4312e84..f1872f7e 100644 --- a/__tests__/__fixtures__/cached/reduceObject-no-initialValue/output.js +++ b/__tests__/__fixtures__/cached/reduceObject-no-initialValue/output.js @@ -1,13 +1,13 @@ -let _hasInitialValue = false; -let _value; -let _result; -for (let _key in object) { - if (_hasInitialValue) { - _value = object[_key]; - _result = fn(_result, _value, _key, object); - } else { - _hasInitialValue = true; - _result = object[_key]; +let _skip = true, + _accumulated = undefined, + _value; +for (const _key in object) { + _value = object[_key]; + if (_skip) { + _accumulated = _value; + _skip = false; + continue; } + _accumulated = fn(_accumulated, _value, _key, object); } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _accumulated; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/reduceObject/output.js b/__tests__/__fixtures__/cached/reduceObject/output.js index b92b20be..fd725968 100644 --- a/__tests__/__fixtures__/cached/reduceObject/output.js +++ b/__tests__/__fixtures__/cached/reduceObject/output.js @@ -1,7 +1,13 @@ -let _value; -let _result = {}; -for (let _key in object) { +let _skip = false, + _accumulated = {}, + _value; +for (const _key in object) { _value = object[_key]; - _result = fn(_result, _value, _key, object); + if (_skip) { + _accumulated = _value; + _skip = false; + continue; + } + _accumulated = fn(_accumulated, _value, _key, object); } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _accumulated; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/reduceRight-no-initialValue/output.js b/__tests__/__fixtures__/cached/reduceRight-no-initialValue/output.js index 6be1cae5..5164bee7 100644 --- a/__tests__/__fixtures__/cached/reduceRight-no-initialValue/output.js +++ b/__tests__/__fixtures__/cached/reduceRight-no-initialValue/output.js @@ -1,7 +1,6 @@ -const _length = array.length; -let _result = array[_length - 1]; -for (let _key = _length - 2, _value; _key >= 0; --_key) { +let _accumulated = array[array.length - 1]; +for (let _key = array.length - 1, _value; --_key >= 1; ) { _value = array[_key]; - _result = fn(_result, _value, _key, array); + _accumulated = fn(_accumulated, _value, _key, array); } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _accumulated; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/reduceRight/output.js b/__tests__/__fixtures__/cached/reduceRight/output.js index ccb3009d..80bfea07 100644 --- a/__tests__/__fixtures__/cached/reduceRight/output.js +++ b/__tests__/__fixtures__/cached/reduceRight/output.js @@ -1,6 +1,6 @@ -let _result = {}; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +let _accumulated = {}; +for (let _key = array.length - 0, _value; --_key >= 0; ) { _value = array[_key]; - _result = fn(_result, _value, _key, array); + _accumulated = fn(_accumulated, _value, _key, array); } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _accumulated; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/some/output.js b/__tests__/__fixtures__/cached/some/output.js index 1f5eaeeb..5877fb2a 100644 --- a/__tests__/__fixtures__/cached/some/output.js +++ b/__tests__/__fixtures__/cached/some/output.js @@ -1,9 +1,14 @@ -let _result = false; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = false; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (fn(_value, _key, array)) { - _result = true; + _result = fn(_value, _key, array); + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/someObject/output.js b/__tests__/__fixtures__/cached/someObject/output.js index ce1ab4df..3b248880 100644 --- a/__tests__/__fixtures__/cached/someObject/output.js +++ b/__tests__/__fixtures__/cached/someObject/output.js @@ -1,10 +1,12 @@ -let _result = false; -let _value; -for (let _key in object) { +let _determination = false, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (fn(_value, _key, object)) { - _result = true; + _result = fn(_value, _key, object); + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/someRight/output.js b/__tests__/__fixtures__/cached/someRight/output.js index 7b23e341..180d841f 100644 --- a/__tests__/__fixtures__/cached/someRight/output.js +++ b/__tests__/__fixtures__/cached/someRight/output.js @@ -1,9 +1,10 @@ -let _result = false; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _determination = false; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (fn(_value, _key, array)) { - _result = true; + _result = fn(_value, _key, array); + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/conditional/output.js b/__tests__/__fixtures__/complex/conditional/output.js index 6630b8e4..e3b7e4fd 100644 --- a/__tests__/__fixtures__/complex/conditional/output.js +++ b/__tests__/__fixtures__/complex/conditional/output.js @@ -1,11 +1,12 @@ function getStuff() { if (foo === 'bar') { - let _result = []; - for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { - _value = array[_key]; - _result[_key] = _value * 2; + const _length = array.length; + const _results = Array(_length); + for (let _key = 0, _v; _key < _length; ++_key) { + _v = array[_key]; + _results[_key] = _v * 2; } - return _result; + return _results; } return array; } \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/contrived/output.js b/__tests__/__fixtures__/complex/contrived/output.js index f8b8aa2a..012e84b8 100644 --- a/__tests__/__fixtures__/complex/contrived/output.js +++ b/__tests__/__fixtures__/complex/contrived/output.js @@ -1,29 +1,32 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _length = array.length; +const _results = Array(_length); +for (let _key = 0, _value; _key < _length; ++_key) { _value = array[_key]; - _result[_key] = _value * 2; + _results[_key] = _value * 2; } -let _result2 = {}; +let _object = {}; for ( - let _key2 = 0, _length2 = _result.length, _value2; + let _key2 = 0, _length2 = _results.length, _value2; _key2 < _length2; ++_key2 ) { - _value2 = _result[_key2]; - _result2 = { - ..._result2, + _value2 = _results[_key2]; + _object = { + ..._object, [_value2]: _value2, }; } -let _result3 = false; -let _value3; -for (let _key3 in _result2) { - _value3 = _result2[_key3]; - if (_value3 > 100) { - _result3 = true; +let _determination = false, + _value3, + _result2; +for (const _key3 in _object) { + _value3 = _object[_key3]; + _result2 = _value3 > 100; + if (_result2) { + _determination = true; break; } } -if (_result3) { +if (_determination) { console.log('I am large!'); } \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/deep-every/output.js b/__tests__/__fixtures__/complex/deep-every/output.js index 26804467..730ab1e4 100644 --- a/__tests__/__fixtures__/complex/deep-every/output.js +++ b/__tests__/__fixtures__/complex/deep-every/output.js @@ -1,23 +1,29 @@ -let _result = true; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = true; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (!(_value > 0)) { - _result = false; + _result = _value > 0; + if (!_result) { + _determination = false; break; } } -let _result2 = true; +let _determination2 = true; for ( - let _key2 = 0, _length2 = array.length, _value2; + let _key2 = 0, _length2 = array.length, _value2, _result2; _key2 < _length2; ++_key2 ) { _value2 = array[_key2]; - if (!(_value2 === ~~_value2)) { - _result2 = false; + _result2 = _value2 === ~~_value2; + if (!_result2) { + _determination2 = false; break; } } -if (`${_result}|${_result2}` === 'true|true') { +if (`${_determination}|${_determination2}` === 'true|true') { console.log('I am positive and an integer!'); } \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/deep-iterable/output.js b/__tests__/__fixtures__/complex/deep-iterable/output.js index 93ea5df2..0a6cfe61 100644 --- a/__tests__/__fixtures__/complex/deep-iterable/output.js +++ b/__tests__/__fixtures__/complex/deep-iterable/output.js @@ -1,13 +1,14 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _length = array.length; +const _results = Array(_length); +for (let _key = 0, _value; _key < _length; ++_key) { _value = array[_key]; - _result[_key] = cachedFn(_value, _key, array); + _results[_key] = cachedFn(_value, _key, array); } for ( - let _key2 = 0, _length2 = _result.length, _value2; + let _key2 = 0, _length2 = _results.length, _value2; _key2 < _length2; ++_key2 ) { - _value2 = _result[_key2]; - otherFn(_value2, _key2, _result); + _value2 = _results[_key2]; + otherFn(_value2, _key2, _results); } \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/deep-map/output.js b/__tests__/__fixtures__/complex/deep-map/output.js index 3681b96d..3df1255b 100644 --- a/__tests__/__fixtures__/complex/deep-map/output.js +++ b/__tests__/__fixtures__/complex/deep-map/output.js @@ -1,8 +1,8 @@ import { deepEqual } from 'fast-equals'; -let _result = {}; -let _value; -for (let _key in object) { +const _results = {}; +let _value, _result; +for (const _key in object) { _value = object[_key]; - _result[_key] = cachedFn(_value, _key, object); + _results[_key] = cachedFn(_value, _key, object); } -const isEqual = deepEqual(Object.values(_result), [1, 2, 3]); \ No newline at end of file +const isEqual = deepEqual(Object.values(_results), [1, 2, 3]); \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/destructured-params-array/output.js b/__tests__/__fixtures__/complex/destructured-params-array/output.js index f9682012..1cc43fd7 100644 --- a/__tests__/__fixtures__/complex/destructured-params-array/output.js +++ b/__tests__/__fixtures__/complex/destructured-params-array/output.js @@ -1,6 +1,10 @@ -const _iterable = []; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; - const [_a, [_b]] = _value; +const _collection = []; +for ( + let _key = 0, _length = _collection.length, _destructured; + _key < _length; + ++_key +) { + _destructured = _collection[_key]; + const [_a, [_b]] = _destructured; console.log(_a, _b); } \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/destructured-params-object/output.js b/__tests__/__fixtures__/complex/destructured-params-object/output.js index 18ca7608..e3a4a590 100644 --- a/__tests__/__fixtures__/complex/destructured-params-object/output.js +++ b/__tests__/__fixtures__/complex/destructured-params-object/output.js @@ -1,9 +1,13 @@ -const _iterable = []; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; +const _collection = []; +for ( + let _key = 0, _length = _collection.length, _destructured; + _key < _length; + ++_key +) { + _destructured = _collection[_key]; const { a: _a, b: { c: _c }, - } = _value; + } = _destructured; console.log(_a, _c); } \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/inlined-large-function/output.js b/__tests__/__fixtures__/complex/inlined-large-function/output.js index ccaa9026..8f1fb3b2 100644 --- a/__tests__/__fixtures__/complex/inlined-large-function/output.js +++ b/__tests__/__fixtures__/complex/inlined-large-function/output.js @@ -1,21 +1,22 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { - _value = array[_key]; - let _result2 = []; - for ( - let _key3 = 0, _length2 = array.length, _value2; - _key3 < _length2; - ++_key3 - ) { - _value2 = array[_key3]; - _result2[_key3] = _value2 * 2; +const _results = []; +for ( + let _index = 0, _length = array.length, _value, _result; + _index < _length; + ++_index +) { + _value = array[_index]; + const _length2 = array.length; + const _results2 = Array(_length2); + for (let _key2 = 0, _value2; _key2 < _length2; ++_key2) { + _value2 = array[_key2]; + _results2[_key2] = _value2 * 2; } // usage inside - const _mapped = _result2; + const _mapped = _results2; // custom for loop with let - for (let i = 0; i < _mapped.length; i++) { - _mapped[i] = _mapped[i] ** 2; + for (let _i = 0; _i < _mapped.length; _i++) { + _mapped[_i] = _mapped[_i] ** 2; } // custom for loop with var @@ -24,28 +25,31 @@ for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { } // another iteration, using the mapped values - let _hasInitialValue = false; - let _value3; - let _result3; - for (let _key4 in object) { - if (_hasInitialValue) { - _value3 = object[_key4]; - _result3 = { - [_result3]: _mapped, - }; - } else { - _hasInitialValue = true; - _result3 = object[_key4]; + let _skip = true, + _value3 = undefined, + _value4; + for (const _key3 in object) { + _value4 = object[_key3]; + if (_skip) { + _value3 = _value4; + _skip = false; + continue; } + _value3 = { + [_value3]: _mapped, + }; } - const _reduced = _result3; + const _reduced = _value3; // custom for-in - for (var _key2 in _reduced) { - if (_reduced[_key2] < 0) { - delete _reduced[_key2]; + for (var _key in _reduced) { + if (_reduced[_key] < 0) { + delete _reduced[_key]; } } - if (_reduced[100]) _result.push(_value); + _result = _reduced[100]; + if (_result) { + _results.push(_value); + } } -const result = _result; \ No newline at end of file +const result = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/jsx/output.js b/__tests__/__fixtures__/complex/jsx/output.js index 913f7847..31170b05 100644 --- a/__tests__/__fixtures__/complex/jsx/output.js +++ b/__tests__/__fixtures__/complex/jsx/output.js @@ -1,20 +1,17 @@ import React from 'react'; function List(props) { - const _iterable = props.items; - let _result = []; - for ( - let _key = 0, _length = _iterable.length, _value; - _key < _length; - ++_key - ) { - _value = _iterable[_key]; - _result[_key] = /*#__PURE__*/ React.createElement( + 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: _value.id, + key: _item.id, }, - _value.value, + _item.value, ); } - return /*#__PURE__*/ React.createElement('ul', null, _result); + return /*#__PURE__*/ React.createElement('ul', null, _results); } \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/shallow-every/output.js b/__tests__/__fixtures__/complex/shallow-every/output.js index d5f78871..405030e0 100644 --- a/__tests__/__fixtures__/complex/shallow-every/output.js +++ b/__tests__/__fixtures__/complex/shallow-every/output.js @@ -1,11 +1,16 @@ -let _result = true; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = true; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (!(_value > 0)) { - _result = false; + _result = _value > 0; + if (!_result) { + _determination = false; break; } } -if (_result) { +if (_determination) { console.log('I am positive!'); } \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/shallow-iterable/output.js b/__tests__/__fixtures__/complex/shallow-iterable/output.js index 5a3620dc..70b5bdcf 100644 --- a/__tests__/__fixtures__/complex/shallow-iterable/output.js +++ b/__tests__/__fixtures__/complex/shallow-iterable/output.js @@ -1,18 +1,16 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { - _value = array[_key]; - let _result2 = true; - for ( - let _key2 = 0, _length2 = _value.length, _value2; - _key2 < _length2; - ++_key2 - ) { - _value2 = _value[_key2]; - if (!(typeof _value2 === 'string')) { - _result2 = false; - break; - } +let _determination = true; +for (let _key = 0, _length = item.length, _v, _result; _key < _length; ++_key) { + _v = item[_key]; + _result = typeof _v === 'string'; + if (!_result) { + _determination = false; + break; } - _result[_key] = _result2; } -const allStrings = _result; \ No newline at end of file +const _length2 = array.length; +const _results2 = Array(_length2); +for (let _key2 = 0, _item; _key2 < _length2; ++_key2) { + _item = array[_key2]; + _results2[_key2] = _determination; +} +const allStrings = _results2; \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/shallow-map/output.js b/__tests__/__fixtures__/complex/shallow-map/output.js index db48dcbd..cbbd8c57 100644 --- a/__tests__/__fixtures__/complex/shallow-map/output.js +++ b/__tests__/__fixtures__/complex/shallow-map/output.js @@ -1,7 +1,8 @@ import { deepEqual } from 'fast-equals'; -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _length = array.length; +const _results = Array(_length); +for (let _key = 0, _value; _key < _length; ++_key) { _value = array[_key]; - _result[_key] = cachedFn(_value, _key, array); + _results[_key] = cachedFn(_value, _key, array); } -const isEqual = deepEqual(_result, [1, 2, 3]); \ No newline at end of file +const isEqual = deepEqual(_results, [1, 2, 3]); \ No newline at end of file diff --git a/__tests__/__fixtures__/complex/this/output.js b/__tests__/__fixtures__/complex/this/output.js index 7b9b95bb..718b074b 100644 --- a/__tests__/__fixtures__/complex/this/output.js +++ b/__tests__/__fixtures__/complex/this/output.js @@ -1,8 +1,12 @@ function foo(array) { - let _result = []; - for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { + const _fn = function (_value) { + return this && this.foo ? _value : null; + }; + const _length = array.length; + const _results = Array(_length); + for (let _key = 0, _value; _key < _length; ++_key) { _value = array[_key]; - _result[_key] = this && this.foo ? _value : null; + _results[_key] = _fn(_value, _key, array); } - return _result; + return _results; } \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/every/output.js b/__tests__/__fixtures__/inlined-arrow-expression/every/output.js index 3f137e1c..d05c1056 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/every/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/every/output.js @@ -1,9 +1,14 @@ -let _result = true; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = true; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (!(_value % 2 === 0)) { - _result = false; + _result = _value % 2 === 0; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/everyObject/output.js b/__tests__/__fixtures__/inlined-arrow-expression/everyObject/output.js index 5d0bb7e9..bc70f8e5 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/everyObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/everyObject/output.js @@ -1,10 +1,12 @@ -let _result = true; -let _value; -for (let _key in object) { +let _determination = true, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (!(_value % 2 === 0)) { - _result = false; + _result = _value % 2 === 0; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/everyRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/everyRight/output.js index f02aa830..f186d920 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/everyRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/everyRight/output.js @@ -1,9 +1,10 @@ -let _result = true; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +let _determination = true; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (!(_value % 2 === 0)) { - _result = false; + _result = _value % 2 === 0; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/filter/output.js b/__tests__/__fixtures__/inlined-arrow-expression/filter/output.js index e486a1ac..c8006f9f 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/filter/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/filter/output.js @@ -1,6 +1,13 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _results = []; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) _result.push(_value); + _result = _value % 2 === 0; + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/filterObject/output.js b/__tests__/__fixtures__/inlined-arrow-expression/filterObject/output.js index 05dbf356..99223721 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/filterObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/filterObject/output.js @@ -1,7 +1,10 @@ -let _result = {}; -let _value; -for (let _key in object) { +const _results = {}; +let _result, _value; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) _result[_key] = _value; + _result = _value % 2 === 0; + if (_result) { + _results[_key] = _value; + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/filterRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/filterRight/output.js index cb0da829..a0ed6540 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/filterRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/filterRight/output.js @@ -1,6 +1,10 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _results = []; +let _result, _value; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (_value % 2 === 0) _result.push(_value); + _result = _value % 2 === 0; + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/find/output.js b/__tests__/__fixtures__/inlined-arrow-expression/find/output.js index cb2ece41..418ca1d4 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/find/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/find/output.js @@ -1,9 +1,14 @@ -let _result; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _match; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) { - _result = _value; + _result = _value % 2 === 0; + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findIndex/output.js b/__tests__/__fixtures__/inlined-arrow-expression/findIndex/output.js index a7211f62..3a5c5c0f 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findIndex/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findIndex/output.js @@ -1,9 +1,14 @@ -let _result = -1; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _match = -1; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) { - _result = _key; + _result = _value % 2 === 0; + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findIndexRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/findIndexRight/output.js index d9da8495..0f8cac96 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findIndexRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findIndexRight/output.js @@ -1,9 +1 @@ -let _result = -1; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - if (_value % 2 === 0) { - _result = _key; - break; - } -} -const firstEven = _result; \ No newline at end of file +const firstEven = findIndexRight(array, (value) => value % 2 === 0); \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findKey/output.js b/__tests__/__fixtures__/inlined-arrow-expression/findKey/output.js index 1b26ad33..e7777457 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findKey/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findKey/output.js @@ -1,10 +1,12 @@ -let _result; -let _value; -for (let _key in object) { +let _match = -1, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) { - _result = _key; + _result = _value % 2 === 0; + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findLastIndex/code.js b/__tests__/__fixtures__/inlined-arrow-expression/findLastIndex/code.js new file mode 100644 index 00000000..78a12627 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/findLastIndex/code.js @@ -0,0 +1,3 @@ +import { findLastIndex } from '../../../../src/inline-loops.macro.js'; + +const firstEven = findLastIndex(array, (value) => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findLastIndex/output.js b/__tests__/__fixtures__/inlined-arrow-expression/findLastIndex/output.js new file mode 100644 index 00000000..2dd89585 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/findLastIndex/output.js @@ -0,0 +1,10 @@ +let _match = -1; +for (let _key = array.length, _value, _result; --_key >= 0; ) { + _value = array[_key]; + _result = _value % 2 === 0; + if (_result) { + _match = _key; + break; + } +} +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findObject/output.js b/__tests__/__fixtures__/inlined-arrow-expression/findObject/output.js index 473fe0ab..7406b4fa 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findObject/output.js @@ -1,10 +1,10 @@ -let _result; -let _value; -for (let _key in object) { +let _match, _value, _result; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) { - _result = _value; + _result = _value % 2 === 0; + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/findRight/output.js index b44deb60..77c4ceee 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findRight/output.js @@ -1,9 +1 @@ -let _result; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - if (_value % 2 === 0) { - _result = _value; - break; - } -} -const lastEven = _result; \ No newline at end of file +const lastEven = findRight(array, (value) => value % 2 === 0); \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/flatMap/output.js b/__tests__/__fixtures__/inlined-arrow-expression/flatMap/output.js index a2ea60d0..38174114 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/flatMap/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/flatMap/output.js @@ -1,6 +1,11 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { - _value = array[_key]; - _result.push.apply(_result, [_value[0]]); +let _results = []; +for ( + let _key = 0, _length = array.length, _entry, _result; + _key < _length; + ++_key +) { + _entry = array[_key]; + _result = [_entry[0]]; + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/output.js index 7b9e11d6..4b23b98e 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/output.js @@ -1,6 +1,7 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - _result.push.apply(_result, [_value[0]]); +let _results = []; +for (let _key = array.length, _entry, _result; --_key >= 0; ) { + _entry = array[_key]; + _result = [_entry[0]]; + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/forEachObject/output.js b/__tests__/__fixtures__/inlined-arrow-expression/forEachObject/output.js index 72704f47..05c2e187 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/forEachObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/forEachObject/output.js @@ -1,5 +1,5 @@ let _value; -for (let _key in object) { +for (const _key in object) { _value = object[_key]; console.log(_value); } \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/forEachRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/forEachRight/output.js index 931750e8..76b9ff77 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/forEachRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/forEachRight/output.js @@ -1,4 +1,4 @@ -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +for (let _key = array.length, _value; --_key >= 0; ) { _value = array[_key]; console.log(_value); } \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/map/output.js b/__tests__/__fixtures__/inlined-arrow-expression/map/output.js index 8a65aae2..b2e8d08e 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/map/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/map/output.js @@ -1,6 +1,7 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _length = array.length; +const _results = Array(_length); +for (let _key = 0, _value; _key < _length; ++_key) { _value = array[_key]; - _result[_key] = _value * 2; + _results[_key] = _value * 2; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/mapObject/output.js b/__tests__/__fixtures__/inlined-arrow-expression/mapObject/output.js index 505e8250..c0c20009 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/mapObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/mapObject/output.js @@ -1,7 +1,7 @@ -let _result = {}; -let _value; -for (let _key in object) { +const _results = {}; +let _value, _result; +for (const _key in object) { _value = object[_key]; - _result[_key] = _value * 2; + _results[_key] = _value * 2; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/mapRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/mapRight/output.js index 53710bee..3ef63e39 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/mapRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/mapRight/output.js @@ -1,6 +1,8 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _length = array.length; +let _key = _length; +const _results = Array(_length); +for (let _value; --_key >= 0; ) { _value = array[_key]; - _result[_result.length] = _value * 2; + _results[_length - _key - 1] = _value * 2; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/output.js index adf6e5a4..90603766 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/output.js @@ -1,6 +1,6 @@ -let _result = array[0]; +let _total = array[0]; for (let _key = 1, _length = array.length, _value; _key < _length; ++_key) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduce/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduce/output.js index 97d5979a..d51cb708 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/reduce/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduce/output.js @@ -1,6 +1,6 @@ -let _result = 0; +let _total = 0; for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/output.js index adfbb94f..21bf4dc2 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/output.js @@ -1,13 +1,13 @@ -let _hasInitialValue = false; -let _value; -let _result; -for (let _key in object) { - if (_hasInitialValue) { - _value = object[_key]; - _result = _result + _value; - } else { - _hasInitialValue = true; - _result = object[_key]; +let _skip = true, + _total = undefined, + _value; +for (const _key in object) { + _value = object[_key]; + if (_skip) { + _total = _value; + _skip = false; + continue; } + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/output.js index b50926df..1d59596e 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/output.js @@ -1,7 +1,13 @@ -let _value; -let _result = 0; -for (let _key in object) { +let _skip = false, + _total = 0, + _value; +for (const _key in object) { _value = object[_key]; - _result = _result + _value; + if (_skip) { + _total = _value; + _skip = false; + continue; + } + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/output.js index 1e97c96a..9176dc10 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/output.js @@ -1,7 +1,6 @@ -const _length = array.length; -let _result = array[_length - 1]; -for (let _key = _length - 2, _value; _key >= 0; --_key) { +let _total = array[array.length - 1]; +for (let _key = array.length - 1, _value; --_key >= 1; ) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/output.js index 2f4bfcb8..102fd74c 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/output.js @@ -1,6 +1,6 @@ -let _result = 0; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +let _total = 0; +for (let _key = array.length - 0, _value; --_key >= 0; ) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/some/output.js b/__tests__/__fixtures__/inlined-arrow-expression/some/output.js index 7d4c4c43..53ffa86d 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/some/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/some/output.js @@ -1,9 +1,14 @@ -let _result = false; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = false; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) { - _result = true; + _result = _value % 2 === 0; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/someObject/output.js b/__tests__/__fixtures__/inlined-arrow-expression/someObject/output.js index 07e67047..9595b4c6 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/someObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/someObject/output.js @@ -1,10 +1,12 @@ -let _result = false; -let _value; -for (let _key in object) { +let _determination = false, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) { - _result = true; + _result = _value % 2 === 0; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-expression/someRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/someRight/output.js index 5d18e4d6..8063a7dd 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/someRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/someRight/output.js @@ -1,9 +1,10 @@ -let _result = false; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _determination = false; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (_value % 2 === 0) { - _result = true; + _result = _value % 2 === 0; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/every/output.js b/__tests__/__fixtures__/inlined-arrow-return/every/output.js index 3f137e1c..d05c1056 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/every/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/every/output.js @@ -1,9 +1,14 @@ -let _result = true; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = true; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (!(_value % 2 === 0)) { - _result = false; + _result = _value % 2 === 0; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/everyObject/output.js b/__tests__/__fixtures__/inlined-arrow-return/everyObject/output.js index 5d0bb7e9..bc70f8e5 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/everyObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/everyObject/output.js @@ -1,10 +1,12 @@ -let _result = true; -let _value; -for (let _key in object) { +let _determination = true, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (!(_value % 2 === 0)) { - _result = false; + _result = _value % 2 === 0; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/everyRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/everyRight/output.js index f02aa830..f186d920 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/everyRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/everyRight/output.js @@ -1,9 +1,10 @@ -let _result = true; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +let _determination = true; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (!(_value % 2 === 0)) { - _result = false; + _result = _value % 2 === 0; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/filter/output.js b/__tests__/__fixtures__/inlined-arrow-return/filter/output.js index e486a1ac..c8006f9f 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/filter/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/filter/output.js @@ -1,6 +1,13 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _results = []; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) _result.push(_value); + _result = _value % 2 === 0; + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/filterObject/output.js b/__tests__/__fixtures__/inlined-arrow-return/filterObject/output.js index 05dbf356..99223721 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/filterObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/filterObject/output.js @@ -1,7 +1,10 @@ -let _result = {}; -let _value; -for (let _key in object) { +const _results = {}; +let _result, _value; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) _result[_key] = _value; + _result = _value % 2 === 0; + if (_result) { + _results[_key] = _value; + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/filterRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/filterRight/output.js index cb0da829..a0ed6540 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/filterRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/filterRight/output.js @@ -1,6 +1,10 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _results = []; +let _result, _value; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (_value % 2 === 0) _result.push(_value); + _result = _value % 2 === 0; + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/find/output.js b/__tests__/__fixtures__/inlined-arrow-return/find/output.js index cb2ece41..418ca1d4 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/find/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/find/output.js @@ -1,9 +1,14 @@ -let _result; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _match; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) { - _result = _value; + _result = _value % 2 === 0; + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/findIndex/output.js b/__tests__/__fixtures__/inlined-arrow-return/findIndex/output.js index a7211f62..3a5c5c0f 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findIndex/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findIndex/output.js @@ -1,9 +1,14 @@ -let _result = -1; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _match = -1; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) { - _result = _key; + _result = _value % 2 === 0; + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/findIndexRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/findIndexRight/output.js index d9da8495..f6b95164 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findIndexRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findIndexRight/output.js @@ -1,9 +1,3 @@ -let _result = -1; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - if (_value % 2 === 0) { - _result = _key; - break; - } -} -const firstEven = _result; \ No newline at end of file +const firstEven = findIndexRight(array, (value) => { + return value % 2 === 0; +}); \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/findKey/output.js b/__tests__/__fixtures__/inlined-arrow-return/findKey/output.js index 1b26ad33..e7777457 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findKey/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findKey/output.js @@ -1,10 +1,12 @@ -let _result; -let _value; -for (let _key in object) { +let _match = -1, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) { - _result = _key; + _result = _value % 2 === 0; + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/findObject/output.js b/__tests__/__fixtures__/inlined-arrow-return/findObject/output.js index 473fe0ab..7406b4fa 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findObject/output.js @@ -1,10 +1,10 @@ -let _result; -let _value; -for (let _key in object) { +let _match, _value, _result; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) { - _result = _value; + _result = _value % 2 === 0; + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/findRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/findRight/output.js index b44deb60..cb5a4d83 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findRight/output.js @@ -1,9 +1,3 @@ -let _result; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - if (_value % 2 === 0) { - _result = _value; - break; - } -} -const lastEven = _result; \ No newline at end of file +const lastEven = findRight(array, (value) => { + return value % 2 === 0; +}); \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/flatMap/output.js b/__tests__/__fixtures__/inlined-arrow-return/flatMap/output.js index a2ea60d0..38174114 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/flatMap/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/flatMap/output.js @@ -1,6 +1,11 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { - _value = array[_key]; - _result.push.apply(_result, [_value[0]]); +let _results = []; +for ( + let _key = 0, _length = array.length, _entry, _result; + _key < _length; + ++_key +) { + _entry = array[_key]; + _result = [_entry[0]]; + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/output.js index 7b9e11d6..4b23b98e 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/output.js @@ -1,6 +1,7 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - _result.push.apply(_result, [_value[0]]); +let _results = []; +for (let _key = array.length, _entry, _result; --_key >= 0; ) { + _entry = array[_key]; + _result = [_entry[0]]; + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/forEachObject/output.js b/__tests__/__fixtures__/inlined-arrow-return/forEachObject/output.js index 72704f47..05c2e187 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/forEachObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/forEachObject/output.js @@ -1,5 +1,5 @@ let _value; -for (let _key in object) { +for (const _key in object) { _value = object[_key]; console.log(_value); } \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/forEachRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/forEachRight/output.js index 931750e8..76b9ff77 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/forEachRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/forEachRight/output.js @@ -1,4 +1,4 @@ -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +for (let _key = array.length, _value; --_key >= 0; ) { _value = array[_key]; console.log(_value); } \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/map/output.js b/__tests__/__fixtures__/inlined-arrow-return/map/output.js index 8a65aae2..b2e8d08e 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/map/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/map/output.js @@ -1,6 +1,7 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _length = array.length; +const _results = Array(_length); +for (let _key = 0, _value; _key < _length; ++_key) { _value = array[_key]; - _result[_key] = _value * 2; + _results[_key] = _value * 2; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/mapObject/output.js b/__tests__/__fixtures__/inlined-arrow-return/mapObject/output.js index 505e8250..c0c20009 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/mapObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/mapObject/output.js @@ -1,7 +1,7 @@ -let _result = {}; -let _value; -for (let _key in object) { +const _results = {}; +let _value, _result; +for (const _key in object) { _value = object[_key]; - _result[_key] = _value * 2; + _results[_key] = _value * 2; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/mapRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/mapRight/output.js index 53710bee..3ef63e39 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/mapRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/mapRight/output.js @@ -1,6 +1,8 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _length = array.length; +let _key = _length; +const _results = Array(_length); +for (let _value; --_key >= 0; ) { _value = array[_key]; - _result[_result.length] = _value * 2; + _results[_length - _key - 1] = _value * 2; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/output.js index adf6e5a4..90603766 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/output.js @@ -1,6 +1,6 @@ -let _result = array[0]; +let _total = array[0]; for (let _key = 1, _length = array.length, _value; _key < _length; ++_key) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduce/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduce/output.js index 97d5979a..d51cb708 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/reduce/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/reduce/output.js @@ -1,6 +1,6 @@ -let _result = 0; +let _total = 0; for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/output.js index adfbb94f..21bf4dc2 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/output.js @@ -1,13 +1,13 @@ -let _hasInitialValue = false; -let _value; -let _result; -for (let _key in object) { - if (_hasInitialValue) { - _value = object[_key]; - _result = _result + _value; - } else { - _hasInitialValue = true; - _result = object[_key]; +let _skip = true, + _total = undefined, + _value; +for (const _key in object) { + _value = object[_key]; + if (_skip) { + _total = _value; + _skip = false; + continue; } + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceObject/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduceObject/output.js index b50926df..1d59596e 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/reduceObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceObject/output.js @@ -1,7 +1,13 @@ -let _value; -let _result = 0; -for (let _key in object) { +let _skip = false, + _total = 0, + _value; +for (const _key in object) { _value = object[_key]; - _result = _result + _value; + if (_skip) { + _total = _value; + _skip = false; + continue; + } + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/output.js index 1e97c96a..9176dc10 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/output.js @@ -1,7 +1,6 @@ -const _length = array.length; -let _result = array[_length - 1]; -for (let _key = _length - 2, _value; _key >= 0; --_key) { +let _total = array[array.length - 1]; +for (let _key = array.length - 1, _value; --_key >= 1; ) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduceRight/output.js index 2f4bfcb8..102fd74c 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/reduceRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceRight/output.js @@ -1,6 +1,6 @@ -let _result = 0; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +let _total = 0; +for (let _key = array.length - 0, _value; --_key >= 0; ) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/some/output.js b/__tests__/__fixtures__/inlined-arrow-return/some/output.js index 7d4c4c43..53ffa86d 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/some/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/some/output.js @@ -1,9 +1,14 @@ -let _result = false; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = false; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) { - _result = true; + _result = _value % 2 === 0; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/someObject/output.js b/__tests__/__fixtures__/inlined-arrow-return/someObject/output.js index 07e67047..9595b4c6 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/someObject/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/someObject/output.js @@ -1,10 +1,12 @@ -let _result = false; -let _value; -for (let _key in object) { +let _determination = false, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) { - _result = true; + _result = _value % 2 === 0; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-arrow-return/someRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/someRight/output.js index 5d18e4d6..8063a7dd 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/someRight/output.js +++ b/__tests__/__fixtures__/inlined-arrow-return/someRight/output.js @@ -1,9 +1,10 @@ -let _result = false; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _determination = false; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (_value % 2 === 0) { - _result = true; + _result = _value % 2 === 0; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/every/output.js b/__tests__/__fixtures__/inlined-function-return/every/output.js index 3f137e1c..d05c1056 100644 --- a/__tests__/__fixtures__/inlined-function-return/every/output.js +++ b/__tests__/__fixtures__/inlined-function-return/every/output.js @@ -1,9 +1,14 @@ -let _result = true; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = true; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (!(_value % 2 === 0)) { - _result = false; + _result = _value % 2 === 0; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/everyObject/output.js b/__tests__/__fixtures__/inlined-function-return/everyObject/output.js index 5d0bb7e9..bc70f8e5 100644 --- a/__tests__/__fixtures__/inlined-function-return/everyObject/output.js +++ b/__tests__/__fixtures__/inlined-function-return/everyObject/output.js @@ -1,10 +1,12 @@ -let _result = true; -let _value; -for (let _key in object) { +let _determination = true, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (!(_value % 2 === 0)) { - _result = false; + _result = _value % 2 === 0; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/everyRight/output.js b/__tests__/__fixtures__/inlined-function-return/everyRight/output.js index f02aa830..f186d920 100644 --- a/__tests__/__fixtures__/inlined-function-return/everyRight/output.js +++ b/__tests__/__fixtures__/inlined-function-return/everyRight/output.js @@ -1,9 +1,10 @@ -let _result = true; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +let _determination = true; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (!(_value % 2 === 0)) { - _result = false; + _result = _value % 2 === 0; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/filter/output.js b/__tests__/__fixtures__/inlined-function-return/filter/output.js index e486a1ac..c8006f9f 100644 --- a/__tests__/__fixtures__/inlined-function-return/filter/output.js +++ b/__tests__/__fixtures__/inlined-function-return/filter/output.js @@ -1,6 +1,13 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _results = []; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) _result.push(_value); + _result = _value % 2 === 0; + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/filterObject/output.js b/__tests__/__fixtures__/inlined-function-return/filterObject/output.js index 05dbf356..99223721 100644 --- a/__tests__/__fixtures__/inlined-function-return/filterObject/output.js +++ b/__tests__/__fixtures__/inlined-function-return/filterObject/output.js @@ -1,7 +1,10 @@ -let _result = {}; -let _value; -for (let _key in object) { +const _results = {}; +let _result, _value; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) _result[_key] = _value; + _result = _value % 2 === 0; + if (_result) { + _results[_key] = _value; + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/filterRight/output.js b/__tests__/__fixtures__/inlined-function-return/filterRight/output.js index cb0da829..a0ed6540 100644 --- a/__tests__/__fixtures__/inlined-function-return/filterRight/output.js +++ b/__tests__/__fixtures__/inlined-function-return/filterRight/output.js @@ -1,6 +1,10 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _results = []; +let _result, _value; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (_value % 2 === 0) _result.push(_value); + _result = _value % 2 === 0; + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/find/output.js b/__tests__/__fixtures__/inlined-function-return/find/output.js index cb2ece41..418ca1d4 100644 --- a/__tests__/__fixtures__/inlined-function-return/find/output.js +++ b/__tests__/__fixtures__/inlined-function-return/find/output.js @@ -1,9 +1,14 @@ -let _result; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _match; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) { - _result = _value; + _result = _value % 2 === 0; + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/findIndex/output.js b/__tests__/__fixtures__/inlined-function-return/findIndex/output.js index a7211f62..3a5c5c0f 100644 --- a/__tests__/__fixtures__/inlined-function-return/findIndex/output.js +++ b/__tests__/__fixtures__/inlined-function-return/findIndex/output.js @@ -1,9 +1,14 @@ -let _result = -1; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _match = -1; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) { - _result = _key; + _result = _value % 2 === 0; + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/findIndexRight/output.js b/__tests__/__fixtures__/inlined-function-return/findIndexRight/output.js index d9da8495..5979d369 100644 --- a/__tests__/__fixtures__/inlined-function-return/findIndexRight/output.js +++ b/__tests__/__fixtures__/inlined-function-return/findIndexRight/output.js @@ -1,9 +1,3 @@ -let _result = -1; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - if (_value % 2 === 0) { - _result = _key; - break; - } -} -const firstEven = _result; \ No newline at end of file +const firstEven = findIndexRight(array, function (value) { + return value % 2 === 0; +}); \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/findKey/output.js b/__tests__/__fixtures__/inlined-function-return/findKey/output.js index 1b26ad33..e7777457 100644 --- a/__tests__/__fixtures__/inlined-function-return/findKey/output.js +++ b/__tests__/__fixtures__/inlined-function-return/findKey/output.js @@ -1,10 +1,12 @@ -let _result; -let _value; -for (let _key in object) { +let _match = -1, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) { - _result = _key; + _result = _value % 2 === 0; + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/findObject/output.js b/__tests__/__fixtures__/inlined-function-return/findObject/output.js index 473fe0ab..7406b4fa 100644 --- a/__tests__/__fixtures__/inlined-function-return/findObject/output.js +++ b/__tests__/__fixtures__/inlined-function-return/findObject/output.js @@ -1,10 +1,10 @@ -let _result; -let _value; -for (let _key in object) { +let _match, _value, _result; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) { - _result = _value; + _result = _value % 2 === 0; + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/findRight/output.js b/__tests__/__fixtures__/inlined-function-return/findRight/output.js index b44deb60..282d457c 100644 --- a/__tests__/__fixtures__/inlined-function-return/findRight/output.js +++ b/__tests__/__fixtures__/inlined-function-return/findRight/output.js @@ -1,9 +1,3 @@ -let _result; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - if (_value % 2 === 0) { - _result = _value; - break; - } -} -const lastEven = _result; \ No newline at end of file +const lastEven = findRight(array, function (value) { + return value % 2 === 0; +}); \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/flatMap/output.js b/__tests__/__fixtures__/inlined-function-return/flatMap/output.js index a2ea60d0..38174114 100644 --- a/__tests__/__fixtures__/inlined-function-return/flatMap/output.js +++ b/__tests__/__fixtures__/inlined-function-return/flatMap/output.js @@ -1,6 +1,11 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { - _value = array[_key]; - _result.push.apply(_result, [_value[0]]); +let _results = []; +for ( + let _key = 0, _length = array.length, _entry, _result; + _key < _length; + ++_key +) { + _entry = array[_key]; + _result = [_entry[0]]; + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/flatMapRight/output.js b/__tests__/__fixtures__/inlined-function-return/flatMapRight/output.js index 7b9e11d6..4b23b98e 100644 --- a/__tests__/__fixtures__/inlined-function-return/flatMapRight/output.js +++ b/__tests__/__fixtures__/inlined-function-return/flatMapRight/output.js @@ -1,6 +1,7 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { - _value = array[_key]; - _result.push.apply(_result, [_value[0]]); +let _results = []; +for (let _key = array.length, _entry, _result; --_key >= 0; ) { + _entry = array[_key]; + _result = [_entry[0]]; + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/forEachObject/output.js b/__tests__/__fixtures__/inlined-function-return/forEachObject/output.js index 72704f47..05c2e187 100644 --- a/__tests__/__fixtures__/inlined-function-return/forEachObject/output.js +++ b/__tests__/__fixtures__/inlined-function-return/forEachObject/output.js @@ -1,5 +1,5 @@ let _value; -for (let _key in object) { +for (const _key in object) { _value = object[_key]; console.log(_value); } \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/forEachRight/output.js b/__tests__/__fixtures__/inlined-function-return/forEachRight/output.js index 931750e8..76b9ff77 100644 --- a/__tests__/__fixtures__/inlined-function-return/forEachRight/output.js +++ b/__tests__/__fixtures__/inlined-function-return/forEachRight/output.js @@ -1,4 +1,4 @@ -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +for (let _key = array.length, _value; --_key >= 0; ) { _value = array[_key]; console.log(_value); } \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/map/output.js b/__tests__/__fixtures__/inlined-function-return/map/output.js index 8a65aae2..b2e8d08e 100644 --- a/__tests__/__fixtures__/inlined-function-return/map/output.js +++ b/__tests__/__fixtures__/inlined-function-return/map/output.js @@ -1,6 +1,7 @@ -let _result = []; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +const _length = array.length; +const _results = Array(_length); +for (let _key = 0, _value; _key < _length; ++_key) { _value = array[_key]; - _result[_key] = _value * 2; + _results[_key] = _value * 2; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/mapObject/output.js b/__tests__/__fixtures__/inlined-function-return/mapObject/output.js index 505e8250..c0c20009 100644 --- a/__tests__/__fixtures__/inlined-function-return/mapObject/output.js +++ b/__tests__/__fixtures__/inlined-function-return/mapObject/output.js @@ -1,7 +1,7 @@ -let _result = {}; -let _value; -for (let _key in object) { +const _results = {}; +let _value, _result; +for (const _key in object) { _value = object[_key]; - _result[_key] = _value * 2; + _results[_key] = _value * 2; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/mapRight/output.js b/__tests__/__fixtures__/inlined-function-return/mapRight/output.js index 53710bee..3ef63e39 100644 --- a/__tests__/__fixtures__/inlined-function-return/mapRight/output.js +++ b/__tests__/__fixtures__/inlined-function-return/mapRight/output.js @@ -1,6 +1,8 @@ -let _result = []; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _length = array.length; +let _key = _length; +const _results = Array(_length); +for (let _value; --_key >= 0; ) { _value = array[_key]; - _result[_result.length] = _value * 2; + _results[_length - _key - 1] = _value * 2; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/output.js b/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/output.js index adf6e5a4..90603766 100644 --- a/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/output.js +++ b/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/output.js @@ -1,6 +1,6 @@ -let _result = array[0]; +let _total = array[0]; for (let _key = 1, _length = array.length, _value; _key < _length; ++_key) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/reduce/output.js b/__tests__/__fixtures__/inlined-function-return/reduce/output.js index 97d5979a..d51cb708 100644 --- a/__tests__/__fixtures__/inlined-function-return/reduce/output.js +++ b/__tests__/__fixtures__/inlined-function-return/reduce/output.js @@ -1,6 +1,6 @@ -let _result = 0; +let _total = 0; for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/output.js b/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/output.js index adfbb94f..21bf4dc2 100644 --- a/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/output.js +++ b/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/output.js @@ -1,13 +1,13 @@ -let _hasInitialValue = false; -let _value; -let _result; -for (let _key in object) { - if (_hasInitialValue) { - _value = object[_key]; - _result = _result + _value; - } else { - _hasInitialValue = true; - _result = object[_key]; +let _skip = true, + _total = undefined, + _value; +for (const _key in object) { + _value = object[_key]; + if (_skip) { + _total = _value; + _skip = false; + continue; } + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/reduceObject/output.js b/__tests__/__fixtures__/inlined-function-return/reduceObject/output.js index b50926df..1d59596e 100644 --- a/__tests__/__fixtures__/inlined-function-return/reduceObject/output.js +++ b/__tests__/__fixtures__/inlined-function-return/reduceObject/output.js @@ -1,7 +1,13 @@ -let _value; -let _result = 0; -for (let _key in object) { +let _skip = false, + _total = 0, + _value; +for (const _key in object) { _value = object[_key]; - _result = _result + _value; + if (_skip) { + _total = _value; + _skip = false; + continue; + } + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/output.js b/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/output.js index 1e97c96a..9176dc10 100644 --- a/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/output.js +++ b/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/output.js @@ -1,7 +1,6 @@ -const _length = array.length; -let _result = array[_length - 1]; -for (let _key = _length - 2, _value; _key >= 0; --_key) { +let _total = array[array.length - 1]; +for (let _key = array.length - 1, _value; --_key >= 1; ) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/reduceRight/output.js b/__tests__/__fixtures__/inlined-function-return/reduceRight/output.js index 2f4bfcb8..102fd74c 100644 --- a/__tests__/__fixtures__/inlined-function-return/reduceRight/output.js +++ b/__tests__/__fixtures__/inlined-function-return/reduceRight/output.js @@ -1,6 +1,6 @@ -let _result = 0; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +let _total = 0; +for (let _key = array.length - 0, _value; --_key >= 0; ) { _value = array[_key]; - _result = _result + _value; + _total = _total + _value; } -const sum = _result; \ No newline at end of file +const sum = _total; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/some/output.js b/__tests__/__fixtures__/inlined-function-return/some/output.js index 7d4c4c43..53ffa86d 100644 --- a/__tests__/__fixtures__/inlined-function-return/some/output.js +++ b/__tests__/__fixtures__/inlined-function-return/some/output.js @@ -1,9 +1,14 @@ -let _result = false; -for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { +let _determination = false; +for ( + let _key = 0, _length = array.length, _value, _result; + _key < _length; + ++_key +) { _value = array[_key]; - if (_value % 2 === 0) { - _result = true; + _result = _value % 2 === 0; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/someObject/output.js b/__tests__/__fixtures__/inlined-function-return/someObject/output.js index 07e67047..9595b4c6 100644 --- a/__tests__/__fixtures__/inlined-function-return/someObject/output.js +++ b/__tests__/__fixtures__/inlined-function-return/someObject/output.js @@ -1,10 +1,12 @@ -let _result = false; -let _value; -for (let _key in object) { +let _determination = false, + _value, + _result; +for (const _key in object) { _value = object[_key]; - if (_value % 2 === 0) { - _result = true; + _result = _value % 2 === 0; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/inlined-function-return/someRight/output.js b/__tests__/__fixtures__/inlined-function-return/someRight/output.js index 5d18e4d6..8063a7dd 100644 --- a/__tests__/__fixtures__/inlined-function-return/someRight/output.js +++ b/__tests__/__fixtures__/inlined-function-return/someRight/output.js @@ -1,9 +1,10 @@ -let _result = false; -for (let _key = array.length - 1, _value; _key >= 0; --_key) { +const _determination = false; +for (let _key = array.length, _value, _result; --_key >= 0; ) { _value = array[_key]; - if (_value % 2 === 0) { - _result = true; + _result = _value % 2 === 0; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/every/output.js b/__tests__/__fixtures__/uncached/every/output.js index e59beefb..bef94101 100644 --- a/__tests__/__fixtures__/uncached/every/output.js +++ b/__tests__/__fixtures__/uncached/every/output.js @@ -1,11 +1,16 @@ -const _iterable = [1, 2, 3, 4]; -let _result = true; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +let _determination = true; +for ( + let _key = 0, _length = _collection.length, _value, _result; + _key < _length; + ++_key +) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (!_isValueEven) { - _result = false; + _result = _isValueEven; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/everyObject/output.js b/__tests__/__fixtures__/uncached/everyObject/output.js index c85b9a24..b20fe66a 100644 --- a/__tests__/__fixtures__/uncached/everyObject/output.js +++ b/__tests__/__fixtures__/uncached/everyObject/output.js @@ -1,17 +1,19 @@ -const _iterable = { +const _collection = { one: 1, two: 2, three: 3, four: 4, }; -let _result = true; -let _value; -for (let _key in _iterable) { - _value = _iterable[_key]; +let _determination = true, + _value, + _result; +for (const _key in _collection) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (!_isValueEven) { - _result = false; + _result = _isValueEven; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/everyRight/output.js b/__tests__/__fixtures__/uncached/everyRight/output.js index b8c5ff52..3cfbc598 100644 --- a/__tests__/__fixtures__/uncached/everyRight/output.js +++ b/__tests__/__fixtures__/uncached/everyRight/output.js @@ -1,11 +1,12 @@ -const _iterable = [1, 2, 3, 4]; -let _result = true; -for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +let _determination = true; +for (let _key = _collection.length, _value, _result; --_key >= 0; ) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (!_isValueEven) { - _result = false; + _result = _isValueEven; + if (!_result) { + _determination = false; break; } } -const areAllEven = _result; \ No newline at end of file +const areAllEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/filter/output.js b/__tests__/__fixtures__/uncached/filter/output.js index 7eef67fc..df678a3e 100644 --- a/__tests__/__fixtures__/uncached/filter/output.js +++ b/__tests__/__fixtures__/uncached/filter/output.js @@ -1,8 +1,15 @@ -const _iterable = [1, 2, 3, 4]; -let _result = []; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +const _results = []; +for ( + let _key = 0, _length = _collection.length, _value, _result; + _key < _length; + ++_key +) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) _result.push(_value); + _result = _isValueEven; + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/filterObject/output.js b/__tests__/__fixtures__/uncached/filterObject/output.js index fd421886..1128891d 100644 --- a/__tests__/__fixtures__/uncached/filterObject/output.js +++ b/__tests__/__fixtures__/uncached/filterObject/output.js @@ -1,14 +1,17 @@ -const _iterable = { +const _collection = { one: 1, two: 2, three: 3, four: 4, }; -let _result = {}; -let _value; -for (let _key in _iterable) { - _value = _iterable[_key]; +const _results = {}; +let _result, _value; +for (const _key in _collection) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) _result[_key] = _value; + _result = _isValueEven; + if (_result) { + _results[_key] = _value; + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/filterRight/output.js b/__tests__/__fixtures__/uncached/filterRight/output.js index bff43ba3..50241452 100644 --- a/__tests__/__fixtures__/uncached/filterRight/output.js +++ b/__tests__/__fixtures__/uncached/filterRight/output.js @@ -1,8 +1,12 @@ -const _iterable = [1, 2, 3, 4]; -let _result = []; -for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +const _results = []; +let _result, _value; +for (let _key = _collection.length, _value, _result; --_key >= 0; ) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) _result.push(_value); + _result = _isValueEven; + if (_result) { + _results.push(_value); + } } -const onlyEven = _result; \ No newline at end of file +const onlyEven = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/find/output.js b/__tests__/__fixtures__/uncached/find/output.js index 1933fd57..33fffa8c 100644 --- a/__tests__/__fixtures__/uncached/find/output.js +++ b/__tests__/__fixtures__/uncached/find/output.js @@ -1,11 +1,16 @@ -const _iterable = [1, 2, 3, 4]; -let _result; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +let _match; +for ( + let _key = 0, _length = _collection.length, _value, _result; + _key < _length; + ++_key +) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) { - _result = _value; + _result = _isValueEven; + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/findIndex/output.js b/__tests__/__fixtures__/uncached/findIndex/output.js index b8098472..56aec479 100644 --- a/__tests__/__fixtures__/uncached/findIndex/output.js +++ b/__tests__/__fixtures__/uncached/findIndex/output.js @@ -1,11 +1,16 @@ -const _iterable = [1, 2, 3, 4]; -let _result = -1; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +let _match = -1; +for ( + let _key = 0, _length = _collection.length, _value, _result; + _key < _length; + ++_key +) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) { - _result = _key; + _result = _isValueEven; + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/findIndexRight/output.js b/__tests__/__fixtures__/uncached/findIndexRight/output.js index 6bdc8b91..516fc798 100644 --- a/__tests__/__fixtures__/uncached/findIndexRight/output.js +++ b/__tests__/__fixtures__/uncached/findIndexRight/output.js @@ -1,11 +1,4 @@ -const _iterable = [1, 2, 3, 4]; -let _result = -1; -for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { - _value = _iterable[_key]; - const _isValueEven = _value % 2 === 0; - if (_isValueEven) { - _result = _key; - break; - } -} -const firstEven = _result; \ No newline at end of file +const firstEven = findIndexRight([1, 2, 3, 4], (value) => { + const isValueEven = value % 2 === 0; + return isValueEven; +}); \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/findKey/output.js b/__tests__/__fixtures__/uncached/findKey/output.js index e1caf543..ef864426 100644 --- a/__tests__/__fixtures__/uncached/findKey/output.js +++ b/__tests__/__fixtures__/uncached/findKey/output.js @@ -1,17 +1,19 @@ -const _iterable = { +const _collection = { one: 1, two: 2, three: 3, four: 4, }; -let _result; -let _value; -for (let _key in _iterable) { - _value = _iterable[_key]; +let _match = -1, + _value, + _result; +for (const _key in _collection) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) { - _result = _key; + _result = _isValueEven; + if (_result) { + _match = _key; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/findObject/output.js b/__tests__/__fixtures__/uncached/findObject/output.js index 97631d8d..b9ed2456 100644 --- a/__tests__/__fixtures__/uncached/findObject/output.js +++ b/__tests__/__fixtures__/uncached/findObject/output.js @@ -1,17 +1,17 @@ -const _iterable = { +const _collection = { one: 1, two: 2, three: 3, four: 4, }; -let _result; -let _value; -for (let _key in _iterable) { - _value = _iterable[_key]; +let _match, _value, _result; +for (const _key in _collection) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) { - _result = _value; + _result = _isValueEven; + if (_result) { + _match = _value; break; } } -const firstEven = _result; \ No newline at end of file +const firstEven = _match; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/findRight/output.js b/__tests__/__fixtures__/uncached/findRight/output.js index de784cbd..7fedcd86 100644 --- a/__tests__/__fixtures__/uncached/findRight/output.js +++ b/__tests__/__fixtures__/uncached/findRight/output.js @@ -1,11 +1,4 @@ -const _iterable = [1, 2, 3, 4]; -let _result; -for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { - _value = _iterable[_key]; - const _isValueEven = _value % 2 === 0; - if (_isValueEven) { - _result = _value; - break; - } -} -const lastEven = _result; \ No newline at end of file +const lastEven = findRight([1, 2, 3, 4], (value) => { + const isValueEven = value % 2 === 0; + return isValueEven; +}); \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/flatMap/output.js b/__tests__/__fixtures__/uncached/flatMap/output.js index 7449b3ce..21813558 100644 --- a/__tests__/__fixtures__/uncached/flatMap/output.js +++ b/__tests__/__fixtures__/uncached/flatMap/output.js @@ -1,11 +1,16 @@ -const _iterable = [ +const _collection = [ ['foo', 'bar'], ['bar', 'baz'], ]; -let _result = []; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; - const [_first] = _value; - _result.push.apply(_result, [_first]); +let _results = []; +for ( + let _key = 0, _length = _collection.length, _entry, _result; + _key < _length; + ++_key +) { + _entry = _collection[_key]; + const [_first] = _entry; + _result = [_first]; + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/flatMapRight/output.js b/__tests__/__fixtures__/uncached/flatMapRight/output.js index e4ff8c85..49a60b67 100644 --- a/__tests__/__fixtures__/uncached/flatMapRight/output.js +++ b/__tests__/__fixtures__/uncached/flatMapRight/output.js @@ -1,11 +1,12 @@ -const _iterable = [ +const _collection = [ ['foo', 'bar'], ['bar', 'baz'], ]; -let _result = []; -for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { - _value = _iterable[_key]; - const [_first] = _value; - _result.push.apply(_result, [_first]); +let _results = []; +for (let _key = _collection.length, _entry, _result; --_key >= 0; ) { + _entry = _collection[_key]; + const [_first] = _entry; + _result = [_first]; + _results = _results.concat(_result); } -const flattened = _result; \ No newline at end of file +const flattened = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/forEach/output.js b/__tests__/__fixtures__/uncached/forEach/output.js index 600b2095..108cca66 100644 --- a/__tests__/__fixtures__/uncached/forEach/output.js +++ b/__tests__/__fixtures__/uncached/forEach/output.js @@ -1,6 +1,11 @@ -const _iterable = [1, 2, 3, 4]; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +for ( + let _key = 0, _length = _collection.length, _value; + _key < _length; + ++_key +) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; _isValueEven; + continue; } \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/forEachObject/output.js b/__tests__/__fixtures__/uncached/forEachObject/output.js index f5c66dfd..596aab6d 100644 --- a/__tests__/__fixtures__/uncached/forEachObject/output.js +++ b/__tests__/__fixtures__/uncached/forEachObject/output.js @@ -1,12 +1,13 @@ -const _iterable = { +const _collection = { one: 1, two: 2, three: 3, four: 4, }; let _value; -for (let _key in _iterable) { - _value = _iterable[_key]; +for (const _key in _collection) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; _isValueEven; + continue; } \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/forEachRight/output.js b/__tests__/__fixtures__/uncached/forEachRight/output.js index bb3e3ed7..436f426e 100644 --- a/__tests__/__fixtures__/uncached/forEachRight/output.js +++ b/__tests__/__fixtures__/uncached/forEachRight/output.js @@ -1,6 +1,7 @@ -const _iterable = [1, 2, 3, 4]; -for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +for (let _key = _collection.length, _value; --_key >= 0; ) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; _isValueEven; + continue; } \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/map/output.js b/__tests__/__fixtures__/uncached/map/output.js index 245406a8..51580164 100644 --- a/__tests__/__fixtures__/uncached/map/output.js +++ b/__tests__/__fixtures__/uncached/map/output.js @@ -1,8 +1,9 @@ -const _iterable = [1, 2, 3, 4]; -let _result = []; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +const _length = _collection.length; +const _results = Array(_length); +for (let _key = 0, _value; _key < _length; ++_key) { + _value = _collection[_key]; const _doubled = _value * 2; - _result[_key] = _doubled; + _results[_key] = _doubled; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/mapObject/output.js b/__tests__/__fixtures__/uncached/mapObject/output.js index 09ef68db..de73ce70 100644 --- a/__tests__/__fixtures__/uncached/mapObject/output.js +++ b/__tests__/__fixtures__/uncached/mapObject/output.js @@ -1,14 +1,14 @@ -const _iterable = { +const _collection = { one: 1, two: 2, three: 3, four: 4, }; -let _result = {}; -let _value; -for (let _key in _iterable) { - _value = _iterable[_key]; +const _results = {}; +let _value, _result; +for (const _key in _collection) { + _value = _collection[_key]; const _doubled = _value * 2; - _result[_key] = _doubled; + _results[_key] = _doubled; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/mapRight/output.js b/__tests__/__fixtures__/uncached/mapRight/output.js index 4e8de33c..2c45f4f7 100644 --- a/__tests__/__fixtures__/uncached/mapRight/output.js +++ b/__tests__/__fixtures__/uncached/mapRight/output.js @@ -1,8 +1,10 @@ -const _iterable = [1, 2, 3, 4]; -let _result = []; -for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +const _length = _collection.length; +let _key = _length; +const _results = Array(_length); +for (let _value; --_key >= 0; ) { + _value = _collection[_key]; const _doubled = _value * 2; - _result[_result.length] = _doubled; + _results[_length - _key - 1] = _doubled; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _results; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/reduce-no-initialValue/output.js b/__tests__/__fixtures__/uncached/reduce-no-initialValue/output.js index a4051d2f..0bf3f2fa 100644 --- a/__tests__/__fixtures__/uncached/reduce-no-initialValue/output.js +++ b/__tests__/__fixtures__/uncached/reduce-no-initialValue/output.js @@ -1,7 +1,12 @@ -const _iterable = [1, 2, 3, 4]; -let _result = _iterable[0]; -for (let _key = 1, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; - _result[_key] = _value * 2; +const _collection = [1, 2, 3, 4]; +let _agg = _collection[0]; +for ( + let _index = 1, _length = _collection.length, _value; + _index < _length; + ++_index +) { + _value = _collection[_index]; + _agg[_index] = _value * 2; + _agg = _agg; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _agg; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/reduce/output.js b/__tests__/__fixtures__/uncached/reduce/output.js index 12345231..d7f4dede 100644 --- a/__tests__/__fixtures__/uncached/reduce/output.js +++ b/__tests__/__fixtures__/uncached/reduce/output.js @@ -1,7 +1,12 @@ -const _iterable = [1, 2, 3, 4]; -let _result = {}; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; - _result[_key] = _value * 2; +const _collection = [1, 2, 3, 4]; +let _agg = {}; +for ( + let _index = 0, _length = _collection.length, _value; + _index < _length; + ++_index +) { + _value = _collection[_index]; + _agg[_index] = _value * 2; + _agg = _agg; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _agg; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/reduceObject-no-initialValue/output.js b/__tests__/__fixtures__/uncached/reduceObject-no-initialValue/output.js index 8dbb8e8a..b63668a9 100644 --- a/__tests__/__fixtures__/uncached/reduceObject-no-initialValue/output.js +++ b/__tests__/__fixtures__/uncached/reduceObject-no-initialValue/output.js @@ -1,19 +1,20 @@ -const _iterable = { +const _collection = { one: 1, two: 2, three: 3, four: 4, }; -let _hasInitialValue = false; -let _value; -let _result; -for (let _key in _iterable) { - if (_hasInitialValue) { - _value = _iterable[_key]; - _result[_key] = _value * 2; - } else { - _hasInitialValue = true; - _result = _iterable[_key]; +let _skip = true, + _agg = undefined, + _value; +for (const _index in _collection) { + _value = _collection[_index]; + if (_skip) { + _agg = _value; + _skip = false; + continue; } + _agg[_index] = _value * 2; + _agg = _agg; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _agg; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/reduceObject/output.js b/__tests__/__fixtures__/uncached/reduceObject/output.js index 2eea8dc2..7bee09f3 100644 --- a/__tests__/__fixtures__/uncached/reduceObject/output.js +++ b/__tests__/__fixtures__/uncached/reduceObject/output.js @@ -1,13 +1,20 @@ -const _iterable = { +const _collection = { one: 1, two: 2, three: 3, four: 4, }; -let _value; -let _result = {}; -for (let _key in _iterable) { - _value = _iterable[_key]; - _result[_key] = _value * 2; +let _skip = false, + _agg = {}, + _value; +for (const _index in _collection) { + _value = _collection[_index]; + if (_skip) { + _agg = _value; + _skip = false; + continue; + } + _agg[_index] = _value * 2; + _agg = _agg; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _agg; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/reduceRight-no-initialValue/output.js b/__tests__/__fixtures__/uncached/reduceRight-no-initialValue/output.js index e14a824d..563cc08b 100644 --- a/__tests__/__fixtures__/uncached/reduceRight-no-initialValue/output.js +++ b/__tests__/__fixtures__/uncached/reduceRight-no-initialValue/output.js @@ -1,8 +1,8 @@ -const _iterable = [1, 2, 3, 4]; -const _length = _iterable.length; -let _result = _iterable[_length - 1]; -for (let _key = _length - 2, _value; _key >= 0; --_key) { - _value = _iterable[_key]; - _result[_key] = _value * 2; +const _collection = [1, 2, 3, 4]; +let _agg = _collection[_collection.length - 1]; +for (let _index = _collection.length - 1, _value; --_index >= 1; ) { + _value = _collection[_index]; + _agg[_index] = _value * 2; + _agg = _agg; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _agg; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/reduceRight/output.js b/__tests__/__fixtures__/uncached/reduceRight/output.js index 509e706e..8e32f9ea 100644 --- a/__tests__/__fixtures__/uncached/reduceRight/output.js +++ b/__tests__/__fixtures__/uncached/reduceRight/output.js @@ -1,7 +1,8 @@ -const _iterable = [1, 2, 3, 4]; -let _result = {}; -for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { - _value = _iterable[_key]; - _result[_key] = _value * 2; +const _collection = [1, 2, 3, 4]; +let _agg = {}; +for (let _index = _collection.length - 0, _value; --_index >= 0; ) { + _value = _collection[_index]; + _agg[_index] = _value * 2; + _agg = _agg; } -const doubledValues = _result; \ No newline at end of file +const doubledValues = _agg; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/some/output.js b/__tests__/__fixtures__/uncached/some/output.js index fb2bcddb..2d29fad5 100644 --- a/__tests__/__fixtures__/uncached/some/output.js +++ b/__tests__/__fixtures__/uncached/some/output.js @@ -1,11 +1,16 @@ -const _iterable = [1, 2, 3, 4]; -let _result = false; -for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +let _determination = false; +for ( + let _key = 0, _length = _collection.length, _value, _result; + _key < _length; + ++_key +) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) { - _result = true; + _result = _isValueEven; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/someObject/output.js b/__tests__/__fixtures__/uncached/someObject/output.js index e9f4854d..ef79a908 100644 --- a/__tests__/__fixtures__/uncached/someObject/output.js +++ b/__tests__/__fixtures__/uncached/someObject/output.js @@ -1,17 +1,19 @@ -const _iterable = { +const _collection = { one: 1, two: 2, three: 3, four: 4, }; -let _result = false; -let _value; -for (let _key in _iterable) { - _value = _iterable[_key]; +let _determination = false, + _value, + _result; +for (const _key in _collection) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) { - _result = true; + _result = _isValueEven; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__fixtures__/uncached/someRight/output.js b/__tests__/__fixtures__/uncached/someRight/output.js index ad3221c4..76f193a5 100644 --- a/__tests__/__fixtures__/uncached/someRight/output.js +++ b/__tests__/__fixtures__/uncached/someRight/output.js @@ -1,11 +1,12 @@ -const _iterable = [1, 2, 3, 4]; -let _result = false; -for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { - _value = _iterable[_key]; +const _collection = [1, 2, 3, 4]; +const _determination = false; +for (let _key = _collection.length, _value, _result; --_key >= 0; ) { + _value = _collection[_key]; const _isValueEven = _value % 2 === 0; - if (_isValueEven) { - _result = true; + _result = _isValueEven; + if (_result) { + _determination = true; break; } } -const areAnyEven = _result; \ No newline at end of file +const areAnyEven = _determination; \ No newline at end of file diff --git a/__tests__/__runtime__/find.js b/__tests__/__runtime__/find.js index a4b978d4..31af9151 100644 --- a/__tests__/__runtime__/find.js +++ b/__tests__/__runtime__/find.js @@ -1,6 +1,6 @@ /* eslint-disable */ -const { find, findObject, findRight } = require('../../src/inline-loops.macro'); +const { find, findObject, findLast } = require('../../src/inline-loops.macro'); const { deepEqual: isEqual } = require('fast-equals'); @@ -14,7 +14,7 @@ const OBJECT = { six: 6, }; -const isEven = value => value % 2 === 0; +const isEven = (value) => value % 2 === 0; const BAD_DECREMENTING_ARRAY_RESULT = 2; const DECREMENTING_ARRAY_RESULT = 6; @@ -28,8 +28,8 @@ const OBJECT_RESULT = 2; module.exports = { cached: { decrementing: { - false: isEqual(findRight(ARRAY, isEven), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(findRight(ARRAY, isEven), DECREMENTING_ARRAY_RESULT), + false: isEqual(findLast(ARRAY, isEven), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(findLast(ARRAY, isEven), DECREMENTING_ARRAY_RESULT), }, object: { false: isEqual(findObject(OBJECT, isEven), BAD_OBJECT_RESULT), @@ -42,28 +42,46 @@ module.exports = { }, inlinedArrowExpression: { decrementing: { - false: isEqual(findRight(ARRAY, value => value % 2 === 0), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(findRight(ARRAY, value => value % 2 === 0), DECREMENTING_ARRAY_RESULT), + false: isEqual( + findLast(ARRAY, (value) => value % 2 === 0), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + findLast(ARRAY, (value) => value % 2 === 0), + DECREMENTING_ARRAY_RESULT, + ), }, object: { - false: isEqual(findObject(OBJECT, value => value % 2 === 0), BAD_OBJECT_RESULT), - true: isEqual(findObject(OBJECT, value => value % 2 === 0), OBJECT_RESULT), + false: isEqual( + findObject(OBJECT, (value) => value % 2 === 0), + BAD_OBJECT_RESULT, + ), + true: isEqual( + findObject(OBJECT, (value) => value % 2 === 0), + OBJECT_RESULT, + ), }, standard: { - false: isEqual(find(ARRAY, value => value % 2 === 0), ARRAY), - true: isEqual(find(ARRAY, value => value % 2 === 0), ARRAY_RESULT), + false: isEqual( + find(ARRAY, (value) => value % 2 === 0), + ARRAY, + ), + true: isEqual( + find(ARRAY, (value) => value % 2 === 0), + ARRAY_RESULT, + ), }, }, inlinedArrowReturn: { decrementing: { false: isEqual( - findRight(ARRAY, value => { + findLast(ARRAY, (value) => { return value % 2 === 0; }), BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - findRight(ARRAY, value => { + findLast(ARRAY, (value) => { return value % 2 === 0; }), DECREMENTING_ARRAY_RESULT, @@ -71,13 +89,13 @@ module.exports = { }, object: { false: isEqual( - findObject(OBJECT, value => { + findObject(OBJECT, (value) => { return value % 2 === 0; }), BAD_OBJECT_RESULT, ), true: isEqual( - findObject(OBJECT, value => { + findObject(OBJECT, (value) => { return value % 2 === 0; }), OBJECT_RESULT, @@ -85,13 +103,13 @@ module.exports = { }, standard: { false: isEqual( - find(ARRAY, value => { + find(ARRAY, (value) => { return value % 2 === 0; }), BAD_ARRAY_RESULT, ), true: isEqual( - find(ARRAY, value => { + find(ARRAY, (value) => { return value % 2 === 0; }), ARRAY_RESULT, @@ -101,13 +119,13 @@ module.exports = { inlinedFunctionReturn: { decrementing: { false: isEqual( - findRight(ARRAY, function(value) { + findLast(ARRAY, function (value) { return value % 2 === 0; }), BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - findRight(ARRAY, function(value) { + findLast(ARRAY, function (value) { return value % 2 === 0; }), DECREMENTING_ARRAY_RESULT, @@ -115,13 +133,13 @@ module.exports = { }, object: { false: isEqual( - findObject(OBJECT, function(value) { + findObject(OBJECT, function (value) { return value % 2 === 0; }), BAD_OBJECT_RESULT, ), true: isEqual( - findObject(OBJECT, function(value) { + findObject(OBJECT, function (value) { return value % 2 === 0; }), OBJECT_RESULT, @@ -131,7 +149,7 @@ module.exports = { uncached: { decrementing: { false: isEqual( - findRight([].concat(ARRAY), value => { + findLast([].concat(ARRAY), (value) => { const isEven = value % 2 === 0; return isEven; @@ -139,7 +157,7 @@ module.exports = { BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - findRight([].concat(ARRAY), value => { + findLast([].concat(ARRAY), (value) => { const isEven = value % 2 === 0; return isEven; @@ -149,7 +167,7 @@ module.exports = { }, object: { false: isEqual( - findObject(Object.assign({}, OBJECT), value => { + findObject(Object.assign({}, OBJECT), (value) => { const isEven = value % 2 === 0; return isEven; @@ -157,7 +175,7 @@ module.exports = { BAD_OBJECT_RESULT, ), true: isEqual( - findObject(Object.assign({}, OBJECT), value => { + findObject(Object.assign({}, OBJECT), (value) => { const isEven = value % 2 === 0; return isEven; @@ -167,7 +185,7 @@ module.exports = { }, standard: { false: isEqual( - find([].concat(ARRAY), value => { + find([].concat(ARRAY), (value) => { const isEven = value % 2 === 0; return isEven; @@ -175,7 +193,7 @@ module.exports = { BAD_ARRAY_RESULT, ), true: isEqual( - find([].concat(ARRAY), value => { + find([].concat(ARRAY), (value) => { const isEven = value % 2 === 0; return isEven; diff --git a/__tests__/__runtime__/findIndex-findKey.js b/__tests__/__runtime__/findIndex-findKey.js index e0d5c046..ad0d9a1e 100644 --- a/__tests__/__runtime__/findIndex-findKey.js +++ b/__tests__/__runtime__/findIndex-findKey.js @@ -1,6 +1,10 @@ /* eslint-disable */ -const { findIndex, findIndexRight, findKey } = require('../../src/inline-loops.macro'); +const { + findIndex, + findLastIndex, + findKey, +} = require('../../src/inline-loops.macro'); const { deepEqual: isEqual } = require('fast-equals'); @@ -14,7 +18,7 @@ const OBJECT = { six: 6, }; -const isEven = value => value % 2 === 0; +const isEven = (value) => value % 2 === 0; const BAD_DECREMENTING_ARRAY_RESULT = 1; const DECREMENTING_ARRAY_RESULT = 5; @@ -28,8 +32,11 @@ const OBJECT_RESULT = 'two'; module.exports = { cached: { decrementing: { - false: isEqual(findIndexRight(ARRAY, isEven), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(findIndexRight(ARRAY, isEven), DECREMENTING_ARRAY_RESULT), + false: isEqual( + findLastIndex(ARRAY, isEven), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual(findLastIndex(ARRAY, isEven), DECREMENTING_ARRAY_RESULT), }, object: { false: isEqual(findKey(OBJECT, isEven), BAD_OBJECT_RESULT), @@ -43,30 +50,45 @@ module.exports = { inlinedArrowExpression: { decrementing: { false: isEqual( - findIndexRight(ARRAY, value => value % 2 === 0), + findLastIndex(ARRAY, (value) => value % 2 === 0), BAD_DECREMENTING_ARRAY_RESULT, ), - true: isEqual(findIndexRight(ARRAY, value => value % 2 === 0), DECREMENTING_ARRAY_RESULT), + true: isEqual( + findLastIndex(ARRAY, (value) => value % 2 === 0), + DECREMENTING_ARRAY_RESULT, + ), }, object: { - false: isEqual(findKey(OBJECT, value => value % 2 === 0), BAD_OBJECT_RESULT), - true: isEqual(findKey(OBJECT, value => value % 2 === 0), OBJECT_RESULT), + false: isEqual( + findKey(OBJECT, (value) => value % 2 === 0), + BAD_OBJECT_RESULT, + ), + true: isEqual( + findKey(OBJECT, (value) => value % 2 === 0), + OBJECT_RESULT, + ), }, standard: { - false: isEqual(findIndex(ARRAY, value => value % 2 === 0), ARRAY), - true: isEqual(findIndex(ARRAY, value => value % 2 === 0), ARRAY_RESULT), + false: isEqual( + findIndex(ARRAY, (value) => value % 2 === 0), + ARRAY, + ), + true: isEqual( + findIndex(ARRAY, (value) => value % 2 === 0), + ARRAY_RESULT, + ), }, }, inlinedArrowReturn: { decrementing: { false: isEqual( - findIndexRight(ARRAY, value => { + findLastIndex(ARRAY, (value) => { return value % 2 === 0; }), BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - findIndexRight(ARRAY, value => { + findLastIndex(ARRAY, (value) => { return value % 2 === 0; }), DECREMENTING_ARRAY_RESULT, @@ -74,13 +96,13 @@ module.exports = { }, object: { false: isEqual( - findKey(OBJECT, value => { + findKey(OBJECT, (value) => { return value % 2 === 0; }), BAD_OBJECT_RESULT, ), true: isEqual( - findKey(OBJECT, value => { + findKey(OBJECT, (value) => { return value % 2 === 0; }), OBJECT_RESULT, @@ -88,13 +110,13 @@ module.exports = { }, standard: { false: isEqual( - findIndex(ARRAY, value => { + findIndex(ARRAY, (value) => { return value % 2 === 0; }), BAD_ARRAY_RESULT, ), true: isEqual( - findIndex(ARRAY, value => { + findIndex(ARRAY, (value) => { return value % 2 === 0; }), ARRAY_RESULT, @@ -104,13 +126,13 @@ module.exports = { inlinedFunctionReturn: { decrementing: { false: isEqual( - findIndexRight(ARRAY, function(value) { + findLastIndex(ARRAY, function (value) { return value % 2 === 0; }), BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - findIndexRight(ARRAY, function(value) { + findLastIndex(ARRAY, function (value) { return value % 2 === 0; }), DECREMENTING_ARRAY_RESULT, @@ -118,13 +140,13 @@ module.exports = { }, object: { false: isEqual( - findKey(OBJECT, function(value) { + findKey(OBJECT, function (value) { return value % 2 === 0; }), BAD_OBJECT_RESULT, ), true: isEqual( - findKey(OBJECT, function(value) { + findKey(OBJECT, function (value) { return value % 2 === 0; }), OBJECT_RESULT, @@ -132,13 +154,13 @@ module.exports = { }, standard: { false: isEqual( - findIndex(ARRAY, function(value) { + findIndex(ARRAY, function (value) { return value % 2 === 0; }), BAD_ARRAY_RESULT, ), true: isEqual( - findIndex(ARRAY, function(value) { + findIndex(ARRAY, function (value) { return value % 2 === 0; }), ARRAY_RESULT, @@ -148,7 +170,7 @@ module.exports = { uncached: { decrementing: { false: isEqual( - findIndexRight([].concat(ARRAY), value => { + findLastIndex([].concat(ARRAY), (value) => { const isEven = value % 2 === 0; return isEven; @@ -156,7 +178,7 @@ module.exports = { BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - findIndexRight([].concat(ARRAY), value => { + findLastIndex([].concat(ARRAY), (value) => { const isEven = value % 2 === 0; return isEven; @@ -166,7 +188,7 @@ module.exports = { }, object: { false: isEqual( - findKey(Object.assign({}, OBJECT), value => { + findKey(Object.assign({}, OBJECT), (value) => { const isEven = value % 2 === 0; return isEven; @@ -174,7 +196,7 @@ module.exports = { BAD_OBJECT_RESULT, ), true: isEqual( - findKey(Object.assign({}, OBJECT), value => { + findKey(Object.assign({}, OBJECT), (value) => { const isEven = value % 2 === 0; return isEven; @@ -184,7 +206,7 @@ module.exports = { }, standard: { false: isEqual( - findIndex([].concat(ARRAY), value => { + findIndex([].concat(ARRAY), (value) => { const isEven = value % 2 === 0; return isEven; @@ -192,7 +214,7 @@ module.exports = { BAD_ARRAY_RESULT, ), true: isEqual( - findIndex([].concat(ARRAY), value => { + findIndex([].concat(ARRAY), (value) => { const isEven = value % 2 === 0; return isEven; diff --git a/__tests__/__runtime__/reduce.js b/__tests__/__runtime__/reduce.js index 4020a89d..f5f69b6e 100644 --- a/__tests__/__runtime__/reduce.js +++ b/__tests__/__runtime__/reduce.js @@ -1,6 +1,10 @@ /* eslint-disable */ -const { reduce, reduceObject, reduceRight } = require('../../src/inline-loops.macro'); +const { + reduce, + reduceObject, + reduceRight, +} = require('../../src/inline-loops.macro'); const { deepEqual: isEqual } = require('fast-equals'); @@ -31,10 +35,26 @@ const DECREMENTING_ARRAY_RESULT = ARRAY.reduceRight(isEven, 10); const BAD_OBJECT_RESULT = Object.assign({}, OBJECT); const OBJECT_RESULT = Object.values(OBJECT).reduce(isEven, 10); +console.log( + reduceRight(ARRAY, (total, value) => + value % 2 === 0 ? total + value : total, + ), + OBJECT_RESULT, + isEqual( + reduceRight(ARRAY, (total, value) => + value % 2 === 0 ? total + value : total, + ), + OBJECT_RESULT, + ), +); + module.exports = { cached: { decrementing: { - false: isEqual(reduceRight(ARRAY, isEven, 10), BAD_DECREMENTING_ARRAY_RESULT), + false: isEqual( + reduceRight(ARRAY, isEven, 10), + BAD_DECREMENTING_ARRAY_RESULT, + ), true: isEqual(reduceRight(ARRAY, isEven, 10), DECREMENTING_ARRAY_RESULT), }, object: { @@ -49,31 +69,55 @@ module.exports = { inlinedArrowExpression: { decrementing: { false: isEqual( - reduceRight(ARRAY, (total, value) => (value % 2 === 0 ? total + value : total), 10), + reduceRight( + ARRAY, + (total, value) => (value % 2 === 0 ? total + value : total), + 10, + ), BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - reduceRight(ARRAY, (total, value) => (value % 2 === 0 ? total + value : total), 10), + reduceRight( + ARRAY, + (total, value) => (value % 2 === 0 ? total + value : total), + 10, + ), DECREMENTING_ARRAY_RESULT, ), }, object: { false: isEqual( - reduceObject(OBJECT, (total, value) => (value % 2 === 0 ? total + value : total), 10), + reduceObject( + OBJECT, + (total, value) => (value % 2 === 0 ? total + value : total), + 10, + ), BAD_OBJECT_RESULT, ), true: isEqual( - reduceObject(OBJECT, (total, value) => (value % 2 === 0 ? total + value : total), 10), + reduceObject( + OBJECT, + (total, value) => (value % 2 === 0 ? total + value : total), + 10, + ), OBJECT_RESULT, ), }, standard: { false: isEqual( - reduce(ARRAY, (total, value) => (value % 2 === 0 ? total + value : total), 10), + reduce( + ARRAY, + (total, value) => (value % 2 === 0 ? total + value : total), + 10, + ), BAD_ARRAY_RESULT, ), true: isEqual( - reduce(ARRAY, (total, value) => (value % 2 === 0 ? total + value : total), 10), + reduce( + ARRAY, + (total, value) => (value % 2 === 0 ? total + value : total), + 10, + ), ARRAY_RESULT, ), }, @@ -151,7 +195,7 @@ module.exports = { false: isEqual( reduceRight( ARRAY, - function(total, value) { + function (total, value) { return value % 2 === 0 ? total + value : total; }, 10, @@ -161,7 +205,7 @@ module.exports = { true: isEqual( reduceRight( ARRAY, - function(total, value) { + function (total, value) { return value % 2 === 0 ? total + value : total; }, 10, @@ -173,7 +217,7 @@ module.exports = { false: isEqual( reduceObject( OBJECT, - function(total, value) { + function (total, value) { return value % 2 === 0 ? total + value : total; }, 10, @@ -183,7 +227,7 @@ module.exports = { true: isEqual( reduceObject( OBJECT, - function(total, value) { + function (total, value) { return value % 2 === 0 ? total + value : total; }, 10, diff --git a/__tests__/runtime.test.js b/__tests__/runtime.test.js index e407a16c..7779def7 100644 --- a/__tests__/runtime.test.js +++ b/__tests__/runtime.test.js @@ -12,7 +12,9 @@ const TRANSFORMED_FILES = fs.readdirSync(TEST_FILES).reduce((tests, file) => { const fn = file.replace('.js', ''); if (fn.startsWith('error-')) { - test(`if ${fn.replace('error-', '').replace('-', ' ')} will throw an error`, () => { + test(`if ${fn + .replace('error-', '') + .replace('-', ' ')} will throw an error`, () => { expect(() => transformFileSync(filename, TRANSFORM_OPTIONS)).toThrow(); }); @@ -21,10 +23,6 @@ const TRANSFORMED_FILES = fs.readdirSync(TEST_FILES).reduce((tests, file) => { const transformed = transformFileSync(filename, TRANSFORM_OPTIONS).code; - // if (fn === 'complex-inliner') { - // console.log(transformed); - // } - tests[fn] = eval(transformed); return tests; diff --git a/src/handlers.js b/src-prev/handlers.js similarity index 100% rename from src/handlers.js rename to src-prev/handlers.js diff --git a/src/helpers.js b/src-prev/helpers.js similarity index 100% rename from src/helpers.js rename to src-prev/helpers.js diff --git a/src-prev/inline-loops.macro.js b/src-prev/inline-loops.macro.js new file mode 100644 index 00000000..d725e049 --- /dev/null +++ b/src-prev/inline-loops.macro.js @@ -0,0 +1,213 @@ +const { createMacro, MacroError } = require('babel-plugin-macros'); + +const { + handleEvery, + handleFilter, + handleFind, + handleFindKey, + handleFlatMap, + handleForEach, + handleMap, + handleReduce, + handleSome, +} = require('./handlers'); + +const METHODS = [ + 'every', + 'filter', + 'find', + 'findIndex', + 'findKey', + 'flatMap', + 'forEach', + 'map', + 'reduce', + 'some', +]; +const ARRAY_ONLY_METHODS = ['findIndex', 'flatMap']; +const OBJECT_ONLY_METHODS = ['findKey']; + +function getCallTypes(references, method) { + const isArrayOnly = ARRAY_ONLY_METHODS.includes(method); + const isObjectOnly = OBJECT_ONLY_METHODS.includes(method); + + const decrementingMethod = `${method}Right`; + const objectMethod = isObjectOnly ? method : `${method}Object`; + + const incrementingCalls = references[method] || []; + const decrementingCalls = references[decrementingMethod] || []; + const objectCalls = references[objectMethod] || []; + + return { + decrementingCalls: decrementingCalls.map((path) => ({ + method: decrementingMethod, + path, + sourceMethod: method, + type: 'decrementing', + })), + decrementingMethod, + incrementingCalls: incrementingCalls.map((path) => ({ + method, + path, + sourceMethod: method, + type: 'incrementing', + })), + isArrayOnly, + isObjectOnly, + objectCalls: objectCalls.map((path) => ({ + method: objectMethod, + path, + sourceMethod: method, + type: 'object', + })), + objectMethod, + }; +} + +function inlineLoops({ references, babel }) { + const { types: t } = babel; + + const allMethods = []; + + METHODS.forEach((method) => { + const { + decrementingCalls, + incrementingCalls, + isArrayOnly, + isObjectOnly, + objectCalls, + } = getCallTypes(references, method); + + if (isArrayOnly) { + return allMethods.push(...incrementingCalls, ...decrementingCalls); + } + + if (isObjectOnly) { + return allMethods.push(...objectCalls); + } + + return allMethods.push( + ...incrementingCalls, + ...decrementingCalls, + ...objectCalls, + ); + }); + + allMethods.forEach(({ path }) => { + path.node.__inlineLoopsMacro = true; + }); + + allMethods.sort(({ path: a }, { path: b }) => { + const aContainer = a.container; + const bContainer = b.container; + + if (aContainer.arguments) { + const [iterableA] = aContainer.arguments; + + if ( + t.isCallExpression(iterableA) && + iterableA.callee.__inlineLoopsMacro && + iterableA.callee === b.node + ) { + return 1; + } + } + + if (bContainer.arguments) { + const [iterableB] = bContainer.arguments; + + if ( + t.isCallExpression(iterableB) && + iterableB.callee.__inlineLoopsMacro && + iterableB.callee === a.node + ) { + return -1; + } + } + + const aStart = a.node.loc.start; + const bStart = b.node.loc.start; + + if (bStart.line > aStart.line) { + return -1; + } + + if (aStart.line > bStart.line) { + return 1; + } + + if (bStart.column > aStart.column) { + return -1; + } + + return 1; + }); + + const handlers = { + every: handleEvery, + filter: handleFilter, + find: handleFind, + findIndex: handleFindKey, + findKey: handleFindKey, + flatMap: handleFlatMap, + forEach: handleForEach, + map: handleMap, + reduce: handleReduce, + some: handleSome, + }; + + function createTransformer(name, transform, isDecrementing, isObject) { + return function _transform(path) { + if (path.findParent((_path) => _path.isConditionalExpression())) { + throw new MacroError( + `You cannot use ${name} in a conditional expression.`, + ); + } + const args = path.parent.arguments; + + if (args.some((arg) => t.isSpreadElement(arg))) { + throw new MacroError( + 'You cannot use spread arguments with the macro, please declare the arguments explicitly.', + ); + } + + const [object, handler, initialValue] = args; + const isHandlerMacro = allMethods.find( + ({ path: methodPath }) => + methodPath.node !== path.node && handler === methodPath.node, + ); + + if (isHandlerMacro) { + throw new MacroError( + 'You cannot use the macro directly as a handler, please wrap it in a function call.', + ); + } + + transform({ + t, + path, + object, + handler, + initialValue, + isDecrementing, + isObject, + }); + }; + } + + allMethods.forEach(({ method, path, sourceMethod, type }) => { + const isDecrementing = type === 'decrementing'; + const isObject = type === 'object'; + + const handler = createTransformer( + method, + handlers[sourceMethod], + isDecrementing, + isObject, + ); + + handler(path); + }); +} + +module.exports = createMacro(inlineLoops); diff --git a/src/inline-loops.macro.js b/src/inline-loops.macro.js index d725e049..5e761022 100644 --- a/src/inline-loops.macro.js +++ b/src/inline-loops.macro.js @@ -1,213 +1,1288 @@ -const { createMacro, MacroError } = require('babel-plugin-macros'); - -const { - handleEvery, - handleFilter, - handleFind, - handleFindKey, - handleFlatMap, - handleForEach, - handleMap, - handleReduce, - handleSome, -} = require('./handlers'); - -const METHODS = [ - 'every', - 'filter', - 'find', - 'findIndex', - 'findKey', - 'flatMap', - 'forEach', - 'map', - 'reduce', - 'some', -]; -const ARRAY_ONLY_METHODS = ['findIndex', 'flatMap']; -const OBJECT_ONLY_METHODS = ['findKey']; - -function getCallTypes(references, method) { - const isArrayOnly = ARRAY_ONLY_METHODS.includes(method); - const isObjectOnly = OBJECT_ONLY_METHODS.includes(method); - - const decrementingMethod = `${method}Right`; - const objectMethod = isObjectOnly ? method : `${method}Object`; - - const incrementingCalls = references[method] || []; - const decrementingCalls = references[decrementingMethod] || []; - const objectCalls = references[objectMethod] || []; - - return { - decrementingCalls: decrementingCalls.map((path) => ({ - method: decrementingMethod, - path, - sourceMethod: method, - type: 'decrementing', - })), - decrementingMethod, - incrementingCalls: incrementingCalls.map((path) => ({ - method, - path, - sourceMethod: method, - type: 'incrementing', - })), - isArrayOnly, - isObjectOnly, - objectCalls: objectCalls.map((path) => ({ - method: objectMethod, - path, - sourceMethod: method, - type: 'object', - })), - objectMethod, - }; -} +const { MacroError, createMacro } = require('babel-plugin-macros'); -function inlineLoops({ references, babel }) { - const { types: t } = babel; +function myMacro({ references, babel }) { + const { template, types: t } = babel; + const { + every = [], + everyObject = [], + everyRight = [], + filter = [], + filterObject = [], + filterRight = [], + find = [], + findObject = [], + findLast = [], + findIndex = [], + findKey = [], + findLastIndex = [], + flatMap = [], + flatMapRight = [], + forEach = [], + forEachObject = [], + forEachRight = [], + map = [], + mapObject = [], + mapRight = [], + reduce = [], + reduceObject = [], + reduceRight = [], + some = [], + someObject = [], + someRight = [], + } = references; - const allMethods = []; + const nthLastItemTemplate = template` + COLLECTION[COLLECTION.length - COUNT] +`; - METHODS.forEach((method) => { - const { - decrementingCalls, - incrementingCalls, - isArrayOnly, - isObjectOnly, - objectCalls, - } = getCallTypes(references, method); + const localVariableTemplate = template` + const LOCAL = VALUE; +`; - if (isArrayOnly) { - return allMethods.push(...incrementingCalls, ...decrementingCalls); + const everyTemplate = template` + let DETERMINATION = true; + for (let KEY = 0, LENGTH = COLLECTION.length, VALUE, RESULT; KEY < LENGTH; ++KEY) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (!RESULT) { + DETERMINATION = false; + break; + } } +`; - if (isObjectOnly) { - return allMethods.push(...objectCalls); + const everyObjectTemplate = template` + let DETERMINATION = true, + VALUE, + RESULT; + for (const KEY in COLLECTION) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (!RESULT) { + DETERMINATION = false; + break; + } + } +`; + + const everyRightTemplate = template` + let DETERMINATION = true; + for (let KEY = COLLECTION.length, VALUE, RESULT; --KEY >= 0;) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (!RESULT) { + DETERMINATION = false; + break; + } } +`; - return allMethods.push( - ...incrementingCalls, - ...decrementingCalls, - ...objectCalls, - ); - }); + const filterTemplate = template` + const RESULTS = []; + for (let KEY = 0, LENGTH = COLLECTION.length, VALUE, RESULT; KEY < LENGTH; ++KEY) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + RESULTS.push(VALUE); + } + } +`; + + const filterObjectTemplate = template` + const RESULTS = {}; + let RESULT, + VALUE; + for (const KEY in COLLECTION) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + RESULTS[KEY] = VALUE; + } + } +`; + + const filterRightTemplate = template` + const RESULTS = []; + let RESULT, + VALUE; + for (let KEY = COLLECTION.length, VALUE, RESULT; --KEY >= 0;) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; - allMethods.forEach(({ path }) => { - path.node.__inlineLoopsMacro = true; - }); + if (RESULT) { + RESULTS.push(VALUE); + } + } +`; + + const findTemplate = template` + let MATCH; + for (let KEY = 0, LENGTH = COLLECTION.length, VALUE, RESULT; KEY < LENGTH; ++KEY) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + MATCH = VALUE; + break; + } + } +`; + + const findObjectTemplate = template` + let MATCH, + VALUE, + RESULT; + for (const KEY in COLLECTION) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + MATCH = VALUE; + break; + } + } +`; + + const findRightTemplate = template` + let MATCH; + for (let KEY = COLLECTION.length, VALUE, RESULT; --KEY >= 0;) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + MATCH = VALUE; + break; + } + } +`; + + const findIndexTemplate = template` + let MATCH = -1; + for (let KEY = 0, LENGTH = COLLECTION.length, VALUE, RESULT; KEY < LENGTH; ++KEY) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + MATCH = KEY; + break; + } + } +`; - allMethods.sort(({ path: a }, { path: b }) => { - const aContainer = a.container; - const bContainer = b.container; + const findKeyTemplate = template` + let MATCH = -1, + VALUE, + RESULT; + for (const KEY in COLLECTION) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + MATCH = KEY; + break; + } + } +`; + + const findIndexRightTemplate = template` + let MATCH = -1; + for (let KEY = COLLECTION.length, VALUE, RESULT; --KEY >= 0;) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + MATCH = KEY; + break; + } + } +`; + + const flatMapTemplate = template` + let RESULTS = []; + for (let KEY = 0, LENGTH = COLLECTION.length, VALUE, RESULT; KEY < LENGTH; ++KEY) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + RESULTS = RESULTS.concat(RESULT); + } +`; + + const flatMapRightTemplate = template` + let RESULTS = []; + for (let KEY = COLLECTION.length, VALUE, RESULT; --KEY >= 0;) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + RESULTS = RESULTS.concat(RESULT); + } +`; + + const forEachTemplate = template` + for (let KEY = 0, LENGTH = COLLECTION.length, VALUE; KEY < LENGTH; ++KEY) { + VALUE = COLLECTION[KEY]; + BODY + } +`; + + const forEachObjectTemplate = template` + let VALUE; + for (const KEY in COLLECTION) { + VALUE = COLLECTION[KEY]; + BODY + } +`; + + const forEachRightTemplate = template` + for (let KEY = COLLECTION.length, VALUE; --KEY >= 0;) { + VALUE = COLLECTION[KEY]; + BODY + } +`; + + const mapTemplate = template` + const LENGTH = COLLECTION.length; + const RESULTS = Array(LENGTH); + for (let KEY = 0, VALUE; KEY < LENGTH; ++KEY) { + VALUE = COLLECTION[KEY]; + BODY + RESULTS[KEY] = LOGIC; + } +`; + + const mapObjectTemplate = template` + const RESULTS = {}; + let VALUE, + RESULT; + for (const KEY in COLLECTION) { + VALUE = COLLECTION[KEY]; + BODY + RESULTS[KEY] = LOGIC; + } +`; + + const mapRightTemplate = template` + const LENGTH = COLLECTION.length; + let KEY = LENGTH; + const RESULTS = Array(LENGTH); + for (let VALUE; --KEY >= 0;) { + VALUE = COLLECTION[KEY]; + BODY + RESULTS[LENGTH - KEY - 1] = LOGIC; + } +`; + + const reduceTemplate = template` + let ACCUMULATED = INITIAL; + for (let KEY = START, LENGTH = COLLECTION.length, VALUE; KEY < LENGTH; ++KEY) { + VALUE = COLLECTION[KEY]; + BODY + ACCUMULATED = LOGIC; + } +`; + + const reduceObjectTemplate = template` + let SKIP = SHOULD_SKIP, + ACCUMULATED = INITIAL, + VALUE; + for (const KEY in COLLECTION) { + VALUE = COLLECTION[KEY]; + + if (SKIP) { + ACCUMULATED = VALUE; + SKIP = false; + continue; + } + + BODY + ACCUMULATED = LOGIC; + } +`; + + const reduceRightTemplate = template` + let ACCUMULATED = INITIAL; + for (let KEY = COLLECTION.length - START, VALUE; --KEY >= START;) { + VALUE = COLLECTION[KEY]; + BODY + ACCUMULATED = LOGIC; + } +`; + + const someTemplate = template` + let DETERMINATION = false; + for (let KEY = 0, LENGTH = COLLECTION.length, VALUE, RESULT; KEY < LENGTH; ++KEY) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + DETERMINATION = true; + break; + } + } +`; - if (aContainer.arguments) { - const [iterableA] = aContainer.arguments; + const someObjectTemplate = template` + let DETERMINATION = false, + VALUE, + RESULT; + for (const KEY in COLLECTION) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + DETERMINATION = true; + break; + } + } +`; + + const someRightTemplate = template` + const DETERMINATION = false; + for (let KEY = COLLECTION.length, VALUE, RESULT; --KEY >= 0;) { + VALUE = COLLECTION[KEY]; + BODY + RESULT = LOGIC; + + if (RESULT) { + DETERMINATION = true; + break; + } + } +`; + + const stripReturnTraverseConfig = { + ReturnStatement(returnPath, returnState = {}) { + const arg = returnPath.get('argument'); + + if (returnState.isForEach) { + if (arg.node) { + const statement = arg.isExpression() + ? t.expressionStatement(arg.node) + : arg.node; + + returnPath.insertBefore(statement); + } + + returnPath.replaceWith(t.continueStatement()); + } else { + returnPath.remove(); + } + }, + }; + + const bodyTraverseConfig = { + ArrayPattern(arrayPatternPath) { + arrayPatternPath.get('elements').forEach((element) => { + if (element.isIdentifier()) { + rename(element); + } + }); + }, + ObjectPattern(objectPatternPath) { + objectPatternPath.get('properties').forEach((property) => { + const value = property.get('value'); + + if (value.isIdentifier()) { + rename(value); + } + }); + }, + ReturnStatement(returnPath, returnState) { + const statementParent = returnPath.parentPath.getStatementParent(); if ( - t.isCallExpression(iterableA) && - iterableA.callee.__inlineLoopsMacro && - iterableA.callee === b.node + statementParent.isIfStatement() || + statementParent.isSwitchStatement() ) { - return 1; + // If conditional returns exist, bail out inlining return statement. + returnState.value = null; + returnState.count = Infinity; + return; + } + + returnState.value = returnState.count + ? null + : returnPath.get('argument').node; + returnState.count++; + }, + ThisExpression(thisPath, thisState) { + thisState.containsThis = true; + }, + VariableDeclarator(variablePath) { + const id = variablePath.get('id'); + + rename(id); + }, + }; + + const handlers = { + every: createHandleEverySome('every-left'), + everyObject: createHandleEverySome('every-object'), + everyRight: createHandleEverySome('every-right'), + filter: createHandleMapFilterForEach('filter-left'), + filterObject: createHandleMapFilterForEach('filter-object'), + filterRight: createHandleMapFilterForEach('filter-right'), + forEach: createHandleMapFilterForEach('for-each-left'), + forEachObject: createHandleMapFilterForEach('for-each-object'), + forEachRight: createHandleMapFilterForEach('for-each-right'), + find: createHandleFind('find-left'), + findIndex: createHandleFind('find-index'), + findKey: createHandleFind('find-key'), + findLast: createHandleFind('find-last'), + findLastIndex: createHandleFind('find-last-index'), + findObject: createHandleFind('find-object'), + flatMap: createHandleMapFilterForEach('flat-map-left'), + flatMapRight: createHandleMapFilterForEach('flat-map-right'), + map: createHandleMapFilterForEach('map-left'), + mapObject: createHandleMapFilterForEach('map-object'), + mapRight: createHandleMapFilterForEach('map-right'), + reduce: createHandleReduce('left'), + reduceObject: createHandleReduce('object'), + reduceRight: createHandleReduce('right'), + some: createHandleEverySome('some-left'), + someObject: createHandleEverySome('some-object'), + someRight: createHandleEverySome('some-right'), + }; + + function getImportedHandlerName(callee) { + const binding = callee.scope.getBinding(callee.node.name); + + if (!binding) { + return; + } + + const owner = binding.path; + + if (owner.isImportSpecifier()) { + const imported = owner.get('imported'); + const name = imported.node.name; + + if (!Object.keys(handlers).some((handler) => handler === name)) { + return; } + + const importSource = owner.parentPath.get('source.value'); + + if (!importSource.node.endsWith('inline-loops.macro')) { + return; + } + + return name; } - if (bContainer.arguments) { - const [iterableB] = bContainer.arguments; + if (owner.isVariableDeclarator()) { + const init = owner.get('init'); if ( - t.isCallExpression(iterableB) && - iterableB.callee.__inlineLoopsMacro && - iterableB.callee === a.node + !init.isCallExpression() || + !init.get('callee').isIdentifier({ name: 'require' }) ) { - return -1; + return; + } + + const requireSource = init.get('arguments.0.value'); + + if (!requireSource.node.endsWith('inline-loops.macro')) { + return; + } + + const imported = owner + .get('id.properties') + .find((property) => + property.get('value').isIdentifier({ name: callee.node.name }), + ); + + if (!imported) { + return; } + + const name = imported.get('key').node.name; + + if (!Object.keys(handlers).some((handler) => handler === name)) { + return; + } + + return name; } + } + + function getInjectedBodyAndLogic({ + callback, + isForEach, + isReduce, + local, + path, + statement, + }) { + const body = callback.get('body'); + const traverseState = { + containsThis: false, + count: 0, + isForEach, + value: null, + }; + + body.traverse(bodyTraverseConfig, traverseState); + + const { + containsThis: callbackContainsThis, + count: returnCount, + value: returnValue, + } = traverseState; + + if (body.isBlockStatement()) { + if (!callbackContainsThis) { + if (returnCount < 2) { + body.traverse(stripReturnTraverseConfig, { isForEach }); + } + + if (returnCount === 0) { + return { + injectedBody: body.node.body, + logic: t.identifier('undefined'), + }; + } + + if (returnCount === 1) { + return { + injectedBody: body.node.body, + logic: returnValue, + }; + } + } + + const localFnName = path.scope.generateUidIdentifier('fn'); + const localFn = localVariableTemplate({ + LOCAL: localFnName, + VALUE: callback.node, + }); - const aStart = a.node.loc.start; - const bStart = b.node.loc.start; + statement.insertBefore(localFn); + + const logic = t.callExpression( + localFnName, + getCachedFnArgs(local, isReduce), + ); - if (bStart.line > aStart.line) { - return -1; + return { + injectedBody: [], + logic, + }; } - if (aStart.line > bStart.line) { - return 1; + if (callback.isFunction()) { + return isForEach + ? { injectedBody: body.node, logic: undefined } + : { injectedBody: [], logic: body.node }; } - if (bStart.column > aStart.column) { - return -1; + if (isForEach) { + const injectedBody = [ + t.expressionStatement( + t.callExpression(callback.node, getCachedFnArgs(local, isReduce)), + ), + ]; + + return { injectedBody, logic: undefined }; } - return 1; - }); + const logic = t.callExpression( + callback.node, + getCachedFnArgs(local, isReduce), + ); - const handlers = { - every: handleEvery, - filter: handleFilter, - find: handleFind, - findIndex: handleFindKey, - findKey: handleFindKey, - flatMap: handleFlatMap, - forEach: handleForEach, - map: handleMap, - reduce: handleReduce, - some: handleSome, - }; + return { injectedBody: [], logic }; + } - function createTransformer(name, transform, isDecrementing, isObject) { - return function _transform(path) { - if (path.findParent((_path) => _path.isConditionalExpression())) { - throw new MacroError( - `You cannot use ${name} in a conditional expression.`, - ); + function getCachedFnArgs(local, isReduce) { + return isReduce + ? [local.accumulated, local.value, local.key, local.collection] + : [local.value, local.key, local.collection]; + } + + function getLocalName(path, name = path.node.name) { + return path.scope.generateUidIdentifier(name); + } + + function getLocalReferences(path, statement, isReduce) { + const args = path.get('arguments'); + + const [collection, callback] = args; + + let localCollection = collection.node; + + if (!collection.isIdentifier()) { + localCollection = getLocalName(path, 'collection'); + + const localVariable = localVariableTemplate({ + LOCAL: localCollection, + VALUE: collection.node, + }); + + statement.insertBefore(localVariable); + } + + let accumulated; + let value; + let key; + let scopedCollection; + + let localDestructuredRefName; + + if (callback.isFunction()) { + if (isReduce) { + [accumulated, value, key, scopedCollection] = callback.get('params'); + } else { + [value, key, scopedCollection] = callback.get('params'); } - const args = path.parent.arguments; - if (args.some((arg) => t.isSpreadElement(arg))) { - throw new MacroError( - 'You cannot use spread arguments with the macro, please declare the arguments explicitly.', - ); + if (value && (value.isArrayPattern() || value.isObjectPattern())) { + localDestructuredRefName = + path.scope.generateUidIdentifier('destructured'); + + const localVariable = localVariableTemplate({ + LOCAL: value.node, + VALUE: localDestructuredRefName, + }); + + const body = callback.get('body'); + + if (!body.isBlockStatement()) { + body.replaceWith(t.blockStatement([t.returnStatement(body.node)])); + } + + body.unshiftContainer('body', localVariable); + value.replaceWith(localDestructuredRefName); } + } + + const localAccumulated = accumulated + ? getLocalName(accumulated) + : getLocalName(path, 'accumulated'); + const localKey = key ? getLocalName(key) : getLocalName(path, 'key'); + const localLength = getLocalName(path, 'length'); + + let localValue; + + if (localDestructuredRefName) { + localValue = localDestructuredRefName; + } else { + localValue = value ? getLocalName(value) : getLocalName(path, 'value'); + } + + if (accumulated) { + rename(accumulated, localAccumulated.name); + } + + if (value) { + rename(value, localValue.name); + } + + if (key) { + rename(key, localKey.name); + } + + if (scopedCollection) { + rename(scopedCollection, localCollection.name); + } + + return { + accumulated: localAccumulated, + collection: localCollection, + key: localKey, + length: localLength, + value: localValue, + }; + } + + function handleInvalidUsage(path) { + const [collection, callback] = path.get('arguments'); - const [object, handler, initialValue] = args; - const isHandlerMacro = allMethods.find( - ({ path: methodPath }) => - methodPath.node !== path.node && handler === methodPath.node, + if (collection.isSpreadElement()) { + throw new MacroError( + 'You cannot use spread arguments with `inline-loops.macro`; please declare the arguments explicitly.', ); + } - if (isHandlerMacro) { - throw new MacroError( - 'You cannot use the macro directly as a handler, please wrap it in a function call.', - ); + const importedHandlerName = getImportedHandlerName(callback); + + if (importedHandlerName) { + throw new MacroError( + 'You cannot use a method from `inline-loops.macro` directly as a handler; please wrap it in a function call.', + ); + } + } + + function processNestedInlineLoopMacros(path) { + if (!path.isCallExpression()) { + return; + } + + const callee = path.get('callee'); + + if (!callee.isIdentifier()) { + return; + } + + const importedHandlerName = getImportedHandlerName(callee); + + if (importedHandlerName) { + handlers[importedHandlerName](path.get('callee')); + } + } + + function rename(path, newName) { + path.scope.rename(path.node.name, newName); + } + + function replaceOrRemove(path, replacement) { + const parentPath = path.parentPath; + + if (parentPath.isExpressionStatement()) { + path.remove(); + } else { + path.replaceWith(replacement); + } + } + + function createHandleEverySome(type) { + return function handleFilter(referencePath) { + const path = referencePath.parentPath; + + if (!path.isCallExpression()) { + return; } - transform({ - t, + handleInvalidUsage(path); + + const [collection, callback] = path.get('arguments'); + + processNestedInlineLoopMacros(collection); + + const statement = path.getStatementParent(); + const local = getLocalReferences(path, statement); + const localResults = getLocalName(path, 'results'); + + const { injectedBody, logic } = getInjectedBodyAndLogic({ + callback, + local, path, - object, - handler, - initialValue, - isDecrementing, - isObject, + statement, }); + + const result = path.scope.generateUidIdentifier('result'); + const determination = path.scope.generateUidIdentifier('determination'); + + let forLoop; + + switch (type) { + case 'every-left': + forLoop = everyTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + DETERMINATION: determination, + KEY: local.key, + LENGTH: local.length, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'some-left': + forLoop = someTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + DETERMINATION: determination, + KEY: local.key, + LENGTH: local.length, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'every-right': + forLoop = everyRightTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + DETERMINATION: determination, + KEY: local.key, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'some-right': + forLoop = someRightTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + DETERMINATION: determination, + KEY: local.key, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'every-object': + forLoop = everyObjectTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + DETERMINATION: determination, + KEY: local.key, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'some-object': + forLoop = someObjectTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + DETERMINATION: determination, + KEY: local.key, + LOGIC: logic, + VALUE: local.value, + }); + break; + + default: + throw new MacroError(`Invalid type ${type} provided`); + } + + statement.insertBefore(forLoop); + + replaceOrRemove(path, determination); }; } - allMethods.forEach(({ method, path, sourceMethod, type }) => { - const isDecrementing = type === 'decrementing'; - const isObject = type === 'object'; + function createHandleFind(type) { + return function handleFilter(referencePath) { + const path = referencePath.parentPath; - const handler = createTransformer( - method, - handlers[sourceMethod], - isDecrementing, - isObject, - ); + if (!path.isCallExpression()) { + return; + } + + handleInvalidUsage(path); + + const [collection, callback] = path.get('arguments'); + + processNestedInlineLoopMacros(collection); + + const statement = path.getStatementParent(); + const local = getLocalReferences(path, statement); + const localResults = getLocalName(path, 'results'); + + const { injectedBody, logic } = getInjectedBodyAndLogic({ + callback, + local, + path, + statement, + }); + + const result = path.scope.generateUidIdentifier('result'); + const match = path.scope.generateUidIdentifier('match'); + + let forLoop; + + switch (type) { + case 'find-left': + forLoop = findTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + MATCH: match, + KEY: local.key, + LENGTH: local.length, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'find-index': + forLoop = findIndexTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + MATCH: match, + KEY: local.key, + LENGTH: local.length, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'find-last': + forLoop = findRightTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + MATCH: match, + KEY: local.key, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'find-last-index': + forLoop = findIndexRightTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + MATCH: match, + KEY: local.key, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'find-object': + forLoop = findObjectTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + MATCH: match, + KEY: local.key, + LOGIC: logic, + VALUE: local.value, + }); + break; + + case 'find-key': + forLoop = findKeyTemplate({ + BODY: injectedBody, + RESULT: result, + COLLECTION: local.collection, + MATCH: match, + KEY: local.key, + LOGIC: logic, + VALUE: local.value, + }); + break; + + default: + throw new MacroError(`Invalid type ${type} provided`); + } + + statement.insertBefore(forLoop); + + replaceOrRemove(path, match); + }; + } + + function createHandleMapFilterForEach(type) { + return function handleMap(referencePath) { + const path = referencePath.parentPath; + + if (!path.isCallExpression()) { + return; + } + + handleInvalidUsage(path); + + const [collection, callback] = path.get('arguments'); + + processNestedInlineLoopMacros(collection); + + const statement = path.getStatementParent(); + const local = getLocalReferences(path, statement); + const localResults = getLocalName(path, 'results'); + const isForEach = type.includes('for-each'); + const result = path.scope.generateUidIdentifier('result'); + + const { injectedBody, logic } = getInjectedBodyAndLogic({ + callback, + isForEach, + local, + path, + statement, + }); + + let forLoop; + + switch (type) { + case 'map-left': + forLoop = mapTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + LENGTH: local.length, + LOGIC: logic, + RESULTS: localResults, + VALUE: local.value, + }); + break; + + case 'filter-left': + forLoop = filterTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + LENGTH: local.length, + LOGIC: logic, + RESULT: result, + RESULTS: localResults, + VALUE: local.value, + }); + break; + + case 'flat-map-left': + forLoop = flatMapTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + LENGTH: local.length, + LOGIC: logic, + RESULT: result, + RESULTS: localResults, + VALUE: local.value, + }); + break; + + case 'for-each-left': + forLoop = forEachTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + LENGTH: local.length, + VALUE: local.value, + }); + break; + + case 'map-right': + forLoop = mapRightTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + LENGTH: local.length, + LOGIC: logic, + RESULTS: localResults, + VALUE: local.value, + }); + break; + + case 'filter-right': + forLoop = filterRightTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + LOGIC: logic, + RESULT: result, + RESULTS: localResults, + VALUE: local.value, + }); + break; + + case 'flat-map-right': + forLoop = flatMapRightTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + LOGIC: logic, + RESULT: result, + RESULTS: localResults, + VALUE: local.value, + }); + break; + + case 'for-each-right': + forLoop = forEachRightTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + VALUE: local.value, + }); + break; + + case 'map-object': + forLoop = mapObjectTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + LOGIC: logic, + RESULT: result, + RESULTS: localResults, + VALUE: local.value, + }); + break; + + case 'filter-object': + forLoop = filterObjectTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + LOGIC: logic, + RESULT: result, + RESULTS: localResults, + VALUE: local.value, + }); + break; + + case 'for-each-object': + forLoop = forEachObjectTemplate({ + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + VALUE: local.value, + }); + break; + } + + statement.insertBefore(forLoop); + + replaceOrRemove( + path, + isForEach ? t.identifier('undefined') : localResults, + ); + }; + } + + function createHandleReduce(type) { + return function handleReduce(referencePath) { + const path = referencePath.parentPath; + + if (!path.isCallExpression()) { + return; + } + + handleInvalidUsage(path); + + const [collection, callback, initialValue] = path.get('arguments'); + + processNestedInlineLoopMacros(collection); + + const statement = path.getStatementParent(); + const local = getLocalReferences(path, statement, true); + const localResults = getLocalName(path, 'results'); + + const { injectedBody, logic } = getInjectedBodyAndLogic({ + callback, + isReduce: true, + local, + path, + statement, + }); + + let initial; + + if (type === 'right') { + initial = initialValue + ? initialValue.node + : nthLastItemTemplate({ + COLLECTION: local.collection, + COUNT: t.numericLiteral(1), + }).expression; + } else { + initial = initialValue + ? initialValue.node + : t.memberExpression(local.collection, t.numericLiteral(0), true); + } + + const start = t.numericLiteral(initialValue ? 0 : 1); + + let forLoop; + + switch (type) { + case 'left': + forLoop = reduceTemplate({ + ACCUMULATED: local.accumulated, + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + INITIAL: initial, + LENGTH: local.length, + LOGIC: logic, + START: start, + VALUE: local.value, + }); + break; + + case 'right': + forLoop = reduceRightTemplate({ + ACCUMULATED: local.accumulated, + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + INITIAL: initial, + LOGIC: logic, + START: start, + VALUE: local.value, + }); + break; + + case 'object': + const skip = path.scope.generateUidIdentifier('skip'); + const shouldSkip = t.booleanLiteral(!initialValue); + + forLoop = reduceObjectTemplate({ + ACCUMULATED: local.accumulated, + BODY: injectedBody, + COLLECTION: local.collection, + KEY: local.key, + INITIAL: initialValue ? initial : t.identifier('undefined'), + LOGIC: logic, + SKIP: skip, + SHOULD_SKIP: shouldSkip, + VALUE: local.value, + }); + break; + } + + statement.insertBefore(forLoop); + + replaceOrRemove(path, local.accumulated); + }; + } - handler(path); - }); + every.forEach(handlers.every); + everyObject.forEach(handlers.everyObject); + everyRight.forEach(handlers.everyRight); + filter.forEach(handlers.filter); + filterObject.forEach(handlers.filterObject); + filterRight.forEach(handlers.filterRight); + forEach.forEach(handlers.forEach); + forEachObject.forEach(handlers.forEachObject); + forEachRight.forEach(handlers.forEachRight); + find.forEach(handlers.find); + findObject.forEach(handlers.findObject); + findLast.forEach(handlers.findLast); + findIndex.forEach(handlers.findIndex); + findKey.forEach(handlers.findKey); + findLastIndex.forEach(handlers.findLastIndex); + flatMap.forEach(handlers.flatMap); + flatMapRight.forEach(handlers.flatMapRight); + map.forEach(handlers.map); + mapObject.forEach(handlers.mapObject); + mapRight.forEach(handlers.mapRight); + reduce.forEach(handlers.reduce); + reduceObject.forEach(handlers.reduceObject); + reduceRight.forEach(handlers.reduceRight); + some.forEach(handlers.some); + someObject.forEach(handlers.someObject); + someRight.forEach(handlers.someRight); } -module.exports = createMacro(inlineLoops); +module.exports = createMacro(myMacro);