@putout/plugin-esm 6.2.0 → 6.3.1
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
|
@@ -94,6 +94,7 @@ export const rules = {};
|
|
|
94
94
|
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)
|
|
95
95
|
|
|
96
96
|
Check out 🐊**Putout Editor**:
|
|
97
|
+
|
|
97
98
|
- [#1](https://putout.cloudcmd.io/#/gist/c9a3983d269745da89c1c7560f3b7fac/3ecb9aa6b910ce3816605bae11c8dd86bdc457e5);
|
|
98
99
|
- [#2](https://putout.cloudcmd.io/#/gist/9b2a0a51acf477291a6bbcbe7d846ddf/fa21768506f518ca0a1073beb82a9b8b8f5e7c19);
|
|
99
100
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {join, dirname} from 'node:path';
|
|
2
|
+
import tryCatch from 'try-catch';
|
|
2
3
|
import putout, {
|
|
3
4
|
parse,
|
|
4
5
|
print,
|
|
@@ -52,7 +53,13 @@ export const scan = (rootPath, {push, trackFile}) => {
|
|
|
52
53
|
|
|
53
54
|
for (const file of trackFile(rootPath, mask)) {
|
|
54
55
|
const content = readFileContent(file);
|
|
55
|
-
const ast = parse
|
|
56
|
+
const [error, ast] = tryCatch(parse, content);
|
|
57
|
+
|
|
58
|
+
if (error) {
|
|
59
|
+
error.message += `\n${content}\n`;
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
|
|
56
63
|
const importsTuples = getImports(file, content, ast);
|
|
57
64
|
|
|
58
65
|
for (const [name, source, importedFilename] of importsTuples) {
|
|
@@ -1,39 +1,58 @@
|
|
|
1
1
|
import {operator, types} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {isExportNamedDeclaration} = types;
|
|
4
3
|
const {remove} = operator;
|
|
4
|
+
const {isExportNamedDeclaration} = types;
|
|
5
5
|
|
|
6
6
|
export const report = () => `Merge export declarations`;
|
|
7
7
|
|
|
8
|
-
export const fix = ({path,
|
|
8
|
+
export const fix = ({path, exports}) => {
|
|
9
9
|
const all = [];
|
|
10
10
|
|
|
11
|
-
for (const
|
|
11
|
+
for (const path of exports) {
|
|
12
12
|
const {node} = path;
|
|
13
13
|
const {specifiers} = node;
|
|
14
14
|
|
|
15
15
|
all.push(...specifiers);
|
|
16
|
-
|
|
17
|
-
if (index < count - 1)
|
|
18
|
-
remove(path);
|
|
16
|
+
remove(path);
|
|
19
17
|
}
|
|
20
18
|
|
|
21
|
-
path.node.specifiers
|
|
19
|
+
path.node.specifiers.push(...all);
|
|
22
20
|
};
|
|
23
21
|
|
|
24
22
|
export const traverse = ({push}) => ({
|
|
25
23
|
Program(path) {
|
|
26
|
-
const
|
|
27
|
-
const count =
|
|
24
|
+
const filteredExports = path.get('body').filter(hasSpecifiers);
|
|
25
|
+
const count = filteredExports.length;
|
|
28
26
|
|
|
29
27
|
if (count < 2)
|
|
30
28
|
return;
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
const lists = [];
|
|
31
|
+
const marked = new Set();
|
|
32
|
+
|
|
33
|
+
for (const [i, a] of filteredExports.entries()) {
|
|
34
|
+
if (marked.has(a))
|
|
35
|
+
continue;
|
|
36
|
+
|
|
37
|
+
for (const b of filteredExports) {
|
|
38
|
+
if (a === b)
|
|
39
|
+
continue;
|
|
40
|
+
|
|
41
|
+
if (isSameSources(a, b)) {
|
|
42
|
+
marked.add(b);
|
|
43
|
+
|
|
44
|
+
lists[i] = lists[i] || [a];
|
|
45
|
+
lists[i].push(b);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (const [path, ...exports] of lists) {
|
|
51
|
+
push({
|
|
52
|
+
path,
|
|
53
|
+
exports,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
37
56
|
},
|
|
38
57
|
});
|
|
39
58
|
|
|
@@ -43,3 +62,16 @@ function hasSpecifiers(path) {
|
|
|
43
62
|
|
|
44
63
|
return path.node.specifiers.length;
|
|
45
64
|
}
|
|
65
|
+
|
|
66
|
+
function isSameSources(a, b) {
|
|
67
|
+
const sourceA = a.node.source;
|
|
68
|
+
const sourceB = b.node.source;
|
|
69
|
+
|
|
70
|
+
if (!sourceA && sourceA === sourceB)
|
|
71
|
+
return true;
|
|
72
|
+
|
|
73
|
+
if (!sourceA || !sourceB)
|
|
74
|
+
return false;
|
|
75
|
+
|
|
76
|
+
return sourceA.value === sourceB.value;
|
|
77
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-esm",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.3.1",
|
|
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",
|
|
@@ -28,7 +28,8 @@
|
|
|
28
28
|
"report": "madrun report"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"parse-import-specifiers": "^1.0.3"
|
|
31
|
+
"parse-import-specifiers": "^1.0.3",
|
|
32
|
+
"try-catch": "^3.0.1"
|
|
32
33
|
},
|
|
33
34
|
"keywords": [
|
|
34
35
|
"putout",
|