Skip to content

Commit

Permalink
feature(@putout/plugin-convert-commonjs-to-esm) add support of declar…
Browse files Browse the repository at this point in the history
…ed require
  • Loading branch information
coderaiser committed Aug 25, 2021
1 parent 6f50235 commit 359c317
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const require = () => {};

function info() {
return require('./package.json');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {createCommons} from 'simport';
const {__filename, __dirname, require} = createCommons(import.meta.url);

function info() {
return require('./package.json');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createCommons} from 'simport';

const {
__filename,
__dirname,
require
} = createCommons(import.meta.url);

function info() {
return require('./package.json');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function info() {
return require('./package.json');
}
9 changes: 8 additions & 1 deletion packages/plugin-convert-commonjs-to-esm/lib/commons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ module.exports.fix = (path) => {
module.exports.include = () => [
'__filename',
'__dirname',
'require',
];

module.exports.filter = ({scope}) => !scope.hasBinding('__dirname') && !scope.hasBinding('__filename');
module.exports.filter = ({scope}) => {
const isDirname = scope.hasBinding('__dirname');
const isFilename = scope.hasBinding('__filename');
const isRequire = scope.hasBinding('require');

return !isDirname && !isFilename && !isRequire;
};

10 changes: 10 additions & 0 deletions packages/plugin-convert-commonjs-to-esm/lib/commons/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ test('plugin-convert-commonjs-to-esm: commons: transform', (t) => {
t.end();
});

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

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

test('plugin-convert-commonjs-to-esm: commons: no transform', (t) => {
t.noTransformCode('const a = 5');
t.end();
Expand Down

0 comments on commit 359c317

Please sign in to comment.