9

So I tried to use emotion-js for the first time and get hooked by the css prop feature.

While trying what the documentation says I get a warning from the eslint.

'jsx' is defined but never used @typescript-eslint/no-unused-vars

The script that I use looks like this.

import React from "react";
//** @jsx jsx */
import { jsx } from "@emotion/core";

export const Component = () => {
    return (
        <div css={{color: red}}>
            This is a component
        </div>
    )
}

I'm using VSCode, so I can see that this import is tagged as never used. (Has transparent color)

The jsx import is transparent (Never used)

But I did use it for my div, and if I remove the import, my css prop is showing an error.

Please help as to how to avoid this eslint warning, or at least make the VSCode recognize that jsx is being used.

Thanks!


Edit: (Adding reference)

Reference: https://emotion.sh/docs/css-prop#jsx-pragma


Edit 2: I tried adding .eslintrc file that looked like this

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "extends": ["plugin:@typescript-eslint/recommended"],
  "rules": {
    "@typescript-eslint/no-unused-vars": [
      2,
      { "vars": "all", "args": "all", "varsIgnorePattern": "^jsx$" }
    ]
  }
}

Still get the warning, did I do it wrong?

Michael Harley
  • 831
  • 1
  • 9
  • 20
  • `jsx` *is* being used, probably by https://babeljs.io/docs/en/babel-plugin-transform-react-jsx – jonrsharpe Feb 22 '20 at 18:37
  • But why is my typescript-eslint sends this warning? It doesn't seem like it recognize that jsx is being used. This jsx warnings will be disturbing when my project is becoming bigger. :s – Michael Harley Feb 22 '20 at 18:44
  • This can be easily fixed by adding `// eslint-disable-next-line`, but does anyone has a better solution? Maybe to get this jsx import to be recignized as being used by the css prop? – Michael Harley Feb 22 '20 at 18:52
  • Because the linter doesn't know that! Have you tried configuring the rule globally to ignore that name? – jonrsharpe Feb 22 '20 at 18:55
  • I will try doing that, I have never tried to change the rule tho. – Michael Harley Feb 23 '20 at 05:01
  • How do I change the eslint rule? Tried doing it but failed :( – Michael Harley Feb 23 '20 at 06:14
  • 1
    it might be the spelling on the comment, i've only gotten it to work with `/* @jsx jsx */` or `/** @jsx jsx */` with a single leading slash – worc Jun 12 '20 at 16:55

2 Answers2

1

Emotion provides its own eslint plugin that should handle this automatically for you.

Another approach is to use emotion's babel plugin to automatically add the jsx import and jsx pragma comment automatically at compile time. This way, you no longer need to add them in each of your file.

Jackyef
  • 4,734
  • 18
  • 26
  • 2
    I tried adding the plugin and using the babel plugin and it didn't stop the `'jsx' is defined but never used` warning from happening. Any other ideas or thoughts on what I might have overlooked? – AdamHinckley May 26 '21 at 22:15
  • Those kinds of warnings probably come from ESLint. Have you tried configuring ESLint to use emotion's provided ESLint plugin? Though if you are using the babel plugi already, you should be able to just remove the jsx import. – Jackyef May 28 '21 at 02:51
1
  • Install Emotion's ESlint plugin
  • Add "@emotion" to the array of plugins in your .eslintrc file
  • Remove the jsx import from your src files
  • Double-check you have the correct linter config comment at the top of your file /** @jsx jsx */ vs /** @jsxImportSource @emotion/react */ emotion css prop docs
  • Restart your app server