From 3d174ca498b2567783c2af0d931d826731af9ad5 Mon Sep 17 00:00:00 2001 From: planttheidea Date: Thu, 6 Jun 2019 08:15:42 -0700 Subject: [PATCH 1/6] allow inlining of returns for reduce Also, organize code a bit better, separating src and dist --- .npmrc | 1 + __tests__/__fixtures__/cached/every/code.js | 2 +- .../__fixtures__/cached/everyObject/code.js | 2 +- .../__fixtures__/cached/everyRight/code.js | 2 +- __tests__/__fixtures__/cached/filter/code.js | 2 +- .../__fixtures__/cached/filterObject/code.js | 2 +- .../__fixtures__/cached/filterRight/code.js | 2 +- __tests__/__fixtures__/cached/find/code.js | 2 +- .../__fixtures__/cached/findIndex/code.js | 2 +- .../cached/findIndexRight/code.js | 2 +- __tests__/__fixtures__/cached/findKey/code.js | 2 +- .../__fixtures__/cached/findObject/code.js | 2 +- .../__fixtures__/cached/findRight/code.js | 2 +- __tests__/__fixtures__/cached/forEach/code.js | 2 +- .../__fixtures__/cached/forEachObject/code.js | 2 +- .../__fixtures__/cached/forEachRight/code.js | 2 +- __tests__/__fixtures__/cached/map/code.js | 2 +- .../__fixtures__/cached/mapObject/code.js | 2 +- .../__fixtures__/cached/mapRight/code.js | 2 +- .../cached/reduce-no-initialValue/code.js | 2 +- __tests__/__fixtures__/cached/reduce/code.js | 2 +- .../reduceObject-no-initialValue/code.js | 2 +- .../__fixtures__/cached/reduceObject/code.js | 2 +- .../reduceRight-no-initialValue/code.js | 2 +- .../__fixtures__/cached/reduceRight/code.js | 2 +- __tests__/__fixtures__/cached/some/code.js | 2 +- .../__fixtures__/cached/someObject/code.js | 2 +- .../__fixtures__/cached/someRight/code.js | 2 +- .../inlined-arrow-expression/every/code.js | 2 +- .../everyObject/code.js | 2 +- .../everyRight/code.js | 2 +- .../inlined-arrow-expression/filter/code.js | 2 +- .../filterObject/code.js | 2 +- .../filterRight/code.js | 2 +- .../inlined-arrow-expression/find/code.js | 2 +- .../findIndex/code.js | 2 +- .../findIndexRight/code.js | 2 +- .../inlined-arrow-expression/findKey/code.js | 2 +- .../findObject/code.js | 2 +- .../findRight/code.js | 2 +- .../inlined-arrow-expression/forEach/code.js | 2 +- .../forEachObject/code.js | 2 +- .../forEachRight/code.js | 2 +- .../inlined-arrow-expression/map/code.js | 2 +- .../mapObject/code.js | 2 +- .../inlined-arrow-expression/mapRight/code.js | 2 +- .../reduce-no-initialValue/code.js | 3 + .../reduce-no-initialValue/output.js | 8 + .../inlined-arrow-expression/reduce/code.js | 3 + .../inlined-arrow-expression/reduce/output.js | 8 + .../reduceObject-no-initialValue/code.js | 3 + .../reduceObject-no-initialValue/output.js | 17 + .../reduceObject/code.js | 3 + .../reduceObject/output.js | 10 + .../reduceRight-no-initialValue/code.js | 3 + .../reduceRight-no-initialValue/output.js | 9 + .../reduceRight/code.js | 3 + .../reduceRight/output.js | 8 + .../inlined-arrow-expression/some/code.js | 2 +- .../someObject/code.js | 2 +- .../someRight/code.js | 2 +- .../inlined-arrow-return/every/code.js | 2 +- .../inlined-arrow-return/everyObject/code.js | 2 +- .../inlined-arrow-return/everyRight/code.js | 2 +- .../inlined-arrow-return/filter/code.js | 2 +- .../inlined-arrow-return/filterObject/code.js | 2 +- .../inlined-arrow-return/filterRight/code.js | 2 +- .../inlined-arrow-return/find/code.js | 2 +- .../inlined-arrow-return/findIndex/code.js | 2 +- .../findIndexRight/code.js | 2 +- .../inlined-arrow-return/findKey/code.js | 2 +- .../inlined-arrow-return/findObject/code.js | 2 +- .../inlined-arrow-return/findRight/code.js | 2 +- .../inlined-arrow-return/forEach/code.js | 2 +- .../forEachObject/code.js | 2 +- .../inlined-arrow-return/forEachRight/code.js | 2 +- .../inlined-arrow-return/map/code.js | 2 +- .../inlined-arrow-return/mapObject/code.js | 2 +- .../inlined-arrow-return/mapRight/code.js | 2 +- .../reduce-no-initialValue/code.js | 5 + .../reduce-no-initialValue/output.js | 8 + .../inlined-arrow-return/reduce/code.js | 5 + .../inlined-arrow-return/reduce/output.js | 8 + .../reduceObject-no-initialValue/code.js | 5 + .../reduceObject-no-initialValue/output.js | 17 + .../inlined-arrow-return/reduceObject/code.js | 5 + .../reduceObject/output.js | 10 + .../reduceRight-no-initialValue/code.js | 5 + .../reduceRight-no-initialValue/output.js | 9 + .../inlined-arrow-return/reduceRight/code.js | 5 + .../reduceRight/output.js | 8 + .../inlined-arrow-return/some/code.js | 2 +- .../inlined-arrow-return/someObject/code.js | 2 +- .../inlined-arrow-return/someRight/code.js | 2 +- .../inlined-function-return/every/code.js | 2 +- .../everyObject/code.js | 2 +- .../everyRight/code.js | 2 +- .../inlined-function-return/filter/code.js | 2 +- .../filterObject/code.js | 2 +- .../filterRight/code.js | 2 +- .../inlined-function-return/find/code.js | 2 +- .../inlined-function-return/findIndex/code.js | 2 +- .../findIndexRight/code.js | 2 +- .../inlined-function-return/findKey/code.js | 2 +- .../findObject/code.js | 2 +- .../inlined-function-return/findRight/code.js | 2 +- .../inlined-function-return/forEach/code.js | 2 +- .../forEachObject/code.js | 2 +- .../forEachRight/code.js | 2 +- .../inlined-function-return/map/code.js | 2 +- .../inlined-function-return/mapObject/code.js | 2 +- .../inlined-function-return/mapRight/code.js | 2 +- .../reduce-no-initialValue/code.js | 5 + .../reduce-no-initialValue/output.js | 8 + .../inlined-function-return/reduce/code.js | 5 + .../inlined-function-return/reduce/output.js | 8 + .../reduceObject-no-initialValue/code.js | 5 + .../reduceObject-no-initialValue/output.js | 17 + .../reduceObject/code.js | 5 + .../reduceObject/output.js | 10 + .../reduceRight-no-initialValue/code.js | 5 + .../reduceRight-no-initialValue/output.js | 9 + .../reduceRight/code.js | 5 + .../reduceRight/output.js | 8 + .../inlined-function-return/some/code.js | 2 +- .../someObject/code.js | 2 +- .../inlined-function-return/someRight/code.js | 2 +- .../__fixtures__/nested/contrived/code.js | 2 +- .../__fixtures__/nested/contrived/output.js | 8 +- .../__fixtures__/nested/deep-every/code.js | 2 +- .../__fixtures__/nested/deep-iterable/code.js | 2 +- .../__fixtures__/nested/deep-map/code.js | 2 +- .../__fixtures__/nested/shallow-every/code.js | 2 +- .../nested/shallow-iterable/code.js | 2 +- .../__fixtures__/nested/shallow-map/code.js | 2 +- __tests__/__fixtures__/uncached/every/code.js | 2 +- .../__fixtures__/uncached/everyObject/code.js | 2 +- .../__fixtures__/uncached/everyRight/code.js | 2 +- .../__fixtures__/uncached/filter/code.js | 2 +- .../uncached/filterObject/code.js | 2 +- .../__fixtures__/uncached/filterRight/code.js | 2 +- __tests__/__fixtures__/uncached/find/code.js | 2 +- .../__fixtures__/uncached/findIndex/code.js | 2 +- .../uncached/findIndexRight/code.js | 2 +- .../__fixtures__/uncached/findKey/code.js | 2 +- .../__fixtures__/uncached/findObject/code.js | 2 +- .../__fixtures__/uncached/findRight/code.js | 2 +- .../__fixtures__/uncached/forEach/code.js | 2 +- .../uncached/forEachObject/code.js | 2 +- .../uncached/forEachRight/code.js | 2 +- __tests__/__fixtures__/uncached/map/code.js | 2 +- .../__fixtures__/uncached/mapObject/code.js | 2 +- .../__fixtures__/uncached/mapRight/code.js | 2 +- .../uncached/reduce-no-initialValue/code.js | 2 +- .../__fixtures__/uncached/reduce/code.js | 2 +- .../reduceObject-no-initialValue/code.js | 2 +- .../uncached/reduceObject/code.js | 2 +- .../reduceRight-no-initialValue/code.js | 2 +- .../__fixtures__/uncached/reduceRight/code.js | 2 +- __tests__/__fixtures__/uncached/some/code.js | 2 +- .../__fixtures__/uncached/someObject/code.js | 2 +- .../__fixtures__/uncached/someRight/code.js | 2 +- .../__runtime__/error-macro-as-handler.js | 2 +- .../__runtime__/error-spread-arguments.js | 2 +- __tests__/__runtime__/every.js | 2 +- __tests__/__runtime__/filter.js | 2 +- __tests__/__runtime__/find.js | 2 +- __tests__/__runtime__/findIndex-findKey.js | 2 +- __tests__/__runtime__/forEach.js | 2 +- __tests__/__runtime__/map.js | 2 +- .../__runtime__/reduce-no-initialValue.js | 2 +- __tests__/__runtime__/reduce.js | 2 +- __tests__/__runtime__/some.js | 2 +- build/handlers.js | 475 ++++++++++++ build/helpers.js | 237 ++++++ build/inline-loops.macro.js | 724 +----------------- dist/handlers.js | 476 ++++++++++++ dist/helpers.js | 374 +++++++++ dist/index.d.ts | 123 +++ dist/inline-loops.macro.js | 211 +++++ package.json | 12 +- inline-loops.macro.js => src/handlers.js | 476 +----------- src/helpers.js | 362 +++++++++ src/inline-loops.macro.js | 200 +++++ 184 files changed, 2924 insertions(+), 1283 deletions(-) create mode 100644 .npmrc create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduce/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduce/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduceObject/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduceObject/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduceRight/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-expression/reduceRight/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduce/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduce/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduceObject/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduceObject/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/output.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduceRight/code.js create mode 100644 __tests__/__fixtures__/inlined-arrow-return/reduceRight/output.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/code.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/output.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduce/code.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduce/output.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/code.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/output.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduceObject/code.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduceObject/output.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/code.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/output.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduceRight/code.js create mode 100644 __tests__/__fixtures__/inlined-function-return/reduceRight/output.js create mode 100644 build/handlers.js create mode 100644 build/helpers.js create mode 100644 dist/handlers.js create mode 100644 dist/helpers.js create mode 100644 dist/index.d.ts create mode 100644 dist/inline-loops.macro.js rename inline-loops.macro.js => src/handlers.js (56%) create mode 100644 src/helpers.js create mode 100644 src/inline-loops.macro.js diff --git a/.npmrc b/.npmrc new file mode 100644 index 00000000..a21347f1 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +scripts-prepend-node-path=true \ No newline at end of file diff --git a/__tests__/__fixtures__/cached/every/code.js b/__tests__/__fixtures__/cached/every/code.js index a694a8e1..64163fd6 100644 --- a/__tests__/__fixtures__/cached/every/code.js +++ b/__tests__/__fixtures__/cached/every/code.js @@ -1,3 +1,3 @@ -import { every } from "../../../../inline-loops.macro"; +import { every } from "../../../../src/inline-loops.macro"; const areAllEven = every(array, fn); diff --git a/__tests__/__fixtures__/cached/everyObject/code.js b/__tests__/__fixtures__/cached/everyObject/code.js index 88065741..67df75ed 100644 --- a/__tests__/__fixtures__/cached/everyObject/code.js +++ b/__tests__/__fixtures__/cached/everyObject/code.js @@ -1,3 +1,3 @@ -import { everyObject } from "../../../../inline-loops.macro"; +import { everyObject } from "../../../../src/inline-loops.macro"; const areAllEven = everyObject(object, fn); diff --git a/__tests__/__fixtures__/cached/everyRight/code.js b/__tests__/__fixtures__/cached/everyRight/code.js index f668e65b..df609e96 100644 --- a/__tests__/__fixtures__/cached/everyRight/code.js +++ b/__tests__/__fixtures__/cached/everyRight/code.js @@ -1,3 +1,3 @@ -import { everyRight } from "../../../../inline-loops.macro"; +import { everyRight } from "../../../../src/inline-loops.macro"; const areAllEven = everyRight(array, fn); diff --git a/__tests__/__fixtures__/cached/filter/code.js b/__tests__/__fixtures__/cached/filter/code.js index 77e54994..14e6daf0 100644 --- a/__tests__/__fixtures__/cached/filter/code.js +++ b/__tests__/__fixtures__/cached/filter/code.js @@ -1,3 +1,3 @@ -import { filter } from "../../../../inline-loops.macro"; +import { filter } from "../../../../src/inline-loops.macro"; const onlyEven = filter(array, fn); diff --git a/__tests__/__fixtures__/cached/filterObject/code.js b/__tests__/__fixtures__/cached/filterObject/code.js index 66db3f05..74404094 100644 --- a/__tests__/__fixtures__/cached/filterObject/code.js +++ b/__tests__/__fixtures__/cached/filterObject/code.js @@ -1,3 +1,3 @@ -import { filterObject } from "../../../../inline-loops.macro"; +import { filterObject } from "../../../../src/inline-loops.macro"; const onlyEven = filterObject(object, fn); diff --git a/__tests__/__fixtures__/cached/filterRight/code.js b/__tests__/__fixtures__/cached/filterRight/code.js index 69fd0d8e..8bfd1785 100644 --- a/__tests__/__fixtures__/cached/filterRight/code.js +++ b/__tests__/__fixtures__/cached/filterRight/code.js @@ -1,3 +1,3 @@ -import { filterRight } from "../../../../inline-loops.macro"; +import { filterRight } from "../../../../src/inline-loops.macro"; const onlyEven = filterRight(array, fn); diff --git a/__tests__/__fixtures__/cached/find/code.js b/__tests__/__fixtures__/cached/find/code.js index c4f54c10..cb082868 100644 --- a/__tests__/__fixtures__/cached/find/code.js +++ b/__tests__/__fixtures__/cached/find/code.js @@ -1,3 +1,3 @@ -import { find } from "../../../../inline-loops.macro"; +import { find } from "../../../../src/inline-loops.macro"; const firstEven = find(array, fn); diff --git a/__tests__/__fixtures__/cached/findIndex/code.js b/__tests__/__fixtures__/cached/findIndex/code.js index 9a3b444a..b8e68d37 100644 --- a/__tests__/__fixtures__/cached/findIndex/code.js +++ b/__tests__/__fixtures__/cached/findIndex/code.js @@ -1,3 +1,3 @@ -import { findIndex } from "../../../../inline-loops.macro"; +import { findIndex } from "../../../../src/inline-loops.macro"; const firstEven = findIndex(array, fn); diff --git a/__tests__/__fixtures__/cached/findIndexRight/code.js b/__tests__/__fixtures__/cached/findIndexRight/code.js index de1ef569..679cc003 100644 --- a/__tests__/__fixtures__/cached/findIndexRight/code.js +++ b/__tests__/__fixtures__/cached/findIndexRight/code.js @@ -1,3 +1,3 @@ -import { findIndexRight } from "../../../../inline-loops.macro"; +import { findIndexRight } from "../../../../src/inline-loops.macro"; const firstEven = findIndexRight(array, fn); diff --git a/__tests__/__fixtures__/cached/findKey/code.js b/__tests__/__fixtures__/cached/findKey/code.js index a424e85c..90174d7c 100644 --- a/__tests__/__fixtures__/cached/findKey/code.js +++ b/__tests__/__fixtures__/cached/findKey/code.js @@ -1,3 +1,3 @@ -import { findKey } from "../../../../inline-loops.macro"; +import { findKey } from "../../../../src/inline-loops.macro"; const firstEven = findKey(object, fn); diff --git a/__tests__/__fixtures__/cached/findObject/code.js b/__tests__/__fixtures__/cached/findObject/code.js index d8343e6b..5d164e19 100644 --- a/__tests__/__fixtures__/cached/findObject/code.js +++ b/__tests__/__fixtures__/cached/findObject/code.js @@ -1,3 +1,3 @@ -import { findObject } from "../../../../inline-loops.macro"; +import { findObject } from "../../../../src/inline-loops.macro"; const firstEven = findObject(object, fn); diff --git a/__tests__/__fixtures__/cached/findRight/code.js b/__tests__/__fixtures__/cached/findRight/code.js index 71015e20..56f0b81d 100644 --- a/__tests__/__fixtures__/cached/findRight/code.js +++ b/__tests__/__fixtures__/cached/findRight/code.js @@ -1,3 +1,3 @@ -import { findRight } from "../../../../inline-loops.macro"; +import { findRight } from "../../../../src/inline-loops.macro"; const lastEven = findRight(array, fn); diff --git a/__tests__/__fixtures__/cached/forEach/code.js b/__tests__/__fixtures__/cached/forEach/code.js index dde51fd0..be56c92b 100644 --- a/__tests__/__fixtures__/cached/forEach/code.js +++ b/__tests__/__fixtures__/cached/forEach/code.js @@ -1,3 +1,3 @@ -import { forEach } from "../../../../inline-loops.macro"; +import { forEach } from "../../../../src/inline-loops.macro"; forEach(array, fn); diff --git a/__tests__/__fixtures__/cached/forEachObject/code.js b/__tests__/__fixtures__/cached/forEachObject/code.js index 509afe6b..fd9f3476 100644 --- a/__tests__/__fixtures__/cached/forEachObject/code.js +++ b/__tests__/__fixtures__/cached/forEachObject/code.js @@ -1,3 +1,3 @@ -import { forEachObject } from "../../../../inline-loops.macro"; +import { forEachObject } from "../../../../src/inline-loops.macro"; forEachObject(object, fn); diff --git a/__tests__/__fixtures__/cached/forEachRight/code.js b/__tests__/__fixtures__/cached/forEachRight/code.js index 464edead..fa68450f 100644 --- a/__tests__/__fixtures__/cached/forEachRight/code.js +++ b/__tests__/__fixtures__/cached/forEachRight/code.js @@ -1,3 +1,3 @@ -import { forEachRight } from "../../../../inline-loops.macro"; +import { forEachRight } from "../../../../src/inline-loops.macro"; forEachRight(array, fn); diff --git a/__tests__/__fixtures__/cached/map/code.js b/__tests__/__fixtures__/cached/map/code.js index 9fe58c68..c5282a92 100644 --- a/__tests__/__fixtures__/cached/map/code.js +++ b/__tests__/__fixtures__/cached/map/code.js @@ -1,3 +1,3 @@ -import { map } from "../../../../inline-loops.macro"; +import { map } from "../../../../src/inline-loops.macro"; const doubledValues = map(array, fn); diff --git a/__tests__/__fixtures__/cached/mapObject/code.js b/__tests__/__fixtures__/cached/mapObject/code.js index 0bec625a..fd748b80 100644 --- a/__tests__/__fixtures__/cached/mapObject/code.js +++ b/__tests__/__fixtures__/cached/mapObject/code.js @@ -1,3 +1,3 @@ -import { mapObject } from "../../../../inline-loops.macro"; +import { mapObject } from "../../../../src/inline-loops.macro"; const doubledValues = mapObject(object, fn); diff --git a/__tests__/__fixtures__/cached/mapRight/code.js b/__tests__/__fixtures__/cached/mapRight/code.js index be951479..de5ed20f 100644 --- a/__tests__/__fixtures__/cached/mapRight/code.js +++ b/__tests__/__fixtures__/cached/mapRight/code.js @@ -1,3 +1,3 @@ -import { mapRight } from "../../../../inline-loops.macro"; +import { mapRight } from "../../../../src/inline-loops.macro"; const doubledValues = mapRight(array, fn); diff --git a/__tests__/__fixtures__/cached/reduce-no-initialValue/code.js b/__tests__/__fixtures__/cached/reduce-no-initialValue/code.js index 2dcfa1b8..e7dedd0d 100644 --- a/__tests__/__fixtures__/cached/reduce-no-initialValue/code.js +++ b/__tests__/__fixtures__/cached/reduce-no-initialValue/code.js @@ -1,3 +1,3 @@ -import { reduce } from "../../../../inline-loops.macro"; +import { reduce } from "../../../../src/inline-loops.macro"; const doubledValues = reduce(array, fn); diff --git a/__tests__/__fixtures__/cached/reduce/code.js b/__tests__/__fixtures__/cached/reduce/code.js index 6d75dc1f..b57be264 100644 --- a/__tests__/__fixtures__/cached/reduce/code.js +++ b/__tests__/__fixtures__/cached/reduce/code.js @@ -1,3 +1,3 @@ -import { reduce } from "../../../../inline-loops.macro"; +import { reduce } from "../../../../src/inline-loops.macro"; const doubledValues = reduce(array, fn, {}); diff --git a/__tests__/__fixtures__/cached/reduceObject-no-initialValue/code.js b/__tests__/__fixtures__/cached/reduceObject-no-initialValue/code.js index 1b26f347..676ff904 100644 --- a/__tests__/__fixtures__/cached/reduceObject-no-initialValue/code.js +++ b/__tests__/__fixtures__/cached/reduceObject-no-initialValue/code.js @@ -1,3 +1,3 @@ -import { reduceObject } from "../../../../inline-loops.macro"; +import { reduceObject } from "../../../../src/inline-loops.macro"; const doubledValues = reduceObject(object, fn); diff --git a/__tests__/__fixtures__/cached/reduceObject/code.js b/__tests__/__fixtures__/cached/reduceObject/code.js index 81b726f4..4f070d20 100644 --- a/__tests__/__fixtures__/cached/reduceObject/code.js +++ b/__tests__/__fixtures__/cached/reduceObject/code.js @@ -1,3 +1,3 @@ -import { reduceObject } from "../../../../inline-loops.macro"; +import { reduceObject } from "../../../../src/inline-loops.macro"; const doubledValues = reduceObject(object, fn, {}); diff --git a/__tests__/__fixtures__/cached/reduceRight-no-initialValue/code.js b/__tests__/__fixtures__/cached/reduceRight-no-initialValue/code.js index 12ea2bf1..f474aa3d 100644 --- a/__tests__/__fixtures__/cached/reduceRight-no-initialValue/code.js +++ b/__tests__/__fixtures__/cached/reduceRight-no-initialValue/code.js @@ -1,3 +1,3 @@ -import { reduceRight } from "../../../../inline-loops.macro"; +import { reduceRight } from "../../../../src/inline-loops.macro"; const doubledValues = reduceRight(array, fn); diff --git a/__tests__/__fixtures__/cached/reduceRight/code.js b/__tests__/__fixtures__/cached/reduceRight/code.js index 0b1ead2b..c9372fc5 100644 --- a/__tests__/__fixtures__/cached/reduceRight/code.js +++ b/__tests__/__fixtures__/cached/reduceRight/code.js @@ -1,3 +1,3 @@ -import { reduceRight } from "../../../../inline-loops.macro"; +import { reduceRight } from "../../../../src/inline-loops.macro"; const doubledValues = reduceRight(array, fn, {}); diff --git a/__tests__/__fixtures__/cached/some/code.js b/__tests__/__fixtures__/cached/some/code.js index 4ed71cc3..682ede85 100644 --- a/__tests__/__fixtures__/cached/some/code.js +++ b/__tests__/__fixtures__/cached/some/code.js @@ -1,3 +1,3 @@ -import { some } from "../../../../inline-loops.macro"; +import { some } from "../../../../src/inline-loops.macro"; const areAnyEven = some(array, fn); diff --git a/__tests__/__fixtures__/cached/someObject/code.js b/__tests__/__fixtures__/cached/someObject/code.js index acd17256..a433ea18 100644 --- a/__tests__/__fixtures__/cached/someObject/code.js +++ b/__tests__/__fixtures__/cached/someObject/code.js @@ -1,3 +1,3 @@ -import { someObject } from "../../../../inline-loops.macro"; +import { someObject } from "../../../../src/inline-loops.macro"; const areAnyEven = someObject(object, fn); diff --git a/__tests__/__fixtures__/cached/someRight/code.js b/__tests__/__fixtures__/cached/someRight/code.js index d64a0d6e..5cba0dbf 100644 --- a/__tests__/__fixtures__/cached/someRight/code.js +++ b/__tests__/__fixtures__/cached/someRight/code.js @@ -1,3 +1,3 @@ -import { someRight } from "../../../../inline-loops.macro"; +import { someRight } from "../../../../src/inline-loops.macro"; const areAnyEven = someRight(array, fn); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/every/code.js b/__tests__/__fixtures__/inlined-arrow-expression/every/code.js index 5ee3807b..f1bcc647 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/every/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/every/code.js @@ -1,3 +1,3 @@ -import { every } from "../../../../inline-loops.macro"; +import { every } from "../../../../src/inline-loops.macro"; const areAllEven = every(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/everyObject/code.js b/__tests__/__fixtures__/inlined-arrow-expression/everyObject/code.js index 23bbcf31..b3f62cea 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/everyObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/everyObject/code.js @@ -1,3 +1,3 @@ -import { everyObject } from "../../../../inline-loops.macro"; +import { everyObject } from "../../../../src/inline-loops.macro"; const areAllEven = everyObject(object, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/everyRight/code.js b/__tests__/__fixtures__/inlined-arrow-expression/everyRight/code.js index f31e3845..f776c063 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/everyRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/everyRight/code.js @@ -1,3 +1,3 @@ -import { everyRight } from "../../../../inline-loops.macro"; +import { everyRight } from "../../../../src/inline-loops.macro"; const areAllEven = everyRight(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/filter/code.js b/__tests__/__fixtures__/inlined-arrow-expression/filter/code.js index fdf3a703..5f5e0847 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/filter/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/filter/code.js @@ -1,3 +1,3 @@ -import { filter } from "../../../../inline-loops.macro"; +import { filter } from "../../../../src/inline-loops.macro"; const onlyEven = filter(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/filterObject/code.js b/__tests__/__fixtures__/inlined-arrow-expression/filterObject/code.js index 0ca1d1e7..c21711b9 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/filterObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/filterObject/code.js @@ -1,3 +1,3 @@ -import { filterObject } from "../../../../inline-loops.macro"; +import { filterObject } from "../../../../src/inline-loops.macro"; const onlyEven = filterObject(object, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/filterRight/code.js b/__tests__/__fixtures__/inlined-arrow-expression/filterRight/code.js index 0a6dd9fd..fe5ddb57 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/filterRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/filterRight/code.js @@ -1,3 +1,3 @@ -import { filterRight } from "../../../../inline-loops.macro"; +import { filterRight } from "../../../../src/inline-loops.macro"; const onlyEven = filterRight(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/find/code.js b/__tests__/__fixtures__/inlined-arrow-expression/find/code.js index c475aac7..0d954cbf 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/find/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/find/code.js @@ -1,3 +1,3 @@ -import { find } from "../../../../inline-loops.macro"; +import { find } from "../../../../src/inline-loops.macro"; const firstEven = find(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findIndex/code.js b/__tests__/__fixtures__/inlined-arrow-expression/findIndex/code.js index 5296f798..022ca8e6 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findIndex/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findIndex/code.js @@ -1,3 +1,3 @@ -import { findIndex } from "../../../../inline-loops.macro"; +import { findIndex } from "../../../../src/inline-loops.macro"; const firstEven = findIndex(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findIndexRight/code.js b/__tests__/__fixtures__/inlined-arrow-expression/findIndexRight/code.js index 345f4185..111db3d0 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findIndexRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findIndexRight/code.js @@ -1,3 +1,3 @@ -import { findIndexRight } from "../../../../inline-loops.macro"; +import { findIndexRight } from "../../../../src/inline-loops.macro"; const firstEven = findIndexRight(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findKey/code.js b/__tests__/__fixtures__/inlined-arrow-expression/findKey/code.js index 7b2a6bfc..00f2fd69 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findKey/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findKey/code.js @@ -1,3 +1,3 @@ -import { findKey } from "../../../../inline-loops.macro"; +import { findKey } from "../../../../src/inline-loops.macro"; const firstEven = findKey(object, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findObject/code.js b/__tests__/__fixtures__/inlined-arrow-expression/findObject/code.js index 26b41003..35fb92d7 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findObject/code.js @@ -1,3 +1,3 @@ -import { findObject } from "../../../../inline-loops.macro"; +import { findObject } from "../../../../src/inline-loops.macro"; const firstEven = findObject(object, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/findRight/code.js b/__tests__/__fixtures__/inlined-arrow-expression/findRight/code.js index 9faea82d..6f28014b 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/findRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/findRight/code.js @@ -1,3 +1,3 @@ -import { findRight } from "../../../../inline-loops.macro"; +import { findRight } from "../../../../src/inline-loops.macro"; const lastEven = findRight(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/forEach/code.js b/__tests__/__fixtures__/inlined-arrow-expression/forEach/code.js index bdfae2f8..412a92cc 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/forEach/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/forEach/code.js @@ -1,3 +1,3 @@ -import { forEach } from "../../../../inline-loops.macro"; +import { forEach } from "../../../../src/inline-loops.macro"; forEach(array, value => console.log(value)); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/forEachObject/code.js b/__tests__/__fixtures__/inlined-arrow-expression/forEachObject/code.js index 85783680..5a099c54 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/forEachObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/forEachObject/code.js @@ -1,3 +1,3 @@ -import { forEachObject } from "../../../../inline-loops.macro"; +import { forEachObject } from "../../../../src/inline-loops.macro"; forEachObject(object, value => console.log(value)); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/forEachRight/code.js b/__tests__/__fixtures__/inlined-arrow-expression/forEachRight/code.js index 8d7bb96a..72f8a08b 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/forEachRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/forEachRight/code.js @@ -1,3 +1,3 @@ -import { forEachRight } from "../../../../inline-loops.macro"; +import { forEachRight } from "../../../../src/inline-loops.macro"; forEachRight(array, value => console.log(value)); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/map/code.js b/__tests__/__fixtures__/inlined-arrow-expression/map/code.js index 40d50434..c01afa0e 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/map/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/map/code.js @@ -1,3 +1,3 @@ -import { map } from "../../../../inline-loops.macro"; +import { map } from "../../../../src/inline-loops.macro"; const doubledValues = map(array, value => value * 2); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/mapObject/code.js b/__tests__/__fixtures__/inlined-arrow-expression/mapObject/code.js index 78987d35..840111b5 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/mapObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/mapObject/code.js @@ -1,3 +1,3 @@ -import { mapObject } from "../../../../inline-loops.macro"; +import { mapObject } from "../../../../src/inline-loops.macro"; const doubledValues = mapObject(object, value => value * 2); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/mapRight/code.js b/__tests__/__fixtures__/inlined-arrow-expression/mapRight/code.js index 405cb85c..41547d56 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/mapRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/mapRight/code.js @@ -1,3 +1,3 @@ -import { mapRight } from "../../../../inline-loops.macro"; +import { mapRight } from "../../../../src/inline-loops.macro"; const doubledValues = mapRight(array, value => value * 2); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/code.js b/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/code.js new file mode 100644 index 00000000..20a749b3 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/code.js @@ -0,0 +1,3 @@ +import { reduce } from '../../../../src/inline-loops.macro'; + +const sum = reduce(array, (total, value) => total + value); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/output.js new file mode 100644 index 00000000..4d71690d --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduce-no-initialValue/output.js @@ -0,0 +1,8 @@ +let _result = array[0]; + +for (let _key = 1, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduce/code.js b/__tests__/__fixtures__/inlined-arrow-expression/reduce/code.js new file mode 100644 index 00000000..43096838 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduce/code.js @@ -0,0 +1,3 @@ +import { reduce } from '../../../../src/inline-loops.macro'; + +const sum = reduce(array, (total, value) => total + value, 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduce/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduce/output.js new file mode 100644 index 00000000..e43bfc35 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduce/output.js @@ -0,0 +1,8 @@ +let _result = 0; + +for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/code.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/code.js new file mode 100644 index 00000000..df1a369f --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/code.js @@ -0,0 +1,3 @@ +import { reduceObject } from '../../../../src/inline-loops.macro'; + +const sum = reduceObject(object, (total, value) => total + value); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/output.js new file mode 100644 index 00000000..42555c03 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject-no-initialValue/output.js @@ -0,0 +1,17 @@ +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]; + } +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/code.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/code.js new file mode 100644 index 00000000..8947de49 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/code.js @@ -0,0 +1,3 @@ +import { reduceObject } from '../../../../src/inline-loops.macro'; + +const sum = reduceObject(object, (total, value) => total + value, 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/output.js new file mode 100644 index 00000000..24e2ca1a --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceObject/output.js @@ -0,0 +1,10 @@ +let _value; + +let _result = 0; + +for (let _key in object) { + _value = object[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/code.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/code.js new file mode 100644 index 00000000..02c5988f --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/code.js @@ -0,0 +1,3 @@ +import { reduceRight } from '../../../../src/inline-loops.macro'; + +const sum = reduceRight(array, (total, value) => total + value); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/output.js new file mode 100644 index 00000000..a9904f3d --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight-no-initialValue/output.js @@ -0,0 +1,9 @@ +const _length = array.length; +let _result = array[_length - 1]; + +for (let _key = _length - 2, _value; _key >= 0; --_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/code.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/code.js new file mode 100644 index 00000000..9b8c4591 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/code.js @@ -0,0 +1,3 @@ +import { reduceRight } from '../../../../src/inline-loops.macro'; + +const sum = reduceRight(array, (total, value) => total + value, 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/output.js b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/output.js new file mode 100644 index 00000000..77006c8b --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-expression/reduceRight/output.js @@ -0,0 +1,8 @@ +let _result = 0; + +for (let _key = array.length - 1, _value; _key >= 0; --_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-expression/some/code.js b/__tests__/__fixtures__/inlined-arrow-expression/some/code.js index 47357836..a517624d 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/some/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/some/code.js @@ -1,3 +1,3 @@ -import { some } from "../../../../inline-loops.macro"; +import { some } from "../../../../src/inline-loops.macro"; const areAnyEven = some(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/someObject/code.js b/__tests__/__fixtures__/inlined-arrow-expression/someObject/code.js index e9f746c5..372de908 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/someObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/someObject/code.js @@ -1,3 +1,3 @@ -import { someObject } from "../../../../inline-loops.macro"; +import { someObject } from "../../../../src/inline-loops.macro"; const areAnyEven = someObject(object, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-expression/someRight/code.js b/__tests__/__fixtures__/inlined-arrow-expression/someRight/code.js index f9ff3914..f597f15b 100644 --- a/__tests__/__fixtures__/inlined-arrow-expression/someRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-expression/someRight/code.js @@ -1,3 +1,3 @@ -import { someRight } from "../../../../inline-loops.macro"; +import { someRight } from "../../../../src/inline-loops.macro"; const areAnyEven = someRight(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/inlined-arrow-return/every/code.js b/__tests__/__fixtures__/inlined-arrow-return/every/code.js index 7066feaf..279e1cd4 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/every/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/every/code.js @@ -1,4 +1,4 @@ -import { every } from "../../../../inline-loops.macro"; +import { every } from "../../../../src/inline-loops.macro"; const areAllEven = every(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/everyObject/code.js b/__tests__/__fixtures__/inlined-arrow-return/everyObject/code.js index a0fce4b6..1ec9b157 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/everyObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/everyObject/code.js @@ -1,4 +1,4 @@ -import { everyObject } from "../../../../inline-loops.macro"; +import { everyObject } from "../../../../src/inline-loops.macro"; const areAllEven = everyObject(object, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/everyRight/code.js b/__tests__/__fixtures__/inlined-arrow-return/everyRight/code.js index 8fd32ddc..f80450c5 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/everyRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/everyRight/code.js @@ -1,4 +1,4 @@ -import { everyRight } from "../../../../inline-loops.macro"; +import { everyRight } from "../../../../src/inline-loops.macro"; const areAllEven = everyRight(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/filter/code.js b/__tests__/__fixtures__/inlined-arrow-return/filter/code.js index 6d7dd9bc..e939491d 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/filter/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/filter/code.js @@ -1,4 +1,4 @@ -import { filter } from "../../../../inline-loops.macro"; +import { filter } from "../../../../src/inline-loops.macro"; const onlyEven = filter(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/filterObject/code.js b/__tests__/__fixtures__/inlined-arrow-return/filterObject/code.js index 9833b3a7..d67ad7f5 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/filterObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/filterObject/code.js @@ -1,4 +1,4 @@ -import { filterObject } from "../../../../inline-loops.macro"; +import { filterObject } from "../../../../src/inline-loops.macro"; const onlyEven = filterObject(object, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/filterRight/code.js b/__tests__/__fixtures__/inlined-arrow-return/filterRight/code.js index 63dd62ed..f29432c0 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/filterRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/filterRight/code.js @@ -1,4 +1,4 @@ -import { filterRight } from "../../../../inline-loops.macro"; +import { filterRight } from "../../../../src/inline-loops.macro"; const onlyEven = filterRight(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/find/code.js b/__tests__/__fixtures__/inlined-arrow-return/find/code.js index 1c05c84d..d55718d4 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/find/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/find/code.js @@ -1,4 +1,4 @@ -import { find } from "../../../../inline-loops.macro"; +import { find } from "../../../../src/inline-loops.macro"; const firstEven = find(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/findIndex/code.js b/__tests__/__fixtures__/inlined-arrow-return/findIndex/code.js index 0d25f855..a386e4ea 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findIndex/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findIndex/code.js @@ -1,4 +1,4 @@ -import { findIndex } from "../../../../inline-loops.macro"; +import { findIndex } from "../../../../src/inline-loops.macro"; const firstEven = findIndex(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/findIndexRight/code.js b/__tests__/__fixtures__/inlined-arrow-return/findIndexRight/code.js index e225ce92..580c2480 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findIndexRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findIndexRight/code.js @@ -1,4 +1,4 @@ -import { findIndexRight } from "../../../../inline-loops.macro"; +import { findIndexRight } from "../../../../src/inline-loops.macro"; const firstEven = findIndexRight(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/findKey/code.js b/__tests__/__fixtures__/inlined-arrow-return/findKey/code.js index 8f860464..1c5d7119 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findKey/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findKey/code.js @@ -1,4 +1,4 @@ -import { findKey } from "../../../../inline-loops.macro"; +import { findKey } from "../../../../src/inline-loops.macro"; const firstEven = findKey(object, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/findObject/code.js b/__tests__/__fixtures__/inlined-arrow-return/findObject/code.js index b3b4e495..a5ae4a57 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findObject/code.js @@ -1,4 +1,4 @@ -import { findObject } from "../../../../inline-loops.macro"; +import { findObject } from "../../../../src/inline-loops.macro"; const firstEven = findObject(object, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/findRight/code.js b/__tests__/__fixtures__/inlined-arrow-return/findRight/code.js index f29422d0..431829e1 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/findRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/findRight/code.js @@ -1,4 +1,4 @@ -import { findRight } from "../../../../inline-loops.macro"; +import { findRight } from "../../../../src/inline-loops.macro"; const lastEven = findRight(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/forEach/code.js b/__tests__/__fixtures__/inlined-arrow-return/forEach/code.js index aea91c91..51ed9c12 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/forEach/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/forEach/code.js @@ -1,4 +1,4 @@ -import { forEach } from "../../../../inline-loops.macro"; +import { forEach } from "../../../../src/inline-loops.macro"; forEach(array, value => { console.log(value); diff --git a/__tests__/__fixtures__/inlined-arrow-return/forEachObject/code.js b/__tests__/__fixtures__/inlined-arrow-return/forEachObject/code.js index aeb3d405..5fa15632 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/forEachObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/forEachObject/code.js @@ -1,4 +1,4 @@ -import { forEachObject } from "../../../../inline-loops.macro"; +import { forEachObject } from "../../../../src/inline-loops.macro"; forEachObject(object, value => { console.log(value); diff --git a/__tests__/__fixtures__/inlined-arrow-return/forEachRight/code.js b/__tests__/__fixtures__/inlined-arrow-return/forEachRight/code.js index 1bd42ca5..817b4e4c 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/forEachRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/forEachRight/code.js @@ -1,4 +1,4 @@ -import { forEachRight } from "../../../../inline-loops.macro"; +import { forEachRight } from "../../../../src/inline-loops.macro"; forEachRight(array, value => { console.log(value); diff --git a/__tests__/__fixtures__/inlined-arrow-return/map/code.js b/__tests__/__fixtures__/inlined-arrow-return/map/code.js index ab75ff48..cf64f5a5 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/map/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/map/code.js @@ -1,4 +1,4 @@ -import { map } from "../../../../inline-loops.macro"; +import { map } from "../../../../src/inline-loops.macro"; const doubledValues = map(array, value => { return value * 2; diff --git a/__tests__/__fixtures__/inlined-arrow-return/mapObject/code.js b/__tests__/__fixtures__/inlined-arrow-return/mapObject/code.js index ceb9b4d0..756623d3 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/mapObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/mapObject/code.js @@ -1,4 +1,4 @@ -import { mapObject } from "../../../../inline-loops.macro"; +import { mapObject } from "../../../../src/inline-loops.macro"; const doubledValues = mapObject(object, value => { return value * 2; diff --git a/__tests__/__fixtures__/inlined-arrow-return/mapRight/code.js b/__tests__/__fixtures__/inlined-arrow-return/mapRight/code.js index 19638d35..9ca8b762 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/mapRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/mapRight/code.js @@ -1,4 +1,4 @@ -import { mapRight } from "../../../../inline-loops.macro"; +import { mapRight } from "../../../../src/inline-loops.macro"; const doubledValues = mapRight(array, value => { return value * 2; diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/code.js b/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/code.js new file mode 100644 index 00000000..1510b679 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/code.js @@ -0,0 +1,5 @@ +import { reduce } from '../../../../src/inline-loops.macro'; + +const sum = reduce(array, (total, value) => { + return total + value; +}); diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/output.js new file mode 100644 index 00000000..4d71690d --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduce-no-initialValue/output.js @@ -0,0 +1,8 @@ +let _result = array[0]; + +for (let _key = 1, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduce/code.js b/__tests__/__fixtures__/inlined-arrow-return/reduce/code.js new file mode 100644 index 00000000..825a5de1 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduce/code.js @@ -0,0 +1,5 @@ +import { reduce } from '../../../../src/inline-loops.macro'; + +const sum = reduce(array, (total, value) => { + return total + value; +}, 0); diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduce/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduce/output.js new file mode 100644 index 00000000..e43bfc35 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduce/output.js @@ -0,0 +1,8 @@ +let _result = 0; + +for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/code.js b/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/code.js new file mode 100644 index 00000000..c25a5dad --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/code.js @@ -0,0 +1,5 @@ +import { reduceObject } from '../../../../src/inline-loops.macro'; + +const sum = reduceObject(object, (total, value) => { + return total + value; +}); diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/output.js new file mode 100644 index 00000000..42555c03 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceObject-no-initialValue/output.js @@ -0,0 +1,17 @@ +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]; + } +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceObject/code.js b/__tests__/__fixtures__/inlined-arrow-return/reduceObject/code.js new file mode 100644 index 00000000..bb8bc985 --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceObject/code.js @@ -0,0 +1,5 @@ +import { reduceObject } from '../../../../src/inline-loops.macro'; + +const sum = reduceObject(object, (total, value) => { + return total + value; +}, 0); diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceObject/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduceObject/output.js new file mode 100644 index 00000000..24e2ca1a --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceObject/output.js @@ -0,0 +1,10 @@ +let _value; + +let _result = 0; + +for (let _key in object) { + _value = object[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/code.js b/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/code.js new file mode 100644 index 00000000..ed2bfadb --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/code.js @@ -0,0 +1,5 @@ +import { reduceRight } from '../../../../src/inline-loops.macro'; + +const sum = reduceRight(array, (total, value) => { + return total + value; +}); diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/output.js new file mode 100644 index 00000000..a9904f3d --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceRight-no-initialValue/output.js @@ -0,0 +1,9 @@ +const _length = array.length; +let _result = array[_length - 1]; + +for (let _key = _length - 2, _value; _key >= 0; --_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceRight/code.js b/__tests__/__fixtures__/inlined-arrow-return/reduceRight/code.js new file mode 100644 index 00000000..f38ce29f --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceRight/code.js @@ -0,0 +1,5 @@ +import { reduceRight } from '../../../../src/inline-loops.macro'; + +const sum = reduceRight(array, (total, value) => { + return total + value; +}, 0); diff --git a/__tests__/__fixtures__/inlined-arrow-return/reduceRight/output.js b/__tests__/__fixtures__/inlined-arrow-return/reduceRight/output.js new file mode 100644 index 00000000..77006c8b --- /dev/null +++ b/__tests__/__fixtures__/inlined-arrow-return/reduceRight/output.js @@ -0,0 +1,8 @@ +let _result = 0; + +for (let _key = array.length - 1, _value; _key >= 0; --_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-arrow-return/some/code.js b/__tests__/__fixtures__/inlined-arrow-return/some/code.js index a72a98b9..d70572f0 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/some/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/some/code.js @@ -1,4 +1,4 @@ -import { some } from "../../../../inline-loops.macro"; +import { some } from "../../../../src/inline-loops.macro"; const areAnyEven = some(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/someObject/code.js b/__tests__/__fixtures__/inlined-arrow-return/someObject/code.js index 17a7e3f0..41d82279 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/someObject/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/someObject/code.js @@ -1,4 +1,4 @@ -import { someObject } from "../../../../inline-loops.macro"; +import { someObject } from "../../../../src/inline-loops.macro"; const areAnyEven = someObject(object, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-arrow-return/someRight/code.js b/__tests__/__fixtures__/inlined-arrow-return/someRight/code.js index a22a97be..b3691179 100644 --- a/__tests__/__fixtures__/inlined-arrow-return/someRight/code.js +++ b/__tests__/__fixtures__/inlined-arrow-return/someRight/code.js @@ -1,4 +1,4 @@ -import { someRight } from "../../../../inline-loops.macro"; +import { someRight } from "../../../../src/inline-loops.macro"; const areAnyEven = someRight(array, value => { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/every/code.js b/__tests__/__fixtures__/inlined-function-return/every/code.js index eccf4c7f..b95648c1 100644 --- a/__tests__/__fixtures__/inlined-function-return/every/code.js +++ b/__tests__/__fixtures__/inlined-function-return/every/code.js @@ -1,4 +1,4 @@ -import { every } from "../../../../inline-loops.macro"; +import { every } from "../../../../src/inline-loops.macro"; const areAllEven = every(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/everyObject/code.js b/__tests__/__fixtures__/inlined-function-return/everyObject/code.js index fa16de94..a4eca7cc 100644 --- a/__tests__/__fixtures__/inlined-function-return/everyObject/code.js +++ b/__tests__/__fixtures__/inlined-function-return/everyObject/code.js @@ -1,4 +1,4 @@ -import { everyObject } from "../../../../inline-loops.macro"; +import { everyObject } from "../../../../src/inline-loops.macro"; const areAllEven = everyObject(object, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/everyRight/code.js b/__tests__/__fixtures__/inlined-function-return/everyRight/code.js index cbfecef4..429c08d5 100644 --- a/__tests__/__fixtures__/inlined-function-return/everyRight/code.js +++ b/__tests__/__fixtures__/inlined-function-return/everyRight/code.js @@ -1,4 +1,4 @@ -import { everyRight } from "../../../../inline-loops.macro"; +import { everyRight } from "../../../../src/inline-loops.macro"; const areAllEven = everyRight(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/filter/code.js b/__tests__/__fixtures__/inlined-function-return/filter/code.js index 722a1750..2be1a03b 100644 --- a/__tests__/__fixtures__/inlined-function-return/filter/code.js +++ b/__tests__/__fixtures__/inlined-function-return/filter/code.js @@ -1,4 +1,4 @@ -import { filter } from "../../../../inline-loops.macro"; +import { filter } from "../../../../src/inline-loops.macro"; const onlyEven = filter(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/filterObject/code.js b/__tests__/__fixtures__/inlined-function-return/filterObject/code.js index 5e0d5348..c0ac60ac 100644 --- a/__tests__/__fixtures__/inlined-function-return/filterObject/code.js +++ b/__tests__/__fixtures__/inlined-function-return/filterObject/code.js @@ -1,4 +1,4 @@ -import { filterObject } from "../../../../inline-loops.macro"; +import { filterObject } from "../../../../src/inline-loops.macro"; const onlyEven = filterObject(object, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/filterRight/code.js b/__tests__/__fixtures__/inlined-function-return/filterRight/code.js index 66f17745..4d14bc20 100644 --- a/__tests__/__fixtures__/inlined-function-return/filterRight/code.js +++ b/__tests__/__fixtures__/inlined-function-return/filterRight/code.js @@ -1,4 +1,4 @@ -import { filterRight } from "../../../../inline-loops.macro"; +import { filterRight } from "../../../../src/inline-loops.macro"; const onlyEven = filterRight(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/find/code.js b/__tests__/__fixtures__/inlined-function-return/find/code.js index 3e327bbc..43f9fe89 100644 --- a/__tests__/__fixtures__/inlined-function-return/find/code.js +++ b/__tests__/__fixtures__/inlined-function-return/find/code.js @@ -1,4 +1,4 @@ -import { find } from "../../../../inline-loops.macro"; +import { find } from "../../../../src/inline-loops.macro"; const firstEven = find(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/findIndex/code.js b/__tests__/__fixtures__/inlined-function-return/findIndex/code.js index 291572b0..f0879e6e 100644 --- a/__tests__/__fixtures__/inlined-function-return/findIndex/code.js +++ b/__tests__/__fixtures__/inlined-function-return/findIndex/code.js @@ -1,4 +1,4 @@ -import { findIndex } from "../../../../inline-loops.macro"; +import { findIndex } from "../../../../src/inline-loops.macro"; const firstEven = findIndex(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/findIndexRight/code.js b/__tests__/__fixtures__/inlined-function-return/findIndexRight/code.js index 4451cd57..332c5f3c 100644 --- a/__tests__/__fixtures__/inlined-function-return/findIndexRight/code.js +++ b/__tests__/__fixtures__/inlined-function-return/findIndexRight/code.js @@ -1,4 +1,4 @@ -import { findIndexRight } from "../../../../inline-loops.macro"; +import { findIndexRight } from "../../../../src/inline-loops.macro"; const firstEven = findIndexRight(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/findKey/code.js b/__tests__/__fixtures__/inlined-function-return/findKey/code.js index c991e01e..30bb2586 100644 --- a/__tests__/__fixtures__/inlined-function-return/findKey/code.js +++ b/__tests__/__fixtures__/inlined-function-return/findKey/code.js @@ -1,4 +1,4 @@ -import { findKey } from "../../../../inline-loops.macro"; +import { findKey } from "../../../../src/inline-loops.macro"; const firstEven = findKey(object, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/findObject/code.js b/__tests__/__fixtures__/inlined-function-return/findObject/code.js index ba81b931..c48f27b6 100644 --- a/__tests__/__fixtures__/inlined-function-return/findObject/code.js +++ b/__tests__/__fixtures__/inlined-function-return/findObject/code.js @@ -1,4 +1,4 @@ -import { findObject } from "../../../../inline-loops.macro"; +import { findObject } from "../../../../src/inline-loops.macro"; const firstEven = findObject(object, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/findRight/code.js b/__tests__/__fixtures__/inlined-function-return/findRight/code.js index d4949567..4f30e790 100644 --- a/__tests__/__fixtures__/inlined-function-return/findRight/code.js +++ b/__tests__/__fixtures__/inlined-function-return/findRight/code.js @@ -1,4 +1,4 @@ -import { findRight } from "../../../../inline-loops.macro"; +import { findRight } from "../../../../src/inline-loops.macro"; const lastEven = findRight(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/forEach/code.js b/__tests__/__fixtures__/inlined-function-return/forEach/code.js index 056ef0dd..a6b7f0d5 100644 --- a/__tests__/__fixtures__/inlined-function-return/forEach/code.js +++ b/__tests__/__fixtures__/inlined-function-return/forEach/code.js @@ -1,4 +1,4 @@ -import { forEach } from "../../../../inline-loops.macro"; +import { forEach } from "../../../../src/inline-loops.macro"; forEach(array, function(value) { console.log(value); diff --git a/__tests__/__fixtures__/inlined-function-return/forEachObject/code.js b/__tests__/__fixtures__/inlined-function-return/forEachObject/code.js index 99dc89c6..f9b9b3cb 100644 --- a/__tests__/__fixtures__/inlined-function-return/forEachObject/code.js +++ b/__tests__/__fixtures__/inlined-function-return/forEachObject/code.js @@ -1,4 +1,4 @@ -import { forEachObject } from "../../../../inline-loops.macro"; +import { forEachObject } from "../../../../src/inline-loops.macro"; forEachObject(object, function(value) { console.log(value); diff --git a/__tests__/__fixtures__/inlined-function-return/forEachRight/code.js b/__tests__/__fixtures__/inlined-function-return/forEachRight/code.js index 85aef694..ccc8a343 100644 --- a/__tests__/__fixtures__/inlined-function-return/forEachRight/code.js +++ b/__tests__/__fixtures__/inlined-function-return/forEachRight/code.js @@ -1,4 +1,4 @@ -import { forEachRight } from "../../../../inline-loops.macro"; +import { forEachRight } from "../../../../src/inline-loops.macro"; forEachRight(array, function(value) { console.log(value); diff --git a/__tests__/__fixtures__/inlined-function-return/map/code.js b/__tests__/__fixtures__/inlined-function-return/map/code.js index a63f427b..86a91a70 100644 --- a/__tests__/__fixtures__/inlined-function-return/map/code.js +++ b/__tests__/__fixtures__/inlined-function-return/map/code.js @@ -1,4 +1,4 @@ -import { map } from "../../../../inline-loops.macro"; +import { map } from "../../../../src/inline-loops.macro"; const doubledValues = map(array, function(value) { return value * 2; diff --git a/__tests__/__fixtures__/inlined-function-return/mapObject/code.js b/__tests__/__fixtures__/inlined-function-return/mapObject/code.js index 33322c68..c39c2feb 100644 --- a/__tests__/__fixtures__/inlined-function-return/mapObject/code.js +++ b/__tests__/__fixtures__/inlined-function-return/mapObject/code.js @@ -1,4 +1,4 @@ -import { mapObject } from "../../../../inline-loops.macro"; +import { mapObject } from "../../../../src/inline-loops.macro"; const doubledValues = mapObject(object, function(value) { return value * 2; diff --git a/__tests__/__fixtures__/inlined-function-return/mapRight/code.js b/__tests__/__fixtures__/inlined-function-return/mapRight/code.js index 7bfb6853..ac93b79a 100644 --- a/__tests__/__fixtures__/inlined-function-return/mapRight/code.js +++ b/__tests__/__fixtures__/inlined-function-return/mapRight/code.js @@ -1,4 +1,4 @@ -import { mapRight } from "../../../../inline-loops.macro"; +import { mapRight } from "../../../../src/inline-loops.macro"; const doubledValues = mapRight(array, function(value) { return value * 2; diff --git a/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/code.js b/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/code.js new file mode 100644 index 00000000..6fa304cb --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/code.js @@ -0,0 +1,5 @@ +import { reduce } from '../../../../src/inline-loops.macro'; + +const sum = reduce(array, function(total, value) { + return total + value; +}); diff --git a/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/output.js b/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/output.js new file mode 100644 index 00000000..4d71690d --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduce-no-initialValue/output.js @@ -0,0 +1,8 @@ +let _result = array[0]; + +for (let _key = 1, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-function-return/reduce/code.js b/__tests__/__fixtures__/inlined-function-return/reduce/code.js new file mode 100644 index 00000000..dcad1cd8 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduce/code.js @@ -0,0 +1,5 @@ +import { reduce } from '../../../../src/inline-loops.macro'; + +const sum = reduce(array, function(total, value) { + return total + value; +}, 0); diff --git a/__tests__/__fixtures__/inlined-function-return/reduce/output.js b/__tests__/__fixtures__/inlined-function-return/reduce/output.js new file mode 100644 index 00000000..e43bfc35 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduce/output.js @@ -0,0 +1,8 @@ +let _result = 0; + +for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/code.js b/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/code.js new file mode 100644 index 00000000..ff2a3e48 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/code.js @@ -0,0 +1,5 @@ +import { reduceObject } from '../../../../src/inline-loops.macro'; + +const sum = reduceObject(object, function(total, value) { + return total + value; +}); diff --git a/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/output.js b/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/output.js new file mode 100644 index 00000000..42555c03 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduceObject-no-initialValue/output.js @@ -0,0 +1,17 @@ +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]; + } +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-function-return/reduceObject/code.js b/__tests__/__fixtures__/inlined-function-return/reduceObject/code.js new file mode 100644 index 00000000..104d7e99 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduceObject/code.js @@ -0,0 +1,5 @@ +import { reduceObject } from '../../../../src/inline-loops.macro'; + +const sum = reduceObject(object, function(total, value) { + return total + value; +}, 0); diff --git a/__tests__/__fixtures__/inlined-function-return/reduceObject/output.js b/__tests__/__fixtures__/inlined-function-return/reduceObject/output.js new file mode 100644 index 00000000..24e2ca1a --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduceObject/output.js @@ -0,0 +1,10 @@ +let _value; + +let _result = 0; + +for (let _key in object) { + _value = object[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/code.js b/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/code.js new file mode 100644 index 00000000..a6877543 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/code.js @@ -0,0 +1,5 @@ +import { reduceRight } from '../../../../src/inline-loops.macro'; + +const sum = reduceRight(array, function(total, value) { + return total + value; +}); diff --git a/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/output.js b/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/output.js new file mode 100644 index 00000000..a9904f3d --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduceRight-no-initialValue/output.js @@ -0,0 +1,9 @@ +const _length = array.length; +let _result = array[_length - 1]; + +for (let _key = _length - 2, _value; _key >= 0; --_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-function-return/reduceRight/code.js b/__tests__/__fixtures__/inlined-function-return/reduceRight/code.js new file mode 100644 index 00000000..e1d77827 --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduceRight/code.js @@ -0,0 +1,5 @@ +import { reduceRight } from '../../../../src/inline-loops.macro'; + +const sum = reduceRight(array, function(total, value) { + return total + value; +}, 0); diff --git a/__tests__/__fixtures__/inlined-function-return/reduceRight/output.js b/__tests__/__fixtures__/inlined-function-return/reduceRight/output.js new file mode 100644 index 00000000..77006c8b --- /dev/null +++ b/__tests__/__fixtures__/inlined-function-return/reduceRight/output.js @@ -0,0 +1,8 @@ +let _result = 0; + +for (let _key = array.length - 1, _value; _key >= 0; --_key) { + _value = array[_key]; + _result = _result + _value; +} + +const sum = _result; diff --git a/__tests__/__fixtures__/inlined-function-return/some/code.js b/__tests__/__fixtures__/inlined-function-return/some/code.js index 757d8591..056432d7 100644 --- a/__tests__/__fixtures__/inlined-function-return/some/code.js +++ b/__tests__/__fixtures__/inlined-function-return/some/code.js @@ -1,4 +1,4 @@ -import { some } from "../../../../inline-loops.macro"; +import { some } from "../../../../src/inline-loops.macro"; const areAnyEven = some(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/someObject/code.js b/__tests__/__fixtures__/inlined-function-return/someObject/code.js index 940f7b7c..220bd0bd 100644 --- a/__tests__/__fixtures__/inlined-function-return/someObject/code.js +++ b/__tests__/__fixtures__/inlined-function-return/someObject/code.js @@ -1,4 +1,4 @@ -import { someObject } from "../../../../inline-loops.macro"; +import { someObject } from "../../../../src/inline-loops.macro"; const areAnyEven = someObject(object, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/inlined-function-return/someRight/code.js b/__tests__/__fixtures__/inlined-function-return/someRight/code.js index 0226fb25..1a687ce8 100644 --- a/__tests__/__fixtures__/inlined-function-return/someRight/code.js +++ b/__tests__/__fixtures__/inlined-function-return/someRight/code.js @@ -1,4 +1,4 @@ -import { someRight } from "../../../../inline-loops.macro"; +import { someRight } from "../../../../src/inline-loops.macro"; const areAnyEven = someRight(array, function(value) { return value % 2 === 0; diff --git a/__tests__/__fixtures__/nested/contrived/code.js b/__tests__/__fixtures__/nested/contrived/code.js index d10cf0dd..00e8021b 100644 --- a/__tests__/__fixtures__/nested/contrived/code.js +++ b/__tests__/__fixtures__/nested/contrived/code.js @@ -1,4 +1,4 @@ -import { map, reduce, someObject } from '../../../../inline-loops.macro'; +import { map, reduce, someObject } from '../../../../src/inline-loops.macro'; if ( someObject( diff --git a/__tests__/__fixtures__/nested/contrived/output.js b/__tests__/__fixtures__/nested/contrived/output.js index 8e27c5bc..fd8efa11 100644 --- a/__tests__/__fixtures__/nested/contrived/output.js +++ b/__tests__/__fixtures__/nested/contrived/output.js @@ -6,15 +6,13 @@ for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { _result.push(_value * 2); } -const _fn2 = (object, value) => ({ ...object, - [value]: value -}); - let _result2 = _result[0]; for (let _key2 = 1, _length2 = _result.length, _value2; _key2 < _length2; ++_key2) { _value2 = _result[_key2]; - _result2 = _fn2(_result2, _value2, _key2, _result); + _result2 = { ..._result2, + [_value2]: _value2 + }; } let _result3 = false; diff --git a/__tests__/__fixtures__/nested/deep-every/code.js b/__tests__/__fixtures__/nested/deep-every/code.js index bc889ee9..fa5db274 100644 --- a/__tests__/__fixtures__/nested/deep-every/code.js +++ b/__tests__/__fixtures__/nested/deep-every/code.js @@ -1,4 +1,4 @@ -import { every } from "../../../../inline-loops.macro"; +import { every } from "../../../../src/inline-loops.macro"; if ( `${every(array, value => value > 0)}|${every( diff --git a/__tests__/__fixtures__/nested/deep-iterable/code.js b/__tests__/__fixtures__/nested/deep-iterable/code.js index d35dae2b..b6089540 100644 --- a/__tests__/__fixtures__/nested/deep-iterable/code.js +++ b/__tests__/__fixtures__/nested/deep-iterable/code.js @@ -1,3 +1,3 @@ -import { forEach, map } from '../../../../inline-loops.macro'; +import { forEach, map } from '../../../../src/inline-loops.macro'; forEach(map(array, cachedFn), otherFn); diff --git a/__tests__/__fixtures__/nested/deep-map/code.js b/__tests__/__fixtures__/nested/deep-map/code.js index a74f1064..74ade0fe 100644 --- a/__tests__/__fixtures__/nested/deep-map/code.js +++ b/__tests__/__fixtures__/nested/deep-map/code.js @@ -1,4 +1,4 @@ -import { mapObject } from "../../../../inline-loops.macro"; +import { mapObject } from "../../../../src/inline-loops.macro"; import { deepEqual } from "fast-equals"; const isEqual = deepEqual(Object.values(mapObject(object, cachedFn)), [ diff --git a/__tests__/__fixtures__/nested/shallow-every/code.js b/__tests__/__fixtures__/nested/shallow-every/code.js index 7077795c..5c66d0bd 100644 --- a/__tests__/__fixtures__/nested/shallow-every/code.js +++ b/__tests__/__fixtures__/nested/shallow-every/code.js @@ -1,4 +1,4 @@ -import { every } from "../../../../inline-loops.macro"; +import { every } from "../../../../src/inline-loops.macro"; if (every(array, value => value > 0)) { console.log("I am positive!"); diff --git a/__tests__/__fixtures__/nested/shallow-iterable/code.js b/__tests__/__fixtures__/nested/shallow-iterable/code.js index 434c5740..c8b47685 100644 --- a/__tests__/__fixtures__/nested/shallow-iterable/code.js +++ b/__tests__/__fixtures__/nested/shallow-iterable/code.js @@ -1,3 +1,3 @@ -import { every, map } from '../../../../inline-loops.macro'; +import { every, map } from '../../../../src/inline-loops.macro'; const allStrings = map(array, item => every(item, v => typeof v === 'string')); diff --git a/__tests__/__fixtures__/nested/shallow-map/code.js b/__tests__/__fixtures__/nested/shallow-map/code.js index e728cd48..6412f617 100644 --- a/__tests__/__fixtures__/nested/shallow-map/code.js +++ b/__tests__/__fixtures__/nested/shallow-map/code.js @@ -1,4 +1,4 @@ -import { map } from "../../../../inline-loops.macro"; +import { map } from "../../../../src/inline-loops.macro"; import { deepEqual } from "fast-equals"; const isEqual = deepEqual(map(array, cachedFn), [1, 2, 3]); diff --git a/__tests__/__fixtures__/uncached/every/code.js b/__tests__/__fixtures__/uncached/every/code.js index 5aab5b4d..cfa610c4 100644 --- a/__tests__/__fixtures__/uncached/every/code.js +++ b/__tests__/__fixtures__/uncached/every/code.js @@ -1,4 +1,4 @@ -import { every } from "../../../../inline-loops.macro"; +import { every } from "../../../../src/inline-loops.macro"; const areAllEven = every([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/everyObject/code.js b/__tests__/__fixtures__/uncached/everyObject/code.js index 3c66aa61..99cdd72f 100644 --- a/__tests__/__fixtures__/uncached/everyObject/code.js +++ b/__tests__/__fixtures__/uncached/everyObject/code.js @@ -1,4 +1,4 @@ -import { everyObject } from "../../../../inline-loops.macro"; +import { everyObject } from "../../../../src/inline-loops.macro"; const areAllEven = everyObject({ one: 1, two: 2, three: 3, four: 4 }, value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/everyRight/code.js b/__tests__/__fixtures__/uncached/everyRight/code.js index f31e3845..f776c063 100644 --- a/__tests__/__fixtures__/uncached/everyRight/code.js +++ b/__tests__/__fixtures__/uncached/everyRight/code.js @@ -1,3 +1,3 @@ -import { everyRight } from "../../../../inline-loops.macro"; +import { everyRight } from "../../../../src/inline-loops.macro"; const areAllEven = everyRight(array, value => value % 2 === 0); diff --git a/__tests__/__fixtures__/uncached/filter/code.js b/__tests__/__fixtures__/uncached/filter/code.js index 045dac2e..61968142 100644 --- a/__tests__/__fixtures__/uncached/filter/code.js +++ b/__tests__/__fixtures__/uncached/filter/code.js @@ -1,4 +1,4 @@ -import { filter } from "../../../../inline-loops.macro"; +import { filter } from "../../../../src/inline-loops.macro"; const onlyEven = filter([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/filterObject/code.js b/__tests__/__fixtures__/uncached/filterObject/code.js index 94d7f73a..4fccaa5f 100644 --- a/__tests__/__fixtures__/uncached/filterObject/code.js +++ b/__tests__/__fixtures__/uncached/filterObject/code.js @@ -1,4 +1,4 @@ -import { filterObject } from "../../../../inline-loops.macro"; +import { filterObject } from "../../../../src/inline-loops.macro"; const onlyEven = filterObject({ one: 1, two: 2, three: 3, four: 4 }, value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/filterRight/code.js b/__tests__/__fixtures__/uncached/filterRight/code.js index dc53dd3c..f2dd28fd 100644 --- a/__tests__/__fixtures__/uncached/filterRight/code.js +++ b/__tests__/__fixtures__/uncached/filterRight/code.js @@ -1,4 +1,4 @@ -import { filterRight } from "../../../../inline-loops.macro"; +import { filterRight } from "../../../../src/inline-loops.macro"; const onlyEven = filterRight([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/find/code.js b/__tests__/__fixtures__/uncached/find/code.js index 0cc03244..5aaccb14 100644 --- a/__tests__/__fixtures__/uncached/find/code.js +++ b/__tests__/__fixtures__/uncached/find/code.js @@ -1,4 +1,4 @@ -import { find } from "../../../../inline-loops.macro"; +import { find } from "../../../../src/inline-loops.macro"; const firstEven = find([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/findIndex/code.js b/__tests__/__fixtures__/uncached/findIndex/code.js index 260a0481..80da0367 100644 --- a/__tests__/__fixtures__/uncached/findIndex/code.js +++ b/__tests__/__fixtures__/uncached/findIndex/code.js @@ -1,4 +1,4 @@ -import { findIndex } from "../../../../inline-loops.macro"; +import { findIndex } from "../../../../src/inline-loops.macro"; const firstEven = findIndex([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/findIndexRight/code.js b/__tests__/__fixtures__/uncached/findIndexRight/code.js index 60fb1423..c7e124ad 100644 --- a/__tests__/__fixtures__/uncached/findIndexRight/code.js +++ b/__tests__/__fixtures__/uncached/findIndexRight/code.js @@ -1,4 +1,4 @@ -import { findIndexRight } from "../../../../inline-loops.macro"; +import { findIndexRight } from "../../../../src/inline-loops.macro"; const firstEven = findIndexRight([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/findKey/code.js b/__tests__/__fixtures__/uncached/findKey/code.js index 11eaf74e..28aadc05 100644 --- a/__tests__/__fixtures__/uncached/findKey/code.js +++ b/__tests__/__fixtures__/uncached/findKey/code.js @@ -1,4 +1,4 @@ -import { findKey } from "../../../../inline-loops.macro"; +import { findKey } from "../../../../src/inline-loops.macro"; const firstEven = findKey({ one: 1, two: 2, three: 3, four: 4 }, value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/findObject/code.js b/__tests__/__fixtures__/uncached/findObject/code.js index bb6c5899..818d9ae5 100644 --- a/__tests__/__fixtures__/uncached/findObject/code.js +++ b/__tests__/__fixtures__/uncached/findObject/code.js @@ -1,4 +1,4 @@ -import { findObject } from "../../../../inline-loops.macro"; +import { findObject } from "../../../../src/inline-loops.macro"; const firstEven = findObject({ one: 1, two: 2, three: 3, four: 4 }, value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/findRight/code.js b/__tests__/__fixtures__/uncached/findRight/code.js index 3e99ffec..0ec9b78d 100644 --- a/__tests__/__fixtures__/uncached/findRight/code.js +++ b/__tests__/__fixtures__/uncached/findRight/code.js @@ -1,4 +1,4 @@ -import { findRight } from "../../../../inline-loops.macro"; +import { findRight } from "../../../../src/inline-loops.macro"; const lastEven = findRight([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/forEach/code.js b/__tests__/__fixtures__/uncached/forEach/code.js index d808d3a3..d032eaaa 100644 --- a/__tests__/__fixtures__/uncached/forEach/code.js +++ b/__tests__/__fixtures__/uncached/forEach/code.js @@ -1,4 +1,4 @@ -import { forEach } from "../../../../inline-loops.macro"; +import { forEach } from "../../../../src/inline-loops.macro"; forEach([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/forEachObject/code.js b/__tests__/__fixtures__/uncached/forEachObject/code.js index 1f42138f..4e83dff6 100644 --- a/__tests__/__fixtures__/uncached/forEachObject/code.js +++ b/__tests__/__fixtures__/uncached/forEachObject/code.js @@ -1,4 +1,4 @@ -import { forEachObject } from "../../../../inline-loops.macro"; +import { forEachObject } from "../../../../src/inline-loops.macro"; forEachObject({ one: 1, two: 2, three: 3, four: 4 }, value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/forEachRight/code.js b/__tests__/__fixtures__/uncached/forEachRight/code.js index cf7a5c52..a563c52c 100644 --- a/__tests__/__fixtures__/uncached/forEachRight/code.js +++ b/__tests__/__fixtures__/uncached/forEachRight/code.js @@ -1,4 +1,4 @@ -import { forEachRight } from "../../../../inline-loops.macro"; +import { forEachRight } from "../../../../src/inline-loops.macro"; forEachRight([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/map/code.js b/__tests__/__fixtures__/uncached/map/code.js index 700e119e..5b8f74da 100644 --- a/__tests__/__fixtures__/uncached/map/code.js +++ b/__tests__/__fixtures__/uncached/map/code.js @@ -1,4 +1,4 @@ -import { map } from "../../../../inline-loops.macro"; +import { map } from "../../../../src/inline-loops.macro"; const doubledValues = map([1, 2, 3, 4], value => { const doubled = value * 2; diff --git a/__tests__/__fixtures__/uncached/mapObject/code.js b/__tests__/__fixtures__/uncached/mapObject/code.js index ba379e57..0a018449 100644 --- a/__tests__/__fixtures__/uncached/mapObject/code.js +++ b/__tests__/__fixtures__/uncached/mapObject/code.js @@ -1,4 +1,4 @@ -import { mapObject } from "../../../../inline-loops.macro"; +import { mapObject } from "../../../../src/inline-loops.macro"; const doubledValues = mapObject({ one: 1, two: 2, three: 3, four: 4 }, value => { const doubled = value * 2; diff --git a/__tests__/__fixtures__/uncached/mapRight/code.js b/__tests__/__fixtures__/uncached/mapRight/code.js index f4f1c118..e6dde179 100644 --- a/__tests__/__fixtures__/uncached/mapRight/code.js +++ b/__tests__/__fixtures__/uncached/mapRight/code.js @@ -1,4 +1,4 @@ -import { mapRight } from "../../../../inline-loops.macro"; +import { mapRight } from "../../../../src/inline-loops.macro"; const doubledValues = mapRight([1, 2, 3, 4], value => { const doubled = value * 2; diff --git a/__tests__/__fixtures__/uncached/reduce-no-initialValue/code.js b/__tests__/__fixtures__/uncached/reduce-no-initialValue/code.js index 0f1bd122..0af17f3c 100644 --- a/__tests__/__fixtures__/uncached/reduce-no-initialValue/code.js +++ b/__tests__/__fixtures__/uncached/reduce-no-initialValue/code.js @@ -1,4 +1,4 @@ -import { reduce } from "../../../../inline-loops.macro"; +import { reduce } from "../../../../src/inline-loops.macro"; const doubledValues = reduce([1, 2, 3, 4], (agg, value, index) => { agg[index] = value * 2; diff --git a/__tests__/__fixtures__/uncached/reduce/code.js b/__tests__/__fixtures__/uncached/reduce/code.js index f47fd442..7fe27272 100644 --- a/__tests__/__fixtures__/uncached/reduce/code.js +++ b/__tests__/__fixtures__/uncached/reduce/code.js @@ -1,4 +1,4 @@ -import { reduce } from "../../../../inline-loops.macro"; +import { reduce } from "../../../../src/inline-loops.macro"; const doubledValues = reduce( [1, 2, 3, 4], diff --git a/__tests__/__fixtures__/uncached/reduceObject-no-initialValue/code.js b/__tests__/__fixtures__/uncached/reduceObject-no-initialValue/code.js index bef206c3..5e42fede 100644 --- a/__tests__/__fixtures__/uncached/reduceObject-no-initialValue/code.js +++ b/__tests__/__fixtures__/uncached/reduceObject-no-initialValue/code.js @@ -1,4 +1,4 @@ -import { reduceObject } from "../../../../inline-loops.macro"; +import { reduceObject } from "../../../../src/inline-loops.macro"; const doubledValues = reduceObject( { one: 1, two: 2, three: 3, four: 4 }, diff --git a/__tests__/__fixtures__/uncached/reduceObject/code.js b/__tests__/__fixtures__/uncached/reduceObject/code.js index 20440884..c0bb7cd8 100644 --- a/__tests__/__fixtures__/uncached/reduceObject/code.js +++ b/__tests__/__fixtures__/uncached/reduceObject/code.js @@ -1,4 +1,4 @@ -import { reduceObject } from "../../../../inline-loops.macro"; +import { reduceObject } from "../../../../src/inline-loops.macro"; const doubledValues = reduceObject( { one: 1, two: 2, three: 3, four: 4 }, diff --git a/__tests__/__fixtures__/uncached/reduceRight-no-initialValue/code.js b/__tests__/__fixtures__/uncached/reduceRight-no-initialValue/code.js index 4263b792..25ec6e83 100644 --- a/__tests__/__fixtures__/uncached/reduceRight-no-initialValue/code.js +++ b/__tests__/__fixtures__/uncached/reduceRight-no-initialValue/code.js @@ -1,4 +1,4 @@ -import { reduceRight } from "../../../../inline-loops.macro"; +import { reduceRight } from "../../../../src/inline-loops.macro"; const doubledValues = reduceRight([1, 2, 3, 4], (agg, value, index) => { agg[index] = value * 2; diff --git a/__tests__/__fixtures__/uncached/reduceRight/code.js b/__tests__/__fixtures__/uncached/reduceRight/code.js index a810bbd9..6f05dd6e 100644 --- a/__tests__/__fixtures__/uncached/reduceRight/code.js +++ b/__tests__/__fixtures__/uncached/reduceRight/code.js @@ -1,4 +1,4 @@ -import { reduceRight } from "../../../../inline-loops.macro"; +import { reduceRight } from "../../../../src/inline-loops.macro"; const doubledValues = reduceRight( [1, 2, 3, 4], diff --git a/__tests__/__fixtures__/uncached/some/code.js b/__tests__/__fixtures__/uncached/some/code.js index bad0c891..8916ef16 100644 --- a/__tests__/__fixtures__/uncached/some/code.js +++ b/__tests__/__fixtures__/uncached/some/code.js @@ -1,4 +1,4 @@ -import { some } from "../../../../inline-loops.macro"; +import { some } from "../../../../src/inline-loops.macro"; const areAnyEven = some([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/someObject/code.js b/__tests__/__fixtures__/uncached/someObject/code.js index 9f6ea3be..916fec77 100644 --- a/__tests__/__fixtures__/uncached/someObject/code.js +++ b/__tests__/__fixtures__/uncached/someObject/code.js @@ -1,4 +1,4 @@ -import { someObject } from "../../../../inline-loops.macro"; +import { someObject } from "../../../../src/inline-loops.macro"; const areAnyEven = someObject({ one: 1, two: 2, three: 3, four: 4 }, value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__fixtures__/uncached/someRight/code.js b/__tests__/__fixtures__/uncached/someRight/code.js index 4a5283b5..04930276 100644 --- a/__tests__/__fixtures__/uncached/someRight/code.js +++ b/__tests__/__fixtures__/uncached/someRight/code.js @@ -1,4 +1,4 @@ -import { someRight } from "../../../../inline-loops.macro"; +import { someRight } from "../../../../src/inline-loops.macro"; const areAnyEven = someRight([1, 2, 3, 4], value => { const isValueEven = value % 2 === 0; diff --git a/__tests__/__runtime__/error-macro-as-handler.js b/__tests__/__runtime__/error-macro-as-handler.js index 0444f82d..6d1def0e 100644 --- a/__tests__/__runtime__/error-macro-as-handler.js +++ b/__tests__/__runtime__/error-macro-as-handler.js @@ -1,3 +1,3 @@ -const { every, map } = require('../../inline-loops.macro'); +const { every, map } = require('../../src/inline-loops.macro'); map([1, 2, 3, 4], every); diff --git a/__tests__/__runtime__/error-spread-arguments.js b/__tests__/__runtime__/error-spread-arguments.js index e91346a4..5e020953 100644 --- a/__tests__/__runtime__/error-spread-arguments.js +++ b/__tests__/__runtime__/error-spread-arguments.js @@ -1,4 +1,4 @@ -const { map } = require('../../inline-loops.macro'); +const { map } = require('../../src/inline-loops.macro'); const fn = v => typeof v === 'number'; const args = [[1, 2, 3], fn]; diff --git a/__tests__/__runtime__/every.js b/__tests__/__runtime__/every.js index 3a959aae..c0e535c8 100644 --- a/__tests__/__runtime__/every.js +++ b/__tests__/__runtime__/every.js @@ -1,4 +1,4 @@ -const { every, everyObject, everyRight } = require('../../inline-loops.macro'); +const { every, everyObject, everyRight } = require('../../src/inline-loops.macro'); const ARRAY = [1, 2, 3, 4, 5, 6]; const OBJECT = { diff --git a/__tests__/__runtime__/filter.js b/__tests__/__runtime__/filter.js index e205b0ab..2ea026be 100644 --- a/__tests__/__runtime__/filter.js +++ b/__tests__/__runtime__/filter.js @@ -1,4 +1,4 @@ -const { filter, filterObject, filterRight } = require('../../inline-loops.macro'); +const { filter, filterObject, filterRight } = require('../../src/inline-loops.macro'); const { deepEqual: isEqual } = require('fast-equals'); diff --git a/__tests__/__runtime__/find.js b/__tests__/__runtime__/find.js index f5033e39..26f1aa32 100644 --- a/__tests__/__runtime__/find.js +++ b/__tests__/__runtime__/find.js @@ -1,4 +1,4 @@ -const { find, findObject, findRight } = require("../../inline-loops.macro"); +const { find, findObject, findRight } = require("../../src/inline-loops.macro"); const { deepEqual: isEqual } = require("fast-equals"); diff --git a/__tests__/__runtime__/findIndex-findKey.js b/__tests__/__runtime__/findIndex-findKey.js index 17816fcd..95829e11 100644 --- a/__tests__/__runtime__/findIndex-findKey.js +++ b/__tests__/__runtime__/findIndex-findKey.js @@ -2,7 +2,7 @@ const { findIndex, findIndexRight, findKey -} = require("../../inline-loops.macro"); +} = require("../../src/inline-loops.macro"); const { deepEqual: isEqual } = require("fast-equals"); diff --git a/__tests__/__runtime__/forEach.js b/__tests__/__runtime__/forEach.js index da527e2d..049a7be1 100644 --- a/__tests__/__runtime__/forEach.js +++ b/__tests__/__runtime__/forEach.js @@ -2,7 +2,7 @@ const { forEach, forEachObject, forEachRight -} = require("../../inline-loops.macro"); +} = require("../../src/inline-loops.macro"); const { deepEqual: isEqual } = require("fast-equals"); diff --git a/__tests__/__runtime__/map.js b/__tests__/__runtime__/map.js index 478783d7..2c76ed21 100644 --- a/__tests__/__runtime__/map.js +++ b/__tests__/__runtime__/map.js @@ -1,4 +1,4 @@ -const { map, mapObject, mapRight } = require("../../inline-loops.macro"); +const { map, mapObject, mapRight } = require("../../src/inline-loops.macro"); const { deepEqual: isEqual } = require("fast-equals"); diff --git a/__tests__/__runtime__/reduce-no-initialValue.js b/__tests__/__runtime__/reduce-no-initialValue.js index 72c383ec..a521863a 100644 --- a/__tests__/__runtime__/reduce-no-initialValue.js +++ b/__tests__/__runtime__/reduce-no-initialValue.js @@ -2,7 +2,7 @@ const { reduce, reduceObject, reduceRight -} = require("../../inline-loops.macro"); +} = require("../../src/inline-loops.macro"); const { deepEqual: isEqual } = require("fast-equals"); diff --git a/__tests__/__runtime__/reduce.js b/__tests__/__runtime__/reduce.js index fb6f7514..0c970b5b 100644 --- a/__tests__/__runtime__/reduce.js +++ b/__tests__/__runtime__/reduce.js @@ -2,7 +2,7 @@ const { reduce, reduceObject, reduceRight -} = require("../../inline-loops.macro"); +} = require("../../src/inline-loops.macro"); const { deepEqual: isEqual } = require("fast-equals"); diff --git a/__tests__/__runtime__/some.js b/__tests__/__runtime__/some.js index eb405c3b..87897d57 100644 --- a/__tests__/__runtime__/some.js +++ b/__tests__/__runtime__/some.js @@ -1,4 +1,4 @@ -const { some, someObject, someRight } = require("../../inline-loops.macro"); +const { some, someObject, someRight } = require("../../src/inline-loops.macro"); const ARRAY = [1, 2, 3, 4, 5, 6]; const OBJECT = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 }; diff --git a/build/handlers.js b/build/handlers.js new file mode 100644 index 00000000..2a444fb4 --- /dev/null +++ b/build/handlers.js @@ -0,0 +1,475 @@ +"use strict"; + +var _require = require('./helpers'), + getDefaultResult = _require.getDefaultResult, + getIds = _require.getIds, + getLoop = _require.getLoop, + getUid = _require.getUid, + getResultStatement = _require.getResultStatement, + insertBeforeParent = _require.insertBeforeParent, + isCachedReference = _require.isCachedReference; + +function handleEvery(_ref) { + var t = _ref.t, + path = _ref.path, + object = _ref.object, + handler = _ref.handler, + isDecrementing = _ref.isDecrementing, + isObject = _ref.isObject; + + var _getIds = getIds(path.scope), + fn = _getIds.fn, + iterable = _getIds.iterable, + key = _getIds.key, + length = _getIds.length, + result = _getIds.result, + value = _getIds.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(t.unaryExpression('!', resultStatement), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, t.booleanLiteral(false))), t.breakStatement()])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: t.booleanLiteral(true), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleFilter(_ref2) { + var t = _ref2.t, + path = _ref2.path, + object = _ref2.object, + handler = _ref2.handler, + isDecrementing = _ref2.isDecrementing, + isObject = _ref2.isObject; + + var _getIds2 = getIds(path.scope), + fn = _getIds2.fn, + iterable = _getIds2.iterable, + key = _getIds2.key, + length = _getIds2.length, + result = _getIds2.result, + value = _getIds2.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultAssignment = isObject ? t.assignmentExpression('=', t.memberExpression(result, key, true), value) : t.callExpression(t.memberExpression(result, t.identifier('push')), [value]); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(resultStatement, t.expressionStatement(resultAssignment)); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + value: value, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: getDefaultResult(t, isObject), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleFind(_ref3) { + var t = _ref3.t, + path = _ref3.path, + object = _ref3.object, + handler = _ref3.handler, + isDecrementing = _ref3.isDecrementing, + isObject = _ref3.isObject; + + var _getIds3 = getIds(path.scope), + fn = _getIds3.fn, + iterable = _getIds3.iterable, + key = _getIds3.key, + length = _getIds3.length, + result = _getIds3.result, + value = _getIds3.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, value)), t.breakStatement()])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleFindKey(_ref4) { + var t = _ref4.t, + path = _ref4.path, + object = _ref4.object, + handler = _ref4.handler, + isDecrementing = _ref4.isDecrementing, + isObject = _ref4.isObject; + + var _getIds4 = getIds(path.scope), + fn = _getIds4.fn, + iterable = _getIds4.iterable, + key = _getIds4.key, + length = _getIds4.length, + result = _getIds4.result, + value = _getIds4.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, key)), t.breakStatement()])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: isObject ? undefined : t.numericLiteral(-1), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleForEach(_ref5) { + var t = _ref5.t, + path = _ref5.path, + object = _ref5.object, + handler = _ref5.handler, + isDecrementing = _ref5.isDecrementing, + isObject = _ref5.isObject; + + var _getIds5 = getIds(path.scope), + fn = _getIds5.fn, + iterable = _getIds5.iterable, + key = _getIds5.key, + length = _getIds5.length, + value = _getIds5.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var call = t.expressionStatement(resultStatement); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, call]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + resultStatement: resultStatement, + t: t, + value: value + }); + path.parentPath.remove(); +} + +function handleMap(_ref6) { + var t = _ref6.t, + path = _ref6.path, + object = _ref6.object, + handler = _ref6.handler, + isDecrementing = _ref6.isDecrementing, + isObject = _ref6.isObject; + + var _getIds6 = getIds(path.scope), + fn = _getIds6.fn, + iterable = _getIds6.iterable, + key = _getIds6.key, + length = _getIds6.length, + result = _getIds6.result, + value = _getIds6.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.expressionStatement(isObject ? t.assignmentExpression('=', t.memberExpression(result, key, true), resultStatement) : t.callExpression(t.memberExpression(result, t.identifier('push')), [resultStatement])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: getDefaultResult(t, isObject), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleReduce(_ref7) { + var t = _ref7.t, + path = _ref7.path, + object = _ref7.object, + handler = _ref7.handler, + initialValue = _ref7.initialValue, + isDecrementing = _ref7.isDecrementing, + isObject = _ref7.isObject; + + var _getIds7 = getIds(path.scope), + fn = _getIds7.fn, + iterable = _getIds7.iterable, + key = _getIds7.key, + length = _getIds7.length, + result = _getIds7.result, + value = _getIds7.value; + + var hasInitialValue = !!initialValue; + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var injected = []; + var hasInitialValueId; + + if (!hasInitialValue) { + if (isObject) { + hasInitialValueId = getUid(path.scope, 'hasInitialValue'); + injected.push(t.variableDeclaration('let', [t.variableDeclarator(hasInitialValueId, t.booleanLiteral(false))])); + } else if (isDecrementing) { + injected.push(t.variableDeclaration('const', [t.variableDeclarator(length, t.memberExpression(iterableUsed, t.identifier('length')))])); + initialValue = t.memberExpression(iterableUsed, t.binaryExpression('-', length, t.numericLiteral(1)), true); + } else { + initialValue = t.memberExpression(iterableUsed, t.numericLiteral(0), true); + } + } + + if (isObject) { + injected.push(t.variableDeclaration('let', [t.variableDeclarator(value)])); + } + + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var call = t.callExpression(fnUsed, [result, value, key, iterableUsed]); + var resultAssignment = t.assignmentExpression('=', result, call); + var block; + + if (!hasInitialValue && isObject) { + var ifHasInitialValue = t.ifStatement(hasInitialValueId, t.blockStatement([valueAssignment, t.expressionStatement(resultAssignment)]), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', hasInitialValueId, t.booleanLiteral(true))), t.expressionStatement(t.assignmentExpression('=', result, t.memberExpression(iterableUsed, key, true)))])); + block = [ifHasInitialValue]; + } else { + block = [valueAssignment, t.expressionStatement(resultAssignment)]; + } + + var loop = getLoop({ + t: t, + body: t.blockStatement(block), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + + if (!hasInitialValue && !isObject) { + var keyValue = loop.init.declarations.find(function (_ref8) { + var id = _ref8.id; + return id.name === key.name; + }); + + if (isDecrementing) { + keyValue.init.left = length; + keyValue.init.right = t.numericLiteral(2); + } else { + keyValue.init = t.numericLiteral(1); + } + } + + var insertBefore = []; + + if (iterableUsed === iterable) { + var iterableVar = t.variableDeclaration('const', [t.variableDeclarator(iterable, object)]); + insertBefore.push(iterableVar); + } + + insertBefore.push.apply(insertBefore, injected); + + if (fnUsed === fn) { + var handlerVar = t.variableDeclaration('const', [t.variableDeclarator(fn, handler)]); + insertBefore.push(handlerVar); + } + + var resultVar = t.variableDeclaration('let', [t.variableDeclarator(result, initialValue)]); + insertBefore.push(resultVar); + insertBefore.push(loop); + path.getStatementParent().insertBefore(insertBefore); + path.parentPath.replaceWith(result); +} + +function handleSome(_ref9) { + var t = _ref9.t, + path = _ref9.path, + object = _ref9.object, + handler = _ref9.handler, + isDecrementing = _ref9.isDecrementing, + isObject = _ref9.isObject; + + var _getIds8 = getIds(path.scope), + fn = _getIds8.fn, + iterable = _getIds8.iterable, + key = _getIds8.key, + length = _getIds8.length, + result = _getIds8.result, + value = _getIds8.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, t.booleanLiteral(true))), t.breakStatement()])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: t.booleanLiteral(false), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +module.exports = { + handleEvery: handleEvery, + handleFilter: handleFilter, + handleFind: handleFind, + handleFindKey: handleFindKey, + handleForEach: handleForEach, + handleMap: handleMap, + handleReduce: handleReduce, + handleSome: handleSome +}; \ No newline at end of file diff --git a/build/helpers.js b/build/helpers.js new file mode 100644 index 00000000..ab1796be --- /dev/null +++ b/build/helpers.js @@ -0,0 +1,237 @@ +"use strict"; + +function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } + +function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } + +function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } + +function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } + +function getDefaultResult(t, isObject) { + return isObject ? t.objectExpression([]) : t.arrayExpression(); +} + +var ID_TYPES = ['fn', 'iterable', 'key', 'length', 'result', 'value']; + +function getIds(scope) { + return ID_TYPES.reduce(function (ids, type) { + ids[type] = getUid(scope, type); + return ids; + }, {}); +} + +function getLoop(_ref) { + var t = _ref.t, + body = _ref.body, + iterable = _ref.iterable, + key = _ref.key, + length = _ref.length, + value = _ref.value, + isDecrementing = _ref.isDecrementing, + isObject = _ref.isObject; + + if (isObject) { + var left = t.variableDeclaration('let', [t.variableDeclarator(key)]); + var right = iterable; + return t.forInStatement(left, right, body); + } + + var assignments; + var test; + var update; + + if (isDecrementing) { + assignments = [t.variableDeclarator(key, t.binaryExpression('-', t.memberExpression(iterable, t.identifier('length')), t.numericLiteral(1)))]; + test = t.binaryExpression('>=', key, t.numericLiteral(0)); + update = t.updateExpression('--', key, true); + } else { + assignments = [t.variableDeclarator(key, t.numericLiteral(0)), t.variableDeclarator(length, t.memberExpression(iterable, t.identifier('length')))]; + test = t.binaryExpression('<', key, length); + update = t.updateExpression('++', key, true); + } + + if (value) { + assignments.push(t.variableDeclarator(value)); + } + + return t.forStatement(t.variableDeclaration('let', assignments), test, update, body); +} + +function getResultStatement(t, handler, fn, value, key, iterable, path) { + if (t.isArrowFunctionExpression(handler)) { + var body = handler.body; + + if (t.isBlockStatement(body)) { + // eslint-disable-next-line prefer-destructuring + body = body.body; + + if (body.length === 1) { + var _handler$params = _slicedToArray(handler.params, 3), + v = _handler$params[0], + k = _handler$params[1], + i = _handler$params[2]; + + var parentPath = path.parentPath; + var node = body[0]; + parentPath.traverse({ + ArrowFunctionExpression: function ArrowFunctionExpression(_path) { + if (v) { + _path.scope.rename(v.name, value.name); + } + + if (k) { + _path.scope.rename(k.name, key.name); + } + + if (i) { + _path.scope.rename(i.name, iterable.name); + } + } + }); + + if (t.isExpression(node)) { + return node; + } + + if (t.isExpressionStatement(node)) { + return node.expression; + } + + if (t.isReturnStatement(node)) { + return node.argument; + } + } + } else { + var _handler$params2 = _slicedToArray(handler.params, 3), + _v = _handler$params2[0], + _k = _handler$params2[1], + _i2 = _handler$params2[2]; + + var _parentPath = path.parentPath; + + _parentPath.traverse({ + ArrowFunctionExpression: function ArrowFunctionExpression(_path) { + if (_v) { + _path.scope.rename(_v.name, value.name); + } + + if (_k) { + _path.scope.rename(_k.name, key.name); + } + + if (_i2) { + _path.scope.rename(_i2.name, iterable.name); + } + } + }); + + if (t.isExpression(body)) { + return body; + } + } + } + + if (t.isFunctionExpression(handler)) { + var _body = handler.body.body; + + if (_body.length === 1) { + var _handler$params3 = _slicedToArray(handler.params, 3), + _v2 = _handler$params3[0], + _k2 = _handler$params3[1], + _i3 = _handler$params3[2]; + + var _parentPath2 = path.parentPath; + var _node = _body[0]; + + _parentPath2.traverse({ + FunctionExpression: function FunctionExpression(_path) { + if (_v2) { + _path.scope.rename(_v2.name, value.name); + } + + if (_k2) { + _path.scope.rename(_k2.name, key.name); + } + + if (_i3) { + _path.scope.rename(_i3.name, iterable.name); + } + } + }); + + if (t.isExpression(_node)) { + return _node; + } + + if (t.isExpressionStatement(_node)) { + return _node.expression; + } + + if (t.isReturnStatement(_node)) { + return _node.argument; + } + } + } + + var callExpression = t.callExpression(fn, [value, key, iterable]); + callExpression.__inlineLoopsMacroFallback = true; + return callExpression; +} + +function getUid(scope, name) { + return scope.generateUidIdentifier(name); +} + +function insertBeforeParent(_ref2) { + var fn = _ref2.fn, + handler = _ref2.handler, + isObject = _ref2.isObject, + iterable = _ref2.iterable, + loop = _ref2.loop, + object = _ref2.object, + path = _ref2.path, + result = _ref2.result, + resultStatement = _ref2.resultStatement, + resultValue = _ref2.resultValue, + t = _ref2.t, + value = _ref2.value; + var insertBefore = []; + + if (!isCachedReference(t, object)) { + var iterableVar = t.variableDeclaration('const', [t.variableDeclarator(iterable, object)]); + insertBefore.push(iterableVar); + } + + if (!isCachedReference(t, handler) && t.isCallExpression(resultStatement) && resultStatement.__inlineLoopsMacroFallback) { + var handlerVar = t.variableDeclaration('const', [t.variableDeclarator(fn, handler)]); + insertBefore.push(handlerVar); + } + + if (result) { + var resultVar = t.variableDeclaration('let', [t.variableDeclarator(result, resultValue)]); + insertBefore.push(resultVar); + } + + if (isObject) { + var valueVar = t.variableDeclaration('let', [t.variableDeclarator(value)]); + insertBefore.push(valueVar); + } + + insertBefore.push(loop); + path.getStatementParent().insertBefore(insertBefore); +} + +function isCachedReference(t, node) { + return t.isIdentifier(node); +} + +module.exports = { + getDefaultResult: getDefaultResult, + getIds: getIds, + getLoop: getLoop, + getUid: getUid, + getResultStatement: getResultStatement, + insertBeforeParent: insertBeforeParent, + isCachedReference: isCachedReference +}; \ No newline at end of file diff --git a/build/inline-loops.macro.js b/build/inline-loops.macro.js index fbaf5eb3..479ff062 100644 --- a/build/inline-loops.macro.js +++ b/build/inline-loops.macro.js @@ -1,13 +1,5 @@ "use strict"; -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } - -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } - -function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } - -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } - function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } @@ -16,681 +8,27 @@ function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } -var _require = require('babel-plugin-macros'), - createMacro = _require.createMacro, - MacroError = _require.MacroError; - -function getDefaultResult(t, isObject) { - return isObject ? t.objectExpression([]) : t.arrayExpression(); -} - -var ID_TYPES = ['fn', 'iterable', 'key', 'length', 'result', 'value']; - -function getIds(scope) { - return ID_TYPES.reduce(function (ids, type) { - ids[type] = getUid(scope, type); - return ids; - }, {}); -} - -function getLoop(_ref) { - var t = _ref.t, - body = _ref.body, - iterable = _ref.iterable, - key = _ref.key, - length = _ref.length, - value = _ref.value, - isDecrementing = _ref.isDecrementing, - isObject = _ref.isObject; - - if (isObject) { - var left = t.variableDeclaration('let', [t.variableDeclarator(key)]); - var right = iterable; - return t.forInStatement(left, right, body); - } - - var assignments; - var test; - var update; - - if (isDecrementing) { - assignments = [t.variableDeclarator(key, t.binaryExpression('-', t.memberExpression(iterable, t.identifier('length')), t.numericLiteral(1)))]; - test = t.binaryExpression('>=', key, t.numericLiteral(0)); - update = t.updateExpression('--', key, true); - } else { - assignments = [t.variableDeclarator(key, t.numericLiteral(0)), t.variableDeclarator(length, t.memberExpression(iterable, t.identifier('length')))]; - test = t.binaryExpression('<', key, length); - update = t.updateExpression('++', key, true); - } - - if (value) { - assignments.push(t.variableDeclarator(value)); - } - - return t.forStatement(t.variableDeclaration('let', assignments), test, update, body); -} - -function getResultStatement(t, handler, fn, value, key, iterable, path) { - if (t.isArrowFunctionExpression(handler)) { - var body = handler.body; - - if (t.isBlockStatement(body)) { - // eslint-disable-next-line prefer-destructuring - body = body.body; - - if (body.length === 1) { - var _handler$params = _slicedToArray(handler.params, 3), - v = _handler$params[0], - k = _handler$params[1], - i = _handler$params[2]; - - var parentPath = path.parentPath; - var node = body[0]; - parentPath.traverse({ - ArrowFunctionExpression: function ArrowFunctionExpression(_path) { - if (v) { - _path.scope.rename(v.name, value.name); - } - - if (k) { - _path.scope.rename(k.name, key.name); - } - - if (i) { - _path.scope.rename(i.name, iterable.name); - } - } - }); - - if (t.isExpression(node)) { - return node; - } - - if (t.isExpressionStatement(node)) { - return node.expression; - } - - if (t.isReturnStatement(node)) { - return node.argument; - } - } - } else { - var _handler$params2 = _slicedToArray(handler.params, 3), - _v = _handler$params2[0], - _k = _handler$params2[1], - _i2 = _handler$params2[2]; - - var _parentPath = path.parentPath; - - _parentPath.traverse({ - ArrowFunctionExpression: function ArrowFunctionExpression(_path) { - if (_v) { - _path.scope.rename(_v.name, value.name); - } - - if (_k) { - _path.scope.rename(_k.name, key.name); - } - - if (_i2) { - _path.scope.rename(_i2.name, iterable.name); - } - } - }); - - if (t.isExpression(body)) { - return body; - } - } - } - - if (t.isFunctionExpression(handler)) { - var _body = handler.body.body; - - if (_body.length === 1) { - var _handler$params3 = _slicedToArray(handler.params, 3), - _v2 = _handler$params3[0], - _k2 = _handler$params3[1], - _i3 = _handler$params3[2]; - - var _parentPath2 = path.parentPath; - var _node = _body[0]; - - _parentPath2.traverse({ - FunctionExpression: function FunctionExpression(_path) { - if (_v2) { - _path.scope.rename(_v2.name, value.name); - } - - if (_k2) { - _path.scope.rename(_k2.name, key.name); - } - - if (_i3) { - _path.scope.rename(_i3.name, iterable.name); - } - } - }); - - if (t.isExpression(_node)) { - return _node; - } - - if (t.isExpressionStatement(_node)) { - return _node.expression; - } - - if (t.isReturnStatement(_node)) { - return _node.argument; - } - } - } - - var callExpression = t.callExpression(fn, [value, key, iterable]); - callExpression.__inlineLoopsMacroFallback = true; - return callExpression; -} - -function getUid(scope, name) { - return scope.generateUidIdentifier(name); -} - -function insertBeforeParent(_ref2) { - var fn = _ref2.fn, - handler = _ref2.handler, - isObject = _ref2.isObject, - iterable = _ref2.iterable, - loop = _ref2.loop, - object = _ref2.object, - path = _ref2.path, - result = _ref2.result, - resultStatement = _ref2.resultStatement, - resultValue = _ref2.resultValue, - t = _ref2.t, - value = _ref2.value; - var insertBefore = []; - - if (!isCachedReference(t, object)) { - var iterableVar = t.variableDeclaration('const', [t.variableDeclarator(iterable, object)]); - insertBefore.push(iterableVar); - } - - if (!isCachedReference(t, handler) && t.isCallExpression(resultStatement) && resultStatement.__inlineLoopsMacroFallback) { - var handlerVar = t.variableDeclaration('const', [t.variableDeclarator(fn, handler)]); - insertBefore.push(handlerVar); - } - - if (result) { - var resultVar = t.variableDeclaration('let', [t.variableDeclarator(result, resultValue)]); - insertBefore.push(resultVar); - } - - if (isObject) { - var valueVar = t.variableDeclaration('let', [t.variableDeclarator(value)]); - insertBefore.push(valueVar); - } - - insertBefore.push(loop); - path.getStatementParent().insertBefore(insertBefore); -} - -function isCachedReference(t, node) { - return t.isIdentifier(node); -} - -function handleEvery(_ref3) { - var t = _ref3.t, - path = _ref3.path, - object = _ref3.object, - handler = _ref3.handler, - isDecrementing = _ref3.isDecrementing, - isObject = _ref3.isObject; - - var _getIds = getIds(path.scope), - fn = _getIds.fn, - iterable = _getIds.iterable, - key = _getIds.key, - length = _getIds.length, - result = _getIds.result, - value = _getIds.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(t.unaryExpression('!', resultStatement), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, t.booleanLiteral(false))), t.breakStatement()])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: t.booleanLiteral(true), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleFilter(_ref4) { - var t = _ref4.t, - path = _ref4.path, - object = _ref4.object, - handler = _ref4.handler, - isDecrementing = _ref4.isDecrementing, - isObject = _ref4.isObject; - - var _getIds2 = getIds(path.scope), - fn = _getIds2.fn, - iterable = _getIds2.iterable, - key = _getIds2.key, - length = _getIds2.length, - result = _getIds2.result, - value = _getIds2.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultAssignment = isObject ? t.assignmentExpression('=', t.memberExpression(result, key, true), value) : t.callExpression(t.memberExpression(result, t.identifier('push')), [value]); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(resultStatement, t.expressionStatement(resultAssignment)); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - value: value, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: getDefaultResult(t, isObject), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleFind(_ref5) { - var t = _ref5.t, - path = _ref5.path, - object = _ref5.object, - handler = _ref5.handler, - isDecrementing = _ref5.isDecrementing, - isObject = _ref5.isObject; - - var _getIds3 = getIds(path.scope), - fn = _getIds3.fn, - iterable = _getIds3.iterable, - key = _getIds3.key, - length = _getIds3.length, - result = _getIds3.result, - value = _getIds3.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, value)), t.breakStatement()])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleFindKey(_ref6) { - var t = _ref6.t, - path = _ref6.path, - object = _ref6.object, - handler = _ref6.handler, - isDecrementing = _ref6.isDecrementing, - isObject = _ref6.isObject; - - var _getIds4 = getIds(path.scope), - fn = _getIds4.fn, - iterable = _getIds4.iterable, - key = _getIds4.key, - length = _getIds4.length, - result = _getIds4.result, - value = _getIds4.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, key)), t.breakStatement()])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: isObject ? undefined : t.numericLiteral(-1), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleForEach(_ref7) { - var t = _ref7.t, - path = _ref7.path, - object = _ref7.object, - handler = _ref7.handler, - isDecrementing = _ref7.isDecrementing, - isObject = _ref7.isObject; - - var _getIds5 = getIds(path.scope), - fn = _getIds5.fn, - iterable = _getIds5.iterable, - key = _getIds5.key, - length = _getIds5.length, - value = _getIds5.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var call = t.expressionStatement(resultStatement); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, call]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - resultStatement: resultStatement, - t: t, - value: value - }); - path.parentPath.remove(); -} - -function handleMap(_ref8) { - var t = _ref8.t, - path = _ref8.path, - object = _ref8.object, - handler = _ref8.handler, - isDecrementing = _ref8.isDecrementing, - isObject = _ref8.isObject; - - var _getIds6 = getIds(path.scope), - fn = _getIds6.fn, - iterable = _getIds6.iterable, - key = _getIds6.key, - length = _getIds6.length, - result = _getIds6.result, - value = _getIds6.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.expressionStatement(isObject ? t.assignmentExpression('=', t.memberExpression(result, key, true), resultStatement) : t.callExpression(t.memberExpression(result, t.identifier('push')), [resultStatement])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: getDefaultResult(t, isObject), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleReduce(_ref9) { - var t = _ref9.t, - path = _ref9.path, - object = _ref9.object, - handler = _ref9.handler, - initialValue = _ref9.initialValue, - isDecrementing = _ref9.isDecrementing, - isObject = _ref9.isObject; - - var _getIds7 = getIds(path.scope), - fn = _getIds7.fn, - iterable = _getIds7.iterable, - key = _getIds7.key, - length = _getIds7.length, - result = _getIds7.result, - value = _getIds7.value; - - var hasInitialValue = !!initialValue; - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var injected = []; - var hasInitialValueId; - - if (!hasInitialValue) { - if (isObject) { - hasInitialValueId = getUid(path.scope, 'hasInitialValue'); - injected.push(t.variableDeclaration('let', [t.variableDeclarator(hasInitialValueId, t.booleanLiteral(false))])); - } else if (isDecrementing) { - injected.push(t.variableDeclaration('const', [t.variableDeclarator(length, t.memberExpression(iterableUsed, t.identifier('length')))])); - initialValue = t.memberExpression(iterableUsed, t.binaryExpression('-', length, t.numericLiteral(1)), true); - } else { - initialValue = t.memberExpression(iterableUsed, t.numericLiteral(0), true); - } - } - - if (isObject) { - injected.push(t.variableDeclaration('let', [t.variableDeclarator(value)])); - } - - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var call = t.callExpression(fnUsed, [result, value, key, iterableUsed]); - var resultAssignment = t.assignmentExpression('=', result, call); - var block; - - if (!hasInitialValue && isObject) { - var ifHasInitialValue = t.ifStatement(hasInitialValueId, t.blockStatement([valueAssignment, t.expressionStatement(resultAssignment)]), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', hasInitialValueId, t.booleanLiteral(true))), t.expressionStatement(t.assignmentExpression('=', result, t.memberExpression(iterableUsed, key, true)))])); - block = [ifHasInitialValue]; - } else { - block = [valueAssignment, t.expressionStatement(resultAssignment)]; - } - - var loop = getLoop({ - t: t, - body: t.blockStatement(block), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - - if (!hasInitialValue && !isObject) { - var keyValue = loop.init.declarations.find(function (_ref10) { - var id = _ref10.id; - return id.name === key.name; - }); - - if (isDecrementing) { - keyValue.init.left = length; - keyValue.init.right = t.numericLiteral(2); - } else { - keyValue.init = t.numericLiteral(1); - } - } - - var insertBefore = []; +function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } - if (iterableUsed === iterable) { - var iterableVar = t.variableDeclaration('const', [t.variableDeclarator(iterable, object)]); - insertBefore.push(iterableVar); - } +function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } - insertBefore.push.apply(insertBefore, injected); +function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } - if (fnUsed === fn) { - var handlerVar = t.variableDeclaration('const', [t.variableDeclarator(fn, handler)]); - insertBefore.push(handlerVar); - } +function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } - var resultVar = t.variableDeclaration('let', [t.variableDeclarator(result, initialValue)]); - insertBefore.push(resultVar); - insertBefore.push(loop); - path.getStatementParent().insertBefore(insertBefore); - path.parentPath.replaceWith(result); -} +var _require = require('babel-plugin-macros'), + createMacro = _require.createMacro, + MacroError = _require.MacroError; -function handleSome(_ref11) { - var t = _ref11.t, - path = _ref11.path, - object = _ref11.object, - handler = _ref11.handler, - isDecrementing = _ref11.isDecrementing, - isObject = _ref11.isObject; - - var _getIds8 = getIds(path.scope), - fn = _getIds8.fn, - iterable = _getIds8.iterable, - key = _getIds8.key, - length = _getIds8.length, - result = _getIds8.result, - value = _getIds8.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, t.booleanLiteral(true))), t.breakStatement()])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: t.booleanLiteral(false), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} +var _require2 = require('./handlers'), + handleEvery = _require2.handleEvery, + handleFilter = _require2.handleFilter, + handleFind = _require2.handleFind, + handleFindKey = _require2.handleFindKey, + handleForEach = _require2.handleForEach, + handleMap = _require2.handleMap, + handleReduce = _require2.handleReduce, + handleSome = _require2.handleSome; var METHODS = ['every', 'filter', 'find', 'findIndex', 'findKey', 'forEach', 'map', 'reduce', 'some']; var ARRAY_ONLY_METHODS = ['findIndex']; @@ -736,9 +74,9 @@ function getCallTypes(references, method) { }; } -function inlineLoops(_ref12) { - var references = _ref12.references, - babel = _ref12.babel; +function inlineLoops(_ref) { + var references = _ref.references, + babel = _ref.babel; var t = babel.types; var allMethods = []; METHODS.forEach(function (method) { @@ -759,13 +97,13 @@ function inlineLoops(_ref12) { return allMethods.push.apply(allMethods, _toConsumableArray(incrementingCalls).concat(_toConsumableArray(decrementingCalls), _toConsumableArray(objectCalls))); }); - allMethods.forEach(function (_ref13) { - var path = _ref13.path; + allMethods.forEach(function (_ref2) { + var path = _ref2.path; path.node.__inlineLoopsMacro = true; }); - allMethods.sort(function (_ref14, _ref15) { - var a = _ref14.path; - var b = _ref15.path; + allMethods.sort(function (_ref3, _ref4) { + var a = _ref3.path; + var b = _ref4.path; var aContainer = a.container; var bContainer = b.container; @@ -837,8 +175,8 @@ function inlineLoops(_ref12) { handler = _args[1], initialValue = _args[2]; - var isHandlerMacro = allMethods.find(function (_ref16) { - var methodPath = _ref16.path; + var isHandlerMacro = allMethods.find(function (_ref5) { + var methodPath = _ref5.path; return methodPath.node !== path.node && handler === methodPath.node; }); @@ -858,11 +196,11 @@ function inlineLoops(_ref12) { }; } - allMethods.forEach(function (_ref17) { - var method = _ref17.method, - path = _ref17.path, - sourceMethod = _ref17.sourceMethod, - type = _ref17.type; + allMethods.forEach(function (_ref6) { + var method = _ref6.method, + path = _ref6.path, + sourceMethod = _ref6.sourceMethod, + type = _ref6.type; var isDecrementing = type === 'decrementing'; var isObject = type === 'object'; var handler = createTransformer(method, handlers[sourceMethod], isDecrementing, isObject); diff --git a/dist/handlers.js b/dist/handlers.js new file mode 100644 index 00000000..fd9a3535 --- /dev/null +++ b/dist/handlers.js @@ -0,0 +1,476 @@ +"use strict"; + +var _require = require('./helpers'), + getDefaultResult = _require.getDefaultResult, + getIds = _require.getIds, + getLoop = _require.getLoop, + getUid = _require.getUid, + getReduceResultStatement = _require.getReduceResultStatement, + getResultStatement = _require.getResultStatement, + insertBeforeParent = _require.insertBeforeParent, + isCachedReference = _require.isCachedReference; + +function handleEvery(_ref) { + var t = _ref.t, + path = _ref.path, + object = _ref.object, + handler = _ref.handler, + isDecrementing = _ref.isDecrementing, + isObject = _ref.isObject; + + var _getIds = getIds(path.scope), + fn = _getIds.fn, + iterable = _getIds.iterable, + key = _getIds.key, + length = _getIds.length, + result = _getIds.result, + value = _getIds.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(t.unaryExpression('!', resultStatement), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, t.booleanLiteral(false))), t.breakStatement()])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: t.booleanLiteral(true), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleFilter(_ref2) { + var t = _ref2.t, + path = _ref2.path, + object = _ref2.object, + handler = _ref2.handler, + isDecrementing = _ref2.isDecrementing, + isObject = _ref2.isObject; + + var _getIds2 = getIds(path.scope), + fn = _getIds2.fn, + iterable = _getIds2.iterable, + key = _getIds2.key, + length = _getIds2.length, + result = _getIds2.result, + value = _getIds2.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultAssignment = isObject ? t.assignmentExpression('=', t.memberExpression(result, key, true), value) : t.callExpression(t.memberExpression(result, t.identifier('push')), [value]); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(resultStatement, t.expressionStatement(resultAssignment)); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + value: value, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: getDefaultResult(t, isObject), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleFind(_ref3) { + var t = _ref3.t, + path = _ref3.path, + object = _ref3.object, + handler = _ref3.handler, + isDecrementing = _ref3.isDecrementing, + isObject = _ref3.isObject; + + var _getIds3 = getIds(path.scope), + fn = _getIds3.fn, + iterable = _getIds3.iterable, + key = _getIds3.key, + length = _getIds3.length, + result = _getIds3.result, + value = _getIds3.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, value)), t.breakStatement()])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleFindKey(_ref4) { + var t = _ref4.t, + path = _ref4.path, + object = _ref4.object, + handler = _ref4.handler, + isDecrementing = _ref4.isDecrementing, + isObject = _ref4.isObject; + + var _getIds4 = getIds(path.scope), + fn = _getIds4.fn, + iterable = _getIds4.iterable, + key = _getIds4.key, + length = _getIds4.length, + result = _getIds4.result, + value = _getIds4.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, key)), t.breakStatement()])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: isObject ? undefined : t.numericLiteral(-1), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleForEach(_ref5) { + var t = _ref5.t, + path = _ref5.path, + object = _ref5.object, + handler = _ref5.handler, + isDecrementing = _ref5.isDecrementing, + isObject = _ref5.isObject; + + var _getIds5 = getIds(path.scope), + fn = _getIds5.fn, + iterable = _getIds5.iterable, + key = _getIds5.key, + length = _getIds5.length, + value = _getIds5.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var call = t.expressionStatement(resultStatement); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, call]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + resultStatement: resultStatement, + t: t, + value: value + }); + path.parentPath.remove(); +} + +function handleMap(_ref6) { + var t = _ref6.t, + path = _ref6.path, + object = _ref6.object, + handler = _ref6.handler, + isDecrementing = _ref6.isDecrementing, + isObject = _ref6.isObject; + + var _getIds6 = getIds(path.scope), + fn = _getIds6.fn, + iterable = _getIds6.iterable, + key = _getIds6.key, + length = _getIds6.length, + result = _getIds6.result, + value = _getIds6.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.expressionStatement(isObject ? t.assignmentExpression('=', t.memberExpression(result, key, true), resultStatement) : t.callExpression(t.memberExpression(result, t.identifier('push')), [resultStatement])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: getDefaultResult(t, isObject), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +function handleReduce(_ref7) { + var t = _ref7.t, + path = _ref7.path, + object = _ref7.object, + handler = _ref7.handler, + initialValue = _ref7.initialValue, + isDecrementing = _ref7.isDecrementing, + isObject = _ref7.isObject; + + var _getIds7 = getIds(path.scope), + fn = _getIds7.fn, + iterable = _getIds7.iterable, + key = _getIds7.key, + length = _getIds7.length, + result = _getIds7.result, + value = _getIds7.value; + + var hasInitialValue = !!initialValue; + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var injected = []; + var hasInitialValueId; + + if (!hasInitialValue) { + if (isObject) { + hasInitialValueId = getUid(path.scope, 'hasInitialValue'); + injected.push(t.variableDeclaration('let', [t.variableDeclarator(hasInitialValueId, t.booleanLiteral(false))])); + } else if (isDecrementing) { + injected.push(t.variableDeclaration('const', [t.variableDeclarator(length, t.memberExpression(iterableUsed, t.identifier('length')))])); + initialValue = t.memberExpression(iterableUsed, t.binaryExpression('-', length, t.numericLiteral(1)), true); + } else { + initialValue = t.memberExpression(iterableUsed, t.numericLiteral(0), true); + } + } + + if (isObject) { + injected.push(t.variableDeclaration('let', [t.variableDeclarator(value)])); + } + + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getReduceResultStatement(t, handler, fnUsed, result, value, key, iterableUsed, path); + var resultAssignment = t.assignmentExpression('=', result, resultStatement); + var block; + + if (!hasInitialValue && isObject) { + var ifHasInitialValue = t.ifStatement(hasInitialValueId, t.blockStatement([valueAssignment, t.expressionStatement(resultAssignment)]), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', hasInitialValueId, t.booleanLiteral(true))), t.expressionStatement(t.assignmentExpression('=', result, t.memberExpression(iterableUsed, key, true)))])); + block = [ifHasInitialValue]; + } else { + block = [valueAssignment, t.expressionStatement(resultAssignment)]; + } + + var loop = getLoop({ + t: t, + body: t.blockStatement(block), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + + if (!hasInitialValue && !isObject) { + var keyValue = loop.init.declarations.find(function (_ref8) { + var id = _ref8.id; + return id.name === key.name; + }); + + if (isDecrementing) { + keyValue.init.left = length; + keyValue.init.right = t.numericLiteral(2); + } else { + keyValue.init = t.numericLiteral(1); + } + } + + var insertBefore = []; + + if (iterableUsed === iterable) { + var iterableVar = t.variableDeclaration('const', [t.variableDeclarator(iterable, object)]); + insertBefore.push(iterableVar); + } + + insertBefore.push.apply(insertBefore, injected); + + if (fnUsed === fn && resultStatement.__inlineLoopsMacroFallback) { + var handlerVar = t.variableDeclaration('const', [t.variableDeclarator(fn, handler)]); + insertBefore.push(handlerVar); + } + + var resultVar = t.variableDeclaration('let', [t.variableDeclarator(result, initialValue)]); + insertBefore.push(resultVar); + insertBefore.push(loop); + path.getStatementParent().insertBefore(insertBefore); + path.parentPath.replaceWith(result); +} + +function handleSome(_ref9) { + var t = _ref9.t, + path = _ref9.path, + object = _ref9.object, + handler = _ref9.handler, + isDecrementing = _ref9.isDecrementing, + isObject = _ref9.isObject; + + var _getIds8 = getIds(path.scope), + fn = _getIds8.fn, + iterable = _getIds8.iterable, + key = _getIds8.key, + length = _getIds8.length, + result = _getIds8.result, + value = _getIds8.value; + + var isHandlerCached = isCachedReference(t, handler); + var isIterableCached = isCachedReference(t, object); + var fnUsed = isHandlerCached ? handler : fn; + var iterableUsed = isIterableCached ? object : iterable; + var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); + var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); + var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, t.booleanLiteral(true))), t.breakStatement()])); + var loop = getLoop({ + t: t, + body: t.blockStatement([valueAssignment, expr]), + iterable: iterableUsed, + key: key, + length: length, + isDecrementing: isDecrementing, + isObject: isObject, + scope: path.scope, + value: value + }); + insertBeforeParent({ + fn: fn, + handler: handler, + isObject: isObject, + iterable: iterable, + loop: loop, + object: object, + path: path, + result: result, + resultStatement: resultStatement, + resultValue: t.booleanLiteral(false), + t: t, + value: value + }); + path.parentPath.replaceWith(result); +} + +module.exports = { + handleEvery: handleEvery, + handleFilter: handleFilter, + handleFind: handleFind, + handleFindKey: handleFindKey, + handleForEach: handleForEach, + handleMap: handleMap, + handleReduce: handleReduce, + handleSome: handleSome +}; \ No newline at end of file diff --git a/dist/helpers.js b/dist/helpers.js new file mode 100644 index 00000000..ac9731ce --- /dev/null +++ b/dist/helpers.js @@ -0,0 +1,374 @@ +"use strict"; + +function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } + +function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } + +function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } + +function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } + +function getDefaultResult(t, isObject) { + return isObject ? t.objectExpression([]) : t.arrayExpression(); +} + +var ID_TYPES = ['fn', 'iterable', 'key', 'length', 'result', 'value']; + +function getIds(scope) { + return ID_TYPES.reduce(function (ids, type) { + ids[type] = getUid(scope, type); + return ids; + }, {}); +} + +function getLoop(_ref) { + var t = _ref.t, + body = _ref.body, + iterable = _ref.iterable, + key = _ref.key, + length = _ref.length, + value = _ref.value, + isDecrementing = _ref.isDecrementing, + isObject = _ref.isObject; + + if (isObject) { + var left = t.variableDeclaration('let', [t.variableDeclarator(key)]); + var right = iterable; + return t.forInStatement(left, right, body); + } + + var assignments; + var test; + var update; + + if (isDecrementing) { + assignments = [t.variableDeclarator(key, t.binaryExpression('-', t.memberExpression(iterable, t.identifier('length')), t.numericLiteral(1)))]; + test = t.binaryExpression('>=', key, t.numericLiteral(0)); + update = t.updateExpression('--', key, true); + } else { + assignments = [t.variableDeclarator(key, t.numericLiteral(0)), t.variableDeclarator(length, t.memberExpression(iterable, t.identifier('length')))]; + test = t.binaryExpression('<', key, length); + update = t.updateExpression('++', key, true); + } + + if (value) { + assignments.push(t.variableDeclarator(value)); + } + + return t.forStatement(t.variableDeclaration('let', assignments), test, update, body); +} + +function getReduceResultStatement(t, handler, fn, result, value, key, iterable, path) { + if (t.isArrowFunctionExpression(handler)) { + var body = handler.body; + + if (t.isBlockStatement(body)) { + // eslint-disable-next-line prefer-destructuring + body = body.body; + + if (body.length === 1) { + var _handler$params = _slicedToArray(handler.params, 4), + r = _handler$params[0], + v = _handler$params[1], + k = _handler$params[2], + i = _handler$params[3]; + + var parentPath = path.parentPath; + var node = body[0]; + parentPath.traverse({ + ArrowFunctionExpression: function ArrowFunctionExpression(_path) { + if (r) { + _path.scope.rename(r.name, result.name); + } + + if (v) { + _path.scope.rename(v.name, value.name); + } + + if (k) { + _path.scope.rename(k.name, key.name); + } + + if (i) { + _path.scope.rename(i.name, iterable.name); + } + } + }); + + if (t.isExpression(node)) { + return node; + } + + if (t.isExpressionStatement(node)) { + return node.expression; + } + + if (t.isReturnStatement(node)) { + return node.argument; + } + } + } else { + var _handler$params2 = _slicedToArray(handler.params, 4), + _r = _handler$params2[0], + _v = _handler$params2[1], + _k = _handler$params2[2], + _i2 = _handler$params2[3]; + + var _parentPath = path.parentPath; + + _parentPath.traverse({ + ArrowFunctionExpression: function ArrowFunctionExpression(_path) { + if (_r) { + _path.scope.rename(_r.name, result.name); + } + + if (_v) { + _path.scope.rename(_v.name, value.name); + } + + if (_k) { + _path.scope.rename(_k.name, key.name); + } + + if (_i2) { + _path.scope.rename(_i2.name, iterable.name); + } + } + }); + + if (t.isExpression(body)) { + return body; + } + } + } + + if (t.isFunctionExpression(handler)) { + var _body = handler.body.body; + + if (_body.length === 1) { + var _handler$params3 = _slicedToArray(handler.params, 4), + _r2 = _handler$params3[0], + _v2 = _handler$params3[1], + _k2 = _handler$params3[2], + _i3 = _handler$params3[3]; + + var _parentPath2 = path.parentPath; + var _node = _body[0]; + + _parentPath2.traverse({ + FunctionExpression: function FunctionExpression(_path) { + if (_r2) { + _path.scope.rename(_r2.name, result.name); + } + + if (_v2) { + _path.scope.rename(_v2.name, value.name); + } + + if (_k2) { + _path.scope.rename(_k2.name, key.name); + } + + if (_i3) { + _path.scope.rename(_i3.name, iterable.name); + } + } + }); + + if (t.isExpression(_node)) { + return _node; + } + + if (t.isExpressionStatement(_node)) { + return _node.expression; + } + + if (t.isReturnStatement(_node)) { + return _node.argument; + } + } + } + + var callExpression = t.callExpression(fn, [result, value, key, iterable]); + callExpression.__inlineLoopsMacroFallback = true; + return callExpression; +} + +function getResultStatement(t, handler, fn, value, key, iterable, path) { + if (t.isArrowFunctionExpression(handler)) { + var body = handler.body; + + if (t.isBlockStatement(body)) { + // eslint-disable-next-line prefer-destructuring + body = body.body; + + if (body.length === 1) { + var _handler$params4 = _slicedToArray(handler.params, 3), + v = _handler$params4[0], + k = _handler$params4[1], + i = _handler$params4[2]; + + var parentPath = path.parentPath; + var node = body[0]; + parentPath.traverse({ + ArrowFunctionExpression: function ArrowFunctionExpression(_path) { + if (v) { + _path.scope.rename(v.name, value.name); + } + + if (k) { + _path.scope.rename(k.name, key.name); + } + + if (i) { + _path.scope.rename(i.name, iterable.name); + } + } + }); + + if (t.isExpression(node)) { + return node; + } + + if (t.isExpressionStatement(node)) { + return node.expression; + } + + if (t.isReturnStatement(node)) { + return node.argument; + } + } + } else { + var _handler$params5 = _slicedToArray(handler.params, 3), + _v3 = _handler$params5[0], + _k3 = _handler$params5[1], + _i4 = _handler$params5[2]; + + var _parentPath3 = path.parentPath; + + _parentPath3.traverse({ + ArrowFunctionExpression: function ArrowFunctionExpression(_path) { + if (_v3) { + _path.scope.rename(_v3.name, value.name); + } + + if (_k3) { + _path.scope.rename(_k3.name, key.name); + } + + if (_i4) { + _path.scope.rename(_i4.name, iterable.name); + } + } + }); + + if (t.isExpression(body)) { + return body; + } + } + } + + if (t.isFunctionExpression(handler)) { + var _body2 = handler.body.body; + + if (_body2.length === 1) { + var _handler$params6 = _slicedToArray(handler.params, 3), + _v4 = _handler$params6[0], + _k4 = _handler$params6[1], + _i5 = _handler$params6[2]; + + var _parentPath4 = path.parentPath; + var _node2 = _body2[0]; + + _parentPath4.traverse({ + FunctionExpression: function FunctionExpression(_path) { + if (_v4) { + _path.scope.rename(_v4.name, value.name); + } + + if (_k4) { + _path.scope.rename(_k4.name, key.name); + } + + if (_i5) { + _path.scope.rename(_i5.name, iterable.name); + } + } + }); + + if (t.isExpression(_node2)) { + return _node2; + } + + if (t.isExpressionStatement(_node2)) { + return _node2.expression; + } + + if (t.isReturnStatement(_node2)) { + return _node2.argument; + } + } + } + + var callExpression = t.callExpression(fn, [value, key, iterable]); + callExpression.__inlineLoopsMacroFallback = true; + return callExpression; +} + +function getUid(scope, name) { + return scope.generateUidIdentifier(name); +} + +function insertBeforeParent(_ref2) { + var fn = _ref2.fn, + handler = _ref2.handler, + isObject = _ref2.isObject, + iterable = _ref2.iterable, + loop = _ref2.loop, + object = _ref2.object, + path = _ref2.path, + result = _ref2.result, + resultStatement = _ref2.resultStatement, + resultValue = _ref2.resultValue, + t = _ref2.t, + value = _ref2.value; + var insertBefore = []; + + if (!isCachedReference(t, object)) { + var iterableVar = t.variableDeclaration('const', [t.variableDeclarator(iterable, object)]); + insertBefore.push(iterableVar); + } + + if (!isCachedReference(t, handler) && t.isCallExpression(resultStatement) && resultStatement.__inlineLoopsMacroFallback) { + var handlerVar = t.variableDeclaration('const', [t.variableDeclarator(fn, handler)]); + insertBefore.push(handlerVar); + } + + if (result) { + var resultVar = t.variableDeclaration('let', [t.variableDeclarator(result, resultValue)]); + insertBefore.push(resultVar); + } + + if (isObject) { + var valueVar = t.variableDeclaration('let', [t.variableDeclarator(value)]); + insertBefore.push(valueVar); + } + + insertBefore.push(loop); + path.getStatementParent().insertBefore(insertBefore); +} + +function isCachedReference(t, node) { + return t.isIdentifier(node); +} + +module.exports = { + getDefaultResult: getDefaultResult, + getIds: getIds, + getLoop: getLoop, + getUid: getUid, + getReduceResultStatement: getReduceResultStatement, + getResultStatement: getResultStatement, + insertBeforeParent: insertBeforeParent, + isCachedReference: isCachedReference +}; \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 00000000..6e1997ca --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,123 @@ +type Dictionary = { + [key: string]: Type; + [index: number]: Type; +}; + +/** + * iterables + */ +type ArrayIterable = any[]; +type ObjectIterable = Dictionary; + +/** + * the handlers for the iterables, specific to type + */ +type ArrayHandler = (value?: any, index?: number, array?: ArrayIterable) => any; +type ObjectHandler = (value?: any, key?: string, object?: ObjectIterable) => any; + +/** + * available exports + */ + +declare function every(iterable: ArrayIterable, handler: ArrayHandler): boolean; +declare function everyObject(iterable: ObjectIterable, handler: ObjectHandler): boolean; +declare function everyRight(iterable: ArrayIterable, handler: ArrayHandler): boolean; + +declare function filter( + iterable: Iterable, + handler: ArrayHandler, +): Iterable; +declare function filterObject( + iterable: ObjectIterable, + handler: ObjectHandler, +): Iterable; +declare function filterRight( + iterable: Iterable, + handler: ArrayHandler, +): Iterable; + +declare function find(iterable: ArrayIterable, handler: ArrayHandler): any; +declare function findObject(iterable: ObjectIterable, handler: ObjectHandler): any; +declare function findRight(iterable: ArrayIterable, handler: ArrayHandler): any; + +declare function findIndex( + iterable: Iterable, + handler: ArrayHandler, +): keyof Iterable | -1; +declare function findIndexRight( + iterable: Iterable, + handler: ArrayHandler, +): keyof Iterable | -1; + +declare function findKey( + iterable: Iterable, + handler: ObjectHandler, +): keyof Iterable | void; + +declare function forEach(iterable: ArrayIterable, handler: ArrayHandler): void; +declare function forEachObject(iterable: ObjectIterable, handler: ObjectHandler): void; +declare function forEachRight(iterable: ArrayIterable, handler: ArrayHandler): void; + +declare function map(iterable: ArrayIterable, handler: ArrayHandler): ArrayIterable; +declare function mapObject(iterable: ObjectIterable, handler: ObjectHandler): ObjectIterable; +declare function mapRight(iterable: ArrayIterable, handler: ArrayHandler): ArrayIterable; + +type ArrayReduceHandler = ( + accumulator?: any, + value?: any, + index?: number, + array?: ArrayIterable, +) => any; +type ObjectReduceHandler = ( + accumulator?: any, + value?: any, + key?: string, + object?: ObjectIterable, +) => any; + +declare function reduce( + iterable: ArrayIterable, + handler: ArrayReduceHandler, + initialValue?: any, +): any; +declare function reduceObject( + iterable: ObjectIterable, + handler: ObjectReduceHandler, + initialValue?: any, +): any; +declare function reduceRight( + iterable: ArrayIterable, + handler: ArrayReduceHandler, + initialValue?: any, +): any; + +declare function some(iterable: ArrayIterable, handler: ArrayHandler): boolean; +declare function someObject(iterable: ObjectIterable, handler: ObjectHandler): boolean; +declare function someRight(iterable: ArrayIterable, handler: ArrayHandler): boolean; + +export { + every, + everyObject, + everyRight, + filter, + filterObject, + filterRight, + find, + findIndex, + findIndexRight, + findKey, + findObject, + findRight, + forEach, + forEachObject, + forEachRight, + map, + mapObject, + mapRight, + reduce, + reduceObject, + reduceRight, + some, + someObject, + someRight, +}; diff --git a/dist/inline-loops.macro.js b/dist/inline-loops.macro.js new file mode 100644 index 00000000..479ff062 --- /dev/null +++ b/dist/inline-loops.macro.js @@ -0,0 +1,211 @@ +"use strict"; + +function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } + +function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } + +function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } + +function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } + +function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } + +function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } + +function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } + +function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } + +var _require = require('babel-plugin-macros'), + createMacro = _require.createMacro, + MacroError = _require.MacroError; + +var _require2 = require('./handlers'), + handleEvery = _require2.handleEvery, + handleFilter = _require2.handleFilter, + handleFind = _require2.handleFind, + handleFindKey = _require2.handleFindKey, + handleForEach = _require2.handleForEach, + handleMap = _require2.handleMap, + handleReduce = _require2.handleReduce, + handleSome = _require2.handleSome; + +var METHODS = ['every', 'filter', 'find', 'findIndex', 'findKey', 'forEach', 'map', 'reduce', 'some']; +var ARRAY_ONLY_METHODS = ['findIndex']; +var OBJECT_ONLY_METHODS = ['findKey']; + +function getCallTypes(references, method) { + var isArrayOnly = ARRAY_ONLY_METHODS.includes(method); + var isObjectOnly = OBJECT_ONLY_METHODS.includes(method); + var decrementingMethod = "".concat(method, "Right"); + var objectMethod = isObjectOnly ? method : "".concat(method, "Object"); + var incrementingCalls = references[method] || []; + var decrementingCalls = references[decrementingMethod] || []; + var objectCalls = references[objectMethod] || []; + return { + decrementingCalls: decrementingCalls.map(function (path) { + return { + method: decrementingMethod, + path: path, + sourceMethod: method, + type: 'decrementing' + }; + }), + decrementingMethod: decrementingMethod, + incrementingCalls: incrementingCalls.map(function (path) { + return { + method: method, + path: path, + sourceMethod: method, + type: 'incrementing' + }; + }), + isArrayOnly: isArrayOnly, + isObjectOnly: isObjectOnly, + objectCalls: objectCalls.map(function (path) { + return { + method: objectMethod, + path: path, + sourceMethod: method, + type: 'object' + }; + }), + objectMethod: objectMethod + }; +} + +function inlineLoops(_ref) { + var references = _ref.references, + babel = _ref.babel; + var t = babel.types; + var allMethods = []; + METHODS.forEach(function (method) { + var _getCallTypes = getCallTypes(references, method), + decrementingCalls = _getCallTypes.decrementingCalls, + incrementingCalls = _getCallTypes.incrementingCalls, + isArrayOnly = _getCallTypes.isArrayOnly, + isObjectOnly = _getCallTypes.isObjectOnly, + objectCalls = _getCallTypes.objectCalls; + + if (isArrayOnly) { + return allMethods.push.apply(allMethods, _toConsumableArray(incrementingCalls).concat(_toConsumableArray(decrementingCalls))); + } + + if (isObjectOnly) { + return allMethods.push.apply(allMethods, _toConsumableArray(objectCalls)); + } + + return allMethods.push.apply(allMethods, _toConsumableArray(incrementingCalls).concat(_toConsumableArray(decrementingCalls), _toConsumableArray(objectCalls))); + }); + allMethods.forEach(function (_ref2) { + var path = _ref2.path; + path.node.__inlineLoopsMacro = true; + }); + allMethods.sort(function (_ref3, _ref4) { + var a = _ref3.path; + var b = _ref4.path; + var aContainer = a.container; + var bContainer = b.container; + + if (aContainer.arguments) { + var _aContainer$arguments = _slicedToArray(aContainer.arguments, 1), + iterableA = _aContainer$arguments[0]; + + if (t.isCallExpression(iterableA) && iterableA.callee.__inlineLoopsMacro && iterableA.callee === b.node) { + return 1; + } + } + + if (bContainer.arguments) { + var _bContainer$arguments = _slicedToArray(bContainer.arguments, 1), + iterableB = _bContainer$arguments[0]; + + if (t.isCallExpression(iterableB) && iterableB.callee.__inlineLoopsMacro && iterableB.callee === a.node) { + return -1; + } + } + + var aStart = a.node.loc.start; + var 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; + }); + var handlers = { + every: handleEvery, + filter: handleFilter, + find: handleFind, + findIndex: handleFindKey, + findKey: handleFindKey, + forEach: handleForEach, + map: handleMap, + reduce: handleReduce, + some: handleSome + }; + + function createTransformer(name, transform, isDecrementing, isObject) { + return function _transform(path) { + if (path.findParent(function (_path) { + return _path.isConditionalExpression(); + })) { + throw new MacroError("You cannot use ".concat(name, " in a conditional expression.")); + } + + var args = path.parent.arguments; + + if (args.some(function (arg) { + return t.isSpreadElement(arg); + })) { + throw new MacroError('You cannot use spread arguments with the macro, please declare the arguments explicitly.'); + } + + var _args = _slicedToArray(args, 3), + object = _args[0], + handler = _args[1], + initialValue = _args[2]; + + var isHandlerMacro = allMethods.find(function (_ref5) { + var methodPath = _ref5.path; + return 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: t, + path: path, + object: object, + handler: handler, + initialValue: initialValue, + isDecrementing: isDecrementing, + isObject: isObject + }); + }; + } + + allMethods.forEach(function (_ref6) { + var method = _ref6.method, + path = _ref6.path, + sourceMethod = _ref6.sourceMethod, + type = _ref6.type; + var isDecrementing = type === 'decrementing'; + var isObject = type === 'object'; + var handler = createTransformer(method, handlers[sourceMethod], isDecrementing, isObject); + handler(path); + }); +} + +module.exports = createMacro(inlineLoops); \ No newline at end of file diff --git a/package.json b/package.json index 05eb864c..4b620366 100644 --- a/package.json +++ b/package.json @@ -51,23 +51,23 @@ "inline-loops" ], "license": "MIT", - "main": "build/inline-loops.macro.js", + "main": "dist/inline-loops.macro.js", "name": "inline-loops.macro", "repository": { "type": "git", "url": "git+https://github.com/planttheidea/inline-loops.macro.git" }, "scripts": { - "build": "babel inline-loops.macro.js -d build", - "copy:types": "cp ./index.d.ts ./build/", + "build": "babel src --out-dir dist", + "copy:types": "cp ./index.d.ts ./dist/", "dist": "npm run build && npm run copy:types", - "lint": "eslint ./inline-loops.macro.js", + "lint": "eslint src", "lint:fix": "npm run lint -- --fix", - "prepublishOnly": "npm run lint && npm run test && npm run dist", + "prepublishOnly": "npm run lint && npm run dist && npm run test", "release": "release-it", "release:beta": "release-it --config=.release-it.beta.json", "test": "jest", - "test:watch": "npm run test -- --watch" + "test:watch": "npm run dist && npm run test -- --watch" }, "typings": "./index.d.ts", "version": "1.0.1" diff --git a/inline-loops.macro.js b/src/handlers.js similarity index 56% rename from inline-loops.macro.js rename to src/handlers.js index fec180ed..ed9be2ae 100644 --- a/inline-loops.macro.js +++ b/src/handlers.js @@ -1,255 +1,13 @@ -const { createMacro, MacroError } = require('babel-plugin-macros'); - -function getDefaultResult(t, isObject) { - return isObject ? t.objectExpression([]) : t.arrayExpression(); -} - -const ID_TYPES = ['fn', 'iterable', 'key', 'length', 'result', 'value']; - -function getIds(scope) { - return ID_TYPES.reduce((ids, type) => { - ids[type] = getUid(scope, type); - - return ids; - }, {}); -} - -function getLoop({ - t, - body, - iterable, - key, - length, - value, - isDecrementing, - isObject, -}) { - if (isObject) { - const left = t.variableDeclaration('let', [t.variableDeclarator(key)]); - const right = iterable; - - return t.forInStatement(left, right, body); - } - - let assignments; - let test; - let update; - - if (isDecrementing) { - assignments = [ - t.variableDeclarator( - key, - t.binaryExpression( - '-', - t.memberExpression(iterable, t.identifier('length')), - t.numericLiteral(1), - ), - ), - ]; - - test = t.binaryExpression('>=', key, t.numericLiteral(0)); - update = t.updateExpression('--', key, true); - } else { - assignments = [ - t.variableDeclarator(key, t.numericLiteral(0)), - t.variableDeclarator( - length, - t.memberExpression(iterable, t.identifier('length')), - ), - ]; - - test = t.binaryExpression('<', key, length); - update = t.updateExpression('++', key, true); - } - - if (value) { - assignments.push(t.variableDeclarator(value)); - } - - return t.forStatement( - t.variableDeclaration('let', assignments), - test, - update, - body, - ); -} - -function getResultStatement(t, handler, fn, value, key, iterable, path) { - if (t.isArrowFunctionExpression(handler)) { - let { body } = handler; - - if (t.isBlockStatement(body)) { - // eslint-disable-next-line prefer-destructuring - body = body.body; - - if (body.length === 1) { - const [v, k, i] = handler.params; - const { parentPath } = path; - const node = body[0]; - - parentPath.traverse({ - ArrowFunctionExpression(_path) { - if (v) { - _path.scope.rename(v.name, value.name); - } - - if (k) { - _path.scope.rename(k.name, key.name); - } - - if (i) { - _path.scope.rename(i.name, iterable.name); - } - }, - }); - - if (t.isExpression(node)) { - return node; - } - - if (t.isExpressionStatement(node)) { - return node.expression; - } - - if (t.isReturnStatement(node)) { - return node.argument; - } - } - } else { - const [v, k, i] = handler.params; - const { parentPath } = path; - - parentPath.traverse({ - ArrowFunctionExpression(_path) { - if (v) { - _path.scope.rename(v.name, value.name); - } - - if (k) { - _path.scope.rename(k.name, key.name); - } - - if (i) { - _path.scope.rename(i.name, iterable.name); - } - }, - }); - - if (t.isExpression(body)) { - return body; - } - } - } - - if (t.isFunctionExpression(handler)) { - const { body } = handler.body; - - if (body.length === 1) { - const [v, k, i] = handler.params; - const { parentPath } = path; - const node = body[0]; - - parentPath.traverse({ - FunctionExpression(_path) { - if (v) { - _path.scope.rename(v.name, value.name); - } - - if (k) { - _path.scope.rename(k.name, key.name); - } - - if (i) { - _path.scope.rename(i.name, iterable.name); - } - }, - }); - - if (t.isExpression(node)) { - return node; - } - - if (t.isExpressionStatement(node)) { - return node.expression; - } - - if (t.isReturnStatement(node)) { - return node.argument; - } - } - } - - const callExpression = t.callExpression(fn, [value, key, iterable]); - - callExpression.__inlineLoopsMacroFallback = true; - - return callExpression; -} - -function getUid(scope, name) { - return scope.generateUidIdentifier(name); -} - -function insertBeforeParent({ - fn, - handler, - isObject, - iterable, - loop, - object, - path, - result, - resultStatement, - resultValue, - t, - value, -}) { - const insertBefore = []; - - if (!isCachedReference(t, object)) { - const iterableVar = t.variableDeclaration('const', [ - t.variableDeclarator(iterable, object), - ]); - - insertBefore.push(iterableVar); - } - - if ( - !isCachedReference(t, handler) - && t.isCallExpression(resultStatement) - && resultStatement.__inlineLoopsMacroFallback - ) { - const handlerVar = t.variableDeclaration('const', [ - t.variableDeclarator(fn, handler), - ]); - - insertBefore.push(handlerVar); - } - - if (result) { - const resultVar = t.variableDeclaration('let', [ - t.variableDeclarator(result, resultValue), - ]); - - insertBefore.push(resultVar); - } - - if (isObject) { - const valueVar = t.variableDeclaration('let', [ - t.variableDeclarator(value), - ]); - - insertBefore.push(valueVar); - } - - insertBefore.push(loop); - - path.getStatementParent().insertBefore(insertBefore); -} - -function isCachedReference(t, node) { - return t.isIdentifier(node); -} +const { + getDefaultResult, + getIds, + getLoop, + getUid, + getReduceResultStatement, + getResultStatement, + insertBeforeParent, + isCachedReference, +} = require('./helpers'); function handleEvery({ t, path, object, handler, isDecrementing, isObject, @@ -721,8 +479,17 @@ function handleReduce({ t.memberExpression(iterableUsed, key, true), ), ); - const call = t.callExpression(fnUsed, [result, value, key, iterableUsed]); - const resultAssignment = t.assignmentExpression('=', result, call); + const resultStatement = getReduceResultStatement( + t, + handler, + fnUsed, + result, + value, + key, + iterableUsed, + path, + ); + const resultAssignment = t.assignmentExpression('=', result, resultStatement); let block; @@ -789,7 +556,7 @@ function handleReduce({ insertBefore.push(...injected); - if (fnUsed === fn) { + if (fnUsed === fn && resultStatement.__inlineLoopsMacroFallback) { const handlerVar = t.variableDeclaration('const', [ t.variableDeclarator(fn, handler), ]); @@ -879,192 +646,13 @@ function handleSome({ path.parentPath.replaceWith(result); } -const METHODS = [ - 'every', - 'filter', - 'find', - 'findIndex', - 'findKey', - 'forEach', - 'map', - 'reduce', - 'some', -]; -const ARRAY_ONLY_METHODS = ['findIndex']; -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, - 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); +module.exports = { + handleEvery, + handleFilter, + handleFind, + handleFindKey, + handleForEach, + handleMap, + handleReduce, + handleSome, +}; diff --git a/src/helpers.js b/src/helpers.js new file mode 100644 index 00000000..31ebba0b --- /dev/null +++ b/src/helpers.js @@ -0,0 +1,362 @@ +function getDefaultResult(t, isObject) { + return isObject ? t.objectExpression([]) : t.arrayExpression(); +} + +const ID_TYPES = ['fn', 'iterable', 'key', 'length', 'result', 'value']; + +function getIds(scope) { + return ID_TYPES.reduce((ids, type) => { + ids[type] = getUid(scope, type); + + return ids; + }, {}); +} + +function getLoop({ + t, body, iterable, key, length, value, isDecrementing, isObject, +}) { + if (isObject) { + const left = t.variableDeclaration('let', [t.variableDeclarator(key)]); + const right = iterable; + + return t.forInStatement(left, right, body); + } + + let assignments; + let test; + let update; + + if (isDecrementing) { + assignments = [ + t.variableDeclarator( + key, + t.binaryExpression( + '-', + t.memberExpression(iterable, t.identifier('length')), + t.numericLiteral(1), + ), + ), + ]; + + test = t.binaryExpression('>=', key, t.numericLiteral(0)); + update = t.updateExpression('--', key, true); + } else { + assignments = [ + t.variableDeclarator(key, t.numericLiteral(0)), + t.variableDeclarator(length, t.memberExpression(iterable, t.identifier('length'))), + ]; + + test = t.binaryExpression('<', key, length); + update = t.updateExpression('++', key, true); + } + + if (value) { + assignments.push(t.variableDeclarator(value)); + } + + return t.forStatement(t.variableDeclaration('let', assignments), test, update, body); +} + +function getReduceResultStatement(t, handler, fn, result, value, key, iterable, path) { + if (t.isArrowFunctionExpression(handler)) { + let { body } = handler; + + if (t.isBlockStatement(body)) { + // eslint-disable-next-line prefer-destructuring + body = body.body; + + if (body.length === 1) { + const [r, v, k, i] = handler.params; + const { parentPath } = path; + const node = body[0]; + + parentPath.traverse({ + ArrowFunctionExpression(_path) { + if (r) { + _path.scope.rename(r.name, result.name); + } + + if (v) { + _path.scope.rename(v.name, value.name); + } + + if (k) { + _path.scope.rename(k.name, key.name); + } + + if (i) { + _path.scope.rename(i.name, iterable.name); + } + }, + }); + + if (t.isExpression(node)) { + return node; + } + + if (t.isExpressionStatement(node)) { + return node.expression; + } + + if (t.isReturnStatement(node)) { + return node.argument; + } + } + } else { + const [r, v, k, i] = handler.params; + const { parentPath } = path; + + parentPath.traverse({ + ArrowFunctionExpression(_path) { + if (r) { + _path.scope.rename(r.name, result.name); + } + + if (v) { + _path.scope.rename(v.name, value.name); + } + + if (k) { + _path.scope.rename(k.name, key.name); + } + + if (i) { + _path.scope.rename(i.name, iterable.name); + } + }, + }); + + if (t.isExpression(body)) { + return body; + } + } + } + + if (t.isFunctionExpression(handler)) { + const { body } = handler.body; + + if (body.length === 1) { + const [r, v, k, i] = handler.params; + const { parentPath } = path; + const node = body[0]; + + parentPath.traverse({ + FunctionExpression(_path) { + if (r) { + _path.scope.rename(r.name, result.name); + } + + if (v) { + _path.scope.rename(v.name, value.name); + } + + if (k) { + _path.scope.rename(k.name, key.name); + } + + if (i) { + _path.scope.rename(i.name, iterable.name); + } + }, + }); + + if (t.isExpression(node)) { + return node; + } + + if (t.isExpressionStatement(node)) { + return node.expression; + } + + if (t.isReturnStatement(node)) { + return node.argument; + } + } + } + + const callExpression = t.callExpression(fn, [result, value, key, iterable]); + + callExpression.__inlineLoopsMacroFallback = true; + + return callExpression; +} + +function getResultStatement(t, handler, fn, value, key, iterable, path) { + if (t.isArrowFunctionExpression(handler)) { + let { body } = handler; + + if (t.isBlockStatement(body)) { + // eslint-disable-next-line prefer-destructuring + body = body.body; + + if (body.length === 1) { + const [v, k, i] = handler.params; + const { parentPath } = path; + const node = body[0]; + + parentPath.traverse({ + ArrowFunctionExpression(_path) { + if (v) { + _path.scope.rename(v.name, value.name); + } + + if (k) { + _path.scope.rename(k.name, key.name); + } + + if (i) { + _path.scope.rename(i.name, iterable.name); + } + }, + }); + + if (t.isExpression(node)) { + return node; + } + + if (t.isExpressionStatement(node)) { + return node.expression; + } + + if (t.isReturnStatement(node)) { + return node.argument; + } + } + } else { + const [v, k, i] = handler.params; + const { parentPath } = path; + + parentPath.traverse({ + ArrowFunctionExpression(_path) { + if (v) { + _path.scope.rename(v.name, value.name); + } + + if (k) { + _path.scope.rename(k.name, key.name); + } + + if (i) { + _path.scope.rename(i.name, iterable.name); + } + }, + }); + + if (t.isExpression(body)) { + return body; + } + } + } + + if (t.isFunctionExpression(handler)) { + const { body } = handler.body; + + if (body.length === 1) { + const [v, k, i] = handler.params; + const { parentPath } = path; + const node = body[0]; + + parentPath.traverse({ + FunctionExpression(_path) { + if (v) { + _path.scope.rename(v.name, value.name); + } + + if (k) { + _path.scope.rename(k.name, key.name); + } + + if (i) { + _path.scope.rename(i.name, iterable.name); + } + }, + }); + + if (t.isExpression(node)) { + return node; + } + + if (t.isExpressionStatement(node)) { + return node.expression; + } + + if (t.isReturnStatement(node)) { + return node.argument; + } + } + } + + const callExpression = t.callExpression(fn, [value, key, iterable]); + + callExpression.__inlineLoopsMacroFallback = true; + + return callExpression; +} + +function getUid(scope, name) { + return scope.generateUidIdentifier(name); +} + +function insertBeforeParent({ + fn, + handler, + isObject, + iterable, + loop, + object, + path, + result, + resultStatement, + resultValue, + t, + value, +}) { + const insertBefore = []; + + if (!isCachedReference(t, object)) { + const iterableVar = t.variableDeclaration('const', [t.variableDeclarator(iterable, object)]); + + insertBefore.push(iterableVar); + } + + if ( + !isCachedReference(t, handler) + && t.isCallExpression(resultStatement) + && resultStatement.__inlineLoopsMacroFallback + ) { + const handlerVar = t.variableDeclaration('const', [t.variableDeclarator(fn, handler)]); + + insertBefore.push(handlerVar); + } + + if (result) { + const resultVar = t.variableDeclaration('let', [t.variableDeclarator(result, resultValue)]); + + insertBefore.push(resultVar); + } + + if (isObject) { + const valueVar = t.variableDeclaration('let', [t.variableDeclarator(value)]); + + insertBefore.push(valueVar); + } + + insertBefore.push(loop); + + path.getStatementParent().insertBefore(insertBefore); +} + +function isCachedReference(t, node) { + return t.isIdentifier(node); +} + +module.exports = { + getDefaultResult, + getIds, + getLoop, + getUid, + getReduceResultStatement, + getResultStatement, + insertBeforeParent, + isCachedReference, +}; diff --git a/src/inline-loops.macro.js b/src/inline-loops.macro.js new file mode 100644 index 00000000..1d951179 --- /dev/null +++ b/src/inline-loops.macro.js @@ -0,0 +1,200 @@ +const { createMacro, MacroError } = require('babel-plugin-macros'); + +const { + handleEvery, + handleFilter, + handleFind, + handleFindKey, + handleForEach, + handleMap, + handleReduce, + handleSome, +} = require('./handlers'); + +const METHODS = [ + 'every', + 'filter', + 'find', + 'findIndex', + 'findKey', + 'forEach', + 'map', + 'reduce', + 'some', +]; +const ARRAY_ONLY_METHODS = ['findIndex']; +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, + 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); From f36be3acf3fcdc7ca94fcf557ee6afecd4e5ccd9 Mon Sep 17 00:00:00 2001 From: planttheidea Date: Thu, 6 Jun 2019 08:16:37 -0700 Subject: [PATCH 2/6] remove legacy build folder --- build/handlers.js | 475 ------------------------------------ build/helpers.js | 237 ------------------ build/index.d.ts | 123 ---------- build/inline-loops.macro.js | 211 ---------------- 4 files changed, 1046 deletions(-) delete mode 100644 build/handlers.js delete mode 100644 build/helpers.js delete mode 100644 build/index.d.ts delete mode 100644 build/inline-loops.macro.js diff --git a/build/handlers.js b/build/handlers.js deleted file mode 100644 index 2a444fb4..00000000 --- a/build/handlers.js +++ /dev/null @@ -1,475 +0,0 @@ -"use strict"; - -var _require = require('./helpers'), - getDefaultResult = _require.getDefaultResult, - getIds = _require.getIds, - getLoop = _require.getLoop, - getUid = _require.getUid, - getResultStatement = _require.getResultStatement, - insertBeforeParent = _require.insertBeforeParent, - isCachedReference = _require.isCachedReference; - -function handleEvery(_ref) { - var t = _ref.t, - path = _ref.path, - object = _ref.object, - handler = _ref.handler, - isDecrementing = _ref.isDecrementing, - isObject = _ref.isObject; - - var _getIds = getIds(path.scope), - fn = _getIds.fn, - iterable = _getIds.iterable, - key = _getIds.key, - length = _getIds.length, - result = _getIds.result, - value = _getIds.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(t.unaryExpression('!', resultStatement), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, t.booleanLiteral(false))), t.breakStatement()])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: t.booleanLiteral(true), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleFilter(_ref2) { - var t = _ref2.t, - path = _ref2.path, - object = _ref2.object, - handler = _ref2.handler, - isDecrementing = _ref2.isDecrementing, - isObject = _ref2.isObject; - - var _getIds2 = getIds(path.scope), - fn = _getIds2.fn, - iterable = _getIds2.iterable, - key = _getIds2.key, - length = _getIds2.length, - result = _getIds2.result, - value = _getIds2.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultAssignment = isObject ? t.assignmentExpression('=', t.memberExpression(result, key, true), value) : t.callExpression(t.memberExpression(result, t.identifier('push')), [value]); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(resultStatement, t.expressionStatement(resultAssignment)); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - value: value, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: getDefaultResult(t, isObject), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleFind(_ref3) { - var t = _ref3.t, - path = _ref3.path, - object = _ref3.object, - handler = _ref3.handler, - isDecrementing = _ref3.isDecrementing, - isObject = _ref3.isObject; - - var _getIds3 = getIds(path.scope), - fn = _getIds3.fn, - iterable = _getIds3.iterable, - key = _getIds3.key, - length = _getIds3.length, - result = _getIds3.result, - value = _getIds3.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, value)), t.breakStatement()])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleFindKey(_ref4) { - var t = _ref4.t, - path = _ref4.path, - object = _ref4.object, - handler = _ref4.handler, - isDecrementing = _ref4.isDecrementing, - isObject = _ref4.isObject; - - var _getIds4 = getIds(path.scope), - fn = _getIds4.fn, - iterable = _getIds4.iterable, - key = _getIds4.key, - length = _getIds4.length, - result = _getIds4.result, - value = _getIds4.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, key)), t.breakStatement()])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: isObject ? undefined : t.numericLiteral(-1), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleForEach(_ref5) { - var t = _ref5.t, - path = _ref5.path, - object = _ref5.object, - handler = _ref5.handler, - isDecrementing = _ref5.isDecrementing, - isObject = _ref5.isObject; - - var _getIds5 = getIds(path.scope), - fn = _getIds5.fn, - iterable = _getIds5.iterable, - key = _getIds5.key, - length = _getIds5.length, - value = _getIds5.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var call = t.expressionStatement(resultStatement); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, call]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - resultStatement: resultStatement, - t: t, - value: value - }); - path.parentPath.remove(); -} - -function handleMap(_ref6) { - var t = _ref6.t, - path = _ref6.path, - object = _ref6.object, - handler = _ref6.handler, - isDecrementing = _ref6.isDecrementing, - isObject = _ref6.isObject; - - var _getIds6 = getIds(path.scope), - fn = _getIds6.fn, - iterable = _getIds6.iterable, - key = _getIds6.key, - length = _getIds6.length, - result = _getIds6.result, - value = _getIds6.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.expressionStatement(isObject ? t.assignmentExpression('=', t.memberExpression(result, key, true), resultStatement) : t.callExpression(t.memberExpression(result, t.identifier('push')), [resultStatement])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: getDefaultResult(t, isObject), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -function handleReduce(_ref7) { - var t = _ref7.t, - path = _ref7.path, - object = _ref7.object, - handler = _ref7.handler, - initialValue = _ref7.initialValue, - isDecrementing = _ref7.isDecrementing, - isObject = _ref7.isObject; - - var _getIds7 = getIds(path.scope), - fn = _getIds7.fn, - iterable = _getIds7.iterable, - key = _getIds7.key, - length = _getIds7.length, - result = _getIds7.result, - value = _getIds7.value; - - var hasInitialValue = !!initialValue; - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var injected = []; - var hasInitialValueId; - - if (!hasInitialValue) { - if (isObject) { - hasInitialValueId = getUid(path.scope, 'hasInitialValue'); - injected.push(t.variableDeclaration('let', [t.variableDeclarator(hasInitialValueId, t.booleanLiteral(false))])); - } else if (isDecrementing) { - injected.push(t.variableDeclaration('const', [t.variableDeclarator(length, t.memberExpression(iterableUsed, t.identifier('length')))])); - initialValue = t.memberExpression(iterableUsed, t.binaryExpression('-', length, t.numericLiteral(1)), true); - } else { - initialValue = t.memberExpression(iterableUsed, t.numericLiteral(0), true); - } - } - - if (isObject) { - injected.push(t.variableDeclaration('let', [t.variableDeclarator(value)])); - } - - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var call = t.callExpression(fnUsed, [result, value, key, iterableUsed]); - var resultAssignment = t.assignmentExpression('=', result, call); - var block; - - if (!hasInitialValue && isObject) { - var ifHasInitialValue = t.ifStatement(hasInitialValueId, t.blockStatement([valueAssignment, t.expressionStatement(resultAssignment)]), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', hasInitialValueId, t.booleanLiteral(true))), t.expressionStatement(t.assignmentExpression('=', result, t.memberExpression(iterableUsed, key, true)))])); - block = [ifHasInitialValue]; - } else { - block = [valueAssignment, t.expressionStatement(resultAssignment)]; - } - - var loop = getLoop({ - t: t, - body: t.blockStatement(block), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - - if (!hasInitialValue && !isObject) { - var keyValue = loop.init.declarations.find(function (_ref8) { - var id = _ref8.id; - return id.name === key.name; - }); - - if (isDecrementing) { - keyValue.init.left = length; - keyValue.init.right = t.numericLiteral(2); - } else { - keyValue.init = t.numericLiteral(1); - } - } - - var insertBefore = []; - - if (iterableUsed === iterable) { - var iterableVar = t.variableDeclaration('const', [t.variableDeclarator(iterable, object)]); - insertBefore.push(iterableVar); - } - - insertBefore.push.apply(insertBefore, injected); - - if (fnUsed === fn) { - var handlerVar = t.variableDeclaration('const', [t.variableDeclarator(fn, handler)]); - insertBefore.push(handlerVar); - } - - var resultVar = t.variableDeclaration('let', [t.variableDeclarator(result, initialValue)]); - insertBefore.push(resultVar); - insertBefore.push(loop); - path.getStatementParent().insertBefore(insertBefore); - path.parentPath.replaceWith(result); -} - -function handleSome(_ref9) { - var t = _ref9.t, - path = _ref9.path, - object = _ref9.object, - handler = _ref9.handler, - isDecrementing = _ref9.isDecrementing, - isObject = _ref9.isObject; - - var _getIds8 = getIds(path.scope), - fn = _getIds8.fn, - iterable = _getIds8.iterable, - key = _getIds8.key, - length = _getIds8.length, - result = _getIds8.result, - value = _getIds8.value; - - var isHandlerCached = isCachedReference(t, handler); - var isIterableCached = isCachedReference(t, object); - var fnUsed = isHandlerCached ? handler : fn; - var iterableUsed = isIterableCached ? object : iterable; - var valueAssignment = t.expressionStatement(t.assignmentExpression('=', value, t.memberExpression(iterableUsed, key, true))); - var resultStatement = getResultStatement(t, handler, fnUsed, value, key, iterableUsed, path); - var expr = t.ifStatement(resultStatement, t.blockStatement([t.expressionStatement(t.assignmentExpression('=', result, t.booleanLiteral(true))), t.breakStatement()])); - var loop = getLoop({ - t: t, - body: t.blockStatement([valueAssignment, expr]), - iterable: iterableUsed, - key: key, - length: length, - isDecrementing: isDecrementing, - isObject: isObject, - scope: path.scope, - value: value - }); - insertBeforeParent({ - fn: fn, - handler: handler, - isObject: isObject, - iterable: iterable, - loop: loop, - object: object, - path: path, - result: result, - resultStatement: resultStatement, - resultValue: t.booleanLiteral(false), - t: t, - value: value - }); - path.parentPath.replaceWith(result); -} - -module.exports = { - handleEvery: handleEvery, - handleFilter: handleFilter, - handleFind: handleFind, - handleFindKey: handleFindKey, - handleForEach: handleForEach, - handleMap: handleMap, - handleReduce: handleReduce, - handleSome: handleSome -}; \ No newline at end of file diff --git a/build/helpers.js b/build/helpers.js deleted file mode 100644 index ab1796be..00000000 --- a/build/helpers.js +++ /dev/null @@ -1,237 +0,0 @@ -"use strict"; - -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } - -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } - -function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } - -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } - -function getDefaultResult(t, isObject) { - return isObject ? t.objectExpression([]) : t.arrayExpression(); -} - -var ID_TYPES = ['fn', 'iterable', 'key', 'length', 'result', 'value']; - -function getIds(scope) { - return ID_TYPES.reduce(function (ids, type) { - ids[type] = getUid(scope, type); - return ids; - }, {}); -} - -function getLoop(_ref) { - var t = _ref.t, - body = _ref.body, - iterable = _ref.iterable, - key = _ref.key, - length = _ref.length, - value = _ref.value, - isDecrementing = _ref.isDecrementing, - isObject = _ref.isObject; - - if (isObject) { - var left = t.variableDeclaration('let', [t.variableDeclarator(key)]); - var right = iterable; - return t.forInStatement(left, right, body); - } - - var assignments; - var test; - var update; - - if (isDecrementing) { - assignments = [t.variableDeclarator(key, t.binaryExpression('-', t.memberExpression(iterable, t.identifier('length')), t.numericLiteral(1)))]; - test = t.binaryExpression('>=', key, t.numericLiteral(0)); - update = t.updateExpression('--', key, true); - } else { - assignments = [t.variableDeclarator(key, t.numericLiteral(0)), t.variableDeclarator(length, t.memberExpression(iterable, t.identifier('length')))]; - test = t.binaryExpression('<', key, length); - update = t.updateExpression('++', key, true); - } - - if (value) { - assignments.push(t.variableDeclarator(value)); - } - - return t.forStatement(t.variableDeclaration('let', assignments), test, update, body); -} - -function getResultStatement(t, handler, fn, value, key, iterable, path) { - if (t.isArrowFunctionExpression(handler)) { - var body = handler.body; - - if (t.isBlockStatement(body)) { - // eslint-disable-next-line prefer-destructuring - body = body.body; - - if (body.length === 1) { - var _handler$params = _slicedToArray(handler.params, 3), - v = _handler$params[0], - k = _handler$params[1], - i = _handler$params[2]; - - var parentPath = path.parentPath; - var node = body[0]; - parentPath.traverse({ - ArrowFunctionExpression: function ArrowFunctionExpression(_path) { - if (v) { - _path.scope.rename(v.name, value.name); - } - - if (k) { - _path.scope.rename(k.name, key.name); - } - - if (i) { - _path.scope.rename(i.name, iterable.name); - } - } - }); - - if (t.isExpression(node)) { - return node; - } - - if (t.isExpressionStatement(node)) { - return node.expression; - } - - if (t.isReturnStatement(node)) { - return node.argument; - } - } - } else { - var _handler$params2 = _slicedToArray(handler.params, 3), - _v = _handler$params2[0], - _k = _handler$params2[1], - _i2 = _handler$params2[2]; - - var _parentPath = path.parentPath; - - _parentPath.traverse({ - ArrowFunctionExpression: function ArrowFunctionExpression(_path) { - if (_v) { - _path.scope.rename(_v.name, value.name); - } - - if (_k) { - _path.scope.rename(_k.name, key.name); - } - - if (_i2) { - _path.scope.rename(_i2.name, iterable.name); - } - } - }); - - if (t.isExpression(body)) { - return body; - } - } - } - - if (t.isFunctionExpression(handler)) { - var _body = handler.body.body; - - if (_body.length === 1) { - var _handler$params3 = _slicedToArray(handler.params, 3), - _v2 = _handler$params3[0], - _k2 = _handler$params3[1], - _i3 = _handler$params3[2]; - - var _parentPath2 = path.parentPath; - var _node = _body[0]; - - _parentPath2.traverse({ - FunctionExpression: function FunctionExpression(_path) { - if (_v2) { - _path.scope.rename(_v2.name, value.name); - } - - if (_k2) { - _path.scope.rename(_k2.name, key.name); - } - - if (_i3) { - _path.scope.rename(_i3.name, iterable.name); - } - } - }); - - if (t.isExpression(_node)) { - return _node; - } - - if (t.isExpressionStatement(_node)) { - return _node.expression; - } - - if (t.isReturnStatement(_node)) { - return _node.argument; - } - } - } - - var callExpression = t.callExpression(fn, [value, key, iterable]); - callExpression.__inlineLoopsMacroFallback = true; - return callExpression; -} - -function getUid(scope, name) { - return scope.generateUidIdentifier(name); -} - -function insertBeforeParent(_ref2) { - var fn = _ref2.fn, - handler = _ref2.handler, - isObject = _ref2.isObject, - iterable = _ref2.iterable, - loop = _ref2.loop, - object = _ref2.object, - path = _ref2.path, - result = _ref2.result, - resultStatement = _ref2.resultStatement, - resultValue = _ref2.resultValue, - t = _ref2.t, - value = _ref2.value; - var insertBefore = []; - - if (!isCachedReference(t, object)) { - var iterableVar = t.variableDeclaration('const', [t.variableDeclarator(iterable, object)]); - insertBefore.push(iterableVar); - } - - if (!isCachedReference(t, handler) && t.isCallExpression(resultStatement) && resultStatement.__inlineLoopsMacroFallback) { - var handlerVar = t.variableDeclaration('const', [t.variableDeclarator(fn, handler)]); - insertBefore.push(handlerVar); - } - - if (result) { - var resultVar = t.variableDeclaration('let', [t.variableDeclarator(result, resultValue)]); - insertBefore.push(resultVar); - } - - if (isObject) { - var valueVar = t.variableDeclaration('let', [t.variableDeclarator(value)]); - insertBefore.push(valueVar); - } - - insertBefore.push(loop); - path.getStatementParent().insertBefore(insertBefore); -} - -function isCachedReference(t, node) { - return t.isIdentifier(node); -} - -module.exports = { - getDefaultResult: getDefaultResult, - getIds: getIds, - getLoop: getLoop, - getUid: getUid, - getResultStatement: getResultStatement, - insertBeforeParent: insertBeforeParent, - isCachedReference: isCachedReference -}; \ No newline at end of file diff --git a/build/index.d.ts b/build/index.d.ts deleted file mode 100644 index 6e1997ca..00000000 --- a/build/index.d.ts +++ /dev/null @@ -1,123 +0,0 @@ -type Dictionary = { - [key: string]: Type; - [index: number]: Type; -}; - -/** - * iterables - */ -type ArrayIterable = any[]; -type ObjectIterable = Dictionary; - -/** - * the handlers for the iterables, specific to type - */ -type ArrayHandler = (value?: any, index?: number, array?: ArrayIterable) => any; -type ObjectHandler = (value?: any, key?: string, object?: ObjectIterable) => any; - -/** - * available exports - */ - -declare function every(iterable: ArrayIterable, handler: ArrayHandler): boolean; -declare function everyObject(iterable: ObjectIterable, handler: ObjectHandler): boolean; -declare function everyRight(iterable: ArrayIterable, handler: ArrayHandler): boolean; - -declare function filter( - iterable: Iterable, - handler: ArrayHandler, -): Iterable; -declare function filterObject( - iterable: ObjectIterable, - handler: ObjectHandler, -): Iterable; -declare function filterRight( - iterable: Iterable, - handler: ArrayHandler, -): Iterable; - -declare function find(iterable: ArrayIterable, handler: ArrayHandler): any; -declare function findObject(iterable: ObjectIterable, handler: ObjectHandler): any; -declare function findRight(iterable: ArrayIterable, handler: ArrayHandler): any; - -declare function findIndex( - iterable: Iterable, - handler: ArrayHandler, -): keyof Iterable | -1; -declare function findIndexRight( - iterable: Iterable, - handler: ArrayHandler, -): keyof Iterable | -1; - -declare function findKey( - iterable: Iterable, - handler: ObjectHandler, -): keyof Iterable | void; - -declare function forEach(iterable: ArrayIterable, handler: ArrayHandler): void; -declare function forEachObject(iterable: ObjectIterable, handler: ObjectHandler): void; -declare function forEachRight(iterable: ArrayIterable, handler: ArrayHandler): void; - -declare function map(iterable: ArrayIterable, handler: ArrayHandler): ArrayIterable; -declare function mapObject(iterable: ObjectIterable, handler: ObjectHandler): ObjectIterable; -declare function mapRight(iterable: ArrayIterable, handler: ArrayHandler): ArrayIterable; - -type ArrayReduceHandler = ( - accumulator?: any, - value?: any, - index?: number, - array?: ArrayIterable, -) => any; -type ObjectReduceHandler = ( - accumulator?: any, - value?: any, - key?: string, - object?: ObjectIterable, -) => any; - -declare function reduce( - iterable: ArrayIterable, - handler: ArrayReduceHandler, - initialValue?: any, -): any; -declare function reduceObject( - iterable: ObjectIterable, - handler: ObjectReduceHandler, - initialValue?: any, -): any; -declare function reduceRight( - iterable: ArrayIterable, - handler: ArrayReduceHandler, - initialValue?: any, -): any; - -declare function some(iterable: ArrayIterable, handler: ArrayHandler): boolean; -declare function someObject(iterable: ObjectIterable, handler: ObjectHandler): boolean; -declare function someRight(iterable: ArrayIterable, handler: ArrayHandler): boolean; - -export { - every, - everyObject, - everyRight, - filter, - filterObject, - filterRight, - find, - findIndex, - findIndexRight, - findKey, - findObject, - findRight, - forEach, - forEachObject, - forEachRight, - map, - mapObject, - mapRight, - reduce, - reduceObject, - reduceRight, - some, - someObject, - someRight, -}; diff --git a/build/inline-loops.macro.js b/build/inline-loops.macro.js deleted file mode 100644 index 479ff062..00000000 --- a/build/inline-loops.macro.js +++ /dev/null @@ -1,211 +0,0 @@ -"use strict"; - -function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } - -function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } - -function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } - -function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } - -function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } - -function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } - -function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } - -function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } - -var _require = require('babel-plugin-macros'), - createMacro = _require.createMacro, - MacroError = _require.MacroError; - -var _require2 = require('./handlers'), - handleEvery = _require2.handleEvery, - handleFilter = _require2.handleFilter, - handleFind = _require2.handleFind, - handleFindKey = _require2.handleFindKey, - handleForEach = _require2.handleForEach, - handleMap = _require2.handleMap, - handleReduce = _require2.handleReduce, - handleSome = _require2.handleSome; - -var METHODS = ['every', 'filter', 'find', 'findIndex', 'findKey', 'forEach', 'map', 'reduce', 'some']; -var ARRAY_ONLY_METHODS = ['findIndex']; -var OBJECT_ONLY_METHODS = ['findKey']; - -function getCallTypes(references, method) { - var isArrayOnly = ARRAY_ONLY_METHODS.includes(method); - var isObjectOnly = OBJECT_ONLY_METHODS.includes(method); - var decrementingMethod = "".concat(method, "Right"); - var objectMethod = isObjectOnly ? method : "".concat(method, "Object"); - var incrementingCalls = references[method] || []; - var decrementingCalls = references[decrementingMethod] || []; - var objectCalls = references[objectMethod] || []; - return { - decrementingCalls: decrementingCalls.map(function (path) { - return { - method: decrementingMethod, - path: path, - sourceMethod: method, - type: 'decrementing' - }; - }), - decrementingMethod: decrementingMethod, - incrementingCalls: incrementingCalls.map(function (path) { - return { - method: method, - path: path, - sourceMethod: method, - type: 'incrementing' - }; - }), - isArrayOnly: isArrayOnly, - isObjectOnly: isObjectOnly, - objectCalls: objectCalls.map(function (path) { - return { - method: objectMethod, - path: path, - sourceMethod: method, - type: 'object' - }; - }), - objectMethod: objectMethod - }; -} - -function inlineLoops(_ref) { - var references = _ref.references, - babel = _ref.babel; - var t = babel.types; - var allMethods = []; - METHODS.forEach(function (method) { - var _getCallTypes = getCallTypes(references, method), - decrementingCalls = _getCallTypes.decrementingCalls, - incrementingCalls = _getCallTypes.incrementingCalls, - isArrayOnly = _getCallTypes.isArrayOnly, - isObjectOnly = _getCallTypes.isObjectOnly, - objectCalls = _getCallTypes.objectCalls; - - if (isArrayOnly) { - return allMethods.push.apply(allMethods, _toConsumableArray(incrementingCalls).concat(_toConsumableArray(decrementingCalls))); - } - - if (isObjectOnly) { - return allMethods.push.apply(allMethods, _toConsumableArray(objectCalls)); - } - - return allMethods.push.apply(allMethods, _toConsumableArray(incrementingCalls).concat(_toConsumableArray(decrementingCalls), _toConsumableArray(objectCalls))); - }); - allMethods.forEach(function (_ref2) { - var path = _ref2.path; - path.node.__inlineLoopsMacro = true; - }); - allMethods.sort(function (_ref3, _ref4) { - var a = _ref3.path; - var b = _ref4.path; - var aContainer = a.container; - var bContainer = b.container; - - if (aContainer.arguments) { - var _aContainer$arguments = _slicedToArray(aContainer.arguments, 1), - iterableA = _aContainer$arguments[0]; - - if (t.isCallExpression(iterableA) && iterableA.callee.__inlineLoopsMacro && iterableA.callee === b.node) { - return 1; - } - } - - if (bContainer.arguments) { - var _bContainer$arguments = _slicedToArray(bContainer.arguments, 1), - iterableB = _bContainer$arguments[0]; - - if (t.isCallExpression(iterableB) && iterableB.callee.__inlineLoopsMacro && iterableB.callee === a.node) { - return -1; - } - } - - var aStart = a.node.loc.start; - var 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; - }); - var handlers = { - every: handleEvery, - filter: handleFilter, - find: handleFind, - findIndex: handleFindKey, - findKey: handleFindKey, - forEach: handleForEach, - map: handleMap, - reduce: handleReduce, - some: handleSome - }; - - function createTransformer(name, transform, isDecrementing, isObject) { - return function _transform(path) { - if (path.findParent(function (_path) { - return _path.isConditionalExpression(); - })) { - throw new MacroError("You cannot use ".concat(name, " in a conditional expression.")); - } - - var args = path.parent.arguments; - - if (args.some(function (arg) { - return t.isSpreadElement(arg); - })) { - throw new MacroError('You cannot use spread arguments with the macro, please declare the arguments explicitly.'); - } - - var _args = _slicedToArray(args, 3), - object = _args[0], - handler = _args[1], - initialValue = _args[2]; - - var isHandlerMacro = allMethods.find(function (_ref5) { - var methodPath = _ref5.path; - return 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: t, - path: path, - object: object, - handler: handler, - initialValue: initialValue, - isDecrementing: isDecrementing, - isObject: isObject - }); - }; - } - - allMethods.forEach(function (_ref6) { - var method = _ref6.method, - path = _ref6.path, - sourceMethod = _ref6.sourceMethod, - type = _ref6.type; - var isDecrementing = type === 'decrementing'; - var isObject = type === 'object'; - var handler = createTransformer(method, handlers[sourceMethod], isDecrementing, isObject); - handler(path); - }); -} - -module.exports = createMacro(inlineLoops); \ No newline at end of file From 25bd9e0bf1c2c425ec7820b013e966b5e4fd8f66 Mon Sep 17 00:00:00 2001 From: planttheidea Date: Thu, 6 Jun 2019 08:17:13 -0700 Subject: [PATCH 3/6] remove build step from test watching --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4b620366..7b5d54b0 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "release": "release-it", "release:beta": "release-it --config=.release-it.beta.json", "test": "jest", - "test:watch": "npm run dist && npm run test -- --watch" + "test:watch": "npm run test -- --watch" }, "typings": "./index.d.ts", "version": "1.0.1" From 003bd9151b15475ee4c90193de8647b86bd12f63 Mon Sep 17 00:00:00 2001 From: planttheidea Date: Thu, 6 Jun 2019 08:35:50 -0700 Subject: [PATCH 4/6] add inline tests for reduce --- .../__runtime__/reduce-no-initialValue.js | 66 +++++++++ __tests__/__runtime__/reduce.js | 125 +++++++++++++----- 2 files changed, 159 insertions(+), 32 deletions(-) diff --git a/__tests__/__runtime__/reduce-no-initialValue.js b/__tests__/__runtime__/reduce-no-initialValue.js index a521863a..c5a4eecf 100644 --- a/__tests__/__runtime__/reduce-no-initialValue.js +++ b/__tests__/__runtime__/reduce-no-initialValue.js @@ -41,6 +41,72 @@ module.exports = { true: isEqual(reduce(ARRAY, isEven), ARRAY_RESULT) } }, + inlinedArrowExpression: { + decrementing: { + false: isEqual(reduceRight(ARRAY, (total, value) => value % 2 === 0 ? total + value : total), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(reduceRight(ARRAY, (total, value) => value % 2 === 0 ? total + value : total), DECREMENTING_ARRAY_RESULT), + }, + object: { + false: isEqual(reduceObject(OBJECT, (total, value) => value % 2 === 0 ? total + value : total), BAD_OBJECT_RESULT), + true: isEqual(reduceObject(OBJECT, (total, value) => value % 2 === 0 ? total + value : total), OBJECT_RESULT), + }, + standard: { + false: isEqual(reduce(ARRAY, (total, value) => value % 2 === 0 ? total + value : total), BAD_ARRAY_RESULT), + true: isEqual(reduce(ARRAY, (total, value) => value % 2 === 0 ? total + value : total), ARRAY_RESULT), + }, + }, + inlinedArrowReturn: { + decrementing: { + false: isEqual(reduceRight(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(reduceRight(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), DECREMENTING_ARRAY_RESULT), + }, + object: { + false: isEqual(reduceObject(OBJECT, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), BAD_OBJECT_RESULT), + true: isEqual(reduceObject(OBJECT, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), OBJECT_RESULT), + }, + standard: { + false: isEqual(reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), BAD_ARRAY_RESULT), + true: isEqual(reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), ARRAY_RESULT), + }, + }, + inlinedFunctionReturn: { + decrementing: { + false: isEqual(reduceRight(ARRAY, function(total, value) { + return value % 2 === 0 ? total + value : total; + }), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(reduceRight(ARRAY, function(total, value) { + return value % 2 === 0 ? total + value : total; + }), DECREMENTING_ARRAY_RESULT), + }, + object: { + false: isEqual(reduceObject(OBJECT, function(total, value) { + return value % 2 === 0 ? total + value : total; + }), BAD_OBJECT_RESULT), + true: isEqual(reduceObject(OBJECT, function(total, value) { + return value % 2 === 0 ? total + value : total; + }), OBJECT_RESULT), + }, + standard: { + false: isEqual(reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), BAD_ARRAY_RESULT), + true: isEqual(reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), ARRAY_RESULT), + }, + }, uncached: { decrementing: { false: isEqual( diff --git a/__tests__/__runtime__/reduce.js b/__tests__/__runtime__/reduce.js index 0c970b5b..11b90dd8 100644 --- a/__tests__/__runtime__/reduce.js +++ b/__tests__/__runtime__/reduce.js @@ -1,13 +1,11 @@ -const { - reduce, - reduceObject, - reduceRight -} = require("../../src/inline-loops.macro"); +const { reduce, reduceObject, reduceRight } = require('../../src/inline-loops.macro'); -const { deepEqual: isEqual } = require("fast-equals"); +const { deepEqual: isEqual } = require('fast-equals'); const ARRAY = [1, 2, 3, 4, 5, 6]; -const OBJECT = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 }; +const OBJECT = { + one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, +}; const isEven = (total, value) => { if (value % 2 === 0) { @@ -29,20 +27,83 @@ const OBJECT_RESULT = Object.values(OBJECT).reduce(isEven, 10); module.exports = { cached: { decrementing: { - false: isEqual( - reduceRight(ARRAY, isEven, 10), - BAD_DECREMENTING_ARRAY_RESULT - ), - true: isEqual(reduceRight(ARRAY, isEven, 10), DECREMENTING_ARRAY_RESULT) + false: isEqual(reduceRight(ARRAY, isEven, 10), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(reduceRight(ARRAY, isEven, 10), DECREMENTING_ARRAY_RESULT), }, object: { false: isEqual(reduceObject(OBJECT, isEven, 10), BAD_OBJECT_RESULT), - true: isEqual(reduceObject(OBJECT, isEven, 10), OBJECT_RESULT) + true: isEqual(reduceObject(OBJECT, isEven, 10), OBJECT_RESULT), }, standard: { false: isEqual(reduce(ARRAY, isEven, 10), BAD_ARRAY_RESULT), - true: isEqual(reduce(ARRAY, isEven, 10), ARRAY_RESULT) - } + true: isEqual(reduce(ARRAY, isEven, 10), ARRAY_RESULT), + }, + }, + inlinedArrowExpression: { + decrementing: { + false: isEqual(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), DECREMENTING_ARRAY_RESULT), + }, + object: { + false: isEqual(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), OBJECT_RESULT), + }, + standard: { + false: isEqual(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), ARRAY_RESULT), + }, + }, + inlinedArrowReturn: { + decrementing: { + false: isEqual(reduceRight(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }, 10), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(reduceRight(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }, 10), DECREMENTING_ARRAY_RESULT), + }, + object: { + false: isEqual(reduceObject(OBJECT, (total, value) => { + return value % 2 === 0 ? total + value : total; + }, 10), BAD_OBJECT_RESULT), + true: isEqual(reduceObject(OBJECT, (total, value) => { + return value % 2 === 0 ? total + value : total; + }, 10), OBJECT_RESULT), + }, + standard: { + false: isEqual(reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }, 10), BAD_ARRAY_RESULT), + true: isEqual(reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }, 10), ARRAY_RESULT), + }, + }, + inlinedFunctionReturn: { + decrementing: { + false: isEqual(reduceRight(ARRAY, function(total, value) { + return value % 2 === 0 ? total + value : total; + }, 10), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(reduceRight(ARRAY, function(total, value) { + return value % 2 === 0 ? total + value : total; + }, 10), DECREMENTING_ARRAY_RESULT), + }, + object: { + false: isEqual(reduceObject(OBJECT, function(total, value) { + return value % 2 === 0 ? total + value : total; + }, 10), BAD_OBJECT_RESULT), + true: isEqual(reduceObject(OBJECT, function(total, value) { + return value % 2 === 0 ? total + value : total; + }, 10), OBJECT_RESULT), + }, + standard: { + false: isEqual(reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }, 10), BAD_ARRAY_RESULT), + true: isEqual(reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }, 10), ARRAY_RESULT), + }, }, uncached: { decrementing: { @@ -56,9 +117,9 @@ module.exports = { return total; }, - 10 + 10, ), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( reduceRight( @@ -70,10 +131,10 @@ module.exports = { return total; }, - 10 + 10, ), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( @@ -86,9 +147,9 @@ module.exports = { return total; }, - 10 + 10, ), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( reduceObject( @@ -100,10 +161,10 @@ module.exports = { return total; }, - 10 + 10, ), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( @@ -116,9 +177,9 @@ module.exports = { return total; }, - 10 + 10, ), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( reduce( @@ -130,10 +191,10 @@ module.exports = { return total; }, - 10 + 10, ), - ARRAY_RESULT - ) - } - } + ARRAY_RESULT, + ), + }, + }, }; From b2cc3e7b8ca04ee006e13610ba57a7c57e3f0fc5 Mon Sep 17 00:00:00 2001 From: planttheidea Date: Thu, 6 Jun 2019 08:52:00 -0700 Subject: [PATCH 5/6] beautify runtime test files also, kind of fix the autoformatting eslint did before --- __tests__/__runtime__/every.js | 62 ++++-- __tests__/__runtime__/filter.js | 69 +++++-- __tests__/__runtime__/find.js | 112 +++++----- __tests__/__runtime__/findIndex-findKey.js | 116 +++++------ __tests__/__runtime__/forEach.js | 61 +++--- __tests__/__runtime__/map.js | 106 +++++----- .../__runtime__/reduce-no-initialValue.js | 178 ++++++++++------ __tests__/__runtime__/reduce.js | 195 ++++++++++++++---- __tests__/__runtime__/some.js | 46 +++-- 9 files changed, 574 insertions(+), 371 deletions(-) diff --git a/__tests__/__runtime__/every.js b/__tests__/__runtime__/every.js index c0e535c8..7388604d 100644 --- a/__tests__/__runtime__/every.js +++ b/__tests__/__runtime__/every.js @@ -1,3 +1,5 @@ +/* eslint-disable */ + const { every, everyObject, everyRight } = require('../../src/inline-loops.macro'); const ARRAY = [1, 2, 3, 4, 5, 6]; @@ -45,30 +47,54 @@ module.exports = { }, inlinedArrowReturn: { decrementing: { - false: everyRight(ARRAY, value => value % 2 === 0), - true: everyRight(ARRAY, value => value > 0), + false: everyRight(ARRAY, value => { + return value % 2 === 0; + }), + true: everyRight(ARRAY, value => { + return value > 0; + }), }, object: { - false: everyObject(OBJECT, value => value % 2 === 0), - true: everyObject(OBJECT, value => value > 0), + false: everyObject(OBJECT, value => { + return value % 2 === 0; + }), + true: everyObject(OBJECT, value => { + return value > 0; + }), }, standard: { - false: every(ARRAY, value => value % 2 === 0), - true: every(ARRAY, value => value > 0), + false: every(ARRAY, value => { + return value % 2 === 0; + }), + true: every(ARRAY, value => { + return value > 0; + }), }, }, inlinedFunctionReturn: { decrementing: { - false: everyRight(ARRAY, value => value % 2 === 0), - true: everyRight(ARRAY, value => value > 0), + false: everyRight(ARRAY, function(value) { + return value % 2 === 0; + }), + true: everyRight(ARRAY, function(value) { + return value > 0; + }), }, object: { - false: everyObject(OBJECT, value => value % 2 === 0), - true: everyObject(OBJECT, value => value > 0), + false: everyObject(OBJECT, function(value) { + return value % 2 === 0; + }), + true: everyObject(OBJECT, function(value) { + return value > 0; + }), }, standard: { - false: every(ARRAY, value => value % 2 === 0), - true: every(ARRAY, value => value > 0), + false: every(ARRAY, function(value) { + return value % 2 === 0; + }), + true: every(ARRAY, function(value) { + return value > 0; + }), }, }, nested: { @@ -79,36 +105,36 @@ module.exports = { }, uncached: { decrementing: { - false: everyRight([].concat(ARRAY), (value) => { + false: everyRight([].concat(ARRAY), value => { const isEven = value % 2 === 0; return isEven; }), - true: everyRight([].concat(ARRAY), (value) => { + true: everyRight([].concat(ARRAY), value => { const isPositive = value > 0; return isPositive; }), }, object: { - false: everyObject(Object.assign({}, OBJECT), (value) => { + false: everyObject(Object.assign({}, OBJECT), value => { const isEven = value % 2 === 0; return isEven; }), - true: everyObject(Object.assign({}, OBJECT), (value) => { + true: everyObject(Object.assign({}, OBJECT), value => { const isPositive = value > 0; return isPositive; }), }, standard: { - false: every([].concat(ARRAY), (value) => { + false: every([].concat(ARRAY), value => { const isEven = value % 2 === 0; return isEven; }), - true: every([].concat(ARRAY), (value) => { + true: every([].concat(ARRAY), value => { const isPositive = value > 0; return isPositive; diff --git a/__tests__/__runtime__/filter.js b/__tests__/__runtime__/filter.js index 2ea026be..47c7fe87 100644 --- a/__tests__/__runtime__/filter.js +++ b/__tests__/__runtime__/filter.js @@ -1,10 +1,17 @@ +/* eslint-disable */ + const { filter, filterObject, filterRight } = require('../../src/inline-loops.macro'); const { deepEqual: isEqual } = require('fast-equals'); const ARRAY = [1, 2, 3, 4, 5, 6]; const OBJECT = { - one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, + one: 1, + two: 2, + three: 3, + four: 4, + five: 5, + six: 6, }; const isEven = value => value % 2 === 0; @@ -56,31 +63,43 @@ module.exports = { inlinedArrowReturn: { decrementing: { false: isEqual( - filterRight(ARRAY, value => value % 2 === 0), + filterRight(ARRAY, value => { + return value % 2 === 0; + }), BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - filterRight(ARRAY, value => value % 2 === 0), + filterRight(ARRAY, value => { + return value % 2 === 0; + }), DECREMENTING_ARRAY_RESULT, ), }, object: { false: isEqual( - filterObject(OBJECT, value => value % 2 === 0), + filterObject(OBJECT, value => { + return value % 2 === 0; + }), BAD_OBJECT_RESULT, ), true: isEqual( - filterObject(OBJECT, value => value % 2 === 0), + filterObject(OBJECT, value => { + return value % 2 === 0; + }), OBJECT_RESULT, ), }, standard: { false: isEqual( - filter(ARRAY, value => value % 2 === 0), + filter(ARRAY, value => { + return value % 2 === 0; + }), BAD_ARRAY_RESULT, ), true: isEqual( - filter(ARRAY, value => value % 2 === 0), + filter(ARRAY, value => { + return value % 2 === 0; + }), ARRAY_RESULT, ), }, @@ -88,31 +107,43 @@ module.exports = { inlinedFunctionReturn: { decrementing: { false: isEqual( - filterRight(ARRAY, value => value % 2 === 0), + filterRight(ARRAY, function(value) { + return value % 2 === 0; + }), BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - filterRight(ARRAY, value => value % 2 === 0), + filterRight(ARRAY, function(value) { + return value % 2 === 0; + }), DECREMENTING_ARRAY_RESULT, ), }, object: { false: isEqual( - filterObject(OBJECT, value => value % 2 === 0), + filterObject(OBJECT, function(value) { + return value % 2 === 0; + }), BAD_OBJECT_RESULT, ), true: isEqual( - filterObject(OBJECT, value => value % 2 === 0), + filterObject(OBJECT, function(value) { + return value % 2 === 0; + }), OBJECT_RESULT, ), }, standard: { false: isEqual( - filter(ARRAY, value => value % 2 === 0), + filter(ARRAY, function(value) { + return value % 2 === 0; + }), BAD_ARRAY_RESULT, ), true: isEqual( - filter(ARRAY, value => value % 2 === 0), + filter(ARRAY, function(value) { + return value % 2 === 0; + }), ARRAY_RESULT, ), }, @@ -120,7 +151,7 @@ module.exports = { uncached: { decrementing: { false: isEqual( - filterRight([].concat(ARRAY), (value) => { + filterRight([].concat(ARRAY), value => { const isEven = value % 2 === 0; return isEven; @@ -128,7 +159,7 @@ module.exports = { BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( - filterRight([].concat(ARRAY), (value) => { + filterRight([].concat(ARRAY), value => { const isEven = value % 2 === 0; return isEven; @@ -138,7 +169,7 @@ module.exports = { }, object: { false: isEqual( - filterObject(Object.assign({}, OBJECT), (value) => { + filterObject(Object.assign({}, OBJECT), value => { const isEven = value % 2 === 0; return isEven; @@ -146,7 +177,7 @@ module.exports = { BAD_OBJECT_RESULT, ), true: isEqual( - filterObject(Object.assign({}, OBJECT), (value) => { + filterObject(Object.assign({}, OBJECT), value => { const isEven = value % 2 === 0; return isEven; @@ -156,7 +187,7 @@ module.exports = { }, standard: { false: isEqual( - filter([].concat(ARRAY), (value) => { + filter([].concat(ARRAY), value => { const isEven = value % 2 === 0; return isEven; @@ -164,7 +195,7 @@ module.exports = { BAD_ARRAY_RESULT, ), true: isEqual( - filter([].concat(ARRAY), (value) => { + filter([].concat(ARRAY), value => { const isEven = value % 2 === 0; return isEven; diff --git a/__tests__/__runtime__/find.js b/__tests__/__runtime__/find.js index 26f1aa32..a4b978d4 100644 --- a/__tests__/__runtime__/find.js +++ b/__tests__/__runtime__/find.js @@ -1,9 +1,18 @@ -const { find, findObject, findRight } = require("../../src/inline-loops.macro"); +/* eslint-disable */ -const { deepEqual: isEqual } = require("fast-equals"); +const { find, findObject, findRight } = require('../../src/inline-loops.macro'); + +const { deepEqual: isEqual } = require('fast-equals'); const ARRAY = [1, 2, 3, 4, 5, 6]; -const OBJECT = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 }; +const OBJECT = { + one: 1, + two: 2, + three: 3, + four: 4, + five: 5, + six: 6, +}; const isEven = value => value % 2 === 0; @@ -20,39 +29,30 @@ module.exports = { cached: { decrementing: { false: isEqual(findRight(ARRAY, isEven), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(findRight(ARRAY, isEven), DECREMENTING_ARRAY_RESULT) + true: isEqual(findRight(ARRAY, isEven), DECREMENTING_ARRAY_RESULT), }, object: { false: isEqual(findObject(OBJECT, isEven), BAD_OBJECT_RESULT), - true: isEqual(findObject(OBJECT, isEven), OBJECT_RESULT) + true: isEqual(findObject(OBJECT, isEven), OBJECT_RESULT), }, standard: { false: isEqual(find(ARRAY, isEven), BAD_ARRAY_RESULT), - true: isEqual(find(ARRAY, isEven), ARRAY_RESULT) - } + true: isEqual(find(ARRAY, isEven), ARRAY_RESULT), + }, }, 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(findRight(ARRAY, value => value % 2 === 0), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(findRight(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) - } + true: isEqual(find(ARRAY, value => value % 2 === 0), ARRAY_RESULT), + }, }, inlinedArrowReturn: { decrementing: { @@ -60,43 +60,43 @@ module.exports = { findRight(ARRAY, value => { return value % 2 === 0; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( findRight(ARRAY, value => { return value % 2 === 0; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( findObject(OBJECT, value => { return value % 2 === 0; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( findObject(OBJECT, value => { return value % 2 === 0; }), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( find(ARRAY, value => { return value % 2 === 0; }), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( find(ARRAY, value => { return value % 2 === 0; }), - ARRAY_RESULT - ) - } + ARRAY_RESULT, + ), + }, }, inlinedFunctionReturn: { decrementing: { @@ -104,43 +104,29 @@ module.exports = { findRight(ARRAY, function(value) { return value % 2 === 0; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( findRight(ARRAY, function(value) { return value % 2 === 0; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( findObject(OBJECT, function(value) { return value % 2 === 0; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( findObject(OBJECT, function(value) { return value % 2 === 0; }), - OBJECT_RESULT - ) - }, - standard: { - false: isEqual( - find(ARRAY, function(value) { - return value % 2 === 0; - }), - BAD_ARRAY_RESULT + OBJECT_RESULT, ), - true: isEqual( - find(ARRAY, function(value) { - return value % 2 === 0; - }), - ARRAY_RESULT - ) - } + }, }, uncached: { decrementing: { @@ -150,7 +136,7 @@ module.exports = { return isEven; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( findRight([].concat(ARRAY), value => { @@ -158,8 +144,8 @@ module.exports = { return isEven; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( @@ -168,7 +154,7 @@ module.exports = { return isEven; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( findObject(Object.assign({}, OBJECT), value => { @@ -176,8 +162,8 @@ module.exports = { return isEven; }), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( @@ -186,7 +172,7 @@ module.exports = { return isEven; }), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( find([].concat(ARRAY), value => { @@ -194,8 +180,8 @@ module.exports = { return isEven; }), - ARRAY_RESULT - ) - } - } + ARRAY_RESULT, + ), + }, + }, }; diff --git a/__tests__/__runtime__/findIndex-findKey.js b/__tests__/__runtime__/findIndex-findKey.js index 95829e11..e0d5c046 100644 --- a/__tests__/__runtime__/findIndex-findKey.js +++ b/__tests__/__runtime__/findIndex-findKey.js @@ -1,13 +1,18 @@ -const { - findIndex, - findIndexRight, - findKey -} = require("../../src/inline-loops.macro"); +/* eslint-disable */ -const { deepEqual: isEqual } = require("fast-equals"); +const { findIndex, findIndexRight, findKey } = require('../../src/inline-loops.macro'); + +const { deepEqual: isEqual } = require('fast-equals'); const ARRAY = [1, 2, 3, 4, 5, 6]; -const OBJECT = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 }; +const OBJECT = { + one: 1, + two: 2, + three: 3, + four: 4, + five: 5, + six: 6, +}; const isEven = value => value % 2 === 0; @@ -17,49 +22,40 @@ const DECREMENTING_ARRAY_RESULT = 5; const BAD_ARRAY_RESULT = 5; const ARRAY_RESULT = ARRAY.findIndex(isEven); -const BAD_OBJECT_RESULT = "six"; -const OBJECT_RESULT = "two"; +const BAD_OBJECT_RESULT = 'six'; +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(findIndexRight(ARRAY, isEven), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(findIndexRight(ARRAY, isEven), DECREMENTING_ARRAY_RESULT), }, object: { false: isEqual(findKey(OBJECT, isEven), BAD_OBJECT_RESULT), - true: isEqual(findKey(OBJECT, isEven), OBJECT_RESULT) + true: isEqual(findKey(OBJECT, isEven), OBJECT_RESULT), }, standard: { false: isEqual(findIndex(ARRAY, isEven), BAD_ARRAY_RESULT), - true: isEqual(findIndex(ARRAY, isEven), ARRAY_RESULT) - } + true: isEqual(findIndex(ARRAY, isEven), ARRAY_RESULT), + }, }, inlinedArrowExpression: { decrementing: { false: isEqual( findIndexRight(ARRAY, value => value % 2 === 0), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), - true: isEqual( - findIndexRight(ARRAY, value => value % 2 === 0), - DECREMENTING_ARRAY_RESULT - ) + true: isEqual(findIndexRight(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) - } + true: isEqual(findIndex(ARRAY, value => value % 2 === 0), ARRAY_RESULT), + }, }, inlinedArrowReturn: { decrementing: { @@ -67,43 +63,43 @@ module.exports = { findIndexRight(ARRAY, value => { return value % 2 === 0; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( findIndexRight(ARRAY, value => { return value % 2 === 0; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( findKey(OBJECT, value => { return value % 2 === 0; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( findKey(OBJECT, value => { return value % 2 === 0; }), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( findIndex(ARRAY, value => { return value % 2 === 0; }), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( findIndex(ARRAY, value => { return value % 2 === 0; }), - ARRAY_RESULT - ) - } + ARRAY_RESULT, + ), + }, }, inlinedFunctionReturn: { decrementing: { @@ -111,43 +107,43 @@ module.exports = { findIndexRight(ARRAY, function(value) { return value % 2 === 0; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( findIndexRight(ARRAY, function(value) { return value % 2 === 0; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( findKey(OBJECT, function(value) { return value % 2 === 0; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( findKey(OBJECT, function(value) { return value % 2 === 0; }), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( findIndex(ARRAY, function(value) { return value % 2 === 0; }), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( findIndex(ARRAY, function(value) { return value % 2 === 0; }), - ARRAY_RESULT - ) - } + ARRAY_RESULT, + ), + }, }, uncached: { decrementing: { @@ -157,7 +153,7 @@ module.exports = { return isEven; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( findIndexRight([].concat(ARRAY), value => { @@ -165,8 +161,8 @@ module.exports = { return isEven; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( @@ -175,7 +171,7 @@ module.exports = { return isEven; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( findKey(Object.assign({}, OBJECT), value => { @@ -183,8 +179,8 @@ module.exports = { return isEven; }), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( @@ -193,7 +189,7 @@ module.exports = { return isEven; }), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( findIndex([].concat(ARRAY), value => { @@ -201,8 +197,8 @@ module.exports = { return isEven; }), - ARRAY_RESULT - ) - } - } + ARRAY_RESULT, + ), + }, + }, }; diff --git a/__tests__/__runtime__/forEach.js b/__tests__/__runtime__/forEach.js index 049a7be1..b150f275 100644 --- a/__tests__/__runtime__/forEach.js +++ b/__tests__/__runtime__/forEach.js @@ -1,15 +1,16 @@ -const { - forEach, - forEachObject, - forEachRight -} = require("../../src/inline-loops.macro"); +/* eslint-disable */ -const { deepEqual: isEqual } = require("fast-equals"); +const { forEach, forEachObject, forEachRight } = require('../../src/inline-loops.macro'); const ARRAY = [1, 2, 3, 4, 5, 6]; -const OBJECT = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 }; - -const isEven = value => value % 2 === 0; +const OBJECT = { + one: 1, + two: 2, + three: 3, + four: 4, + five: 5, + six: 6, +}; module.exports = { cached: { @@ -20,7 +21,7 @@ module.exports = { forEachRight(ARRAY, spy); return spy.mock.calls.length === 6; - })() + })(), }, object: { true: (() => { @@ -29,7 +30,7 @@ module.exports = { forEachObject(OBJECT, spy); return spy.mock.calls.length === 6; - })() + })(), }, standard: { true: (() => { @@ -38,8 +39,8 @@ module.exports = { forEach(ARRAY, spy); return spy.mock.calls.length === 6; - })() - } + })(), + }, }, inlinedArrowExpression: { decrementing: { @@ -49,7 +50,7 @@ module.exports = { forEachRight(ARRAY, () => count++); return count === 6; - })() + })(), }, object: { true: (() => { @@ -58,7 +59,7 @@ module.exports = { forEachObject(OBJECT, () => count++); return count === 6; - })() + })(), }, standard: { true: (() => { @@ -67,8 +68,8 @@ module.exports = { forEach(ARRAY, () => count++); return count === 6; - })() - } + })(), + }, }, inlinedArrowReturn: { decrementing: { @@ -80,7 +81,7 @@ module.exports = { }); return count === 6; - })() + })(), }, object: { true: (() => { @@ -91,7 +92,7 @@ module.exports = { }); return count === 6; - })() + })(), }, standard: { true: (() => { @@ -102,8 +103,8 @@ module.exports = { }); return count === 6; - })() - } + })(), + }, }, inlinedFunctionReturn: { decrementing: { @@ -115,7 +116,7 @@ module.exports = { }); return count === 6; - })() + })(), }, object: { true: (() => { @@ -126,7 +127,7 @@ module.exports = { }); return count === 6; - })() + })(), }, standard: { true: (() => { @@ -137,8 +138,8 @@ module.exports = { }); return count === 6; - })() - } + })(), + }, }, uncached: { decrementing: { @@ -150,7 +151,7 @@ module.exports = { }); return count === 6; - })() + })(), }, object: { true: (() => { @@ -161,7 +162,7 @@ module.exports = { }); return count === 6; - })() + })(), }, standard: { true: (() => { @@ -172,7 +173,7 @@ module.exports = { }); return count === 6; - })() - } - } + })(), + }, + }, }; diff --git a/__tests__/__runtime__/map.js b/__tests__/__runtime__/map.js index 2c76ed21..31561f62 100644 --- a/__tests__/__runtime__/map.js +++ b/__tests__/__runtime__/map.js @@ -1,9 +1,18 @@ -const { map, mapObject, mapRight } = require("../../src/inline-loops.macro"); +/* eslint-disable */ -const { deepEqual: isEqual } = require("fast-equals"); +const { map, mapObject, mapRight } = require('../../src/inline-loops.macro'); + +const { deepEqual: isEqual } = require('fast-equals'); const ARRAY = [1, 2, 3, 4, 5, 6]; -const OBJECT = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 }; +const OBJECT = { + one: 1, + two: 2, + three: 3, + four: 4, + five: 5, + six: 6, +}; const isEven = value => value % 2 === 0; @@ -24,39 +33,30 @@ module.exports = { cached: { decrementing: { false: isEqual(mapRight(ARRAY, isEven), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(mapRight(ARRAY, isEven), DECREMENTING_ARRAY_RESULT) + true: isEqual(mapRight(ARRAY, isEven), DECREMENTING_ARRAY_RESULT), }, object: { false: isEqual(mapObject(OBJECT, isEven), BAD_OBJECT_RESULT), - true: isEqual(mapObject(OBJECT, isEven), OBJECT_RESULT) + true: isEqual(mapObject(OBJECT, isEven), OBJECT_RESULT), }, standard: { false: isEqual(map(ARRAY, isEven), BAD_ARRAY_RESULT), - true: isEqual(map(ARRAY, isEven), ARRAY_RESULT) - } + true: isEqual(map(ARRAY, isEven), ARRAY_RESULT), + }, }, inlinedArrowExpression: { decrementing: { - false: isEqual( - mapRight(ARRAY, value => value % 2 === 0), - BAD_DECREMENTING_ARRAY_RESULT - ), - true: isEqual( - mapRight(ARRAY, value => value % 2 === 0), - DECREMENTING_ARRAY_RESULT - ) + false: isEqual(mapRight(ARRAY, value => value % 2 === 0), BAD_DECREMENTING_ARRAY_RESULT), + true: isEqual(mapRight(ARRAY, value => value % 2 === 0), DECREMENTING_ARRAY_RESULT), }, object: { - false: isEqual( - mapObject(OBJECT, value => value % 2 === 0), - BAD_OBJECT_RESULT - ), - true: isEqual(mapObject(OBJECT, value => value % 2 === 0), OBJECT_RESULT) + false: isEqual(mapObject(OBJECT, value => value % 2 === 0), BAD_OBJECT_RESULT), + true: isEqual(mapObject(OBJECT, value => value % 2 === 0), OBJECT_RESULT), }, standard: { false: isEqual(map(ARRAY, value => value % 2 === 0), BAD_ARRAY_RESULT), - true: isEqual(map(ARRAY, value => value % 2 === 0), ARRAY_RESULT) - } + true: isEqual(map(ARRAY, value => value % 2 === 0), ARRAY_RESULT), + }, }, inlinedArrowReturn: { decrementing: { @@ -64,43 +64,43 @@ module.exports = { mapRight(ARRAY, value => { return value % 2 === 0; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( mapRight(ARRAY, value => { return value % 2 === 0; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( mapObject(OBJECT, value => { return value % 2 === 0; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( mapObject(OBJECT, value => { return value % 2 === 0; }), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( map(ARRAY, value => { return value % 2 === 0; }), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( map(ARRAY, value => { return value % 2 === 0; }), - ARRAY_RESULT - ) - } + ARRAY_RESULT, + ), + }, }, inlinedFunctionReturn: { decrementing: { @@ -108,43 +108,43 @@ module.exports = { mapRight(ARRAY, function(value) { return value % 2 === 0; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( mapRight(ARRAY, function(value) { return value % 2 === 0; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( mapObject(OBJECT, function(value) { return value % 2 === 0; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( mapObject(OBJECT, function(value) { return value % 2 === 0; }), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( map(ARRAY, function(value) { return value % 2 === 0; }), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( map(ARRAY, function(value) { return value % 2 === 0; }), - ARRAY_RESULT - ) - } + ARRAY_RESULT, + ), + }, }, uncached: { decrementing: { @@ -154,7 +154,7 @@ module.exports = { return isEven; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( mapRight([].concat(ARRAY), value => { @@ -162,8 +162,8 @@ module.exports = { return isEven; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( @@ -172,7 +172,7 @@ module.exports = { return isEven; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( mapObject(Object.assign({}, OBJECT), value => { @@ -180,8 +180,8 @@ module.exports = { return isEven; }), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( @@ -190,7 +190,7 @@ module.exports = { return isEven; }), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( map([].concat(ARRAY), value => { @@ -198,8 +198,8 @@ module.exports = { return isEven; }), - ARRAY_RESULT - ) - } - } + ARRAY_RESULT, + ), + }, + }, }; diff --git a/__tests__/__runtime__/reduce-no-initialValue.js b/__tests__/__runtime__/reduce-no-initialValue.js index c5a4eecf..b1f7bfa4 100644 --- a/__tests__/__runtime__/reduce-no-initialValue.js +++ b/__tests__/__runtime__/reduce-no-initialValue.js @@ -1,10 +1,8 @@ -const { - reduce, - reduceObject, - reduceRight -} = require("../../src/inline-loops.macro"); +/* eslint-disable */ -const { deepEqual: isEqual } = require("fast-equals"); +const { reduce, reduceObject, reduceRight } = require('../../src/inline-loops.macro'); + +const { deepEqual: isEqual } = require('fast-equals'); const ARRAY = [1, 2, 3, 4, 5, 6]; const OBJECT = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 }; @@ -30,81 +28,135 @@ module.exports = { cached: { decrementing: { false: isEqual(reduceRight(ARRAY, isEven), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(reduceRight(ARRAY, isEven), DECREMENTING_ARRAY_RESULT) + true: isEqual(reduceRight(ARRAY, isEven), DECREMENTING_ARRAY_RESULT), }, object: { false: isEqual(reduceObject(OBJECT, isEven), BAD_OBJECT_RESULT), - true: isEqual(reduceObject(OBJECT, isEven), OBJECT_RESULT) + true: isEqual(reduceObject(OBJECT, isEven), OBJECT_RESULT), }, standard: { false: isEqual(reduce(ARRAY, isEven), BAD_ARRAY_RESULT), - true: isEqual(reduce(ARRAY, isEven), ARRAY_RESULT) - } + true: isEqual(reduce(ARRAY, isEven), ARRAY_RESULT), + }, }, inlinedArrowExpression: { decrementing: { - false: isEqual(reduceRight(ARRAY, (total, value) => value % 2 === 0 ? total + value : total), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(reduceRight(ARRAY, (total, value) => value % 2 === 0 ? total + value : total), DECREMENTING_ARRAY_RESULT), + false: isEqual( + reduceRight(ARRAY, (total, value) => (value % 2 === 0 ? total + value : total)), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + reduceRight(ARRAY, (total, value) => (value % 2 === 0 ? total + value : total)), + DECREMENTING_ARRAY_RESULT, + ), }, object: { - false: isEqual(reduceObject(OBJECT, (total, value) => value % 2 === 0 ? total + value : total), BAD_OBJECT_RESULT), - true: isEqual(reduceObject(OBJECT, (total, value) => value % 2 === 0 ? total + value : total), OBJECT_RESULT), + false: isEqual( + reduceObject(OBJECT, (total, value) => (value % 2 === 0 ? total + value : total)), + BAD_OBJECT_RESULT, + ), + true: isEqual( + reduceObject(OBJECT, (total, value) => (value % 2 === 0 ? total + value : total)), + OBJECT_RESULT, + ), }, standard: { - false: isEqual(reduce(ARRAY, (total, value) => value % 2 === 0 ? total + value : total), BAD_ARRAY_RESULT), - true: isEqual(reduce(ARRAY, (total, value) => value % 2 === 0 ? total + value : total), ARRAY_RESULT), + false: isEqual( + reduce(ARRAY, (total, value) => (value % 2 === 0 ? total + value : total)), + BAD_ARRAY_RESULT, + ), + true: isEqual( + reduce(ARRAY, (total, value) => (value % 2 === 0 ? total + value : total)), + ARRAY_RESULT, + ), }, }, inlinedArrowReturn: { decrementing: { - false: isEqual(reduceRight(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(reduceRight(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }), DECREMENTING_ARRAY_RESULT), + false: isEqual( + reduceRight(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + reduceRight(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), + DECREMENTING_ARRAY_RESULT, + ), }, object: { - false: isEqual(reduceObject(OBJECT, (total, value) => { - return value % 2 === 0 ? total + value : total; - }), BAD_OBJECT_RESULT), - true: isEqual(reduceObject(OBJECT, (total, value) => { - return value % 2 === 0 ? total + value : total; - }), OBJECT_RESULT), + false: isEqual( + reduceObject(OBJECT, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), + BAD_OBJECT_RESULT, + ), + true: isEqual( + reduceObject(OBJECT, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), + OBJECT_RESULT, + ), }, standard: { - false: isEqual(reduce(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }), BAD_ARRAY_RESULT), - true: isEqual(reduce(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }), ARRAY_RESULT), + false: isEqual( + reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), + BAD_ARRAY_RESULT, + ), + true: isEqual( + reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), + ARRAY_RESULT, + ), }, }, inlinedFunctionReturn: { decrementing: { - false: isEqual(reduceRight(ARRAY, function(total, value) { - return value % 2 === 0 ? total + value : total; - }), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(reduceRight(ARRAY, function(total, value) { - return value % 2 === 0 ? total + value : total; - }), DECREMENTING_ARRAY_RESULT), + false: isEqual( + reduceRight(ARRAY, function(total, value) { + return value % 2 === 0 ? total + value : total; + }), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + reduceRight(ARRAY, function(total, value) { + return value % 2 === 0 ? total + value : total; + }), + DECREMENTING_ARRAY_RESULT, + ), }, object: { - false: isEqual(reduceObject(OBJECT, function(total, value) { - return value % 2 === 0 ? total + value : total; - }), BAD_OBJECT_RESULT), - true: isEqual(reduceObject(OBJECT, function(total, value) { - return value % 2 === 0 ? total + value : total; - }), OBJECT_RESULT), + false: isEqual( + reduceObject(OBJECT, function(total, value) { + return value % 2 === 0 ? total + value : total; + }), + BAD_OBJECT_RESULT, + ), + true: isEqual( + reduceObject(OBJECT, function(total, value) { + return value % 2 === 0 ? total + value : total; + }), + OBJECT_RESULT, + ), }, standard: { - false: isEqual(reduce(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }), BAD_ARRAY_RESULT), - true: isEqual(reduce(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }), ARRAY_RESULT), + false: isEqual( + reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), + BAD_ARRAY_RESULT, + ), + true: isEqual( + reduce(ARRAY, (total, value) => { + return value % 2 === 0 ? total + value : total; + }), + ARRAY_RESULT, + ), }, }, uncached: { @@ -117,7 +169,7 @@ module.exports = { return total; }), - BAD_DECREMENTING_ARRAY_RESULT + BAD_DECREMENTING_ARRAY_RESULT, ), true: isEqual( reduceRight([].concat(ARRAY), (total, value) => { @@ -127,8 +179,8 @@ module.exports = { return total; }), - DECREMENTING_ARRAY_RESULT - ) + DECREMENTING_ARRAY_RESULT, + ), }, object: { false: isEqual( @@ -139,7 +191,7 @@ module.exports = { return total; }), - BAD_OBJECT_RESULT + BAD_OBJECT_RESULT, ), true: isEqual( reduceObject(Object.assign({}, OBJECT), (total, value) => { @@ -149,8 +201,8 @@ module.exports = { return total; }), - OBJECT_RESULT - ) + OBJECT_RESULT, + ), }, standard: { false: isEqual( @@ -161,7 +213,7 @@ module.exports = { return total; }), - BAD_ARRAY_RESULT + BAD_ARRAY_RESULT, ), true: isEqual( reduce([].concat(ARRAY), (total, value) => { @@ -171,8 +223,8 @@ module.exports = { return total; }), - ARRAY_RESULT - ) - } - } + ARRAY_RESULT, + ), + }, + }, }; diff --git a/__tests__/__runtime__/reduce.js b/__tests__/__runtime__/reduce.js index 11b90dd8..725dc76d 100644 --- a/__tests__/__runtime__/reduce.js +++ b/__tests__/__runtime__/reduce.js @@ -1,10 +1,17 @@ +/* eslint-disable */ + const { reduce, reduceObject, reduceRight } = require('../../src/inline-loops.macro'); const { deepEqual: isEqual } = require('fast-equals'); const ARRAY = [1, 2, 3, 4, 5, 6]; const OBJECT = { - one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, + one: 1, + two: 2, + three: 3, + four: 4, + five: 5, + six: 6, }; const isEven = (total, value) => { @@ -41,68 +48,170 @@ module.exports = { }, inlinedArrowExpression: { decrementing: { - false: isEqual(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), DECREMENTING_ARRAY_RESULT), + false: isEqual( + 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), + DECREMENTING_ARRAY_RESULT, + ), }, object: { - false: isEqual(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), OBJECT_RESULT), + false: isEqual( + 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), + OBJECT_RESULT, + ), }, standard: { - false: isEqual(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), ARRAY_RESULT), + false: isEqual( + 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), + ARRAY_RESULT, + ), }, }, inlinedArrowReturn: { decrementing: { - false: isEqual(reduceRight(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }, 10), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(reduceRight(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }, 10), DECREMENTING_ARRAY_RESULT), + false: isEqual( + reduceRight( + ARRAY, + (total, value) => { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + reduceRight( + ARRAY, + (total, value) => { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + DECREMENTING_ARRAY_RESULT, + ), }, object: { - false: isEqual(reduceObject(OBJECT, (total, value) => { - return value % 2 === 0 ? total + value : total; - }, 10), BAD_OBJECT_RESULT), - true: isEqual(reduceObject(OBJECT, (total, value) => { - return value % 2 === 0 ? total + value : total; - }, 10), OBJECT_RESULT), + false: isEqual( + reduceObject( + OBJECT, + (total, value) => { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + BAD_OBJECT_RESULT, + ), + true: isEqual( + reduceObject( + OBJECT, + (total, value) => { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + OBJECT_RESULT, + ), }, standard: { - false: isEqual(reduce(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }, 10), BAD_ARRAY_RESULT), - true: isEqual(reduce(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }, 10), ARRAY_RESULT), + false: isEqual( + reduce( + ARRAY, + (total, value) => { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + BAD_ARRAY_RESULT, + ), + true: isEqual( + reduce( + ARRAY, + (total, value) => { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + ARRAY_RESULT, + ), }, }, inlinedFunctionReturn: { decrementing: { - false: isEqual(reduceRight(ARRAY, function(total, value) { - return value % 2 === 0 ? total + value : total; - }, 10), BAD_DECREMENTING_ARRAY_RESULT), - true: isEqual(reduceRight(ARRAY, function(total, value) { - return value % 2 === 0 ? total + value : total; - }, 10), DECREMENTING_ARRAY_RESULT), + false: isEqual( + reduceRight( + ARRAY, + function(total, value) { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + BAD_DECREMENTING_ARRAY_RESULT, + ), + true: isEqual( + reduceRight( + ARRAY, + function(total, value) { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + DECREMENTING_ARRAY_RESULT, + ), }, object: { - false: isEqual(reduceObject(OBJECT, function(total, value) { - return value % 2 === 0 ? total + value : total; - }, 10), BAD_OBJECT_RESULT), - true: isEqual(reduceObject(OBJECT, function(total, value) { - return value % 2 === 0 ? total + value : total; - }, 10), OBJECT_RESULT), + false: isEqual( + reduceObject( + OBJECT, + function(total, value) { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + BAD_OBJECT_RESULT, + ), + true: isEqual( + reduceObject( + OBJECT, + function(total, value) { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + OBJECT_RESULT, + ), }, standard: { - false: isEqual(reduce(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }, 10), BAD_ARRAY_RESULT), - true: isEqual(reduce(ARRAY, (total, value) => { - return value % 2 === 0 ? total + value : total; - }, 10), ARRAY_RESULT), + false: isEqual( + reduce( + ARRAY, + (total, value) => { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + BAD_ARRAY_RESULT, + ), + true: isEqual( + reduce( + ARRAY, + (total, value) => { + return value % 2 === 0 ? total + value : total; + }, + 10, + ), + ARRAY_RESULT, + ), }, }, uncached: { diff --git a/__tests__/__runtime__/some.js b/__tests__/__runtime__/some.js index 87897d57..0e61d46d 100644 --- a/__tests__/__runtime__/some.js +++ b/__tests__/__runtime__/some.js @@ -1,4 +1,6 @@ -const { some, someObject, someRight } = require("../../src/inline-loops.macro"); +/* eslint-disable */ + +const { some, someObject, someRight } = require('../../src/inline-loops.macro'); const ARRAY = [1, 2, 3, 4, 5, 6]; const OBJECT = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6 }; @@ -10,30 +12,30 @@ module.exports = { cached: { decrementing: { false: someRight(ARRAY, isNegative), - true: someRight(ARRAY, isEven) + true: someRight(ARRAY, isEven), }, object: { false: someObject(OBJECT, isNegative), - true: someObject(OBJECT, isEven) + true: someObject(OBJECT, isEven), }, standard: { false: some(ARRAY, isNegative), - true: some(ARRAY, isEven) - } + true: some(ARRAY, isEven), + }, }, inlinedArrowExpression: { decrementing: { false: someRight(ARRAY, value => value < 0), - true: someRight(ARRAY, value => value % 2 === 0) + true: someRight(ARRAY, value => value % 2 === 0), }, object: { false: someObject(OBJECT, value => value < 0), - true: someObject(OBJECT, value => value % 2 === 0) + true: someObject(OBJECT, value => value % 2 === 0), }, standard: { false: some(ARRAY, value => value < 0), - true: some(ARRAY, value => value % 2 === 0) - } + true: some(ARRAY, value => value % 2 === 0), + }, }, inlinedArrowReturn: { decrementing: { @@ -42,7 +44,7 @@ module.exports = { }), true: someRight(ARRAY, value => { return value % 2 === 0; - }) + }), }, object: { false: someObject(OBJECT, value => { @@ -50,7 +52,7 @@ module.exports = { }), true: someObject(OBJECT, value => { return value % 2 === 0; - }) + }), }, standard: { false: some(ARRAY, value => { @@ -58,8 +60,8 @@ module.exports = { }), true: some(ARRAY, value => { return value % 2 === 0; - }) - } + }), + }, }, inlinedFunctionReturn: { decrementing: { @@ -68,7 +70,7 @@ module.exports = { }), true: someRight(ARRAY, function(value) { return value % 2 === 0; - }) + }), }, object: { false: someObject(OBJECT, function(value) { @@ -76,7 +78,7 @@ module.exports = { }), true: someObject(OBJECT, function(value) { return value % 2 === 0; - }) + }), }, standard: { false: some(ARRAY, function(value) { @@ -84,8 +86,8 @@ module.exports = { }), true: some(ARRAY, function(value) { return value % 2 === 0; - }) - } + }), + }, }, uncached: { decrementing: { @@ -98,7 +100,7 @@ module.exports = { const isEven = value % 2 === 0; return isEven; - }) + }), }, object: { false: someObject(Object.assign({}, OBJECT), value => { @@ -110,7 +112,7 @@ module.exports = { const isEven = value % 2 === 0; return isEven; - }) + }), }, standard: { false: some([].concat(ARRAY), value => { @@ -122,7 +124,7 @@ module.exports = { const isEven = value % 2 === 0; return isEven; - }) - } - } + }), + }, + }, }; From 425647bcf09f351c75ab6fe4ec789f2a89f70ae3 Mon Sep 17 00:00:00 2001 From: planttheidea Date: Thu, 6 Jun 2019 08:56:35 -0700 Subject: [PATCH 6/6] fix contrived example, both in test and in README --- README.md | 2 +- __tests__/__fixtures__/nested/contrived/code.js | 12 ++++++++---- __tests__/__fixtures__/nested/contrived/output.js | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4459322e..2df82d6e 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ function contrivedExample(array) { const doubleObject = reduce(doubled, (object, value) => ({ ...object, [value]: value - }); + }, {}); if (someObject(doubleObject, (value) => value > 100)) { console.log('I am large!'); diff --git a/__tests__/__fixtures__/nested/contrived/code.js b/__tests__/__fixtures__/nested/contrived/code.js index 00e8021b..0af757f0 100644 --- a/__tests__/__fixtures__/nested/contrived/code.js +++ b/__tests__/__fixtures__/nested/contrived/code.js @@ -2,10 +2,14 @@ import { map, reduce, someObject } from '../../../../src/inline-loops.macro'; if ( someObject( - reduce(map(array, value => value * 2), (object, value) => ({ - ...object, - [value]: value, - })), + reduce( + map(array, value => value * 2), + (object, value) => ({ + ...object, + [value]: value, + }), + {}, + ), value => value > 100, ) ) { diff --git a/__tests__/__fixtures__/nested/contrived/output.js b/__tests__/__fixtures__/nested/contrived/output.js index fd8efa11..16f8e11b 100644 --- a/__tests__/__fixtures__/nested/contrived/output.js +++ b/__tests__/__fixtures__/nested/contrived/output.js @@ -6,9 +6,9 @@ for (let _key = 0, _length = array.length, _value; _key < _length; ++_key) { _result.push(_value * 2); } -let _result2 = _result[0]; +let _result2 = {}; -for (let _key2 = 1, _length2 = _result.length, _value2; _key2 < _length2; ++_key2) { +for (let _key2 = 0, _length2 = _result.length, _value2; _key2 < _length2; ++_key2) { _value2 = _result[_key2]; _result2 = { ..._result2, [_value2]: _value2