Skip to content

Commit

Permalink
better handling of lazy usages
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Dec 17, 2023
1 parent 0ce46f3 commit d7a62cf
Show file tree
Hide file tree
Showing 23 changed files with 126 additions and 35 deletions.
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/complex/async-await/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { find } from '../../../../src/inline-loops.macro';

async function getStuff(promises) {
return await find(promises, (promise) => !!promise);
}
16 changes: 16 additions & 0 deletions __tests__/__fixtures__/complex/async-await/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
async function getStuff(promises) {
let _match;
for (
let _key = 0, _length = promises.length, _promise, _result;
_key < _length;
++_key
) {
_promise = promises[_key];
_result = !!_promise;
if (_result) {
_match = _promise;
break;
}
}
return await _match;
}
8 changes: 8 additions & 0 deletions __tests__/__fixtures__/complex/binary-expression/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function getStuff(array) {
let _a = array[0];
for (let _key = 1, _length = array.length, _v; _key < _length; ++_key) {
_v = array[_key];
_a = _a + _v * 2;
}
return array ** _a;
}
13 changes: 0 additions & 13 deletions __tests__/__fixtures__/complex/conditional-binary/output.js

This file was deleted.

13 changes: 0 additions & 13 deletions __tests__/__fixtures__/complex/conditional-unary/output.js

This file was deleted.

5 changes: 5 additions & 0 deletions __tests__/__fixtures__/complex/default-param/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { map } from '../../../../src/inline-loops.macro';

function getStuff(array, doubled = map(array, (v) => v * 2)) {
return doubled;
}
14 changes: 14 additions & 0 deletions __tests__/__fixtures__/complex/default-param/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function getStuff(
array,
doubled = (() => {
const _length = array.length;
const _results = Array(_length);
for (let _key = 0, _v; _key < _length; ++_key) {
_v = array[_key];
_results[_key] = _v * 2;
}
return _results;
})(),
) {
return doubled;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { reduce } from '../../../../src/inline-loops.macro';

function getStuff(array) {
return reduce(array, (a, v) => a + v * 2, 0) || 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function getStuff(array) {
let _a = 0;
for (let _key = 0, _length = array.length, _v; _key < _length; ++_key) {
_v = array[_key];
_a = _a + _v * 2;
}
return _a || 1;
}
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/complex/ternary-alternative/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { map } from '../../../../src/inline-loops.macro';

function getStuff(array, foo) {
return foo === 'bar' ? array : map(array, (v) => v * 2);
}
13 changes: 13 additions & 0 deletions __tests__/__fixtures__/complex/ternary-alternative/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function getStuff(array, foo) {
return foo === 'bar'
? array
: (() => {
const _length = array.length;
const _results = Array(_length);
for (let _key = 0, _v; _key < _length; ++_key) {
_v = array[_key];
_results[_key] = _v * 2;
}
return _results;
})();
}
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/complex/ternary-condition/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { reduce } from '../../../../src/inline-loops.macro';

function getStuff(array, foo) {
return reduce(array, (a, v) => a + v * 2, 0) ? 'stuff' : null;
}
8 changes: 8 additions & 0 deletions __tests__/__fixtures__/complex/ternary-condition/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function getStuff(array, foo) {
let _a = 0;
for (let _key = 0, _length = array.length, _v; _key < _length; ++_key) {
_v = array[_key];
_a = _a + _v * 2;
}
return _a ? 'stuff' : null;
}
8 changes: 8 additions & 0 deletions __tests__/__fixtures__/complex/unary-expression/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function getStuff(array) {
let _a = array[0];
for (let _key = 1, _length = array.length, _v; _key < _length; ++_key) {
_v = array[_key];
_a = _a + _v * 2;
}
return array + +_a;
}
35 changes: 26 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,33 @@ export function handleInvalidUsage(
}
}

export function isConditionalUsage(path: Path<CallExpression>): boolean {
export function isLazyUsage(path: Path<CallExpression>): boolean {
const parentPath = path.parentPath;

return (
parentPath.isAwaitExpression() ||
parentPath.isBinaryExpression() ||
parentPath.isConditionalExpression() ||
parentPath.isLogicalExpression() ||
parentPath.isUnaryExpression()
);
if (parentPath.isPattern()) {
return true;
}

if (!parentPath.isExpression()) {
return false;
}

const grandparentPath = parentPath.parentPath;
const maybeNestedConditional =
grandparentPath.isPattern() ||
(grandparentPath.isExpression() && !grandparentPath.isBinaryExpression());

if (parentPath.isLogicalExpression()) {
return (
!maybeNestedConditional && parentPath.get('right').node === path.node
);
}

if (parentPath.isConditionalExpression()) {
return !maybeNestedConditional && parentPath.get('test').node !== path.node;
}

return maybeNestedConditional;
}

export function isMacroHandlerName(
Expand Down Expand Up @@ -183,7 +200,7 @@ export function replaceOrRemove(
functionParent &&
((Array.isArray(contents) && contents.length > 1) ||
local.contents.length > 1 ||
isConditionalUsage(path));
isLazyUsage(path));

if (shouldWrapInIife) {
if (!t.isIdentifier(replacement, { name: 'undefined' })) {
Expand Down

0 comments on commit d7a62cf

Please sign in to comment.