eslint-plugin-smarthr 0.2.15 → 0.2.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/libs/common_domain.js +3 -1
- package/package.json +1 -1
- package/rules/a11y-radio-has-name-attribute/README.md +58 -0
- package/rules/a11y-radio-has-name-attribute/index.js +49 -0
- package/rules/prohibit-path-within-template-literal/index.js +1 -1
- package/test/a11y-radio-has-name-attribute.js +36 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.2.16](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.15...v0.2.16) (2022-12-21)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* ラジオボタンにnameを強制するルールを追加 ([97792cf](https://github.com/kufu/eslint-plugin-smarthr/commit/97792cf276e3400cb6e01a01ef4cf104de5db190))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* no-import-other-domain、require-barrel-import でignoresオプションを指定するとエラーが発生する問題を修正する ([#49](https://github.com/kufu/eslint-plugin-smarthr/issues/49)) ([8526236](https://github.com/kufu/eslint-plugin-smarthr/commit/85262365d105c1afa4fd53662825c4c51cb84531))
|
|
16
|
+
* prohibit-path-within-template-literalのpathオブジェクト名の解析の際、エラーになるパターンがあったため修正する ([#48](https://github.com/kufu/eslint-plugin-smarthr/issues/48)) ([715e485](https://github.com/kufu/eslint-plugin-smarthr/commit/715e485f3679e4ca2eb9675b67b8f452f9707096))
|
|
17
|
+
|
|
5
18
|
### [0.2.15](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.12...v0.2.15) (2022-12-15)
|
|
6
19
|
|
|
7
20
|
|
package/libs/common_domain.js
CHANGED
|
@@ -13,8 +13,9 @@ const calculateDomainContext = (context) => {
|
|
|
13
13
|
throw new Error('tsconfig.json の compilerOptions.paths に `"@/*": ["any_path/*"]` 形式でフロントエンドのroot dir を指定してください')
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
const filename = context.getFilename()
|
|
16
17
|
const parentDir = (() => {
|
|
17
|
-
const dir =
|
|
18
|
+
const dir = filename.split('/')
|
|
18
19
|
dir.pop()
|
|
19
20
|
return dir.join('/')
|
|
20
21
|
})()
|
|
@@ -23,6 +24,7 @@ const calculateDomainContext = (context) => {
|
|
|
23
24
|
return {
|
|
24
25
|
option: context.options[0],
|
|
25
26
|
parentDir,
|
|
27
|
+
filename,
|
|
26
28
|
humanizeParentDir,
|
|
27
29
|
isTarget: humanizeParentDir !== parentDir,
|
|
28
30
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# smarthr/a11y-radio-has-bane-attribute
|
|
2
|
+
|
|
3
|
+
- ラジオボタンに name 属性を設定することを強制するルールです。
|
|
4
|
+
- name を適切に設定することでラジオグループが確立され、キーボード操作しやすくなる等のメリットがあります。
|
|
5
|
+
|
|
6
|
+
## rules
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
{
|
|
10
|
+
rules: {
|
|
11
|
+
'smarthr/a11y-radio-has-name-attribute': 'error', // 'warn', 'off'
|
|
12
|
+
},
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## ❌ Incorrect
|
|
17
|
+
|
|
18
|
+
```jsx
|
|
19
|
+
<RadioButton />
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```jsx
|
|
23
|
+
<Input type="radio" />
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```jsx
|
|
27
|
+
<input type="radio" />
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```jsx
|
|
31
|
+
import styled from 'styled-components';
|
|
32
|
+
|
|
33
|
+
const StyledHoge = styled.input``;
|
|
34
|
+
const StyledFuga = styled(Input)``;
|
|
35
|
+
const StyledPiyo = styled(RadioButton)``;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## ✅ Correct
|
|
39
|
+
|
|
40
|
+
```jsx
|
|
41
|
+
<RadioButton name="hoge" />
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```jsx
|
|
45
|
+
<Input type="radio" name="hoge" />
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```jsx
|
|
49
|
+
<input type="radio" name="hoge" />
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```jsx
|
|
53
|
+
import styled from 'styled-components';
|
|
54
|
+
|
|
55
|
+
const StyledInput = styled.input``;
|
|
56
|
+
const StyledInput = styled(Input)``;
|
|
57
|
+
const StyledRadioButton = styled(RadioButton)``;
|
|
58
|
+
```
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const { generateTagFormatter } = require('../../libs/format_styled_components');
|
|
2
|
+
|
|
3
|
+
const EXPECTED_NAMES = {
|
|
4
|
+
RadioButton$: 'RadioButton$',
|
|
5
|
+
'(i|I)nput$': 'Input$',
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
meta: {
|
|
10
|
+
type: 'problem',
|
|
11
|
+
messages: {
|
|
12
|
+
'format-styled-components': '{{ message }}',
|
|
13
|
+
'a11y-radio-has-name-attribute': '{{ message }}',
|
|
14
|
+
},
|
|
15
|
+
schema: [],
|
|
16
|
+
},
|
|
17
|
+
create(context) {
|
|
18
|
+
return {
|
|
19
|
+
...generateTagFormatter({ context, EXPECTED_NAMES }),
|
|
20
|
+
JSXOpeningElement: (node) => {
|
|
21
|
+
const name = node.name.name || '';
|
|
22
|
+
const isRadio =
|
|
23
|
+
(name.match(/(i|I)nput$/) &&
|
|
24
|
+
node.attributes.some(
|
|
25
|
+
(a) => a.name?.name === 'type' && a.value.value === 'radio'
|
|
26
|
+
)) ||
|
|
27
|
+
name.match(/RadioButton$/);
|
|
28
|
+
|
|
29
|
+
if (!isRadio) return;
|
|
30
|
+
|
|
31
|
+
const hasName = node.attributes.some(
|
|
32
|
+
(attribute) => attribute?.name?.name === 'name'
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
if (!hasName) {
|
|
36
|
+
context.report({
|
|
37
|
+
node,
|
|
38
|
+
messageId: 'a11y-radio-has-name-attribute',
|
|
39
|
+
data: {
|
|
40
|
+
message:
|
|
41
|
+
'ラジオボタンにはname属性を指定してください。nameを適切に設定することでラジオグループが確立され、キーボード操作しやすくなる等のメリットがあります。',
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
module.exports.schema = [];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const recursiveFetchName = (obj, chained = '') => {
|
|
2
2
|
const o = obj.callee || obj
|
|
3
|
-
const name = o
|
|
3
|
+
const name = o?.name || o?.property?.name || ''
|
|
4
4
|
const nextChained = chained ? `${name}.${chained}` : name
|
|
5
5
|
|
|
6
6
|
if (o.property && o.object) {
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const rule = require('../rules/a11y-radio-has-name-attribute');
|
|
2
|
+
const RuleTester = require('eslint').RuleTester;
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
parserOptions: {
|
|
6
|
+
ecmaVersion: 2018,
|
|
7
|
+
ecmaFeatures: {
|
|
8
|
+
experimentalObjectRestSpread: true,
|
|
9
|
+
jsx: true,
|
|
10
|
+
},
|
|
11
|
+
sourceType: 'module',
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
ruleTester.run('a11y-radio-has-name-attribute', rule, {
|
|
16
|
+
valid: [
|
|
17
|
+
{ code: `import styled from 'styled-components'` },
|
|
18
|
+
{ code: `import styled, { css } from 'styled-components'` },
|
|
19
|
+
{ code: `import { css } from 'styled-components'` },
|
|
20
|
+
{ code: 'const HogeInput = styled.input``' },
|
|
21
|
+
{ code: 'const HogeInput = styled(Input)``' },
|
|
22
|
+
{ code: 'const HogeRadioButton = styled(RadioButton)``' },
|
|
23
|
+
{ code: '<input type="radio" name="hoge" />' },
|
|
24
|
+
{ code: '<HogeInput type="radio" name="hoge" />' },
|
|
25
|
+
{ code: '<HogeRadioButton name="hoge" />' },
|
|
26
|
+
],
|
|
27
|
+
invalid: [
|
|
28
|
+
{ code: `import hoge from 'styled-components'`, errors: [ { message: "styled-components をimportする際は、名称が`styled` となるようにしてください。例: `import styled from 'styled-components'`" } ] },
|
|
29
|
+
{ code: 'const Hoge = styled.input``', errors: [ { message: `Hogeを正規表現 "/Input$/" がmatchする名称に変更してください` } ] },
|
|
30
|
+
{ code: 'const Hoge = styled.Input``', errors: [ { message: `Hogeを正規表現 "/Input$/" がmatchする名称に変更してください` } ] },
|
|
31
|
+
{ code: 'const Hoge = styled(RadioButton)``', errors: [ { message: `Hogeを正規表現 "/RadioButton$/" がmatchする名称に変更してください` } ] },
|
|
32
|
+
{ code: '<input type="radio" />', errors: [ { message: 'ラジオボタンにはname属性を指定してください。nameを適切に設定することでラジオグループが確立され、キーボード操作しやすくなる等のメリットがあります。' } ] },
|
|
33
|
+
{ code: '<HogeInput type="radio" />', errors: [ { message: 'ラジオボタンにはname属性を指定してください。nameを適切に設定することでラジオグループが確立され、キーボード操作しやすくなる等のメリットがあります。' } ] },
|
|
34
|
+
{ code: '<HogeRadioButton />', errors: [ { message: 'ラジオボタンにはname属性を指定してください。nameを適切に設定することでラジオグループが確立され、キーボード操作しやすくなる等のメリットがあります。' } ] },
|
|
35
|
+
],
|
|
36
|
+
});
|