Skip to content

Commit 39a8a51

Browse files
committed
feature: @putout/plugin-esm: merge-declaration-with-export: add
1 parent 1e780c4 commit 39a8a51

File tree

68 files changed

+244
-406
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+244
-406
lines changed
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {operator} from 'putout';
22

33
const {ignore, __json} = operator;
4-
const {
4+
5+
export const {
56
match,
67
replace,
78
report,
@@ -12,9 +13,3 @@ const {
1213
'**/*.config.*',
1314
],
1415
});
15-
16-
export {
17-
match,
18-
replace,
19-
report,
20-
};

packages/plugin-eslint/lib/convert-rc-to-flat/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import * as rcToFlat from './rc-to-flat/index.js';
33
import * as declare from '../declare/index.js';
44

55
const {matchFiles} = operator;
6-
const {
6+
7+
export const {
78
report,
89
fix,
910
scan,
@@ -20,9 +21,3 @@ const {
2021
],
2122
},
2223
});
23-
24-
export {
25-
report,
26-
fix,
27-
scan,
28-
};

packages/plugin-esm/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ npm i putout @putout/plugin-esm -D
2323
-[declare-imports-first](#declare-imports-first);
2424
-[group-imports-by-source](#group-imports-by-source);
2525
-[merge-duplicate-imports](#merge-duplicate-imports);
26+
-[merge-declaration-with-export](#merge-declaration-with-export);
2627
-[remove-quotes-from-import-assertions](#remove-quotes-from-import-assertions);
2728
-[remove-empty-import](#remove-empty-import);
2829
-[remove-empty-export](#remove-empty-export);
@@ -43,6 +44,7 @@ npm i putout @putout/plugin-esm -D
4344
"esm/declare-imports-first": "on",
4445
"esm/group-imports-by-source": "on",
4546
"esm/merge-duplicate-imports": "on",
47+
"esm/merge-declaration-with-export": "on",
4648
"esm/remove-quotes-from-import-assertions": "on",
4749
"esm/remove-empty-export": "on",
4850
"esm/remove-empty-import": ["on", {
@@ -162,6 +164,36 @@ import ss from '../../bb/ss.js';
162164
const c = 5;
163165
```
164166

167+
### merge-declaration-with-export
168+
169+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/3fad517d76942d0a1f51d7f58a2799af/7a052ab18a31fa382228d6513c412be37091cfb8).
170+
171+
#### ❌ Example of incorrect code
172+
173+
```js
174+
const {
175+
report,
176+
fix,
177+
scan,
178+
} = createRemoveFiles(['*.swp', '*.swo']);
179+
180+
export {
181+
report,
182+
fix,
183+
scan,
184+
};
185+
```
186+
187+
##### ✅ Example of correct code
188+
189+
```js
190+
export const {
191+
report,
192+
fix,
193+
scan,
194+
} = createRemoveFiles(['*.swp', '*.swo']);
195+
```
196+
165197
### merge-duplicate-imports
166198

167199
#### join

packages/plugin-esm/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as sortImportsBySpecifiers from './sort-imports-by-specifiers/index.js'
88
import * as removeEmptyImport from './remove-empty-import/index.js';
99
import * as removeEmptyExport from './remove-empty-export/index.js';
1010
import * as mergeDuplicateImports from './merge-duplicate-imports/index.js';
11+
import * as mergeDeclarationWithExport from './merge-declaration-with-export/index.js';
1112
import * as convertAssertToWith from './convert-assert-to-with/index.js';
1213
import * as applyExportFrom from './apply-export-from/index.js';
1314

@@ -24,4 +25,5 @@ export const rules = {
2425
'sort-imports-by-specifiers': sortImportsBySpecifiers,
2526
'resolve-imported-file': ['off', resolveImportedFile],
2627
'apply-namespace-import-to-file': ['off', applyNamespaceImportToFile],
28+
'merge-declaration-with-export': mergeDeclarationWithExport,
2729
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import matchErrors from 'abc';
2+
3+
export {
4+
matchErrors,
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const {
2+
report,
3+
fix,
4+
scan,
5+
} = createRemoveFiles(['*.swp', '*.swo']);
6+
7+
export const x = () => {};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const {
2+
report,
3+
fix,
4+
scan,
5+
} = createRemoveFiles(['*.swp', '*.swo']);
6+
7+
export {
8+
report,
9+
fix,
10+
scan,
11+
};
12+
13+
export const x = () => {};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {operator, types} from 'putout';
2+
3+
const {remove, replaceWith} = operator;
4+
const {
5+
exportNamedDeclaration,
6+
isExportSpecifier,
7+
isVariableDeclarator,
8+
} = types;
9+
10+
export const report = () => `Use 'if condition' instead of 'ternary expression'`;
11+
12+
export const fix = ({path, bindingPath}) => {
13+
const {parentPath} = bindingPath;
14+
const {node} = parentPath;
15+
16+
replaceWith(parentPath, exportNamedDeclaration(node));
17+
remove(path);
18+
};
19+
export const traverse = ({push}) => ({
20+
ExportNamedDeclaration(path) {
21+
const {specifiers} = path.node;
22+
23+
if (!specifiers.length)
24+
return;
25+
26+
const bindingPaths = [];
27+
28+
for (const spec of specifiers) {
29+
if (!isExportSpecifier(spec))
30+
return;
31+
32+
const {local} = spec;
33+
34+
const {name} = local;
35+
const binding = path.scope.bindings[name];
36+
37+
if (!binding)
38+
return;
39+
40+
const {path: bindingPath} = binding;
41+
42+
if (!isVariableDeclarator(bindingPath))
43+
return;
44+
45+
if (!bindingPaths.length) {
46+
bindingPaths.push(bindingPath);
47+
continue;
48+
}
49+
}
50+
51+
const [bindingPath] = bindingPaths;
52+
53+
push({
54+
path,
55+
bindingPath,
56+
});
57+
},
58+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['merge-declaration-with-export', plugin],
7+
],
8+
});
9+
10+
test('esm: merge-declaration-with-export: report', (t) => {
11+
t.report('merge-declaration-with-export', `Use 'if condition' instead of 'ternary expression'`);
12+
t.end();
13+
});
14+
15+
test('esm: merge-declaration-with-export: transform', (t) => {
16+
t.transform('merge-declaration-with-export');
17+
t.end();
18+
});
19+
20+
test('esm: merge-declaration-with-export: no report: import', (t) => {
21+
t.noReport('import');
22+
t.end();
23+
});

packages/plugin-esm/lib/merge-duplicate-imports/join/fixture/duplicate-specifier-fix.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import {
55
types,
66
} from 'putout';
77

8-
{
9-
const {replaceWith} = operator;
10-
}
118
traverse(filesystem, {
129
StringLiteral(path) {
1310
if (isIdentifier(path.node.id, {name: 'b'}))

0 commit comments

Comments
 (0)