-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: @putout/plugin-minify: merge-loops: add
- Loading branch information
1 parent
213bcdf
commit e7b7491
Showing
8 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/plugin-minify/lib/merge-loops/fixture/merge-loops-fix.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
packages/plugin-minify/lib/merge-loops/fixture/merge-loops.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters