eslint-plugin-jsx-a11y for Typescript

Viewed 1733

I am trying to setup for my typescript react project that while I am working it will give me warnings/errors if I am doing something that is not accessible. My editor already gives me listing errors, but I tried setting up eslint-plugin-jsx-a11y and I just cannot get it to work.

Here is the eslint section in my package.json

{
  "eslintConfig": {
    "parser": "@typescript-eslint/parser",
    "extends": [
      "react-app",
      "react-app/jest",
      "shared-config",
      "plugin:jsx-a11y/recommended"
    ],
    "rules": {
      "additional-rule": "warn"
    },
    "overrides": [
      {
        "files": [
          "**/*.ts?(x)"
        ],
        "rules": {
          "additional-typescript-only-rule": "warn"
        }
      }
    ]
  }
}

Not sure what I am missing. Thanks

2 Answers

What do you mean just cannot get it to work?

From the looks of it, I'm guessing you are missing the plugins key that is required to use any given plugin per eslint docs.

The eslint-plugin-jsx-a11y docs even mentions this in the usage section.

The extends key only applies a ruleset whereas the plugins key makes certain rules available, see a longer explanation here.

{
  "eslintConfig": {
    "parser": '@typescript-eslint/parser',
    "extends": [
      "react-app",
      "react-app/jest",
      "shared-config",
      "plugin:jsx-a11y/recommended"
    ],
    "plugins": [
      "jsx-a11y"
    ],
    "rules": {
      "additional-rule": "warn"
    },
    "overrides": [
      {
        "files": [
          "**/*.ts?(x)"
        ],
        "rules": {
          "additional-typescript-only-rule": "warn"
        }
      }
    ]
  }
}

If this doesn't work please elaborate on what isn't working or what errors you get.

first lets make sure you have all install this modules

"devDependencies": {
"@types/eslint": "7.28.0",
"@typescript-eslint/eslint-plugin": "4.28.2",
"@typescript-eslint/parser": "4.28.2",
"babel-eslint": "10.1.0",
"eslint": "7.30.0",
"eslint-config-airbnb-typescript": "12.3.1",
"eslint-config-react-app": "6.0.0",
"eslint-plugin-flowtype": "5.8.0",
"eslint-plugin-import": "2.23.4",
"eslint-plugin-jest": "24.3.6",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-react": "7.24.0",
"eslint-plugin-react-hooks": "4.2.0",
"eslint-plugin-testing-library": "3.10.2",
"eslint-webpack-plugin": "2.5.4",}

when you install them modules at yow scripts section add a new prop like this one

"lint": "eslint --config ./.eslintrc.js \"./src/**/*.ts\" \"./src/**/*.tsx\" --fix --color"

here is a functional example


module.exports = {
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "react-app",
        "plugin:jsx-a11y/recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:@typescript-eslint/recommended-requiring-type-checking",
    ],
    "env": {
        "browser": false,
        "es6": true,
        "node": true
    },
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "project": "./tsconfig.json",
        "ecmaVersion": 2020,
        "sourceType": "module"
    },
    "plugins": [
        "import",
        "jsx-a11y",
        "react",
        "react-hooks",
        "@typescript-eslint"
    ],
    "settings": {
        "react": {
            "createClass": "createReactClass", // Regex for Component Factory to use,
            // default to "createReactClass"
            "pragma": "React", // Pragma to use, default to "React"
            "fragment": "Fragment", // Fragment to use (may be a property of <pragma>), default to "Fragment"
            "version": "detect", // React version. "detect" automatically picks the version you have installed.
            // You can also use `16.0`, `16.3`, etc, if you want to override the detected value.
            // default to latest and warns if missing
            // It will default to "detect" in the future
            "flowVersion": "0.53" // Flow version
        },
        "propWrapperFunctions": [
            // The names of any function used to wrap propTypes, e.g. `forbidExtraProps`. If this isn"t set, any propTypes wrapped in a function will be skipped.
            "forbidExtraProps",
            {
                "property": "freeze",
                "object": "Object"
            },
            {
                "property": "myFavoriteWrapper"
            }
        ],
        "componentWrapperFunctions": [
            // The name of any function used to wrap components, e.g. Mobx `observer` function. If this isn"t set, components wrapped by these functions will be skipped.
            "observer", // `property`
            {
                "property": "styled"
            }, // `object` is optional
            {
                "property": "observer",
                "object": "Mobx"
            },
            {
                "property": "observer",
                "object": "<pragma>"
            } // sets `object` to whatever value `settings.react.pragma` is set to
        ],
        "linkComponents": [
            // Components used as alternatives to <a> for linking, eg. <Link to={ url } />
            "Hyperlink",
            {
                "name": "Link",
                "linkAttribute": "to"
            }
        ]
     },
    rules": {
// yow rules
   }
};

Related