Skip to content

Commit

Permalink
feature: @putout/plugin-minify: merge-loops: add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jan 8, 2024
1 parent 213bcdf commit e7b7491
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/plugin-minify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ npm i @putout/plugin-putout -D
"mangleClassNames": true
}],
"minify/merge-variables": "on",
"minify/merge-loops": "on",
"minify/remove-var-undefined": "on",
"minify/remove-return-undefined": "on",
"minify/simplify-floor": "on",
Expand Down Expand Up @@ -329,6 +330,27 @@ var a;
var b;
```

## merge-loops

Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c9b77f37202389d30924135c2db3db9e/345edfc73c65b6cdf53c9b5e68bf689e4128291f).

### ❌ Example of incorrect code

```js
for (const aa of a)
d.push(aa);

for (const bb of b)
d.push(bb);
```

### ✅ Example of correct code

```js
for (const aa of [...a, ...b])
d.push(aa);
```

## simplify-floor

Not only shorter, but faster:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
for (const aa of [...a, ...b])
d.push(aa);

b.forEach((bb) => d.push(bb));

for (const aa of [...a, ...b])
d.push(aa);
11 changes: 11 additions & 0 deletions packages/plugin-minify/lib/merge-loops/fixture/merge-loops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
a.forEach(aa=>d.push(aa)),
b.forEach(bb=>d.push(bb));

b.forEach(bb=>d.push(bb));


for (const aa of a)
d.push(aa);

for (const bb of b)
d.push(bb);
40 changes: 40 additions & 0 deletions packages/plugin-minify/lib/merge-loops/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
operator,
template,
} from 'putout';

const {
remove,
getTemplateValues,
compare,
} = operator;

const FOR = 'for (const __a of __b)__c(__e)';

const createLoop = template(`for (const __a of [...__b, ...__z])__c(__e)`, {
placeholderPattern: /^__[a-z]$/,
});

export const report = () => `Merge loops`;

export const replace = () => ({
'__a.forEach((__b) => __e(__b)),__c.forEach((__d) => __e(__d))': 'for (const __b of [...__a, ...__c])__e(__b)',
[FOR]: ({__a, __b, __c, __e}, path) => {
const next = path.getNextSibling();

if (compare(next, FOR)) {
const {__b: __z} = getTemplateValues(next, FOR);
remove(next);

return createLoop({
__a,
__b,
__c,
__e,
__z,
});
}

return path;
},
});
19 changes: 19 additions & 0 deletions packages/plugin-minify/lib/merge-loops/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {createTest} from '@putout/test';
import * as plugin from './index.js';

const test = createTest(import.meta.url, {
printer: 'putout',
plugins: [
['merge-loops', plugin],
],
});

test('packages: merge-loops: report', (t) => {
t.report('merge-loops', `Merge loops`);
t.end();
});

test('packages: merge-loops: transform', (t) => {
t.transform('merge-loops');
t.end();
});
9 changes: 9 additions & 0 deletions packages/plugin-minify/test/fixture/merge-loops-fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(a.forEach((A) => d.push(A)), b.forEach((_) => d.push(_)));

b.forEach((B) => d.push(B));

for (const c of a)
d.push(c);

for (const C of b)
d.push(C);
11 changes: 11 additions & 0 deletions packages/plugin-minify/test/fixture/merge-loops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
a.forEach(aa=>d.push(aa)),
b.forEach(bb=>d.push(bb));

b.forEach(bb=>d.push(bb));


for (const aa of a)
d.push(aa);

for (const bb of b)
d.push(bb);
5 changes: 5 additions & 0 deletions packages/plugin-minify/test/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,8 @@ test('plugin-minify: transform: if', (t) => {
t.transform('if');
t.end();
});

test('plugin-minify: transform: merge-loops', (t) => {
t.transform('merge-loops');
t.end();
});

0 comments on commit e7b7491

Please sign in to comment.