@putout/plugin-esm 10.0.2 → 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/apply-privately-imported-file/index.js +3 -5
- package/lib/convert-const-to-import/index.js +68 -0
- package/lib/index.js +2 -0
- package/lib/resolve-imported-file/change-imports/index.js +2 -0
- package/lib/resolve-imported-file/get-imports/index.js +2 -0
- package/lib/resolve-imported-file/index.js +2 -5
- package/lib/shorten-imported-file/get-imports/index.js +2 -0
- package/lib/shorten-imported-file/index.js +0 -4
- 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
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
parse,
|
|
4
4
|
print,
|
|
5
5
|
transform,
|
|
6
|
+
findPlaces,
|
|
6
7
|
operator,
|
|
7
8
|
} from 'putout';
|
|
8
9
|
import * as getImports from '#get-imports';
|
|
@@ -20,6 +21,7 @@ const getMessage = (a) => a.message;
|
|
|
20
21
|
export const report = (file, {from, to, filename}) => {
|
|
21
22
|
return `Apply private import: '${from}' -> '${to}' in '${filename}'`;
|
|
22
23
|
};
|
|
24
|
+
|
|
23
25
|
export const fix = (file, {ast, from, to}) => {
|
|
24
26
|
transform(ast, {
|
|
25
27
|
rules: {
|
|
@@ -48,10 +50,6 @@ export const scan = (rootPath, {push, trackFile}) => {
|
|
|
48
50
|
|
|
49
51
|
for (const file of trackFile(rootPath, mask)) {
|
|
50
52
|
const content = readFileContent(file);
|
|
51
|
-
|
|
52
|
-
if (!content.includes('import'))
|
|
53
|
-
continue;
|
|
54
|
-
|
|
55
53
|
const privateImports = getPrivateImports(file);
|
|
56
54
|
|
|
57
55
|
if (!privateImports.size)
|
|
@@ -59,7 +57,7 @@ export const scan = (rootPath, {push, trackFile}) => {
|
|
|
59
57
|
|
|
60
58
|
const ast = parse(content);
|
|
61
59
|
|
|
62
|
-
const places =
|
|
60
|
+
const places = findPlaces(ast, {
|
|
63
61
|
plugins: [
|
|
64
62
|
['get-imports', getImports],
|
|
65
63
|
],
|
|
@@ -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
|
};
|
|
@@ -11,6 +11,8 @@ export const replace = ({options}) => {
|
|
|
11
11
|
if (!to.endsWith('json'))
|
|
12
12
|
return {
|
|
13
13
|
[`import __imports from '${from}'`]: `import __imports from '${to}'`,
|
|
14
|
+
[`export __exports from '${from}'`]: `export __exports from '${to}'`,
|
|
15
|
+
[`export * from '${from}'`]: `export * from '${to}'`,
|
|
14
16
|
[`import('${from}')`]: `import('${to}')`,
|
|
15
17
|
};
|
|
16
18
|
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
print,
|
|
5
5
|
transform,
|
|
6
6
|
operator,
|
|
7
|
+
findPlaces,
|
|
7
8
|
} from 'putout';
|
|
8
9
|
import * as changeImports from '#change-imports';
|
|
9
10
|
import * as getImports from './get-imports/index.js';
|
|
@@ -47,13 +48,9 @@ export const scan = (rootPath, {push, trackFile, crawlFile}) => {
|
|
|
47
48
|
|
|
48
49
|
for (const file of trackFile(rootPath, mask)) {
|
|
49
50
|
const content = readFileContent(file);
|
|
50
|
-
|
|
51
|
-
if (!content.includes('import'))
|
|
52
|
-
continue;
|
|
53
|
-
|
|
54
51
|
const ast = parse(content);
|
|
55
52
|
|
|
56
|
-
const places =
|
|
53
|
+
const places = findPlaces(ast, {
|
|
57
54
|
plugins: [
|
|
58
55
|
['get-imports-source', getImports],
|
|
59
56
|
],
|
|
@@ -51,10 +51,6 @@ export const scan = (rootPath, {push, trackFile}) => {
|
|
|
51
51
|
|
|
52
52
|
for (const file of trackFile(rootPath, mask)) {
|
|
53
53
|
const content = readFileContent(file);
|
|
54
|
-
|
|
55
|
-
if (!content.includes('import'))
|
|
56
|
-
continue;
|
|
57
|
-
|
|
58
54
|
const [error, ast] = tryCatch(parse, content);
|
|
59
55
|
|
|
60
56
|
if (error)
|
package/package.json
CHANGED