Repository: depcheck/depcheck
Pull request #656 (2021-07-22 08:23:54)

Support babel.config.js(on) files

rumpl: @crossjs thank you for opening this, could you take a look at the failures and fix them please?

Patch:
diff --git a/src/special/babel.js b/src/special/babel.js
index 3ce7622..ef6165d 100755
--- a/src/special/babel.js
+++ b/src/special/babel.js
@@ -82,7 +82,7 @@ function checkOptions(deps, options = {}) {
   return optDeps.concat(envDeps);
 }
 
-const regex = /^(\.babelrc|babelrc\.js|babel\.config\.js)?$/;
+const regex = /^(\.babelrc(\.(cjs|js(on)?))?|babel\.config\.(cjs|js(on)?))?$/;
 
 export default async function parseBabel(filename, deps, rootDir) {
   const config = await loadConfig('babel', regex, filename, rootDir);
diff --git a/src/special/bin.js b/src/special/bin.js
index 423ef38..30ec637 100644
--- a/src/special/bin.js
+++ b/src/special/bin.js
@@ -30,8 +30,6 @@ function getBinaryFeatures(dep, [key, value]) {
     key,
     `--require ${key}`,
     `--require ${key}/register`,
-    `-r ${key}`,
-    `-r ${key}/register`,
     `$(npm bin)/${key}`,
     `node_modules/.bin/${key}`,
     `./node_modules/.bin/${key}`,


Test patch:
diff --git a/test/special/babel.js b/test/special/babel.js
index 222d8ee..f0af99d 100755
--- a/test/special/babel.js
+++ b/test/special/babel.js
@@ -155,7 +155,7 @@ describe('babel special parser', () => {
   });
 
   it('should detect babel.config.js', async () => {
-    const content = "module.exports = { presets: ['es2015' ] }";
+    const content = "module.exports = { presets: ['es2015'] }";
 
     const result = await testParser(content, '/path/to/babel.config.js', [
       'babel-preset-es2015',
@@ -164,6 +164,26 @@ describe('babel special parser', () => {
     result.should.deepEqual(['babel-preset-es2015']);
   });
 
+  it('should detect babel.config.cjs', async () => {
+    const content = "module.exports = { presets: ['es2015'] }";
+
+    const result = await testParser(content, '/path/to/babel.config.cjs', [
+      'babel-preset-es2015',
+      'dep',
+    ]);
+    result.should.deepEqual(['babel-preset-es2015']);
+  });
+
+  it('should detect babel.config.json', async () => {
+    const content = '{ "presets": ["es2015"] }';
+
+    const result = await testParser(content, '/path/to/babel.config.json', [
+      'babel-preset-es2015',
+      'dep',
+    ]);
+    result.should.deepEqual(['babel-preset-es2015']);
+  });
+
   testCases.forEach((testCase) =>
     it(`should ${testCase.name} in .babelrc file`, () =>
       testBabel('.babelrc', testCase.deps, testCase.options)),
diff --git a/test/special/bin.js b/test/special/bin.js
index fac1a8e..70d27ec 100644
--- a/test/special/bin.js
+++ b/test/special/bin.js
@@ -95,18 +95,6 @@ const testCases = [
     dependencies: ['binary-package'],
     expected: ['binary-package'],
   },
-  {
-    name: 'detect packages used with -r and /register',
-    script: 'module -r binary-entry/register',
-    dependencies: ['binary-package'],
-    expected: ['binary-package'],
-  },
-  {
-    name: 'detect packages used with -r',
-    script: 'module -r binary-entry',
-    dependencies: ['binary-package'],
-    expected: ['binary-package'],
-  },
   {
     name: 'detect packages with single binary',
     script: 'single-binary-package --argument',