Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get eslint to select jsx elements that have a className with a particular class?

I am implementing eslint, and I am using 'no-restricted-syntax' to create custom selections my selection looks like this:

  'no-restricted-syntax': [
    'error',
    {
      selector: 'JSXElement JSXAttribute JSXIdentifier[name="className"] + Literal[value="bg-red"]',
      message: 'Do not set background colors directly.',
    },
  ],

I am testing the code on this test component, and it isn’t finding the error:

export function TestComponent() {
  return <button className="bg-red">Click me</button>;
}

I have also tried modifying my selector to these without luck:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// On the attribute
JSXElement JSXAttribute[name="className"][value="bg-red"]

// removing the + Literal
JSXElement JSXAttribute JSXIdentifier[name="className"][value="bg-red"]

How can I get eslint to select jsx elements that have a className containing a particular class (or even better a class that starts with bg)?

If it helps, here is the ESLint explorer

>Solution :

looking at the given ESLint explorer in json,

the selector should be JSXAttribute[name.name="className"][value.value instead of JSXAttribute[name="className"][value=

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading