@putout/plugin-esm 2.2.0 → 3.0.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 +26 -0
- package/lib/apply-export-from/index.js +43 -0
- package/lib/convert-assert-to-with/index.js +4 -6
- package/lib/declare-imports-first/index.js +4 -5
- package/lib/group-imports-by-source/index.js +5 -6
- package/lib/index.js +11 -11
- package/lib/merge-duplicate-imports/index.js +3 -5
- package/lib/merge-duplicate-imports/join/index.js +4 -6
- package/lib/merge-duplicate-imports/rename/index.js +4 -5
- package/lib/remove-empty-export/index.js +5 -6
- package/lib/remove-empty-import/index.js +5 -6
- package/lib/remove-quotes-from-import-assertions/index.js +2 -4
- package/lib/sort-imports-by-specifiers/index.js +5 -6
- package/package.json +9 -10
package/README.md
CHANGED
|
@@ -18,6 +18,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
18
18
|
|
|
19
19
|
## Rules
|
|
20
20
|
|
|
21
|
+
- ✅ [apply-export-from](#apply-export-from);
|
|
21
22
|
- ✅ [declare-imports-first](#declare-imports-first);
|
|
22
23
|
- ✅ [group-imports-by-source](#group-imports-by-source);
|
|
23
24
|
- ✅ [merge-duplicate-imports](#merge-duplicate-imports);
|
|
@@ -31,6 +32,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
31
32
|
```json
|
|
32
33
|
{
|
|
33
34
|
"rules": {
|
|
35
|
+
"esm/apply-export-from": "on",
|
|
34
36
|
"esm/declare-imports-first": "on",
|
|
35
37
|
"esm/group-imports-by-source": "on",
|
|
36
38
|
"esm/merge-duplicate-imports": "on",
|
|
@@ -44,6 +46,30 @@ npm i putout @putout/plugin-esm -D
|
|
|
44
46
|
}
|
|
45
47
|
```
|
|
46
48
|
|
|
49
|
+
## apply-export-from
|
|
50
|
+
|
|
51
|
+
> The `export` declaration is used to export values from a JavaScript module.
|
|
52
|
+
>
|
|
53
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)
|
|
54
|
+
|
|
55
|
+
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c9a3983d269745da89c1c7560f3b7fac/3ecb9aa6b910ce3816605bae11c8dd86bdc457e5).
|
|
56
|
+
|
|
57
|
+
## ❌ Example of incorrect code
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
import * as ns_1 from 'x';
|
|
61
|
+
|
|
62
|
+
export {
|
|
63
|
+
ns_1 as ns,
|
|
64
|
+
};
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## ✅ Example of correct code
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
export * as ns from 'x';
|
|
71
|
+
```
|
|
72
|
+
|
|
47
73
|
## declare-imports-first
|
|
48
74
|
|
|
49
75
|
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).
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
compare,
|
|
5
|
+
getTemplateValues,
|
|
6
|
+
remove,
|
|
7
|
+
} = operator;
|
|
8
|
+
|
|
9
|
+
const {exportNamespaceSpecifier} = types;
|
|
10
|
+
|
|
11
|
+
export const report = () => `Use 'export *' instead of 'import/export'`;
|
|
12
|
+
|
|
13
|
+
const IMPORT = 'import * as __a from "__b"';
|
|
14
|
+
|
|
15
|
+
export const fix = ({path, current}) => {
|
|
16
|
+
const {exported} = current.node.specifiers[0];
|
|
17
|
+
|
|
18
|
+
current.node.source = path.node.source;
|
|
19
|
+
|
|
20
|
+
delete current.node.local;
|
|
21
|
+
delete current.node.exported;
|
|
22
|
+
|
|
23
|
+
current.node.specifiers = [
|
|
24
|
+
exportNamespaceSpecifier(exported),
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
remove(path);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const traverse = ({push}) => ({
|
|
31
|
+
[IMPORT]: (path) => {
|
|
32
|
+
const {__a} = getTemplateValues(path, IMPORT);
|
|
33
|
+
const {name} = __a;
|
|
34
|
+
|
|
35
|
+
for (const current of path.parentPath.get('body')) {
|
|
36
|
+
if (compare(current, `export {${name} as __b}`))
|
|
37
|
+
push({
|
|
38
|
+
path,
|
|
39
|
+
current,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
});
|
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const setImportType = (path, type) => {
|
|
4
2
|
path.node.options.properties[0].key.name = type;
|
|
5
3
|
};
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
export const report = () => `Use 'with' instead of 'assert'`;
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
export const include = () => [
|
|
10
8
|
'ImportDeclaration',
|
|
11
9
|
'import(__a, {assert: {type: "json"}})',
|
|
12
10
|
];
|
|
13
11
|
|
|
14
|
-
|
|
12
|
+
export const fix = (path) => {
|
|
15
13
|
if (path.isImportDeclaration()) {
|
|
16
14
|
delete path.node.extra.deprecatedAssertSyntax;
|
|
17
15
|
return;
|
|
@@ -20,7 +18,7 @@ module.exports.fix = (path) => {
|
|
|
20
18
|
setImportType(path, 'with');
|
|
21
19
|
};
|
|
22
20
|
|
|
23
|
-
|
|
21
|
+
export const filter = (path) => {
|
|
24
22
|
const {extra} = path.node;
|
|
25
23
|
|
|
26
24
|
if (path.isImportDeclaration())
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {types, operator} = require('putout');
|
|
4
3
|
const {
|
|
5
4
|
isImportDeclaration,
|
|
6
5
|
isExportDeclaration,
|
|
@@ -9,9 +8,9 @@ const {
|
|
|
9
8
|
const {replaceWith} = operator;
|
|
10
9
|
const isESMNode = (a) => isImportDeclaration(a) || isExportDeclaration(a);
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
export const report = () => `Declare imports first`;
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
export const fix = ({path, importPath}) => {
|
|
15
14
|
let prev = path;
|
|
16
15
|
let preventInfiniteLoop = 500;
|
|
17
16
|
|
|
@@ -29,7 +28,7 @@ module.exports.fix = ({path, importPath}) => {
|
|
|
29
28
|
}
|
|
30
29
|
};
|
|
31
30
|
|
|
32
|
-
|
|
31
|
+
export const traverse = ({push, pathStore}) => ({
|
|
33
32
|
ImportDeclaration: (path) => {
|
|
34
33
|
pathStore(path);
|
|
35
34
|
},
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import {isDeepStrictEqual} from 'node:util';
|
|
2
|
+
import {types, operator} from 'putout';
|
|
2
3
|
|
|
3
|
-
const {isDeepStrictEqual} = require('node:util');
|
|
4
|
-
const {types, operator} = require('putout');
|
|
5
4
|
const {isImportDeclaration} = types;
|
|
6
5
|
|
|
7
6
|
const {
|
|
@@ -9,9 +8,9 @@ const {
|
|
|
9
8
|
remove,
|
|
10
9
|
} = operator;
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
export const report = () => `Group imports by source: 'builtins', 'external', 'hashed', 'internal'`;
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
export const fix = ({grouped}) => {
|
|
15
14
|
const [first, ...others] = grouped;
|
|
16
15
|
const nodes = [first.node];
|
|
17
16
|
|
|
@@ -24,7 +23,7 @@ module.exports.fix = ({grouped}) => {
|
|
|
24
23
|
replaceWithMultiple(first, nodes);
|
|
25
24
|
};
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
export const traverse = ({pathStore, push}) => ({
|
|
28
27
|
ImportDeclaration: pathStore,
|
|
29
28
|
Program: {
|
|
30
29
|
exit(path) {
|
package/lib/index.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
import * as declareImportsFirst from './declare-imports-first/index.js';
|
|
2
|
+
import * as groupImportsBySource from './group-imports-by-source/index.js';
|
|
3
|
+
import * as removeQuotesFromImportAssertions from './remove-quotes-from-import-assertions/index.js';
|
|
4
|
+
import * as sortImportsBySpecifiers from './sort-imports-by-specifiers/index.js';
|
|
5
|
+
import * as removeEmptyImport from './remove-empty-import/index.js';
|
|
6
|
+
import * as removeEmptyExport from './remove-empty-export/index.js';
|
|
7
|
+
import * as mergeDuplicateImports from './merge-duplicate-imports/index.js';
|
|
8
|
+
import * as convertAssertToWith from './convert-assert-to-with/index.js';
|
|
9
|
+
import * as applyExportFrom from './apply-export-from/index.js';
|
|
2
10
|
|
|
3
|
-
const
|
|
4
|
-
const groupImportsBySource = require('./group-imports-by-source');
|
|
5
|
-
const removeQuotesFromImportAssertions = require('./remove-quotes-from-import-assertions');
|
|
6
|
-
const sortImportsBySpecifiers = require('./sort-imports-by-specifiers');
|
|
7
|
-
const removeEmptyImport = require('./remove-empty-import');
|
|
8
|
-
const removeEmptyExport = require('./remove-empty-export');
|
|
9
|
-
const mergeDuplicateImports = require('./merge-duplicate-imports');
|
|
10
|
-
const convertAssertToWith = require('./convert-assert-to-with');
|
|
11
|
-
|
|
12
|
-
module.exports.rules = {
|
|
11
|
+
export const rules = {
|
|
13
12
|
...mergeDuplicateImports.rules,
|
|
14
13
|
'declare-imports-first': declareImportsFirst,
|
|
15
14
|
'group-imports-by-source': groupImportsBySource,
|
|
@@ -18,4 +17,5 @@ module.exports.rules = {
|
|
|
18
17
|
'remove-empty-export': removeEmptyExport,
|
|
19
18
|
'sort-imports-by-specifiers': sortImportsBySpecifiers,
|
|
20
19
|
'convert-assert-to-with': convertAssertToWith,
|
|
20
|
+
'apply-export-from': applyExportFrom,
|
|
21
21
|
};
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import * as mergeDuplicateImportsJoin from './join/index.js';
|
|
2
|
+
import * as mergeDuplicateImportsRename from './rename/index.js';
|
|
2
3
|
|
|
3
|
-
const
|
|
4
|
-
const mergeDuplicateImportsRename = require('./rename');
|
|
5
|
-
|
|
6
|
-
module.exports.rules = {
|
|
4
|
+
export const rules = {
|
|
7
5
|
'merge-duplicate-imports-join': mergeDuplicateImportsJoin,
|
|
8
6
|
'merge-duplicate-imports-rename': mergeDuplicateImportsRename,
|
|
9
7
|
};
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const {types, operator} = require('putout');
|
|
1
|
+
import {types, operator} from 'putout';
|
|
4
2
|
|
|
5
3
|
const {remove, compareAny} = operator;
|
|
6
4
|
const {values} = Object;
|
|
@@ -11,9 +9,9 @@ const {
|
|
|
11
9
|
isImportDeclaration,
|
|
12
10
|
} = types;
|
|
13
11
|
|
|
14
|
-
|
|
12
|
+
export const report = () => `Avoid duplicate imports`;
|
|
15
13
|
|
|
16
|
-
|
|
14
|
+
export const fix = ({path, imports}) => {
|
|
17
15
|
const all = [];
|
|
18
16
|
|
|
19
17
|
for (const imp of imports) {
|
|
@@ -37,7 +35,7 @@ module.exports.fix = ({path, imports}) => {
|
|
|
37
35
|
path.node.specifiers.push(...all);
|
|
38
36
|
};
|
|
39
37
|
|
|
40
|
-
|
|
38
|
+
export const traverse = ({push, pathStore}) => ({
|
|
41
39
|
ImportDeclaration(path) {
|
|
42
40
|
pathStore(path);
|
|
43
41
|
},
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {types, operator} = require('putout');
|
|
4
3
|
const {rename, remove} = operator;
|
|
5
4
|
const {isImportDefaultSpecifier} = types;
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
export const report = () => 'Avoid duplicate imports';
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
export const fix = ({path, imports}) => {
|
|
10
9
|
const {name} = path.node.specifiers[0].local;
|
|
11
10
|
remove(path);
|
|
12
11
|
|
|
@@ -22,7 +21,7 @@ module.exports.fix = ({path, imports}) => {
|
|
|
22
21
|
}
|
|
23
22
|
};
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
export const traverse = ({push, uplist}) => ({
|
|
26
25
|
ImportDeclaration: (path) => {
|
|
27
26
|
const {value} = path.node.source;
|
|
28
27
|
const [specifier, ...other] = path.node.specifiers;
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import {operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {operator} = require('putout');
|
|
4
3
|
const {remove} = operator;
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
export const report = () => 'Remove empty export';
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
export const fix = (path) => {
|
|
9
8
|
remove(path);
|
|
10
9
|
};
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
export const include = () => [
|
|
13
12
|
'ExportNamedDeclaration',
|
|
14
13
|
];
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
export const filter = (path) => {
|
|
17
16
|
const {specifiers, declaration} = path.node;
|
|
18
17
|
|
|
19
18
|
return !declaration && !specifiers.length;
|
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
import {operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {operator} = require('putout');
|
|
4
3
|
const {remove} = operator;
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
export const report = () => `Avoid empty 'import' statement`;
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
export const fix = (path) => {
|
|
9
8
|
remove(path);
|
|
10
9
|
};
|
|
11
10
|
|
|
12
11
|
const isCSS = (a) => /\.css/.test(a);
|
|
13
12
|
const isMin = (a) => /\.min\./.test(a);
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
export const include = () => [
|
|
16
15
|
'ImportDeclaration',
|
|
17
16
|
];
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
export const filter = (path, {options}) => {
|
|
20
19
|
const {specifiers, source} = path.node;
|
|
21
20
|
|
|
22
21
|
const {ignore = []} = options;
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
'
|
|
1
|
+
export const report = () => 'Remove quotes from import assertions';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
module.exports.replace = () => ({
|
|
3
|
+
export const replace = () => ({
|
|
6
4
|
'import __imports from "__a" with {"type": "__b"}': 'import __imports from "__a" with {type: "__b"}',
|
|
7
5
|
});
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import {parseImportSpecifiers} from 'parse-import-specifiers';
|
|
2
|
+
import {operator} from 'putout';
|
|
2
3
|
|
|
3
|
-
const {parseImportSpecifiers} = require('parse-import-specifiers');
|
|
4
|
-
const {operator} = require('putout');
|
|
5
4
|
const {insertBefore, remove} = operator;
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
export const report = () => `Sort imports by specifiers count`;
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
export const fix = ({path, nextPath}) => {
|
|
10
9
|
const {node} = nextPath;
|
|
11
10
|
remove(nextPath);
|
|
12
11
|
insertBefore(path, node);
|
|
13
12
|
};
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
export const traverse = ({push}) => ({
|
|
16
15
|
ImportDeclaration(path) {
|
|
17
16
|
const {node} = path;
|
|
18
17
|
const {source, specifiers} = node;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-esm",
|
|
3
|
-
"version": "
|
|
4
|
-
"type": "
|
|
3
|
+
"version": "3.0.0",
|
|
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",
|
|
7
7
|
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-esm#readme",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"esm"
|
|
38
38
|
],
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@putout/eslint-flat": "^
|
|
40
|
+
"@putout/eslint-flat": "^3.0.0",
|
|
41
41
|
"@putout/plugin-declare": "*",
|
|
42
42
|
"@putout/plugin-declare-before-reference": "*",
|
|
43
43
|
"@putout/plugin-nodejs": "*",
|
|
@@ -45,23 +45,22 @@
|
|
|
45
45
|
"@putout/plugin-reuse-duplicate-init": "*",
|
|
46
46
|
"@putout/plugin-tape": "*",
|
|
47
47
|
"@putout/plugin-typescript": "*",
|
|
48
|
-
"@putout/test": "^
|
|
48
|
+
"@putout/test": "^13.0.0",
|
|
49
49
|
"c8": "^10.0.0",
|
|
50
50
|
"eslint": "^9.0.0",
|
|
51
51
|
"eslint-plugin-n": "^17.0.0",
|
|
52
|
-
"eslint-plugin-putout": "^
|
|
53
|
-
"
|
|
54
|
-
"madrun": "^10.0.0",
|
|
52
|
+
"eslint-plugin-putout": "^26.0.0",
|
|
53
|
+
"madrun": "^11.0.0",
|
|
55
54
|
"montag": "^1.2.1",
|
|
56
55
|
"nodemon": "^3.0.1",
|
|
57
|
-
"supertape": "^
|
|
56
|
+
"supertape": "^11.0.3"
|
|
58
57
|
},
|
|
59
58
|
"peerDependencies": {
|
|
60
|
-
"putout": ">=
|
|
59
|
+
"putout": ">=39"
|
|
61
60
|
},
|
|
62
61
|
"license": "MIT",
|
|
63
62
|
"engines": {
|
|
64
|
-
"node": ">=
|
|
63
|
+
"node": ">=20"
|
|
65
64
|
},
|
|
66
65
|
"publishConfig": {
|
|
67
66
|
"access": "public"
|