@putout/plugin-esm 4.3.0 → 4.4.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 +32 -1
- package/lib/index.js +2 -0
- package/lib/inline-export/index.js +67 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,6 +28,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
28
28
|
- ✅ [remove-empty-import](#remove-empty-import);
|
|
29
29
|
- ✅ [remove-empty-export](#remove-empty-export);
|
|
30
30
|
- ✅ [sort-imports-by-specifiers](#sort-imports-by-specifiers);
|
|
31
|
+
- ✅ [inline-export](#inline-export);
|
|
31
32
|
|
|
32
33
|
## File rules
|
|
33
34
|
|
|
@@ -52,7 +53,8 @@ npm i putout @putout/plugin-esm -D
|
|
|
52
53
|
}],
|
|
53
54
|
"esm/sort-imports-by-specifiers": "on",
|
|
54
55
|
"esm/resolve-imported-file": "off",
|
|
55
|
-
"esm/apply-namespace-of-file": "off"
|
|
56
|
+
"esm/apply-namespace-of-file": "off",
|
|
57
|
+
"esm/inline-export": "off"
|
|
56
58
|
}
|
|
57
59
|
}
|
|
58
60
|
```
|
|
@@ -106,6 +108,35 @@ export {
|
|
|
106
108
|
export * as ns from 'x';
|
|
107
109
|
```
|
|
108
110
|
|
|
111
|
+
### inline-export
|
|
112
|
+
|
|
113
|
+
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/##/gist/c9a3983d269745da89c1c7560f3b7fac/3ecb9aa6b910ce3816605bae11c8dd86bdc457e5).
|
|
114
|
+
|
|
115
|
+
#### ❌ Example of incorrect code
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
const stack = [];
|
|
119
|
+
|
|
120
|
+
function sum(a, b) {
|
|
121
|
+
i32.add(local.get(), local.get());
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export {
|
|
125
|
+
sum,
|
|
126
|
+
stack,
|
|
127
|
+
};
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### ✅ Example of correct code
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
export const stack = [];
|
|
134
|
+
|
|
135
|
+
export function sum(a, b) {
|
|
136
|
+
i32.add(local.get(), local.get());
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
109
140
|
### declare-imports-first
|
|
110
141
|
|
|
111
142
|
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,4 @@
|
|
|
1
|
+
import * as inlineExport from './inline-export/index.js';
|
|
1
2
|
import * as applyNamespaceImportToFile from './apply-namespace-import-to-file/index.js';
|
|
2
3
|
import * as resolveImportedFile from './resolve-imported-file/index.js';
|
|
3
4
|
import * as addIndexToImport from './add-index-to-import/index.js';
|
|
@@ -26,4 +27,5 @@ export const rules = {
|
|
|
26
27
|
'resolve-imported-file': ['off', resolveImportedFile],
|
|
27
28
|
'apply-namespace-import-to-file': ['off', applyNamespaceImportToFile],
|
|
28
29
|
'merge-declaration-with-export': mergeDeclarationWithExport,
|
|
30
|
+
'inline-export': inlineExport,
|
|
29
31
|
};
|
|
@@ -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
|
+
};
|
package/package.json
CHANGED