Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing to find eslint packages when overrides config is not an array #839

Open
bxt opened this issue Oct 9, 2023 · 1 comment
Open

Comments

@bxt
Copy link

bxt commented Oct 9, 2023

Bug Description

Is seems depcheck is failing to find all eslint-related packages when the overrides config of one plugin is not an array.

Somehow the MDX eslint plugin uses just an object instead of an array to speficy its linter overrides: https://github.com/mdx-js/eslint-mdx/blob/master/packages/eslint-plugin-mdx/src/configs/overrides.ts

This makes the checkConfig from the ESLint special fail with an exception:

TypeError: Found non-callable @@iterator
    at checkConfig (/bxt/foo/node_modules/depcheck/dist/special/eslint.js:98:13)
    at /bxt/foo/node_modules/depcheck/dist/special/eslint.js:113:128
    at arrayMap (/bxt/foo/node_modules/lodash/lodash.js:653:23)
    at Function.map (/bxt/foo/node_modules/lodash/lodash.js:9622:14)
    at interceptor (/bxt/foo/node_modules/lodash/lodash.js:17094:35)
    at thru (/bxt/foo/node_modules/lodash/lodash.js:8859:14)
    at /bxt/foo/node_modules/lodash/lodash.js:4430:28
    at arrayReduce (/bxt/foo/node_modules/lodash/lodash.js:697:21)
    at baseWrapperValue (/bxt/foo/node_modules/lodash/lodash.js:4429:14)
    at LazyWrapper.lazyValue [as value] (/bxt/foo/node_modules/lodash/lodash.js:1901:16)

This means that all eslint related packages are marked as unused instead of being detected from the config.

Code snippets

Where the dependency is used:

ESLint config in package.json:

{
  // ...
  "eslintConfig": {
    "root": true,
    "extends": [
      "eslint:recommended",
      // ...
    ],
    "rules": {
      // ...
    },
    "overrides": [
      {
        "files": [
          "*.md",
          "*.mdx"
        ],
        "extends": [
          "plugin:mdx/recommended"
        ],
      }
    ]
  }
}

Where the dependency is listed in package.json:

{
  "devDependencies": {
    "eslint": "^8.0.0",
    "eslint-plugin-mdx": "^2.2.0",
      // ...
  }
}

Versions

  • node -v: v18.17.1
  • npm -v: 9.1.2
  • depcheck --version: 1.4.6

Extra info

I used patch-package to patch depcheck@1.4.6 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/depcheck/dist/special/eslint.js b/node_modules/depcheck/dist/special/eslint.js
index cc2cfd3..b4c8993 100644
--- a/node_modules/depcheck/dist/special/eslint.js
+++ b/node_modules/depcheck/dist/special/eslint.js
@@ -88,7 +88,11 @@ function resolvePresetPackage(preset, rootDir) {
 function checkConfig(config, rootDir, includedDeps = []) {
   const configs = [config];
   if (config.overrides) {
-    configs.push(...config.overrides);
+    if (typeof config.overrides[Symbol.iterator] === 'function') {
+      configs.push(...config.overrides);
+    } else {
+      configs.push(config.overrides);
+    }
   }
   const plugins = (0, _lodash.default)(configs).map(value => (0, _utils.wrapToArray)(value.plugins)).flatten().map(plugin => normalizePackageName(plugin, 'eslint-plugin')).value();
   const parser = (0, _lodash.default)(configs).map(value => (0, _utils.wrapToArray)(value.parser)).flatten().value();
@rumpl
Copy link
Member

rumpl commented Oct 10, 2023

Looking at the config schema in eslint it would seem that this is an issue with MDX, you might want to open an issue over there. I'm not sure we want to handle misconfigured eslint plugins in depcheck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants