@putout/plugin-nodejs 11.7.1 β†’ 11.8.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
@@ -56,12 +56,18 @@ Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/534093e0bf0a4
56
56
 
57
57
  ```js
58
58
  import fs from 'fs';
59
+
60
+ const path = require('path');
61
+ await import('path');
59
62
  ```
60
63
 
61
64
  ### βœ… Example of correct code
62
65
 
63
66
  ```js
64
67
  import fs from 'node:fs';
68
+
69
+ const path = require('node:path');
70
+ await import('node:path');
65
71
  ```
66
72
 
67
73
  ### Comparison
@@ -382,7 +388,7 @@ Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/779e7fb720af5
382
388
 
383
389
  ## rename-file-cjs-to-js
384
390
 
385
- Rename `*.cjs` files when `module !== "module"`:
391
+ Rename `*.cjs` files when `type === "commonjs"`:
386
392
 
387
393
  ```diff
388
394
  /
@@ -396,7 +402,7 @@ Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/8d8f3cd6662b7
396
402
 
397
403
  ## rename-file-mjs-to-js
398
404
 
399
- Rename `*.mjs` files when `module === "module"`:
405
+ Rename `*.mjs` files when `type === "module"`:
400
406
 
401
407
  ```diff
402
408
  /
@@ -1,28 +1,68 @@
1
1
  'use strict';
2
2
 
3
- const {operator} = require('putout');
4
- const {isBuiltin} = require('module');
5
- const {setLiteralValue} = operator;
3
+ const {types, operator} = require('putout');
4
+ const {isBuiltin} = require('node:module');
5
+ const {
6
+ setLiteralValue,
7
+ getTemplateValues,
8
+ } = operator;
6
9
 
7
- module.exports.report = () => `Add 'node:' prefix`;
10
+ const {isCallExpression} = types;
8
11
 
9
- module.exports.fix = (path) => {
10
- const {source} = path.node;
11
- const {value} = source;
12
+ const REQUIRE = 'require("__a")';
13
+ const IMPORT = 'import("__a")';
14
+
15
+ module.exports.report = ({value}) => {
16
+ return `Use 'node:${value}' instead of '${value}'`;
17
+ };
18
+
19
+ module.exports.fix = ({path, value}) => {
20
+ if (isCallExpression(path)) {
21
+ const arg = path.get('arguments.0');
22
+ setLiteralValue(arg, `node:${value}`);
23
+
24
+ return;
25
+ }
12
26
 
27
+ const {source} = path.node;
13
28
  setLiteralValue(source, `node:${value}`);
14
29
  };
15
30
 
16
31
  module.exports.traverse = ({push}) => ({
17
- ImportDeclaration(path) {
18
- const {value} = path.node.source;
32
+ [IMPORT](path) {
33
+ const {__a} = getTemplateValues(path, IMPORT);
34
+ const {value} = __a;
19
35
 
20
- if (value.startsWith('node:'))
21
- return;
36
+ if (check(value))
37
+ push({
38
+ path,
39
+ value,
40
+ });
41
+ },
42
+ [REQUIRE](path) {
43
+ const {__a} = getTemplateValues(path, REQUIRE);
44
+ const {value} = __a;
22
45
 
23
- if (!isBuiltin(value))
24
- return;
46
+ if (check(value))
47
+ push({
48
+ path,
49
+ value,
50
+ });
51
+ },
52
+ ImportDeclaration(path) {
53
+ const {value} = path.node.source;
25
54
 
26
- push(path);
55
+ if (check(value))
56
+ push({
57
+ path,
58
+ value,
59
+ });
27
60
  },
28
61
  });
62
+
63
+ function check(value) {
64
+ if (value.startsWith('node:'))
65
+ return false;
66
+
67
+ return isBuiltin(value);
68
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "11.7.1",
3
+ "version": "11.8.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to transform code to new API of Node.js",