Skip to content

Commit ea07075

Browse files
committed
feature: @putout/plugin-for-of: reduce: inside ExportNamedDeclaration
1 parent 0a02381 commit ea07075

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const b = a.reduce((c, d) => f(c, d), []);
2+
export const c = a.reduce((c, d) => f(c, d));
3+
export const d = a.reduceRight((c, d) => f(c, d));

packages/plugin-for-of/lib/reduce/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ const {
44
isBlockStatement,
55
isArrayExpression,
66
isCallExpression,
7+
isExportNamedDeclaration,
78
} = types;
89

910
export const report = () => `Use 'for...of' instead of '.reduce()'`;
1011

1112
export const match = () => ({
12-
'const __a = __b.reduce((__c, __d) => __e)': ({__b, __e}) => {
13+
'const __a = __b.reduce((__c, __d) => __e)': ({__b, __e}, path) => {
14+
if (isExportNamedDeclaration(path.parentPath))
15+
return false;
16+
1317
if (isArrayExpression(__b))
1418
return false;
1519

@@ -18,13 +22,21 @@ export const match = () => ({
1822

1923
return !isBlockStatement(__e);
2024
},
21-
'const __a = __b.reduceRight((__c, __d) => __e)': ({__b, __e}) => {
25+
'const __a = __b.reduceRight((__c, __d) => __e)': ({__b, __e}, path) => {
26+
if (isExportNamedDeclaration(path.parentPath))
27+
return false;
28+
2229
if (isArrayExpression(__b))
2330
return false;
2431

2532
return !isBlockStatement(__e);
2633
},
27-
'const __a = __b.reduce((__c, __d) => __e, __f)': ({__e}) => !isBlockStatement(__e),
34+
'const __a = __b.reduce((__c, __d) => __e, __f)': ({__e}, path) => {
35+
if (isExportNamedDeclaration(path.parentPath))
36+
return false;
37+
38+
return !isBlockStatement(__e);
39+
},
2840
});
2941

3042
export const replace = () => ({

packages/plugin-for-of/lib/reduce/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ test('plugin-convert-reduce-to-for-of: transform: two', (t) => {
2424
t.end();
2525
});
2626

27+
test('plugin-convert-reduce-to-for-of: no report: export', (t) => {
28+
t.noReport('export');
29+
t.end();
30+
});
31+
2732
test('plugin-convert-reduce-to-for-of: transform: initial', (t) => {
2833
t.transform('initial', {
2934
'remove-nested-blocks': removeNestedBlocks,

0 commit comments

Comments
 (0)