Skip to content

Commit 1d627a9

Browse files
committed
feature: @putout/plugin-remove-useless-export-specifiers: add
1 parent 53d5c40 commit 1d627a9

File tree

15 files changed

+144
-4
lines changed

15 files changed

+144
-4
lines changed

docs/syntax-errors.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,15 @@ export const rules = [
349349
```
350350

351351
</details>
352+
353+
354+
<details><summary>remove useless export specifier</summary>
355+
356+
```diff
357+
export const hello = () => 'world';
358+
export const {
359+
- hello,
360+
}
361+
```
362+
363+
</details>

packages/plugin-esm/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ npm i putout @putout/plugin-esm -D
2727
-[remove-quotes-from-import-assertions](#remove-quotes-from-import-assertions);
2828
-[remove-empty-import](#remove-empty-import);
2929
-[remove-empty-export](#remove-empty-export);
30+
-[remove-useless-export-specifiers](#remove-useless-export-specifiers);
3031
-[sort-imports-by-specifiers](#sort-imports-by-specifiers);
3132
-[inline-export](#inline-export);
3233

@@ -54,7 +55,8 @@ npm i putout @putout/plugin-esm -D
5455
"esm/sort-imports-by-specifiers": "on",
5556
"esm/resolve-imported-file": "off",
5657
"esm/apply-namespace-of-file": "off",
57-
"esm/inline-export": "off"
58+
"esm/inline-export": "off",
59+
"esm/remove-useless-export-specifiers": "off"
5860
}
5961
}
6062
```
@@ -137,6 +139,20 @@ export function sum(a, b) {
137139
}
138140
```
139141

142+
143+
### remove-useless-export-specifiers
144+
145+
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e5c3ea469437ade0f4467323dcec9a36/7c298c7078b004ae3aba2a29e38579bf8f48a098).
146+
147+
#### ❌ Example of incorrect code
148+
149+
```diff
150+
export const hello = () => 'world';
151+
export const {
152+
- hello,
153+
}
154+
```
155+
140156
### declare-imports-first
141157

142158
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/b1c18e5d726afe4ebb69d6b7a7dda82b/8189590815a1b8adb35bb8a846e28228e3c7fadf). For **CommonJS** use [nodejs/declare-after-require](https://github.com/coderaiser/putout/tree/master/packages/plugin-nodejs#declare-after-require).

packages/plugin-esm/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as removeUselessExportSpecifiers from './remove-useless-export-specifiers/index.js';
12
import * as inlineExport from './inline-export/index.js';
23
import * as applyNamespaceImportToFile from './apply-namespace-import-to-file/index.js';
34
import * as resolveImportedFile from './resolve-imported-file/index.js';
@@ -28,4 +29,5 @@ export const rules = {
2829
'apply-namespace-import-to-file': ['off', applyNamespaceImportToFile],
2930
'merge-declaration-with-export': mergeDeclarationWithExport,
3031
'inline-export': inlineExport,
32+
'remove-useless-export-specifiers': removeUselessExportSpecifiers,
3133
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * as ns from 'x';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
import * as ns_1 from "x";
3+
export { ns_1 as ns };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export {};
2+
3+
export const hello = () => 'world';
4+
5+
export {};
6+
7+
const x = 3;
8+
9+
export {
10+
x,
11+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export {
2+
z,
3+
}
4+
5+
export const hello = () => 'world';
6+
7+
export {
8+
hello,
9+
}
10+
11+
const x = 3;
12+
13+
export {
14+
hello,
15+
x,
16+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {types, operator} from 'putout';
2+
3+
const {remove} = operator;
4+
const {isExportNamedDeclaration} = types;
5+
6+
export const report = () => `Avoid useless export specifier`;
7+
8+
export const fix = (path) => {
9+
remove(path);
10+
};
11+
12+
export const traverse = ({push}) => ({
13+
ExportSpecifier(path) {
14+
const {node, scope} = path;
15+
const {local} = node;
16+
const {name} = local;
17+
18+
const binding = scope.bindings[name];
19+
20+
if (binding) {
21+
if (isExportNamedDeclaration(binding.path.parentPath.parentPath))
22+
push(path);
23+
24+
return;
25+
}
26+
27+
push(path);
28+
},
29+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
import * as applyExportFrom from '../apply-export-from/index.js';
4+
5+
const test = createTest(import.meta.url, {
6+
plugins: [
7+
['remove-useless-export-specifiers', plugin],
8+
],
9+
});
10+
11+
test('esm: remove-useless-export: report: remove-useless-export-specifiers', (t) => {
12+
t.report('remove-useless-export-specifiers', `Avoid useless export specifier`);
13+
t.end();
14+
});
15+
16+
test('esm: remove-useless-export-specifiers: transform', (t) => {
17+
t.transform('remove-useless-export-specifiers');
18+
t.end();
19+
});
20+
21+
test('esm: remove-useless-export-specifiers: transform: apply-export-from', (t) => {
22+
t.transform('apply-export-from', {
23+
applyExportFrom,
24+
});
25+
t.end();
26+
});

packages/plugin-esm/test/esm.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ test('plugin-esm: transform: inline-export', (t) => {
7676
t.transform('inline-export');
7777
t.end();
7878
});
79+
80+
test('plugin-esm: transform: remove-useless-export-specifiers', (t) => {
81+
t.transform('remove-useless-export-specifiers');
82+
t.end();
83+
});

0 commit comments

Comments
 (0)