Skip to content

Commit 58f32a1

Browse files
committed
feature: @putout/plugin-esm: apply-export-from: add
1 parent 579b190 commit 58f32a1

File tree

9 files changed

+106
-0
lines changed

9 files changed

+106
-0
lines changed

packages/plugin-esm/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ npm i putout @putout/plugin-esm -D
1818

1919
## Rules
2020

21+
-[apply-export-from](#apply-export-from);
2122
-[declare-imports-first](#declare-imports-first);
2223
-[group-imports-by-source](#group-imports-by-source);
2324
-[merge-duplicate-imports](#merge-duplicate-imports);
@@ -31,6 +32,7 @@ npm i putout @putout/plugin-esm -D
3132
```json
3233
{
3334
"rules": {
35+
"esm/apply-export-from": "on",
3436
"esm/declare-imports-first": "on",
3537
"esm/group-imports-by-source": "on",
3638
"esm/merge-duplicate-imports": "on",
@@ -44,6 +46,30 @@ npm i putout @putout/plugin-esm -D
4446
}
4547
```
4648

49+
## apply-export-from
50+
51+
> The `export` declaration is used to export values from a JavaScript module.
52+
>
53+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)
54+
55+
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c9a3983d269745da89c1c7560f3b7fac/3ecb9aa6b910ce3816605bae11c8dd86bdc457e5).
56+
57+
## ❌ Example of incorrect code
58+
59+
```js
60+
import * as ns_1 from 'x';
61+
62+
export {
63+
ns_1 as ns,
64+
};
65+
```
66+
67+
## ✅ Example of correct code
68+
69+
```js
70+
export * as ns from 'x';
71+
```
72+
4773
## declare-imports-first
4874

4975
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).
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const {types, operator} = require('putout');
4+
5+
const {
6+
compare,
7+
getTemplateValues,
8+
remove,
9+
} = operator;
10+
11+
const {ExportNamespaceSpecifier} = types;
12+
13+
module.exports.report = () => `Use 'export *' instead of 'import/export'`;
14+
15+
const IMPORT = 'import * as __a from "__b"';
16+
17+
module.exports.fix = ({path, current}) => {
18+
const {exported} = current.node.specifiers[0];
19+
20+
current.node.source = path.node.source;
21+
22+
delete current.node.local;
23+
delete current.node.exported;
24+
25+
current.node.specifiers = [
26+
ExportNamespaceSpecifier(exported),
27+
];
28+
29+
remove(path);
30+
};
31+
32+
module.exports.traverse = ({push}) => ({
33+
[IMPORT]: (path) => {
34+
const {__a} = getTemplateValues(path, IMPORT);
35+
const {name} = __a;
36+
37+
for (const current of path.parentPath.get('body')) {
38+
if (compare(current, `export {${name} as __b}`))
39+
push({
40+
path,
41+
current,
42+
});
43+
}
44+
},
45+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['apply-export-from', plugin],
9+
],
10+
});
11+
12+
test('esm: apply-export-from: report', (t) => {
13+
t.report('apply-export-from', `Use 'export *' instead of 'import/export'`);
14+
t.end();
15+
});
16+
17+
test('esm: apply-export-from: transform', (t) => {
18+
t.transform('apply-export-from');
19+
t.end();
20+
});

packages/plugin-esm/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const removeEmptyImport = require('./remove-empty-import');
88
const removeEmptyExport = require('./remove-empty-export');
99
const mergeDuplicateImports = require('./merge-duplicate-imports');
1010
const convertAssertToWith = require('./convert-assert-to-with');
11+
const applyExportFrom = require('./apply-export-from');
1112

1213
module.exports.rules = {
1314
...mergeDuplicateImports.rules,
@@ -18,4 +19,5 @@ module.exports.rules = {
1819
'remove-empty-export': removeEmptyExport,
1920
'sort-imports-by-specifiers': sortImportsBySpecifiers,
2021
'convert-assert-to-with': convertAssertToWith,
22+
'apply-export-from': applyExportFrom,
2123
};

packages/plugin-esm/test/esm.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ test('putout: plugin: esm: merge-duplicate-imports: convert-assert-to-with: tran
4848
t.transform('convert-assert-to-with');
4949
t.end();
5050
});
51+
52+
test('plugin-esm: transform: apply-export-from', (t) => {
53+
t.transform('apply-export-from');
54+
t.end();
55+
});
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 };

0 commit comments

Comments
 (0)