eslint-plugin-enforce-ids-in-jsx v1.0.0
eslint-plugin-enforce-ids-in-jsx
This is a small collection of ESLint rules to improve usage of id attributes in your markup.
Table of Contents
Why should I (or my team) use it?
These rules will help you in using id attributes consistently in your jsx markup. This is helpful for writing unit and end-to-end testing, as having unique identifiers on the dom makes it easier to target elements.
Installation
You'll first need to install ESLint:
$ npm i eslint --save-devNext, install eslint-plugin-enforce-ids-in-jsx:
$ npm install eslint-plugin-enforce-ids-in-jsx --save-devNote: If you installed ESLint globally (using the -g flag) then you must also install eslint-plugin-enforce-ids-in-jsx globally.
Usage
Add enforce-ids-in-jsx to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:
{
"plugins": [
"enforce-ids-in-jsx"
]
}Then configure the rules you want to use under the rules section.
{
"rules": {
"enforce-ids-in-jsx/missing-ids": 2,
"enforce-ids-in-jsx/unique-ids": 2,
}
}The names "missing-ids" and "unique-ids" are the names of rules in ESLint. The first value is the error level of the rule and can be one of these values:
"off"or0- turn the rule off"warn"or1- turn the rule on as a warning (doesn't affect exit code)"error"or2- turn the rule on as an error (exit code will be 1)
If you're using a linter in your CI process, you may want to set the error level to 1 for some time to prevent your pipeline from breaking immediatly.
The second value is an optional object to set options on the rule.
Supported Rules
missing-ids: This rule will trigger when an element is missing an
idattribute. By default, it will only target form elements, but you can adjust the options to target any element you need (React components included).Why? This ensures that every form element in your layout can be identified easily.
// bad <input type="text" name="username" /> <button type="submit">Submit me!</button> // good <input type="text" name="username" id="usernameTextInput" /> <button type="submit" id="submitButton">Submit me!</button>Using the auto-fix feature will add an
idbased on the type of element (ie. input, button, select...), and on thetypeandnameattributes if possible.Options for this rule are:
"target", an array that take values from this list:all,form,material,none. Default to['form']all: will trigger the rule on EVERY element
form: will trigger the rule on basic form elements
material: will trigger the rule on these Material components: "NativeSelect", "Select", "MenuItem", "Button", "IconButton", "Checkbox", "Radio", "Slider", "Switch", "TextField", "Input", "OutlinedInput", "Modal"
none: will prevent the rule to trigger on any element except those defined in the second option,customTarget. Be careful, ifnoneis defined in thetargetarray, it will override the rest of the values!
targetCustom, an array that take any value you want (basic html elements, custom components, etc). Default to[]suggestionsEnabled, a boolean. If set to true, the rule will provide a suggestion for anidand enable the auto-fix feature. This is still experimental!. Default tofalsepriorityOverSpread, a boolean. If set to false, the rule won't trigger on any element where there is a spread operator. It may be useful in case you're already using{...rest}to pass ids and don't want eslint to warn you about false-positives. Default totrueExamples of configuration:
"enforce-ids-in-jsx/missing-ids": ['error', { target: ['form', 'material'], targetCustom: ['AddressField', 'CustomCheckbox', 'MoviesListContainer'], priorityOverSpread: false, }],"enforce-ids-in-jsx/missing-ids": ['warning', { target: ['none'], targetCustom: ['ul', 'ol', 'header', 'footer', 'section'], priorityOverSpread: false, }],
unique-ids: This rule will trigger when a form element is using an
idalready set on a previous element.Why? This ensures that every element as an unique id and can't be confused with another element.
// bad <input type="text" name="favoriteCake" id="favorite" /> <select name="favoriteGame" id="favorite"></select> // good <input type="text" name="favoriteCake" id="favoriteCakeTextInput" /> <select name="favoriteGame" id="favoriteGameSelect"></select>:warning: Currently, this rule will only alert you on literal strings! Using a variable as an
idwill not raise any warning, at least for now.
6 years ago