@putout/plugin-nodejs 18.1.0 → 18.3.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.
@@ -5,6 +5,7 @@ const {
5
5
  isIdentifier,
6
6
  isImportSpecifier,
7
7
  exportNamedDeclaration,
8
+ isObjectExpression,
8
9
  } = types;
9
10
 
10
11
  const {replaceWith} = operator;
@@ -38,8 +39,47 @@ export const match = () => ({
38
39
  },
39
40
  });
40
41
 
42
+ function createDeclaration(name, value) {
43
+ return `export const ${name} = ${value};`;
44
+ }
45
+
41
46
  export const replace = () => ({
42
- 'module.exports = __a': 'export default __a',
47
+ 'module.exports = __a': ({__a}) => {
48
+ if (!isObjectExpression(__a))
49
+ return 'export default __a';
50
+
51
+ const result = ['export {'];
52
+ const declarations = [];
53
+
54
+ for (const {key, value} of __a.properties) {
55
+ if (!isIdentifier(value)) {
56
+ declarations.push(createDeclaration(key.name, value.value));
57
+ continue;
58
+ }
59
+
60
+ if (key.name === value.name) {
61
+ result.push(`${key.name},`);
62
+ continue;
63
+ }
64
+
65
+ result.push(`${key.name} as ${value.name}`);
66
+ }
67
+
68
+ result.push('};');
69
+
70
+ if (!declarations.length)
71
+ return result.join('\n');
72
+
73
+ if (declarations.length === __a.properties.length)
74
+ return `{
75
+ ${declarations.join('')}
76
+ }`;
77
+
78
+ return `{
79
+ ${declarations.join('\n')}
80
+ ${result.join('\n')}
81
+ }`;
82
+ },
43
83
  'module.exports.__a = __b': ({__a, __b}, path) => {
44
84
  const {name} = __a;
45
85
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-nodejs",
3
- "version": "18.1.0",
3
+ "version": "18.3.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",
@@ -44,7 +44,7 @@
44
44
  "@putout/plugin-declare-before-reference": "*",
45
45
  "@putout/plugin-esm": "*",
46
46
  "@putout/plugin-putout": "*",
47
- "@putout/plugin-reuse-duplicate-init": "*",
47
+ "@putout/plugin-variables": "*",
48
48
  "@putout/plugin-typescript": "*",
49
49
  "@putout/test": "^14.0.0",
50
50
  "c8": "^10.0.0",