Skip to content

Commit

Permalink
support unary and binary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
planttheidea committed Dec 16, 2023
1 parent fa79b69 commit f616ec1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/complex/conditional-binary/code.js
@@ -0,0 +1,5 @@
import { reduce } from '../../../../src/inline-loops.macro';

function getStuff(array) {
return array ** reduce(array, (a, v) => a + v * 2);
}
13 changes: 13 additions & 0 deletions __tests__/__fixtures__/complex/conditional-binary/output.js
@@ -0,0 +1,13 @@
function getStuff(array) {
return (
array **
(() => {
let _a = array[0];
for (let _key = 1, _length = array.length, _v; _key < _length; ++_key) {
_v = array[_key];
_a = _a + _v * 2;
}
return _a;
})()
);
}
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/complex/conditional-unary/code.js
@@ -0,0 +1,5 @@
import { reduce } from '../../../../src/inline-loops.macro';

function getStuff(array) {
return array + +reduce(array, (a, v) => a + v * 2);
}
13 changes: 13 additions & 0 deletions __tests__/__fixtures__/complex/conditional-unary/output.js
@@ -0,0 +1,13 @@
function getStuff(array) {
return (
array +
+(() => {
let _a = array[0];
for (let _key = 1, _length = array.length, _v; _key < _length; ++_key) {
_v = array[_key];
_a = _a + _v * 2;
}
return _a;
})()
);
}
5 changes: 4 additions & 1 deletion src/utils.ts
Expand Up @@ -132,7 +132,10 @@ export function isConditionalUsage(path: Path<CallExpression>): boolean {
const parentPath = path.parentPath;

return (
parentPath.isConditionalExpression() || parentPath.isLogicalExpression()
parentPath.isConditionalExpression() ||
parentPath.isLogicalExpression() ||
parentPath.isUnaryExpression() ||
parentPath.isBinaryExpression()
);
}

Expand Down

0 comments on commit f616ec1

Please sign in to comment.