@putout/plugin-esm 6.1.1 β†’ 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -93,15 +93,19 @@ export const rules = {};
93
93
  >
94
94
  > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)
95
95
 
96
- Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/##/gist/c9a3983d269745da89c1c7560f3b7fac/3ecb9aa6b910ce3816605bae11c8dd86bdc457e5).
96
+ Check out 🐊**Putout Editor**:
97
+ - [#1](https://putout.cloudcmd.io/#/gist/c9a3983d269745da89c1c7560f3b7fac/3ecb9aa6b910ce3816605bae11c8dd86bdc457e5);
98
+ - [#2](https://putout.cloudcmd.io/#/gist/9b2a0a51acf477291a6bbcbe7d846ddf/fa21768506f518ca0a1073beb82a9b8b8f5e7c19);
97
99
 
98
100
  #### ❌ Example of incorrect code
99
101
 
100
102
  ```js
101
103
  import * as ns_1 from 'x';
104
+ import {createAsyncLoader} from './load/async-loader.js';
102
105
 
103
106
  export {
104
107
  ns_1 as ns,
108
+ createAsyncLoader,
105
109
  };
106
110
  ```
107
111
 
@@ -109,6 +113,7 @@ export {
109
113
 
110
114
  ```js
111
115
  export * as ns from 'x';
116
+ export {createAsyncLoader} from './load/async-loader.js';
112
117
  ```
113
118
 
114
119
  ### merge-declaration-with-export
@@ -1,43 +1,78 @@
1
1
  import {types, operator} from 'putout';
2
2
 
3
3
  const {
4
- compare,
5
- getTemplateValues,
6
- remove,
7
- } = operator;
4
+ isExportSpecifier,
5
+ isImportSpecifier,
6
+ exportNamedDeclaration,
7
+ exportNamespaceSpecifier,
8
+ isImportNamespaceSpecifier,
9
+ } = types;
8
10
 
9
- const {exportNamespaceSpecifier} = types;
11
+ const {values} = Object;
12
+ const {remove, insertAfter} = operator;
10
13
 
11
- export const report = () => `Use 'export *' instead of 'import/export'`;
14
+ export const report = () => `Use 'export from' instead of 'import' + 'export'`;
12
15
 
13
- const IMPORT = 'import * as __a from "__b"';
14
-
15
- export const fix = ({path, current}) => {
16
- const {exported} = current.node.specifiers[0];
17
-
18
- current.node.source = path.node.source;
19
-
20
- delete current.node.local;
21
- delete current.node.exported;
22
-
23
- current.node.specifiers = [
24
- exportNamespaceSpecifier(exported),
25
- ];
16
+ export const fix = ({path, reference}) => {
17
+ const {parentPath} = path;
18
+ const parentReference = reference.parentPath;
19
+ const exportNode = createExport(path, parentReference);
26
20
 
27
- remove(path);
21
+ insertAfter(parentPath, exportNode);
22
+ removeSpecifier(path);
23
+ removeSpecifier(parentReference);
28
24
  };
29
25
 
30
26
  export const traverse = ({push}) => ({
31
- [IMPORT]: (path) => {
32
- const {__a} = getTemplateValues(path, IMPORT);
33
- const {name} = __a;
27
+ Program(path) {
28
+ const {bindings} = path.scope;
29
+ const imports = values(bindings).filter(isExported);
34
30
 
35
- for (const current of path.parentPath.get('body')) {
36
- if (compare(current, `export {${name} as __b}`))
37
- push({
38
- path,
39
- current,
40
- });
31
+ for (const {path, referencePaths} of imports) {
32
+ const [reference] = referencePaths;
33
+
34
+ push({
35
+ path,
36
+ reference,
37
+ });
41
38
  }
42
39
  },
43
40
  });
41
+
42
+ function isExported(binding) {
43
+ const {
44
+ path,
45
+ references,
46
+ referencePaths,
47
+ } = binding;
48
+
49
+ if (references !== 1)
50
+ return false;
51
+
52
+ if (!isImportSpecifier(path) && !isImportNamespaceSpecifier(path))
53
+ return false;
54
+
55
+ const [refPath] = referencePaths;
56
+
57
+ return isExportSpecifier(refPath.parentPath);
58
+ }
59
+
60
+ function removeSpecifier(path) {
61
+ if (path.parentPath.node.specifiers.length === 1)
62
+ remove(path.parentPath);
63
+ else
64
+ remove(path);
65
+ }
66
+
67
+ function createExport(path, parentReference) {
68
+ const {parentPath} = path;
69
+ const {source} = parentPath.node;
70
+
71
+ if (isImportSpecifier(path))
72
+ return exportNamedDeclaration(null, [parentReference.node], source);
73
+
74
+ const {exported} = parentReference.node;
75
+ const specifier = exportNamespaceSpecifier(exported);
76
+
77
+ return exportNamedDeclaration(null, [specifier], source);
78
+ }
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as applyExportFrom from './apply-export-from/index.js';
1
2
  import * as mergeExportDeclarations from './merge-export-declarations/index.js';
2
3
  import * as removeUselessExportSpecifiers from './remove-useless-export-specifiers/index.js';
3
4
  import * as mergeDeclarationWithExport from './merge-declaration-with-export/index.js';
@@ -12,7 +13,6 @@ import * as removeEmptyImport from './remove-empty-import/index.js';
12
13
  import * as removeEmptyExport from './remove-empty-export/index.js';
13
14
  import * as mergeDuplicateImports from './merge-duplicate-imports/index.js';
14
15
  import * as convertAssertToWith from './convert-assert-to-with/index.js';
15
- import * as applyExportFrom from './apply-export-from/index.js';
16
16
 
17
17
  export const rules = {
18
18
  'add-index-to-import': ['off', addIndexToImport],
@@ -25,6 +25,9 @@ export const filter = (path) => {
25
25
  const {scope} = path;
26
26
  const specifiers = path.get('specifiers');
27
27
 
28
+ if (!specifiers.length)
29
+ return false;
30
+
28
31
  for (const spec of specifiers) {
29
32
  const {local, exported} = spec.node;
30
33
  const {name} = local;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "6.1.1",
3
+ "version": "6.2.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin improves ability to transform ESM code",