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

no-unused-vars error when using eslint-plugin-mdx without eslint-plugin-react. #444

Open
4 tasks done
chenxsan opened this issue Dec 23, 2022 · 7 comments
Open
4 tasks done

Comments

@chenxsan
Copy link
Contributor

Initial checklist

Affected packages and versions

Latest

Link to runnable example

https://stackblitz.com/edit/node-cppxmm?file=index.mdx

Steps to reproduce

  1. Open reproducible link
  2. run npx eslint ./index.mdx

Expected behavior

No error reported.

Actual behavior

/home/projects/node-cppxmm/index.mdx
  1:8  error  'MyForm' is defined but never used  no-unused-vars

Enabling eslint-plugin-react would fix the error.

Runtime

No response

Package manager

No response

OS

No response

Build and bundle tools

No response

@chenxsan
Copy link
Contributor Author

chenxsan commented Dec 23, 2022

All right, here's why eslint-plugin-react would suppress the error:

  1. https://github.com/jsx-eslint/eslint-plugin-react/blob/13d23b898de83557861b0e8bd419477f122e4839/lib/rules/jsx-uses-vars.js#L55

According to react/jsx-uses-vars, ESLint has stopped detecting variables used in JSX.

Which means if we're going to use eslint-plugin-mdx without eslint-plugin-react, we will be sured to run into this no-unused-vars error.

Hence I would suggest adding a notice to the readme, e.g.,

It's required to enable `eslint-plugin-react` to use alongside `eslint-plugin-mdx`.
Otherwise you could run into this `no-unused-vars` error.

Another option is to make the rule of react/jsx-uses-vars into mdx/recommended, but I'm not sure it's a good idea.

@JounQin JounQin pinned this issue Apr 1, 2023
@kfayelun
Copy link

I have this issue, all my Storybook MDX components like Meta, Canvas, Story gives me a no-unused-vars error that I have downgraded to warning in my eslint config. I tried adding eslint-plugin-react to my config as described above, but that did not fix it. Is this the correct way to do that:

module.exports = {
  root: true,
  extends: [
    "eslint:recommended",
    "plugin:storybook/recommended",
    "turbo",
    "prettier",
  ],
  env: {
    browser: true,
    node: true,
    es2021: true,
  },
  parserOptions: {
    ecmaVersion: "latest",
    sourceType: "module",
  },
  overrides: [
    {
      files: ["*.ts", "*.tsx"],
      parser: "@typescript-eslint/parser",
      extends: [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:storybook/recommended",
      ],
      plugins: ["@typescript-eslint"],
      rules: {
        "@typescript-eslint/no-var-requires": "warn",
      },
    },
    {
      files: ["*.md", "*.mdx"],
      extends: ["plugin:react/recommended", "plugin:mdx/recommended"],
      settings: {
        "mdx/code-blocks": false,
      },
      rules: {
        "no-unused-vars": "warn", // TODO: not sure how to fix this one for MDX
      },
    },
  ],
};

@JounQin
Copy link
Member

JounQin commented Jul 12, 2023

@kfayelun Please provide a minimal and runnable reproduction.

@JounQin
Copy link
Member

JounQin commented Aug 3, 2023

Maybe we should mark eslint-plugin-react as an optional peer dependency.

@kfayelun
Copy link

kfayelun commented Aug 4, 2023

Hey! Working on a big monorepo, producing a runnable repro is quite the task so I had to put this on the backlog. And now... when I went back to check, this isn't happening anymore :( I've restructured so much in this repo, so I'm not sure what did it. I'm sorry, I know this is not very helpful.

levino added a commit to levino/levinkeller.de that referenced this issue Aug 7, 2023
@levino
Copy link

levino commented Aug 7, 2023

I have this or a very similar issue in one of my repos. PR. Failing CI Job. I guess the best way of action would be to get @typescript-eslint to understand the usage of imports in .mdx files. But I have not idea how complex that would be.

@JounQin
Copy link
Member

JounQin commented Nov 26, 2023

@levino Sorry I didn't notice your comment previously, did you try to install eslint-plugin-react as this issue described?

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

No branches or pull requests

4 participants