Skip to content

Commit

Permalink
prevent forEach methods from hoisting early returns, instead just c…
Browse files Browse the repository at this point in the history
…onverting to `continue` statements (#33)
  • Loading branch information
planttheidea committed Dec 18, 2023
1 parent 2ecb4da commit dbf6ab0
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 4 deletions.
12 changes: 12 additions & 0 deletions __tests__/__fixtures__/complex/for-each-early-return/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { forEach } = require('../../../../src/inline-loops.macro');
import { log } from './log';

function logItems(items) {
forEach(items, (item) => {
if (!item.position) {
return;
}

log(item);
});
}
10 changes: 10 additions & 0 deletions __tests__/__fixtures__/complex/for-each-early-return/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { log } from './log';
function logItems(items) {
for (let _key = 0, _length = items.length, _item; _key < _length; ++_key) {
_item = items[_key];
if (!_item.position) {
continue;
}
log(_item);
}
}
2 changes: 1 addition & 1 deletion __tests__/__fixtures__/complex/for-each-hoisted/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { log } from './log';

function logItems(items) {
forEach(items, (item) => {
if (!item.position) {
if (!this.position) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion __tests__/__fixtures__/complex/for-each-hoisted/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { log } from './log';
function logItems(items) {
(() => {
const _fn = (_item) => {
if (!_item.position) {
if (!this.position) {
return;
}
log(_item);
Expand Down
12 changes: 12 additions & 0 deletions __tests__/__fixtures__/complex/for-each-right-early-return/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { forEachRight } = require('../../../../src/inline-loops.macro');
import { log } from './log';

function logItems(items) {
forEachRight(items, (item) => {
if (!item.position) {
return;
}

log(item);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { log } from './log';
function logItems(items) {
for (let _key = items.length, _item; --_key >= 0; ) {
_item = items[_key];
if (!_item.position) {
continue;
}
log(_item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { log } from './log';

function logItems(items) {
forEachRight(items, (item) => {
if (!item.position) {
if (!this.position) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { log } from './log';
function logItems(items) {
(() => {
const _fn = (_item) => {
if (!_item.position) {
if (!this.position) {
return;
}
log(_item);
Expand Down
9 changes: 9 additions & 0 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ export function createHandlers(babel: MacroParams['babel']) {

if (body.isBlockStatement()) {
if (!callbackContainsThis) {
if (isForEach) {
body.traverse(traverseConfigs.stripReturn, { isForEach });

return {
injectedBody: body.node.body,
returned: t.identifier('undefined'),
};
}

if (returnCount < 2) {
body.traverse(traverseConfigs.stripReturn, { isForEach });
}
Expand Down

0 comments on commit dbf6ab0

Please sign in to comment.