@putout/plugin-nodejs 18.11.0 → 18.12.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.
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
types,
|
|
3
|
+
operator,
|
|
4
|
+
template,
|
|
5
|
+
} from 'putout';
|
|
2
6
|
|
|
3
7
|
const {
|
|
8
|
+
arrowFunctionExpression,
|
|
4
9
|
exportSpecifier,
|
|
5
10
|
isIdentifier,
|
|
6
11
|
isImportSpecifier,
|
|
@@ -14,6 +19,7 @@ const {
|
|
|
14
19
|
} = types;
|
|
15
20
|
|
|
16
21
|
const {replaceWith} = operator;
|
|
22
|
+
const createFn = template('export const NAME = BODY');
|
|
17
23
|
|
|
18
24
|
export const report = () => `Use 'ESM' instead of 'CommonJS'`;
|
|
19
25
|
|
|
@@ -30,9 +36,6 @@ export const match = () => ({
|
|
|
30
36
|
for (const property of __a.properties) {
|
|
31
37
|
if (isSpreadElement(property))
|
|
32
38
|
return false;
|
|
33
|
-
|
|
34
|
-
if (isObjectMethod(property))
|
|
35
|
-
return false;
|
|
36
39
|
}
|
|
37
40
|
|
|
38
41
|
return true;
|
|
@@ -63,14 +66,27 @@ function createDeclaration(name, value) {
|
|
|
63
66
|
}
|
|
64
67
|
|
|
65
68
|
export const replace = () => ({
|
|
66
|
-
'module.exports = __a': ({__a}) => {
|
|
69
|
+
'module.exports = __a': ({__a}, path) => {
|
|
67
70
|
if (!isObjectExpression(__a) || isStringLiteral(__a.properties[0].key))
|
|
68
71
|
return 'export default __a';
|
|
69
72
|
|
|
70
73
|
const result = ['export {'];
|
|
71
74
|
const declarations = [];
|
|
75
|
+
const programPath = path.scope.getProgramParent().path;
|
|
72
76
|
|
|
73
|
-
for (const
|
|
77
|
+
for (const [index, property] of __a.properties.entries()) {
|
|
78
|
+
const {key, value} = property;
|
|
79
|
+
|
|
80
|
+
if (isObjectMethod(property)) {
|
|
81
|
+
declareFunction({
|
|
82
|
+
object: __a,
|
|
83
|
+
property,
|
|
84
|
+
index,
|
|
85
|
+
programPath,
|
|
86
|
+
});
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
74
90
|
if (!isIdentifier(value)) {
|
|
75
91
|
declarations.push(createDeclaration(key.name, value.value));
|
|
76
92
|
continue;
|
|
@@ -146,3 +162,14 @@ function parseBindingPath(path) {
|
|
|
146
162
|
|
|
147
163
|
return path;
|
|
148
164
|
}
|
|
165
|
+
|
|
166
|
+
function declareFunction({index, property, object, programPath}) {
|
|
167
|
+
const {key, body} = property;
|
|
168
|
+
const fnNode = createFn({
|
|
169
|
+
NAME: key,
|
|
170
|
+
BODY: arrowFunctionExpression([], body),
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
delete object.properties[index];
|
|
174
|
+
programPath.node.body.push(fnNode);
|
|
175
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-nodejs",
|
|
3
|
-
"version": "18.
|
|
3
|
+
"version": "18.12.0",
|
|
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",
|