# 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
1. [Why should I (or my team) use it?](#why-should-i-use-it)
2. [Installation](#installation)
3. [Usage](#usage)
3. [Supported rules](#supported-rules)
## <a name="why-should-i-use-it"></a> 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.
## <a name="installation"></a> Installation
You'll first need to install [ESLint](http://eslint.org):
```
$ npm i eslint --save-dev
```
Next, install `eslint-plugin-enforce-ids-in-jsx`:
```
$ npm install eslint-plugin-enforce-ids-in-jsx --save-dev
```
**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-enforce-ids-in-jsx` globally.
## <a name="usage"></a> Usage
Add `enforce-ids-in-jsx` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
```json
{
"plugins": [
"enforce-ids-in-jsx"
]
}
```
Then configure the rules you want to use under the rules section.
```json
{
"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](#supported-rules) in ESLint. The first value is the error level of the rule and can be one of these values:
* `"off"` or `0` - turn the rule off
* `"warn"` or `1` - turn the rule on as a warning (doesn't affect exit code)
* `"error"` or `2` - 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.
## <a name="supported-rules"></a> Supported Rules
- **missing-ids**: This rule will trigger when an element is missing an `id` attribute. 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.
```javascript
// 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 `id` based on the type of element (ie. input, button, select...), and on the `type` and `name` attributes 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, if `none` is defined in the `target` array, 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 an `id` and enable the auto-fix feature. **This is still experimental!**. Default to `false`
* `priorityOverSpread`, 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 to `true`
Examples of configuration:
```javascript
"enforce-ids-in-jsx/missing-ids": ['error', {
target: ['form', 'material'],
targetCustom: ['AddressField', 'CustomCheckbox', 'MoviesListContainer'],
priorityOverSpread: false,
}],
```
```javascript
"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 `id` already set on a previous element.
> Why? This ensures that every element as an unique id and can't be confused with another element.
```javascript
// 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 `id` will not raise any warning, at least for now.
没有合适的资源?快使用搜索试试~ 我知道了~
eslint-plugin-enforce-ids-in-jsx:一小撮ESLint规则,可改善标记中id属性的使用
共22个文件
js:12个
md:2个
json:2个
需积分: 9 1 下载量 109 浏览量
2021-05-12
12:46:11
上传
评论
收藏 71KB ZIP 举报
温馨提示
eslint-plugin-enforce-ids-in-jsx 这是ESLint规则的一小部分集合,用于改善标记中id属性的使用。 目录 为什么我(或我的团队)使用它? 这些规则将帮助您在jsx标记中一致地使用id属性。 这对于编写单元测试和端到端测试很有帮助,因为在dom上具有唯一的标识符可以更轻松地定位元素。 安装 您首先需要安装 : $ npm i eslint --save-dev 接下来,安装eslint-plugin-enforce-ids-in-jsx : $ npm install eslint-plugin-enforce-ids-in-jsx --save-dev 注意:如果全局安装了ESLint(使用-g标志),则还必须全局安装eslint-plugin-enforce-ids-in-jsx 。 用法 将enforce-ids-in-jsx添加到.esl
资源详情
资源评论
资源推荐
收起资源包目录
eslint-plugin-enforce-ids-in-jsx-master.zip (22个子文件)
eslint-plugin-enforce-ids-in-jsx-master
.eslintrc 574B
src
helpers.js 2KB
constants.js 202B
index.js 573B
rules
unique-ids.js 2KB
missing-ids.js 6KB
.babelrc 114B
lib
helpers.js 4KB
constants.js 682B
index.js 571B
rules
unique-ids.js 1KB
missing-ids.js 6KB
.npmignore 6B
LICENSE 1KB
.prettierrc 91B
README.md 5KB
docs
rules
enforce-ids-on-form-element.md 654B
tests
lib
rules
unique-ids.js 937B
missing-ids.js 6KB
.gitignore 42B
package-lock.json 246KB
package.json 1KB
共 22 条
- 1
我是卖报的小砖家
- 粉丝: 20
- 资源: 4617
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0