@putout/plugin-esm 4.2.2 → 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
CHANGED
|
@@ -23,10 +23,12 @@ npm i putout @putout/plugin-esm -D
|
|
|
23
23
|
- ✅ [declare-imports-first](#declare-imports-first);
|
|
24
24
|
- ✅ [group-imports-by-source](#group-imports-by-source);
|
|
25
25
|
- ✅ [merge-duplicate-imports](#merge-duplicate-imports);
|
|
26
|
+
- ✅ [merge-declaration-with-export](#merge-declaration-with-export);
|
|
26
27
|
- ✅ [remove-quotes-from-import-assertions](#remove-quotes-from-import-assertions);
|
|
27
28
|
- ✅ [remove-empty-import](#remove-empty-import);
|
|
28
29
|
- ✅ [remove-empty-export](#remove-empty-export);
|
|
29
30
|
- ✅ [sort-imports-by-specifiers](#sort-imports-by-specifiers);
|
|
31
|
+
- ✅ [inline-export](#inline-export);
|
|
30
32
|
|
|
31
33
|
## File rules
|
|
32
34
|
|
|
@@ -43,6 +45,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
43
45
|
"esm/declare-imports-first": "on",
|
|
44
46
|
"esm/group-imports-by-source": "on",
|
|
45
47
|
"esm/merge-duplicate-imports": "on",
|
|
48
|
+
"esm/merge-declaration-with-export": "on",
|
|
46
49
|
"esm/remove-quotes-from-import-assertions": "on",
|
|
47
50
|
"esm/remove-empty-export": "on",
|
|
48
51
|
"esm/remove-empty-import": ["on", {
|
|
@@ -50,7 +53,8 @@ npm i putout @putout/plugin-esm -D
|
|
|
50
53
|
}],
|
|
51
54
|
"esm/sort-imports-by-specifiers": "on",
|
|
52
55
|
"esm/resolve-imported-file": "off",
|
|
53
|
-
"esm/apply-namespace-of-file": "off"
|
|
56
|
+
"esm/apply-namespace-of-file": "off",
|
|
57
|
+
"esm/inline-export": "off"
|
|
54
58
|
}
|
|
55
59
|
}
|
|
56
60
|
```
|
|
@@ -104,6 +108,35 @@ export {
|
|
|
104
108
|
export * as ns from 'x';
|
|
105
109
|
```
|
|
106
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
|
+
|
|
107
140
|
### declare-imports-first
|
|
108
141
|
|
|
109
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).
|
|
@@ -162,6 +195,36 @@ import ss from '../../bb/ss.js';
|
|
|
162
195
|
const c = 5;
|
|
163
196
|
```
|
|
164
197
|
|
|
198
|
+
### merge-declaration-with-export
|
|
199
|
+
|
|
200
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/3fad517d76942d0a1f51d7f58a2799af/7a052ab18a31fa382228d6513c412be37091cfb8).
|
|
201
|
+
|
|
202
|
+
#### ❌ Example of incorrect code
|
|
203
|
+
|
|
204
|
+
```js
|
|
205
|
+
const {
|
|
206
|
+
report,
|
|
207
|
+
fix,
|
|
208
|
+
scan,
|
|
209
|
+
} = createRemoveFiles(['*.swp', '*.swo']);
|
|
210
|
+
|
|
211
|
+
export {
|
|
212
|
+
report,
|
|
213
|
+
fix,
|
|
214
|
+
scan,
|
|
215
|
+
};
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
##### ✅ Example of correct code
|
|
219
|
+
|
|
220
|
+
```js
|
|
221
|
+
export const {
|
|
222
|
+
report,
|
|
223
|
+
fix,
|
|
224
|
+
scan,
|
|
225
|
+
} = createRemoveFiles(['*.swp', '*.swo']);
|
|
226
|
+
```
|
|
227
|
+
|
|
165
228
|
### merge-duplicate-imports
|
|
166
229
|
|
|
167
230
|
#### join
|
|
@@ -315,6 +378,10 @@ import('foo.json', {
|
|
|
315
378
|
|
|
316
379
|
### apply-namespace-import-to-file
|
|
317
380
|
|
|
381
|
+
The rule fixes:
|
|
382
|
+
|
|
383
|
+
> `SyntaxError: The requested module './a.js' does not provide an export named 'default'`
|
|
384
|
+
|
|
318
385
|
Check out in 🐊**Putout Editor**:
|
|
319
386
|
|
|
320
387
|
- ✅ [`apply-namespace-import-to-file`](https://putout.cloudcmd.io/#/gist/1492d584559e5798325047de679222a0/c6a37a803b80823de1b64ab944f2427aecefb51b);
|
|
@@ -345,10 +412,6 @@ import a from './a.js';
|
|
|
345
412
|
import * as a from './a.js';
|
|
346
413
|
```
|
|
347
414
|
|
|
348
|
-
## License
|
|
349
|
-
|
|
350
|
-
MIT
|
|
351
|
-
|
|
352
415
|
### resolve-imported-file
|
|
353
416
|
|
|
354
417
|
Check out in 🐊**Putout Editor**:
|
|
@@ -5,7 +5,6 @@ import putout, {
|
|
|
5
5
|
transform,
|
|
6
6
|
operator,
|
|
7
7
|
} from 'putout';
|
|
8
|
-
|
|
9
8
|
import * as isESMPlugin from './is-esm/index.js';
|
|
10
9
|
import * as hasExportDefaultPlugin from './has-export-default/index.js';
|
|
11
10
|
import * as applyNamespaceImportPlugin from './apply-namespace-import/index.js';
|
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';
|
|
@@ -8,6 +9,7 @@ import * as sortImportsBySpecifiers from './sort-imports-by-specifiers/index.js'
|
|
|
8
9
|
import * as removeEmptyImport from './remove-empty-import/index.js';
|
|
9
10
|
import * as removeEmptyExport from './remove-empty-export/index.js';
|
|
10
11
|
import * as mergeDuplicateImports from './merge-duplicate-imports/index.js';
|
|
12
|
+
import * as mergeDeclarationWithExport from './merge-declaration-with-export/index.js';
|
|
11
13
|
import * as convertAssertToWith from './convert-assert-to-with/index.js';
|
|
12
14
|
import * as applyExportFrom from './apply-export-from/index.js';
|
|
13
15
|
|
|
@@ -24,4 +26,6 @@ export const rules = {
|
|
|
24
26
|
'sort-imports-by-specifiers': sortImportsBySpecifiers,
|
|
25
27
|
'resolve-imported-file': ['off', resolveImportedFile],
|
|
26
28
|
'apply-namespace-import-to-file': ['off', applyNamespaceImportToFile],
|
|
29
|
+
'merge-declaration-with-export': mergeDeclarationWithExport,
|
|
30
|
+
'inline-export': inlineExport,
|
|
27
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
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {operator, types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {remove, replaceWith} = operator;
|
|
4
|
+
const {
|
|
5
|
+
exportNamedDeclaration,
|
|
6
|
+
isExportSpecifier,
|
|
7
|
+
isVariableDeclarator,
|
|
8
|
+
} = types;
|
|
9
|
+
|
|
10
|
+
export const report = () => `Use 'if condition' instead of 'ternary expression'`;
|
|
11
|
+
|
|
12
|
+
export const fix = ({path, bindingPath}) => {
|
|
13
|
+
const {parentPath} = bindingPath;
|
|
14
|
+
const {node} = parentPath;
|
|
15
|
+
|
|
16
|
+
replaceWith(parentPath, exportNamedDeclaration(node));
|
|
17
|
+
remove(path);
|
|
18
|
+
};
|
|
19
|
+
export const traverse = ({push}) => ({
|
|
20
|
+
ExportNamedDeclaration(path) {
|
|
21
|
+
const {specifiers} = path.node;
|
|
22
|
+
|
|
23
|
+
if (!specifiers.length)
|
|
24
|
+
return;
|
|
25
|
+
|
|
26
|
+
const bindingPaths = [];
|
|
27
|
+
|
|
28
|
+
for (const spec of specifiers) {
|
|
29
|
+
if (!isExportSpecifier(spec))
|
|
30
|
+
return;
|
|
31
|
+
|
|
32
|
+
const {local} = spec;
|
|
33
|
+
|
|
34
|
+
const {name} = local;
|
|
35
|
+
const binding = path.scope.bindings[name];
|
|
36
|
+
|
|
37
|
+
if (!binding)
|
|
38
|
+
return;
|
|
39
|
+
|
|
40
|
+
const {path: bindingPath} = binding;
|
|
41
|
+
|
|
42
|
+
if (!isVariableDeclarator(bindingPath))
|
|
43
|
+
return;
|
|
44
|
+
|
|
45
|
+
if (!bindingPaths.length) {
|
|
46
|
+
bindingPaths.push(bindingPath);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const [bindingPath] = bindingPaths;
|
|
52
|
+
|
|
53
|
+
push({
|
|
54
|
+
path,
|
|
55
|
+
bindingPath,
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {remove
|
|
3
|
+
const {remove} = operator;
|
|
4
4
|
const {values} = Object;
|
|
5
5
|
|
|
6
6
|
const {
|
|
@@ -23,9 +23,6 @@ export const fix = ({path, imports}) => {
|
|
|
23
23
|
continue;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
if (compareAny(spec, path.node.specifiers))
|
|
27
|
-
continue;
|
|
28
|
-
|
|
29
26
|
all.push(spec);
|
|
30
27
|
}
|
|
31
28
|
|
package/package.json
CHANGED