@putout/plugin-nodejs 19.2.0 → 19.2.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,9 +1,4 @@
1
- import {
2
- join,
3
- dirname,
4
- resolve,
5
- } from 'node:path';
6
- import {tryCatch} from 'try-catch';
1
+ import {join, dirname} from 'node:path';
7
2
  import {
8
3
  parse,
9
4
  print,
@@ -12,19 +7,14 @@ import {
12
7
  } from 'putout';
13
8
  import * as getImports from './get-require/index.js';
14
9
  import * as changeImports from './change-require/index.js';
15
-
16
- const isString = (a) => typeof a === 'string';
10
+ import {createGetPrivateImports} from './private-imports.js';
17
11
 
18
12
  const {
19
13
  getFilename,
20
14
  readFileContent,
21
15
  writeFileContent,
22
- findFileUp,
23
16
  } = operator;
24
17
 
25
- const {entries} = Object;
26
- const {parse: parseJson} = JSON;
27
-
28
18
  const getMessage = (a) => a.message;
29
19
 
30
20
  export const report = (file, {from, to}) => `Apply privately required source: '${from}' -> '${to}'`;
@@ -60,7 +50,7 @@ export const scan = (rootPath, {push, trackFile}) => {
60
50
  if (!content.includes('require'))
61
51
  continue;
62
52
 
63
- const [, privateImports] = getPrivateImports(file);
53
+ const privateImports = getPrivateImports(file);
64
54
 
65
55
  if (!privateImports.size)
66
56
  continue;
@@ -91,58 +81,3 @@ export const scan = (rootPath, {push, trackFile}) => {
91
81
  }
92
82
  }
93
83
  };
94
-
95
- const createGetPrivateImports = (importsCache = new Map(), emptyMap = new Map()) => (file) => {
96
- const filename = getFilename(file);
97
- const dir = dirname(filename);
98
-
99
- if (importsCache.has(dir))
100
- return [dir, importsCache.get(dir)];
101
-
102
- const [packageDirectory, packagePath] = findFileUp(file, 'package.json');
103
-
104
- if (!packagePath) {
105
- importsCache.set(dir, {});
106
- return ['', emptyMap];
107
- }
108
-
109
- const packageContent = readFileContent(packagePath);
110
- const [error, packageJson] = tryCatch(parseJson, packageContent);
111
-
112
- if (error) {
113
- importsCache.set(dir, emptyMap);
114
-
115
- return ['', emptyMap];
116
- }
117
-
118
- const {imports = {}} = packageJson;
119
- const importsEntries = new Map();
120
-
121
- for (const [alias, property] of entries(imports)) {
122
- const filePath = parseProperty(property);
123
-
124
- if (!filePath)
125
- continue;
126
-
127
- const resolvedPath = resolve(packageDirectory, filePath);
128
-
129
- importsEntries.set(resolvedPath, alias);
130
- }
131
-
132
- importsCache.set(dir, [packageDirectory, importsEntries]);
133
-
134
- return [packageDirectory, importsEntries];
135
- };
136
-
137
- function parseProperty(property) {
138
- if (isString(property))
139
- return property;
140
-
141
- const {
142
- default: filePath,
143
- node,
144
- browser,
145
- } = property;
146
-
147
- return filePath || node || browser;
148
- }
@@ -0,0 +1,81 @@
1
+ import {dirname, resolve} from 'node:path';
2
+ import {tryCatch} from 'try-catch';
3
+ import {operator} from 'putout';
4
+
5
+ const {
6
+ getFilename,
7
+ readFileContent,
8
+ findFileUp,
9
+ } = operator;
10
+
11
+ const {entries} = Object;
12
+ const {parse: parseJson} = JSON;
13
+ const isString = (a) => typeof a === 'string';
14
+
15
+ function insert(a, b, {importsEntries, aliasBased}) {
16
+ if (aliasBased) {
17
+ importsEntries.set(b, a);
18
+ return;
19
+ }
20
+
21
+ importsEntries.set(a, b);
22
+ }
23
+
24
+ export const createGetPrivateImports = (importsCache = new Map(), emptyMap = new Map()) => (file, options = {}) => {
25
+ const filename = getFilename(file);
26
+ const dir = dirname(filename);
27
+ const {aliasBased = false} = options;
28
+
29
+ if (importsCache.has(dir))
30
+ return importsCache.get(dir);
31
+
32
+ const [packageDirectory, packagePath] = findFileUp(file, 'package.json');
33
+
34
+ if (!packagePath) {
35
+ importsCache.set(dir, emptyMap);
36
+ return emptyMap;
37
+ }
38
+
39
+ const packageContent = readFileContent(packagePath);
40
+ const [error, packageJson] = tryCatch(parseJson, packageContent);
41
+
42
+ if (error) {
43
+ importsCache.set(dir, emptyMap);
44
+
45
+ return emptyMap;
46
+ }
47
+
48
+ const {imports = {}} = packageJson;
49
+ const importsEntries = new Map();
50
+
51
+ for (const [alias, property] of entries(imports)) {
52
+ const filePath = parseProperty(property);
53
+
54
+ if (!filePath)
55
+ continue;
56
+
57
+ const resolvedPath = resolve(packageDirectory, filePath);
58
+
59
+ insert(resolvedPath, alias, {
60
+ importsEntries,
61
+ aliasBased,
62
+ });
63
+ }
64
+
65
+ importsCache.set(dir, importsEntries);
66
+
67
+ return importsEntries;
68
+ };
69
+
70
+ function parseProperty(property) {
71
+ if (isString(property))
72
+ return property;
73
+
74
+ const {
75
+ default: filePath,
76
+ node,
77
+ browser,
78
+ } = property;
79
+
80
+ return filePath || node || browser;
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "19.2.0",
3
+ "version": "19.2.1",
4
4
  "type": "module",
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",
@@ -51,7 +51,7 @@
51
51
  "c8": "^10.0.0",
52
52
  "eslint": "^10.0.0-alpha.0",
53
53
  "eslint-plugin-n": "^17.0.0",
54
- "eslint-plugin-putout": "^29.0.0",
54
+ "eslint-plugin-putout": "^30.0.0",
55
55
  "madrun": "^12.0.0",
56
56
  "montag": "^1.2.1",
57
57
  "nodemon": "^3.0.1"