Skip to content

Commit

Permalink
feature(@putout/plugin-convert-commonjs-to-esm) require: add support …
Browse files Browse the repository at this point in the history
…of chaining
  • Loading branch information
coderaiser committed Aug 25, 2021
1 parent ba3dfe6 commit edb89a4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const os = () => {};
const HOME = require('os').homedir();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
import os from 'os';
const HOME = os.homedir();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
import superOs from 'super-os';
const HOME = superOs.homedir();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const HOME = require('super-os').homedir();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const HOME = require('os').homedir();
12 changes: 12 additions & 0 deletions packages/plugin-convert-commonjs-to-esm/lib/require/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ module.exports.match = () => ({
const name = camelCase(__b.value);
return !path.scope.bindings[name];
},
'const __a = require("__b").__c(__args)': ({__b}, path) => {
const name = camelCase(__b.value);
return !path.scope.bindings[name];
},
});

module.exports.replace = () => ({
Expand Down Expand Up @@ -115,6 +119,14 @@ module.exports.replace = () => ({
const __a = ${name}(__args);
}`;
},
'const __a = require("__b").__c(__args)': ({__b}) => {
const name = camelCase(__b.value);

return `{
import ${name} from "__b";
const __a = ${name}.__c(__args);
}`;
},
'const __a = require(__b).default': ({__a, __b}, path) => {
const name = `_${__a.name}`;
const importNode = createImport({
Expand Down
15 changes: 15 additions & 0 deletions packages/plugin-convert-commonjs-to-esm/lib/require/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ test('plugin-convert-commonjs-to-esm: require: transform: call', (t) => {
t.end();
});

test('plugin-convert-commonjs-to-esm: require: transform: chain', (t) => {
t.transform('chain');
t.end();
});

test('plugin-convert-commonjs-to-esm: require: no transform: chain declared', (t) => {
t.noTransform('chain-declared');
t.end();
});

test('plugin-convert-commonjs-to-esm: require: transform: chain kebab', (t) => {
t.transform('chain-kebab');
t.end();
});

test('plugin-convert-commonjs-to-esm: require: transform: call: kebab', (t) => {
t.transform('call-kebab');
t.end();
Expand Down

0 comments on commit edb89a4

Please sign in to comment.