@putout/plugin-esm 5.3.0 → 5.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.
@@ -1,15 +1,17 @@
1
1
  import {parseImportSpecifiers} from 'parse-import-specifiers';
2
2
  import {operator, types} from 'putout';
3
3
 
4
- const {insertBefore, remove} = operator;
4
+ const {insertAfter, remove} = operator;
5
+
5
6
  const {isImportDeclaration} = types;
6
7
 
7
8
  export const report = () => `Sort imports by specifiers count`;
8
9
 
9
10
  export const fix = ({path, nextPath}) => {
10
- const {node} = nextPath;
11
- remove(nextPath);
12
- insertBefore(path, node);
11
+ const {node} = path;
12
+
13
+ remove(path);
14
+ insertAfter(nextPath, node);
13
15
  };
14
16
 
15
17
  export const traverse = ({push}) => ({
@@ -18,40 +20,15 @@ export const traverse = ({push}) => ({
18
20
 
19
21
  for (const path of paths) {
20
22
  const {node} = path;
21
-
22
- if (!node)
23
- continue;
24
-
25
- const {source, specifiers} = node;
23
+ const {specifiers} = node;
26
24
  const {imports} = parseImportSpecifiers(specifiers);
27
25
 
28
26
  if (imports.length < 4)
29
27
  continue;
30
28
 
31
- const nextPath = path.getNextSibling();
32
-
33
- if (!nextPath.isImportDeclaration())
34
- continue;
29
+ const nextPath = getLastNextPath(path);
35
30
 
36
- if (nextPath.node.specifiers.length !== 1)
37
- continue;
38
-
39
- const first = source.value;
40
- const second = nextPath.node.source.value;
41
-
42
- const is = isExcluded(first, second, {
43
- direct: [
44
- ['node:', 'node:'],
45
- ['#', '#'],
46
- ],
47
- reversed: [
48
- ['./', './'],
49
- ['../', '../'],
50
- ['#', '#'],
51
- ],
52
- });
53
-
54
- if (is)
31
+ if (!nextPath)
55
32
  continue;
56
33
 
57
34
  push({
@@ -62,6 +39,52 @@ export const traverse = ({push}) => ({
62
39
  },
63
40
  });
64
41
 
42
+ function getLastNextPath(path) {
43
+ let next = path;
44
+ let prev;
45
+
46
+ do {
47
+ prev = next;
48
+ next = getNextPath(prev);
49
+ } while (next);
50
+
51
+ if (prev === path)
52
+ return null;
53
+
54
+ return prev;
55
+ }
56
+
57
+ function getNextPath(path) {
58
+ const nextPath = path.getNextSibling();
59
+
60
+ if (!nextPath.isImportDeclaration())
61
+ return null;
62
+
63
+ if (nextPath.node.specifiers.length !== 1)
64
+ return null;
65
+
66
+ const {source} = path.node;
67
+ const first = source.value;
68
+ const second = nextPath.node.source.value;
69
+
70
+ const is = isExcluded(first, second, {
71
+ direct: [
72
+ ['node:', 'node:'],
73
+ ['#', '#'],
74
+ ],
75
+ reversed: [
76
+ ['./', './'],
77
+ ['../', '../'],
78
+ ['#', '#'],
79
+ ],
80
+ });
81
+
82
+ if (is)
83
+ return null;
84
+
85
+ return nextPath;
86
+ }
87
+
65
88
  function isExcluded(first, second, {direct, reversed}) {
66
89
  for (const [current, next] of direct) {
67
90
  if (first.startsWith(current) && !second.startsWith(next))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "5.3.0",
3
+ "version": "5.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",