@putout/plugin-nodejs 18.11.0 β†’ 18.13.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
@@ -380,6 +380,8 @@ const args = minimist({
380
380
 
381
381
  ### exports
382
382
 
383
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/39dd730c35e488db3e2cf8d4b4df0c5a/feb042a49539e6b4b2165558a1c636b705726c47).
384
+
383
385
  ### ❌ Example of incorrect code
384
386
 
385
387
  ```js
@@ -1,4 +1,8 @@
1
- import {types, operator} from 'putout';
1
+ import {
2
+ types,
3
+ operator,
4
+ template,
5
+ } from 'putout';
2
6
 
3
7
  const {
4
8
  exportSpecifier,
@@ -11,9 +15,11 @@ const {
11
15
  exportNamespaceSpecifier,
12
16
  isSpreadElement,
13
17
  isObjectMethod,
18
+ isFunction,
14
19
  } = types;
15
20
 
16
21
  const {replaceWith} = operator;
22
+ const createFn = template('export const NAME = (PARAMS) => 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,45 @@ 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 {key, value} of __a.properties) {
77
+ for (const [index, property] of __a.properties.entries()) {
78
+ const {key, value} = property;
79
+
80
+ if (isObjectMethod(property)) {
81
+ const {params, body} = property;
82
+
83
+ declareFunction({
84
+ object: __a,
85
+ property,
86
+ index,
87
+ programPath,
88
+ params,
89
+ body,
90
+ });
91
+ continue;
92
+ }
93
+
94
+ if (isFunction(value)) {
95
+ const {params, body} = property.value;
96
+
97
+ declareFunction({
98
+ object: __a,
99
+ property,
100
+ index,
101
+ programPath,
102
+ params,
103
+ body,
104
+ });
105
+ continue;
106
+ }
107
+
74
108
  if (!isIdentifier(value)) {
75
109
  declarations.push(createDeclaration(key.name, value.value));
76
110
  continue;
@@ -146,3 +180,15 @@ function parseBindingPath(path) {
146
180
 
147
181
  return path;
148
182
  }
183
+
184
+ function declareFunction({index, property, object, params, body, programPath}) {
185
+ const {key} = property;
186
+ const fnNode = createFn({
187
+ NAME: key,
188
+ PARAMS: params,
189
+ BODY: body,
190
+ });
191
+
192
+ delete object.properties[index];
193
+ programPath.node.body.push(fnNode);
194
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "18.11.0",
3
+ "version": "18.13.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",