@putout/plugin-esm 4.3.0 → 4.5.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
@@ -27,7 +27,9 @@ npm i putout @putout/plugin-esm -D
27
27
  - ✅ [remove-quotes-from-import-assertions](#remove-quotes-from-import-assertions);
28
28
  - ✅ [remove-empty-import](#remove-empty-import);
29
29
  - ✅ [remove-empty-export](#remove-empty-export);
30
+ - ✅ [remove-useless-export-specifiers](#remove-useless-export-specifiers);
30
31
  - ✅ [sort-imports-by-specifiers](#sort-imports-by-specifiers);
32
+ - ✅ [inline-export](#inline-export);
31
33
 
32
34
  ## File rules
33
35
 
@@ -52,7 +54,9 @@ npm i putout @putout/plugin-esm -D
52
54
  }],
53
55
  "esm/sort-imports-by-specifiers": "on",
54
56
  "esm/resolve-imported-file": "off",
55
- "esm/apply-namespace-of-file": "off"
57
+ "esm/apply-namespace-of-file": "off",
58
+ "esm/inline-export": "off",
59
+ "esm/remove-useless-export-specifiers": "off"
56
60
  }
57
61
  }
58
62
  ```
@@ -106,6 +110,49 @@ export {
106
110
  export * as ns from 'x';
107
111
  ```
108
112
 
113
+ ### inline-export
114
+
115
+ Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/##/gist/c9a3983d269745da89c1c7560f3b7fac/3ecb9aa6b910ce3816605bae11c8dd86bdc457e5).
116
+
117
+ #### ❌ Example of incorrect code
118
+
119
+ ```js
120
+ const stack = [];
121
+
122
+ function sum(a, b) {
123
+ i32.add(local.get(), local.get());
124
+ }
125
+
126
+ export {
127
+ sum,
128
+ stack,
129
+ };
130
+ ```
131
+
132
+ #### ✅ Example of correct code
133
+
134
+ ```js
135
+ export const stack = [];
136
+
137
+ export function sum(a, b) {
138
+ i32.add(local.get(), local.get());
139
+ }
140
+ ```
141
+
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
+
109
156
  ### declare-imports-first
110
157
 
111
158
  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).
package/lib/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import * as removeUselessExportSpecifiers from './remove-useless-export-specifiers/index.js';
2
+ import * as inlineExport from './inline-export/index.js';
1
3
  import * as applyNamespaceImportToFile from './apply-namespace-import-to-file/index.js';
2
4
  import * as resolveImportedFile from './resolve-imported-file/index.js';
3
5
  import * as addIndexToImport from './add-index-to-import/index.js';
@@ -26,4 +28,6 @@ export const rules = {
26
28
  'resolve-imported-file': ['off', resolveImportedFile],
27
29
  'apply-namespace-import-to-file': ['off', applyNamespaceImportToFile],
28
30
  'merge-declaration-with-export': mergeDeclarationWithExport,
31
+ 'inline-export': inlineExport,
32
+ 'remove-useless-export-specifiers': removeUselessExportSpecifiers,
29
33
  };
@@ -0,0 +1,67 @@
1
+ import {types, operator} from 'putout';
2
+
3
+ const {remove, replaceWith} = operator;
4
+ const {
5
+ variableDeclaration,
6
+ isVariableDeclarator,
7
+ exportNamedDeclaration,
8
+ isFunctionDeclaration,
9
+ isImportSpecifier,
10
+ isImportDefaultSpecifier,
11
+ } = types;
12
+
13
+ export const report = () => `Inline export`;
14
+ export const include = () => [
15
+ 'export {__exports}',
16
+ ];
17
+ export const exclude = () => [`export * as __a from '__b'`];
18
+
19
+ export const filter = (path) => {
20
+ const {scope} = path;
21
+ const specifiers = path.get('specifiers');
22
+
23
+ for (const spec of specifiers) {
24
+ const {local} = spec.node;
25
+ const {name} = local;
26
+ const binding = scope.bindings[name];
27
+
28
+ if (!binding)
29
+ return false;
30
+
31
+ const bindingPath = binding.path;
32
+
33
+ if (isImportSpecifier(bindingPath))
34
+ return false;
35
+
36
+ if (isImportDefaultSpecifier(bindingPath))
37
+ return false;
38
+ }
39
+
40
+ return true;
41
+ };
42
+
43
+ export const fix = (path) => {
44
+ const {scope} = path;
45
+ const specifiers = path.get('specifiers');
46
+
47
+ for (const spec of specifiers) {
48
+ const {local} = spec.node;
49
+ const {name} = local;
50
+
51
+ const binding = scope.bindings[name];
52
+ const bindingPath = binding.path;
53
+
54
+ if (isFunctionDeclaration(bindingPath)) {
55
+ replaceWith(bindingPath, exportNamedDeclaration(bindingPath.node));
56
+ continue;
57
+ }
58
+
59
+ if (isVariableDeclarator(bindingPath)) {
60
+ const declaration = variableDeclaration('const', [bindingPath.node]);
61
+ replaceWith(bindingPath.parentPath, exportNamedDeclaration(declaration));
62
+ continue;
63
+ }
64
+ }
65
+
66
+ remove(path);
67
+ };
@@ -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
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "4.3.0",
3
+ "version": "4.5.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",