@putout/plugin-nodejs 18.10.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 {types, operator} from 'putout';
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,
@@ -10,9 +15,11 @@ const {
10
15
  isImportDefaultSpecifier,
11
16
  exportNamespaceSpecifier,
12
17
  isSpreadElement,
18
+ isObjectMethod,
13
19
  } = types;
14
20
 
15
21
  const {replaceWith} = operator;
22
+ const createFn = template('export const NAME = BODY');
16
23
 
17
24
  export const report = () => `Use 'ESM' instead of 'CommonJS'`;
18
25
 
@@ -59,14 +66,27 @@ function createDeclaration(name, value) {
59
66
  }
60
67
 
61
68
  export const replace = () => ({
62
- 'module.exports = __a': ({__a}) => {
69
+ 'module.exports = __a': ({__a}, path) => {
63
70
  if (!isObjectExpression(__a) || isStringLiteral(__a.properties[0].key))
64
71
  return 'export default __a';
65
72
 
66
73
  const result = ['export {'];
67
74
  const declarations = [];
75
+ const programPath = path.scope.getProgramParent().path;
68
76
 
69
- 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
+ declareFunction({
82
+ object: __a,
83
+ property,
84
+ index,
85
+ programPath,
86
+ });
87
+ continue;
88
+ }
89
+
70
90
  if (!isIdentifier(value)) {
71
91
  declarations.push(createDeclaration(key.name, value.value));
72
92
  continue;
@@ -142,3 +162,14 @@ function parseBindingPath(path) {
142
162
 
143
163
  return path;
144
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.10.0",
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",