Repository: depcheck/depcheck
Pull request #724 (2022-06-03 00:30:41)

If babel config is a function, call it to get config
When you define a `babel.config.js` you can return a function, e.g.

https://gist.github.com/gtzilla/a6ae9777a987df1ee1357df51bf9cd61

When parsing this config file, if it a returns a function the function has to be called and provided a stub implementation of that babel api.

See https://babeljs.io/docs/en/config-files#config-function-api

Fix for https://github.com/depcheck/depcheck/issues/723



codecov-commenter: # [Codecov](https://codecov.io/gh/depcheck/depcheck/pull/724?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck) Report
> Merging [#724](https://codecov.io/gh/depcheck/depcheck/pull/724?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck) (0e6ca35) into [main](https://codecov.io/gh/depcheck/depcheck/commit/73c2bec4ae85404c01249b805e7dafe4f8499c65?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck) (73c2bec) will **decrease** coverage by `0.57%`.
> The diff coverage is `70.00%`.

[![Impacted file tree graph](https://codecov.io/gh/depcheck/depcheck/pull/724/graphs/tree.svg?width=650&height=150&src=pr&token=pgMcKAUD3w&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck)](https://codecov.io/gh/depcheck/depcheck/pull/724?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck)

```diff
@@            Coverage Diff             @@
##             main     #724      +/-   ##
==========================================
- Coverage   98.64%   98.08%   -0.56%     
==========================================
  Files          49       49              
  Lines        1023     1037      +14     
==========================================
+ Hits         1009     1017       +8     
- Misses         14       20       +6     
```


| [Impacted Files](https://codecov.io/gh/depcheck/depcheck/pull/724?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck) | Coverage Δ | |
|---|---|---|
| [src/utils/configuration-reader.js](https://codecov.io/gh/depcheck/depcheck/pull/724/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck#diff-c3JjL3V0aWxzL2NvbmZpZ3VyYXRpb24tcmVhZGVyLmpz) | `100.00% <ø> (ø)` | |
| [src/special/babel.js](https://codecov.io/gh/depcheck/depcheck/pull/724/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck#diff-c3JjL3NwZWNpYWwvYmFiZWwuanM=) | `89.37% <42.86%> (-8.19%)` | :arrow_down: |
| [src/cli.js](https://codecov.io/gh/depcheck/depcheck/pull/724/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck#diff-c3JjL2NsaS5qcw==) | `95.56% <75.00%> (-4.44%)` | :arrow_down: |
| [src/detector/importDeclaration.js](https://codecov.io/gh/depcheck/depcheck/pull/724/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck#diff-c3JjL2RldGVjdG9yL2ltcG9ydERlY2xhcmF0aW9uLmpz) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/depcheck/depcheck/pull/724?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/depcheck/depcheck/pull/724?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck). Last update [3d1a45f...0e6ca35](https://codecov.io/gh/depcheck/depcheck/pull/724?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=depcheck).

rumpl: Code looks good but it would be great to have a test for this

Patch:
diff --git a/index.d.tmpl b/index.d.tmpl
index 3b9bfcb..4067e22 100644
--- a/index.d.tmpl
+++ b/index.d.tmpl
@@ -7,9 +7,11 @@ declare function depcheck<T>(
 ): Promise<T>;
 
 declare namespace depcheck {
-  type Node = Record<string, any>;
+  interface Node {
+    [key: string]: any;
+  }
 
-  type Parser = (filePath: string, deps: ReadonlyArray<string>, rootDir: string) => Node | string[];
+  type Parser = (content: string, filePath: string, deps: ReadonlyArray<string>, rootDir: string) => Node;
 
   type Detector = (node: Node) => ReadonlyArray<string> | string;
 
diff --git a/src/special/babel.js b/src/special/babel.js
index ef6165d..0e067f2 100755
--- a/src/special/babel.js
+++ b/src/special/babel.js
@@ -84,11 +84,31 @@ function checkOptions(deps, options = {}) {
 
 const regex = /^(\.babelrc(\.(cjs|js(on)?))?|babel\.config\.(cjs|js(on)?))?$/;
 
+function callConfigIfFunction(config) {
+  if (typeof config === 'function') {
+    return config({
+      cache: Object.assign(() => null, {
+        forever() {},
+        never() {},
+        using() {},
+        invalidate() {},
+      }),
+      caller(cb) {
+        cb({ name: 'depcheck' });
+      },
+      env() {
+        return true;
+      },
+    });
+  }
+  return config;
+}
+
 export default async function parseBabel(filename, deps, rootDir) {
   const config = await loadConfig('babel', regex, filename, rootDir);
 
   if (config) {
-    return checkOptions(deps, config);
+    return checkOptions(deps, callConfigIfFunction(config));
   }
 
   if (path.basename(filename) === 'package.json') {


Test patch:
diff --git a/test/special/babel.js b/test/special/babel.js
index f0af99d..c092ed0 100755
--- a/test/special/babel.js
+++ b/test/special/babel.js
@@ -164,6 +164,17 @@ describe('babel special parser', () => {
     result.should.deepEqual(['babel-preset-es2015']);
   });
 
+  it('should detect babel.config.js exporting a function', async () => {
+    const content =
+      "module.exports = api => ({ presets: ['es2015'], ignore: api.env('test') ? [] : ['**/*.test.*'] })";
+
+    const result = await testParser(content, '/path/to/babel.config.js', [
+      'babel-preset-es2015',
+      'dep',
+    ]);
+    result.should.deepEqual(['babel-preset-es2015']);
+  });
+
   it('should detect babel.config.cjs', async () => {
     const content = "module.exports = { presets: ['es2015'] }";