@putout/plugin-esm 10.1.0 → 10.3.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 +20 -1
- package/lib/convert-const-to-import/index.js +68 -0
- package/lib/index.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,6 +22,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
22
22
|
- ✅ [apply-export-from](#apply-export-from);
|
|
23
23
|
- ✅ [apply-import-import](#apply-import-attributes);
|
|
24
24
|
- ✅ [convert-assert-to-with](#convert-assert-to-with);
|
|
25
|
+
- ✅ [convert-const-to-import](#convert-const-to-import);
|
|
25
26
|
- ✅ [declare-imports-first](#declare-imports-first);
|
|
26
27
|
- ✅ [group-imports-by-source](#group-imports-by-source);
|
|
27
28
|
- ✅ [merge-duplicate-imports](#merge-duplicate-imports);
|
|
@@ -51,6 +52,8 @@ npm i putout @putout/plugin-esm -D
|
|
|
51
52
|
"esm/apply-export-from": "on",
|
|
52
53
|
"esm/apply-import-attirbutes": "on",
|
|
53
54
|
"esm/declare-imports-first": "on",
|
|
55
|
+
"esm/convert-assert-to-with": "on",
|
|
56
|
+
"esm/convert-const-to-import": "on",
|
|
54
57
|
"esm/group-imports-by-source": "on",
|
|
55
58
|
"esm/merge-duplicate-imports": "on",
|
|
56
59
|
"esm/merge-declaration-with-export": "on",
|
|
@@ -61,9 +64,9 @@ npm i putout @putout/plugin-esm -D
|
|
|
61
64
|
"ignore": []
|
|
62
65
|
}],
|
|
63
66
|
"esm/sort-imports-by-specifiers": "on",
|
|
64
|
-
"esm/apply-js-imported-file": "off",
|
|
65
67
|
"esm/resolve-imported-file": "off",
|
|
66
68
|
"esm/shorten-imported-file": "off",
|
|
69
|
+
"esm/apply-js-imported-file": "off",
|
|
67
70
|
"esm/apply-name-to-imported-file": "off",
|
|
68
71
|
"esm/apply-namespace-to-imported-file": "off",
|
|
69
72
|
"esm/apply-privately-imported-file": "off",
|
|
@@ -433,6 +436,22 @@ import('foo.json', {
|
|
|
433
436
|
});
|
|
434
437
|
```
|
|
435
438
|
|
|
439
|
+
### convert-const-to-import
|
|
440
|
+
|
|
441
|
+
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/2682a3899fe6916e22073abf0b56b13c/f9d7ca3ddb65331bb836af78825d80fd258b3f94).
|
|
442
|
+
|
|
443
|
+
#### ❌ Example of incorrect code
|
|
444
|
+
|
|
445
|
+
```
|
|
446
|
+
const {Server} from 'socket.io';
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
#### ✅ Example of correct code
|
|
450
|
+
|
|
451
|
+
```js
|
|
452
|
+
import {Server} from 'socket.io';
|
|
453
|
+
```
|
|
454
|
+
|
|
436
455
|
## File Rules
|
|
437
456
|
|
|
438
457
|
### apply-namespace-to-imported-file
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
template,
|
|
3
|
+
types,
|
|
4
|
+
operator,
|
|
5
|
+
} from 'putout';
|
|
6
|
+
|
|
7
|
+
const {remove, replaceWith} = operator;
|
|
8
|
+
const {
|
|
9
|
+
importSpecifier,
|
|
10
|
+
isObjectPattern,
|
|
11
|
+
isIdentifier,
|
|
12
|
+
} = types;
|
|
13
|
+
|
|
14
|
+
export const report = () => `Use 'import' instead of 'const'`;
|
|
15
|
+
|
|
16
|
+
export const fix = ({path, second, third}) => {
|
|
17
|
+
const {value} = third.node.expression;
|
|
18
|
+
const {id} = path.node.declarations[0];
|
|
19
|
+
|
|
20
|
+
if (isIdentifier(id)) {
|
|
21
|
+
const nodeImport = template.ast(`import ${id.name} from '${value}'`);
|
|
22
|
+
replaceWith(path, nodeImport);
|
|
23
|
+
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (isObjectPattern(id)) {
|
|
28
|
+
const nodeImport = template.ast.fresh(`import {} from '${value}'`);
|
|
29
|
+
|
|
30
|
+
for (const {key, value} of id.properties) {
|
|
31
|
+
nodeImport.specifiers.push(importSpecifier(key, value));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
replaceWith(path, nodeImport);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
remove(second);
|
|
38
|
+
remove(third);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const traverse = ({push}) => ({
|
|
42
|
+
VariableDeclaration(path) {
|
|
43
|
+
if (path.node.declarations[0].init)
|
|
44
|
+
return;
|
|
45
|
+
|
|
46
|
+
const second = path.getNextSibling();
|
|
47
|
+
|
|
48
|
+
if (!second.isExpressionStatement())
|
|
49
|
+
return;
|
|
50
|
+
|
|
51
|
+
if (!second.get('expression').isIdentifier({name: 'from'}))
|
|
52
|
+
return;
|
|
53
|
+
|
|
54
|
+
const third = second.getNextSibling();
|
|
55
|
+
|
|
56
|
+
if (!third.isExpressionStatement())
|
|
57
|
+
return;
|
|
58
|
+
|
|
59
|
+
if (!third.get('expression').isStringLiteral())
|
|
60
|
+
return;
|
|
61
|
+
|
|
62
|
+
push({
|
|
63
|
+
path,
|
|
64
|
+
second,
|
|
65
|
+
third,
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as convertConstToImport from './convert-const-to-import/index.js';
|
|
1
2
|
import * as applyImportAttributes from './apply-import-attributes/index.js';
|
|
2
3
|
import * as applyDefaultImport from './apply-default-import/index.js';
|
|
3
4
|
import * as applyExportFrom from './apply-export-from/index.js';
|
|
@@ -41,4 +42,5 @@ export const rules = {
|
|
|
41
42
|
'apply-js-imported-file': ['off', applyJsImportedFile],
|
|
42
43
|
'apply-default-import': applyDefaultImport,
|
|
43
44
|
'apply-import-attributes': applyImportAttributes,
|
|
45
|
+
'convert-const-to-import': convertConstToImport,
|
|
44
46
|
};
|
package/package.json
CHANGED