@putout/plugin-esm 5.3.0 β†’ 6.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 CHANGED
@@ -362,7 +362,7 @@ Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9f85897b9
362
362
 
363
363
  #### ❌ Example of incorrect code
364
364
 
365
- ```js
365
+ ```
366
366
  import json from './foo.json' assert {
367
367
  type: 'json',
368
368
  };
@@ -1,6 +1,11 @@
1
- const setImportType = (path, type) => {
2
- path.node.options.properties[0].key.name = type;
3
- };
1
+ import {operator, types} from 'putout';
2
+
3
+ const {
4
+ importAttribute,
5
+ identifier,
6
+ } = types;
7
+
8
+ const {compare, remove} = operator;
4
9
 
5
10
  export const report = () => `Use 'with' instead of 'assert'`;
6
11
 
@@ -10,19 +15,38 @@ export const include = () => [
10
15
  ];
11
16
 
12
17
  export const fix = (path) => {
13
- if (path.isImportDeclaration()) {
14
- delete path.node.extra.deprecatedAssertSyntax;
18
+ if (path.isImportExpression()) {
19
+ path.node.options.properties[0].key.name = 'with';
15
20
  return;
16
21
  }
17
22
 
18
- setImportType(path, 'with');
23
+ const next = path.getNextSibling();
24
+ const nextNext = next.getNextSibling();
25
+ const nextNextNext = nextNext.getNextSibling();
26
+ const {body} = nextNext.node.body[0];
27
+
28
+ remove(next);
29
+ remove(nextNext);
30
+
31
+ if (nextNextNext.isEmptyStatement())
32
+ remove(nextNextNext);
33
+
34
+ path.node.attributes = [
35
+ importAttribute(identifier('type'), body.expression),
36
+ ];
19
37
  };
20
38
 
21
39
  export const filter = (path) => {
22
- const {extra} = path.node;
23
-
24
- if (path.isImportDeclaration())
25
- return extra?.deprecatedAssertSyntax;
40
+ if (path.isImportDeclaration()) {
41
+ const next = path.getNextSibling();
42
+
43
+ if (!compare(next, 'assert;'))
44
+ return false;
45
+
46
+ const nextNext = next.getNextSibling();
47
+
48
+ return compare(nextNext, '{type: "__";}');
49
+ }
26
50
 
27
- return true;
51
+ return path.isImportExpression();
28
52
  };
@@ -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": "6.0.0",
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",
@@ -57,7 +57,7 @@
57
57
  "supertape": "^11.0.3"
58
58
  },
59
59
  "peerDependencies": {
60
- "putout": ">=40"
60
+ "putout": ">=41"
61
61
  },
62
62
  "license": "MIT",
63
63
  "engines": {