From 6fb763e42a7db9e76b8f4189a558a9e30bed8d6b Mon Sep 17 00:00:00 2001 From: planttheidea Date: Sat, 15 Jun 2019 09:26:40 -0700 Subject: [PATCH 1/4] add flatmap handler - Arrays only - Transform fixtures - Runtime fixtures tbd --- __tests__/__fixtures__/cached/flatMap/code.js | 3 + .../__fixtures__/cached/flatMap/output.js | 9 +++ .../__fixtures__/cached/flatMapRight/code.js | 3 + .../cached/flatMapRight/output.js | 9 +++ .../inlined-arrow-expression/flatMap/code.js | 3 + .../flatMap/output.js | 9 +++ .../flatMapRight/code.js | 3 + .../flatMapRight/output.js | 9 +++ .../inlined-arrow-return/flatMap/code.js | 5 ++ .../inlined-arrow-return/flatMap/output.js | 9 +++ .../inlined-arrow-return/flatMapRight/code.js | 5 ++ .../flatMapRight/output.js | 9 +++ .../inlined-function-return/flatMap/code.js | 5 ++ .../inlined-function-return/flatMap/output.js | 9 +++ .../flatMapRight/code.js | 5 ++ .../flatMapRight/output.js | 9 +++ .../__fixtures__/uncached/flatMap/code.js | 7 +++ .../__fixtures__/uncached/flatMap/output.js | 16 ++++++ .../uncached/flatMapRight/code.js | 7 +++ .../uncached/flatMapRight/output.js | 16 ++++++ src/handlers.js | 55 +++++++++++++++++++ src/inline-loops.macro.js | 5 +- 22 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 __tests__/__fixtures__/cached/flatMap/code.js create mode 100644 __tests__/__fixtures__/cached/flatMap/output.js create mode 100644 __tests__/__fixtures__/cached/flatMapRight/code.js create mode 100644 __tests__/__fixtures__/cached/flatMapRight/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/flatMap/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/flatMap/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/flatMapRight/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/flatMapRight/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/flatMap/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/flatMap/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/flatMapRight/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/flatMapRight/output.js create mode 100644 __tests__/__fixtures__/inlined-function-return/flatMap/code.js create mode 100644 __tests__/__fixtures__/inlined-function-return/flatMap/output.js create mode 100644 __tests__/__fixtures__/inlined-function-return/flatMapRight/code.js create mode 100644 __tests__/__fixtures__/inlined-function-return/flatMapRight/output.js create mode 100644 __tests__/__fixtures__/uncached/flatMap/code.js create mode 100644 __tests__/__fixtures__/uncached/flatMap/output.js create mode 100644 __tests__/__fixtures__/uncached/flatMapRight/code.js create mode 100644 __tests__/__fixtures__/uncached/flatMapRight/output.js diff --git a/__tests__/__fixtures__/cached/flatMap/code.js b/__tests__/__fixtures__/cached/flatMap/code.js new file mode 100644 index 00000000..6379f267 --- /dev/null +++ b/__tests__/__fixtures__/cached/flatMap/code.js @@ -0,0 +1,3 @@ +import { flatMap } from '../../../../src/inline-loops.macro'; + +const flattened = flatMap(array, fn); diff --git a/__tests__/__fixtures__/cached/flatMap/output.js b/__tests__/__fixtures__/cached/flatMap/output.js new file mode 100644 index 00000000..b006a5a7 --- /dev/null +++ b/__tests__/__fixtures__/cached/flatMap/output.js @@ -0,0 +1,9 @@ +let _result = []; + +for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + + _result.push.apply(_result, fn(_value, _key, array)); +} + +const flattened = _result; diff --git a/__tests__/__fixtures__/cached/flatMapRight/code.js b/__tests__/__fixtures__/cached/flatMapRight/code.js new file mode 100644 index 00000000..567417cd --- /dev/null +++ b/__tests__/__fixtures__/cached/flatMapRight/code.js @@ -0,0 +1,3 @@ +import { flatMapRight } from '../../../../src/inline-loops.macro'; + +const flattened = flatMapRight(array, fn); diff --git a/__tests__/__fixtures__/cached/flatMapRight/output.js b/__tests__/__fixtures__/cached/flatMapRight/output.js new file mode 100644 index 00000000..a1102585 --- /dev/null +++ b/__tests__/__fixtures__/cached/flatMapRight/output.js @@ -0,0 +1,9 @@ +let _result = []; + +for (let _key = array.length - 1, _value; _key >= 0; --_key) { + _value = array[_key]; + + _result.push.apply(_result, fn(_value, _key, array)); +} + +const flattened = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-expression/flatMap/code.js b/__tests__/__fixtures__/inlined-arrow-expression/flatMap/code.js new file mode 100644 index 00000000..dce61ed6 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/flatMap/code.js @@ -0,0 +1,3 @@ +import { flatMap } from '../../../../src/inline-loops.macro'; + +const flattened = flatMap(array, entry => [entry[0]]); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/flatMap/output.js b/__tests__/__fixtures__/inlined-arrow-expression/flatMap/output.js new file mode 100644 index 00000000..e0a7dd33 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/flatMap/output.js @@ -0,0 +1,9 @@ +let _result = []; + +for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + + _result.push.apply(_result, [_value[0]]); +} + +const flattened = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/code.js b/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/code.js new file mode 100644 index 00000000..b7cdb692 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/code.js @@ -0,0 +1,3 @@ +import { flatMapRight } from '../../../../src/inline-loops.macro'; + +const flattened = flatMapRight(array, entry => [entry[0]]); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/output.js new file mode 100644 index 00000000..99ceb2ef --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/flatMapRight/output.js @@ -0,0 +1,9 @@ +let _result = []; + +for (let _key = array.length - 1, _value; _key >= 0; --_key) { + _value = array[_key]; + + _result.push.apply(_result, [_value[0]]); +} + +const flattened = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-return/flatMap/code.js b/__tests__/__fixtures__/inlined-arrow-return/flatMap/code.js new file mode 100644 index 00000000..7fa2ea88 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/flatMap/code.js @@ -0,0 +1,5 @@ +import { flatMap } from '../../../../src/inline-loops.macro'; + +const flattened = flatMap(array, entry => { + return [entry[0]]; +}); diff --git a/__tests__/__fixtures__/inlined-arrow-return/flatMap/output.js b/__tests__/__fixtures__/inlined-arrow-return/flatMap/output.js new file mode 100644 index 00000000..e0a7dd33 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/flatMap/output.js @@ -0,0 +1,9 @@ +let _result = []; + +for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + + _result.push.apply(_result, [_value[0]]); +} + +const flattened = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/code.js b/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/code.js new file mode 100644 index 00000000..0359a632 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/code.js @@ -0,0 +1,5 @@ +import { flatMapRight } from '../../../../src/inline-loops.macro'; + +const flattened = flatMapRight(array, entry => { + return [entry[0]]; +}); diff --git a/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/output.js new file mode 100644 index 00000000..99ceb2ef --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/flatMapRight/output.js @@ -0,0 +1,9 @@ +let _result = []; + +for (let _key = array.length - 1, _value; _key >= 0; --_key) { + _value = array[_key]; + + _result.push.apply(_result, [_value[0]]); +} + +const flattened = _result; diff --git a/__tests__/__fixtures__/inlined-function-return/flatMap/code.js b/__tests__/__fixtures__/inlined-function-return/flatMap/code.js new file mode 100644 index 00000000..bce3b433 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/flatMap/code.js @@ -0,0 +1,5 @@ +import { flatMap } from '../../../../src/inline-loops.macro'; + +const flattened = flatMap(array, function(entry) { + return [entry[0]]; +}); diff --git a/__tests__/__fixtures__/inlined-function-return/flatMap/output.js b/__tests__/__fixtures__/inlined-function-return/flatMap/output.js new file mode 100644 index 00000000..e0a7dd33 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/flatMap/output.js @@ -0,0 +1,9 @@ +let _result = []; + +for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + + _result.push.apply(_result, [_value[0]]); +} + +const flattened = _result; diff --git a/__tests__/__fixtures__/inlined-function-return/flatMapRight/code.js b/__tests__/__fixtures__/inlined-function-return/flatMapRight/code.js new file mode 100644 index 00000000..86f770f8 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/flatMapRight/code.js @@ -0,0 +1,5 @@ +import { flatMapRight } from '../../../../src/inline-loops.macro'; + +const flattened = flatMapRight(array, function(entry) { + return [entry[0]]; +}); diff --git a/__tests__/__fixtures__/inlined-function-return/flatMapRight/output.js b/__tests__/__fixtures__/inlined-function-return/flatMapRight/output.js new file mode 100644 index 00000000..99ceb2ef --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/flatMapRight/output.js @@ -0,0 +1,9 @@ +let _result = []; + +for (let _key = array.length - 1, _value; _key >= 0; --_key) { + _value = array[_key]; + + _result.push.apply(_result, [_value[0]]); +} + +const flattened = _result; diff --git a/__tests__/__fixtures__/uncached/flatMap/code.js b/__tests__/__fixtures__/uncached/flatMap/code.js new file mode 100644 index 00000000..dec48d46 --- /dev/null +++ b/__tests__/__fixtures__/uncached/flatMap/code.js @@ -0,0 +1,7 @@ +import { flatMap } from '../../../../src/inline-loops.macro'; + +const flattened = flatMap([['foo', 'bar'], ['bar', 'baz']], (entry) => { + const [first] = entry; + + return [first]; +}); diff --git a/__tests__/__fixtures__/uncached/flatMap/output.js b/__tests__/__fixtures__/uncached/flatMap/output.js new file mode 100644 index 00000000..739c8908 --- /dev/null +++ b/__tests__/__fixtures__/uncached/flatMap/output.js @@ -0,0 +1,16 @@ +const _iterable = [['foo', 'bar'], ['bar', 'baz']]; + +const _fn = entry => { + const [first] = entry; + return [first]; +}; + +let _result = []; + +for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { + _value = _iterable[_key]; + + _result.push.apply(_result, _fn(_value, _key, _iterable)); +} + +const flattened = _result; diff --git a/__tests__/__fixtures__/uncached/flatMapRight/code.js b/__tests__/__fixtures__/uncached/flatMapRight/code.js new file mode 100644 index 00000000..1152d932 --- /dev/null +++ b/__tests__/__fixtures__/uncached/flatMapRight/code.js @@ -0,0 +1,7 @@ +import { flatMapRight } from '../../../../src/inline-loops.macro'; + +const flattened = flatMapRight([['foo', 'bar'], ['bar', 'baz']], (entry) => { + const [first] = entry; + + return [first]; +}); diff --git a/__tests__/__fixtures__/uncached/flatMapRight/output.js b/__tests__/__fixtures__/uncached/flatMapRight/output.js new file mode 100644 index 00000000..b45b99b2 --- /dev/null +++ b/__tests__/__fixtures__/uncached/flatMapRight/output.js @@ -0,0 +1,16 @@ +const _iterable = [['foo', 'bar'], ['bar', 'baz']]; + +const _fn = entry => { + const [first] = entry; + return [first]; +}; + +let _result = []; + +for (let _key = _iterable.length - 1, _value; _key >= 0; --_key) { + _value = _iterable[_key]; + + _result.push.apply(_result, _fn(_value, _key, _iterable)); +} + +const flattened = _result; diff --git a/src/handlers.js b/src/handlers.js index 28cbc842..30ad17bc 100644 --- a/src/handlers.js +++ b/src/handlers.js @@ -226,6 +226,60 @@ function handleFindKey({ path.parentPath.replaceWith(result); } +function handleFlatMap({ + t, path, object, handler, isDecrementing, isObject, +}) { + const { + fn, iterable, key, length, result, value, + } = getIds(path.scope); + + const isHandlerCached = isCachedReference(t, handler); + const isIterableCached = isCachedReference(t, object); + + const fnUsed = isHandlerCached ? handler : fn; + const iterableUsed = isIterableCached ? object : iterable; + + const valueAssignment = t.expressionStatement( + t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true)), + ); + const resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + const expr = t.expressionStatement( + t.callExpression( + t.memberExpression(t.memberExpression(result, t.identifier('push')), t.identifier('apply')), + [result, resultStatement], + ), + ); + + const loop = getLoop({ + t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key, + length, + isDecrementing, + isObject, + scope: path.scope, + value, + }); + + insertBeforeParent({ + fn, + handler, + isObject, + iterable, + loop, + object, + path, + result, + resultStatement, + resultValue: getDefaultResult(t, isObject), + t, + value, + }); + + path.parentPath.replaceWith(result); +} + function handleForEach({ t, path, object, handler, isDecrementing, isObject, }) { @@ -524,6 +578,7 @@ module.exports = { handleFilter, handleFind, handleFindKey, + handleFlatMap, handleForEach, handleMap, handleReduce, diff --git a/src/inline-loops.macro.js b/src/inline-loops.macro.js index 1d951179..b4937976 100644 --- a/src/inline-loops.macro.js +++ b/src/inline-loops.macro.js @@ -5,6 +5,7 @@ const { handleFilter, handleFind, handleFindKey, + handleFlatMap, handleForEach, handleMap, handleReduce, @@ -17,12 +18,13 @@ const METHODS = [ 'find', 'findIndex', 'findKey', + 'flatMap', 'forEach', 'map', 'reduce', 'some', ]; -const ARRAY_ONLY_METHODS = ['findIndex']; +const ARRAY_ONLY_METHODS = ['findIndex', 'flatMap']; const OBJECT_ONLY_METHODS = ['findKey']; function getCallTypes(references, method) { @@ -143,6 +145,7 @@ function inlineLoops({ references, babel }) { find: handleFind, findIndex: handleFindKey, findKey: handleFindKey, + flatMap: handleFlatMap, forEach: handleForEach, map: handleMap, reduce: handleReduce, From 24d9e95191f1737b9f27f967b50307574f22d36f Mon Sep 17 00:00:00 2001 From: planttheidea Date: Sat, 15 Jun 2019 09:37:05 -0700 Subject: [PATCH 2/4] add runtime tests and README values for flatMap --- README.md | 25 +++-- __tests__/__runtime__/flatMap.js | 154 +++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 8 deletions(-) create mode 100644 __tests__/__runtime__/flatMap.js diff --git a/README.md b/README.md index 6c21e544..e22266bd 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,9 @@ import { map, reduce, someObject } from 'inline-loops.macro'; function contrivedExample(array) { const doubled = map(array, (value) => value * 2); - const doubleObject = reduce(doubled, (object, value) => ({ - ...object, - [value]: value + const doubleObject = reduce(doubled, (object, value) => ({ + ...object, + [value]: value }, {}); if (someObject(doubleObject, (value) => value > 100)) { @@ -55,6 +55,9 @@ function contrivedExample(array) { - `findIndex` ([MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)) - `findIndexRight` => same as `findIndex`, but iterating in reverse - `findKey` => same as `findIndex` but iterating over objects intead of arrays +- `flatMap` ([MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap)) + - `flatMapRight` => same as `findIndex`, but iterating in reverse + - There is no object method, as the use cases and expected results are not clearly defined, nor is the expected outcome obvious - `forEach` ([MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)) - `forEachRight` => same as `forEach`, but iterating in reverse - `forEachObject` => same as `forEach` but iterating over objects intead of arrays @@ -110,8 +113,8 @@ Notice that there is no reference to the original function, because it used the ```javascript // this -const isAllTuples = every(array, tuple => - every(tuple, (value) => Array.isArray(value) && value.length === 2) +const isAllTuples = every(array, tuple => + every(tuple, value => Array.isArray(value) && value.length === 2), ); // becomes this @@ -122,7 +125,11 @@ for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { let _result2 = true; - for (let _key2 = 0, _length2 = _value.length, _value2; _key2 < _length2; ++_key2) { + for ( + let _key2 = 0, _length2 = _value.length, _value2; + _key2 < _length2; + ++_key2 + ) { _value2 = _value[_key2]; if (!(Array.isArray(_value2) && _value2.length === 2)) { @@ -130,7 +137,7 @@ for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { break; } } - + if (!_result2) { _result = false; break; @@ -172,7 +179,9 @@ If you need to incorporate this, you can do it one of two ways: **Add filtering (iterates twice, but arguably cleaner semantics)** ```javascript -const raw = mapObject(object, (value, key) => object.hasOwnProperty(key) ? value * 2 : null); +const raw = mapObject(object, (value, key) => + object.hasOwnProperty(key) ? value * 2 : null, +); const doubled = filterObject(raw, value => value !== null); ``` diff --git a/__tests__/__runtime__/flatMap.js b/__tests__/__runtime__/flatMap.js new file mode 100644 index 00000000..0744404d --- /dev/null +++ b/__tests__/__runtime__/flatMap.js @@ -0,0 +1,154 @@ +/* eslint-disable */ + +const { flatMap, flatMapRight } = require('../../src/inline-loops.macro'); + +const { deepEqual: isEqual } = require('fast-equals'); + +const ARRAY = [[1], [2], [3], [4], [5], [6]]; + +const withTriples = value => [value, value * 2, value * 3]; + +const BAD_ARRAY_RESULT = [...ARRAY]; +const ARRAY_RESULT = ARRAY.reduce((accumulator, value) => { + const result = withTriples(value); + + accumulator.push(...result); + + return accumulator; +}, []); + +const BAD_DECREMENTING_ARRAY_RESULT = [...ARRAY].reverse(); +const DECREMENTING_ARRAY_RESULT = ARRAY.reduceRight((accumulator, value) => { + const result = withTriples(value); + + accumulator.push(...result); + + return accumulator; +}, []); + +module.exports = { + cached: { + decrementing: { + false: isEqual(flatMapRight(ARRAY, withTriples), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(flatMapRight(ARRAY, withTriples), DECREMENTING_ARRAY_RESULT), + }, + standard: { + false: isEqual(flatMap(ARRAY, withTriples), BAD_ARRAY_RESULT), + true: isEqual(flatMap(ARRAY, withTriples), ARRAY_RESULT), + }, + }, + inlinedArrowExpression: { + decrementing: { + false: isEqual( + flatMapRight(ARRAY, value => [value, value * 2, value * 3]), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + flatMapRight(ARRAY, value => [value, value * 2, value * 3]), + DECREMENTING_ARRAY_RESULT, + ), + }, + standard: { + false: isEqual(flatMap(ARRAY, value => [value, value * 2, value * 3]), BAD_ARRAY_RESULT), + true: isEqual(flatMap(ARRAY, value => [value, value * 2, value * 3]), ARRAY_RESULT), + }, + }, + inlinedArrowReturn: { + decrementing: { + false: isEqual( + flatMapRight(ARRAY, value => { + return [value, value * 2, value * 3]; + }), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + flatMapRight(ARRAY, value => { + return [value, value * 2, value * 3]; + }), + DECREMENTING_ARRAY_RESULT, + ), + }, + standard: { + false: isEqual( + flatMap(ARRAY, value => { + return [value, value * 2, value * 3]; + }), + BAD_ARRAY_RESULT, + ), + true: isEqual( + flatMap(ARRAY, value => { + return [value, value * 2, value * 3]; + }), + ARRAY_RESULT, + ), + }, + }, + inlinedFunctionReturn: { + decrementing: { + false: isEqual( + flatMapRight(ARRAY, function(value) { + return [value, value * 2, value * 3]; + }), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + flatMapRight(ARRAY, function(value) { + return [value, value * 2, value * 3]; + }), + DECREMENTING_ARRAY_RESULT, + ), + }, + standard: { + false: isEqual( + flatMap(ARRAY, function(value) { + return [value, value * 2, value * 3]; + }), + BAD_ARRAY_RESULT, + ), + true: isEqual( + flatMap(ARRAY, function(value) { + return [value, value * 2, value * 3]; + }), + ARRAY_RESULT, + ), + }, + }, + uncached: { + decrementing: { + false: isEqual( + flatMapRight([].concat(ARRAY), value => { + const withTriples = [value, value * 2, value * 3]; + + return withTriples; + }), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + flatMapRight([].concat(ARRAY), value => { + const withTriples = [value, value * 2, value * 3]; + + return withTriples; + }), + DECREMENTING_ARRAY_RESULT, + ), + }, + standard: { + false: isEqual( + flatMap([].concat(ARRAY), value => { + const withTriples = [value, value * 2, value * 3]; + + return withTriples; + }), + BAD_ARRAY_RESULT, + ), + true: isEqual( + flatMap([].concat(ARRAY), value => { + const withTriples = [value, value * 2, value * 3]; + + return withTriples; + }), + ARRAY_RESULT, + ), + }, + }, +}; From 0bbc0fb45cfee8567276ffa7d179921a03a7b203 Mon Sep 17 00:00:00 2001 From: planttheidea Date: Sat, 15 Jun 2019 09:39:28 -0700 Subject: [PATCH 3/4] increase prettier line length for README readability --- README.md | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e22266bd..720354ba 100644 --- a/README.md +++ b/README.md @@ -113,9 +113,7 @@ Notice that there is no reference to the original function, because it used the ```javascript // this -const isAllTuples = every(array, tuple => - every(tuple, value => Array.isArray(value) && value.length === 2), -); +const isAllTuples = every(array, tuple => every(tuple, value => Array.isArray(value) && value.length === 2)); // becomes this let _result = true; @@ -125,11 +123,7 @@ for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { let _result2 = true; - for ( - let _key2 = 0, _length2 = _value.length, _value2; - _key2 < _length2; - ++_key2 - ) { + for (let _key2 = 0, _length2 = _value.length, _value2; _key2 < _length2; ++_key2) { _value2 = _value[_key2]; if (!(Array.isArray(_value2) && _value2.length === 2)) { @@ -179,9 +173,7 @@ If you need to incorporate this, you can do it one of two ways: **Add filtering (iterates twice, but arguably cleaner semantics)** ```javascript -const raw = mapObject(object, (value, key) => - object.hasOwnProperty(key) ? value * 2 : null, -); +const raw = mapObject(object, (value, key) => (object.hasOwnProperty(key) ? value * 2 : null)); const doubled = filterObject(raw, value => value !== null); ``` From b5a45a199aa6c0875c0db4b5130abd5fd38330f9 Mon Sep 17 00:00:00 2001 From: planttheidea Date: Sat, 15 Jun 2019 09:53:46 -0700 Subject: [PATCH 4/4] bailout of aggressive inlining when destructuring params Fixes #4 --- .../{nested => complex}/contrived/code.js | 0 .../{nested => complex}/contrived/output.js | 0 .../{nested => complex}/deep-every/code.js | 0 .../{nested => complex}/deep-every/output.js | 0 .../{nested => complex}/deep-iterable/code.js | 0 .../{nested => complex}/deep-iterable/output.js | 0 .../__fixtures__/{nested => complex}/deep-map/code.js | 0 .../{nested => complex}/deep-map/output.js | 0 .../__fixtures__/complex/destructured-params/code.js | 5 +++++ .../complex/destructured-params/output.js | 11 +++++++++++ .../__fixtures__/{nested => complex}/jsx/code.js | 0 .../__fixtures__/{nested => complex}/jsx/output.js | 0 .../{nested => complex}/shallow-every/code.js | 0 .../{nested => complex}/shallow-every/output.js | 0 .../{nested => complex}/shallow-iterable/code.js | 0 .../{nested => complex}/shallow-iterable/output.js | 0 .../{nested => complex}/shallow-map/code.js | 0 .../{nested => complex}/shallow-map/output.js | 0 __tests__/transform.test.js | 4 ++-- src/helpers.js | 4 ++-- 20 files changed, 20 insertions(+), 4 deletions(-) rename __tests__/__fixtures__/{nested => complex}/contrived/code.js (100%) rename __tests__/__fixtures__/{nested => complex}/contrived/output.js (100%) rename __tests__/__fixtures__/{nested => complex}/deep-every/code.js (100%) rename __tests__/__fixtures__/{nested => complex}/deep-every/output.js (100%) rename __tests__/__fixtures__/{nested => complex}/deep-iterable/code.js (100%) rename __tests__/__fixtures__/{nested => complex}/deep-iterable/output.js (100%) rename __tests__/__fixtures__/{nested => complex}/deep-map/code.js (100%) rename __tests__/__fixtures__/{nested => complex}/deep-map/output.js (100%) create mode 100644 __tests__/__fixtures__/complex/destructured-params/code.js create mode 100644 __tests__/__fixtures__/complex/destructured-params/output.js rename __tests__/__fixtures__/{nested => complex}/jsx/code.js (100%) rename __tests__/__fixtures__/{nested => complex}/jsx/output.js (100%) rename __tests__/__fixtures__/{nested => complex}/shallow-every/code.js (100%) rename __tests__/__fixtures__/{nested => complex}/shallow-every/output.js (100%) rename __tests__/__fixtures__/{nested => complex}/shallow-iterable/code.js (100%) rename __tests__/__fixtures__/{nested => complex}/shallow-iterable/output.js (100%) rename __tests__/__fixtures__/{nested => complex}/shallow-map/code.js (100%) rename __tests__/__fixtures__/{nested => complex}/shallow-map/output.js (100%) diff --git a/__tests__/__fixtures__/nested/contrived/code.js b/__tests__/__fixtures__/complex/contrived/code.js similarity index 100% rename from __tests__/__fixtures__/nested/contrived/code.js rename to __tests__/__fixtures__/complex/contrived/code.js diff --git a/__tests__/__fixtures__/nested/contrived/output.js b/__tests__/__fixtures__/complex/contrived/output.js similarity index 100% rename from __tests__/__fixtures__/nested/contrived/output.js rename to __tests__/__fixtures__/complex/contrived/output.js diff --git a/__tests__/__fixtures__/nested/deep-every/code.js b/__tests__/__fixtures__/complex/deep-every/code.js similarity index 100% rename from __tests__/__fixtures__/nested/deep-every/code.js rename to __tests__/__fixtures__/complex/deep-every/code.js diff --git a/__tests__/__fixtures__/nested/deep-every/output.js b/__tests__/__fixtures__/complex/deep-every/output.js similarity index 100% rename from __tests__/__fixtures__/nested/deep-every/output.js rename to __tests__/__fixtures__/complex/deep-every/output.js diff --git a/__tests__/__fixtures__/nested/deep-iterable/code.js b/__tests__/__fixtures__/complex/deep-iterable/code.js similarity index 100% rename from __tests__/__fixtures__/nested/deep-iterable/code.js rename to __tests__/__fixtures__/complex/deep-iterable/code.js diff --git a/__tests__/__fixtures__/nested/deep-iterable/output.js b/__tests__/__fixtures__/complex/deep-iterable/output.js similarity index 100% rename from __tests__/__fixtures__/nested/deep-iterable/output.js rename to __tests__/__fixtures__/complex/deep-iterable/output.js diff --git a/__tests__/__fixtures__/nested/deep-map/code.js b/__tests__/__fixtures__/complex/deep-map/code.js similarity index 100% rename from __tests__/__fixtures__/nested/deep-map/code.js rename to __tests__/__fixtures__/complex/deep-map/code.js diff --git a/__tests__/__fixtures__/nested/deep-map/output.js b/__tests__/__fixtures__/complex/deep-map/output.js similarity index 100% rename from __tests__/__fixtures__/nested/deep-map/output.js rename to __tests__/__fixtures__/complex/deep-map/output.js diff --git a/__tests__/__fixtures__/complex/destructured-params/code.js b/__tests__/__fixtures__/complex/destructured-params/code.js new file mode 100644 index 00000000..c820b4f3 --- /dev/null +++ b/__tests__/__fixtures__/complex/destructured-params/code.js @@ -0,0 +1,5 @@ +import { forEach } from '../../../../src/inline-loops.macro'; + +forEach([], ([a, b]) => { + console.log(a, b); +}); diff --git a/__tests__/__fixtures__/complex/destructured-params/output.js b/__tests__/__fixtures__/complex/destructured-params/output.js new file mode 100644 index 00000000..c717f4ff --- /dev/null +++ b/__tests__/__fixtures__/complex/destructured-params/output.js @@ -0,0 +1,11 @@ +const _iterable = []; + +const _fn = ([a, b]) => { + console.log(a, b); +}; + +for (let _key = 0, _length = _iterable.length, _value; _key < _length; ++_key) { + _value = _iterable[_key]; + + _fn(_value, _key, _iterable); +} diff --git a/__tests__/__fixtures__/nested/jsx/code.js b/__tests__/__fixtures__/complex/jsx/code.js similarity index 100% rename from __tests__/__fixtures__/nested/jsx/code.js rename to __tests__/__fixtures__/complex/jsx/code.js diff --git a/__tests__/__fixtures__/nested/jsx/output.js b/__tests__/__fixtures__/complex/jsx/output.js similarity index 100% rename from __tests__/__fixtures__/nested/jsx/output.js rename to __tests__/__fixtures__/complex/jsx/output.js diff --git a/__tests__/__fixtures__/nested/shallow-every/code.js b/__tests__/__fixtures__/complex/shallow-every/code.js similarity index 100% rename from __tests__/__fixtures__/nested/shallow-every/code.js rename to __tests__/__fixtures__/complex/shallow-every/code.js diff --git a/__tests__/__fixtures__/nested/shallow-every/output.js b/__tests__/__fixtures__/complex/shallow-every/output.js similarity index 100% rename from __tests__/__fixtures__/nested/shallow-every/output.js rename to __tests__/__fixtures__/complex/shallow-every/output.js diff --git a/__tests__/__fixtures__/nested/shallow-iterable/code.js b/__tests__/__fixtures__/complex/shallow-iterable/code.js similarity index 100% rename from __tests__/__fixtures__/nested/shallow-iterable/code.js rename to __tests__/__fixtures__/complex/shallow-iterable/code.js diff --git a/__tests__/__fixtures__/nested/shallow-iterable/output.js b/__tests__/__fixtures__/complex/shallow-iterable/output.js similarity index 100% rename from __tests__/__fixtures__/nested/shallow-iterable/output.js rename to __tests__/__fixtures__/complex/shallow-iterable/output.js diff --git a/__tests__/__fixtures__/nested/shallow-map/code.js b/__tests__/__fixtures__/complex/shallow-map/code.js similarity index 100% rename from __tests__/__fixtures__/nested/shallow-map/code.js rename to __tests__/__fixtures__/complex/shallow-map/code.js diff --git a/__tests__/__fixtures__/nested/shallow-map/output.js b/__tests__/__fixtures__/complex/shallow-map/output.js similarity index 100% rename from __tests__/__fixtures__/nested/shallow-map/output.js rename to __tests__/__fixtures__/complex/shallow-map/output.js diff --git a/__tests__/transform.test.js b/__tests__/transform.test.js index bf39d601..7e4d0807 100644 --- a/__tests__/transform.test.js +++ b/__tests__/transform.test.js @@ -43,8 +43,8 @@ pluginTester({ pluginTester({ babelOptions: BABEL_OPTIONS, - fixtures: path.join(__dirname, '__fixtures__', 'nested'), + fixtures: path.join(__dirname, '__fixtures__', 'complex'), filename: __filename, plugin, - title: 'Nested references', + title: 'Complex references', }); diff --git a/src/helpers.js b/src/helpers.js index 97ea7efe..1067435c 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -85,7 +85,7 @@ function getReduceResultStatement(t, handler, fn, result, value, key, iterable, // eslint-disable-next-line prefer-destructuring body = body.body; - if (body.length === 1) { + if (body.length === 1 && handler.params.every(param => t.isIdentifier(param))) { const [r, v, k, i] = handler.params; const node = body[0]; @@ -153,7 +153,7 @@ function getResultStatement(t, handler, fn, value, key, iterable, path) { // eslint-disable-next-line prefer-destructuring body = body.body; - if (body.length === 1) { + if (body.length === 1 && handler.params.every(param => t.isIdentifier(param))) { const [v, k, i] = handler.params; const node = body[0];