eslint-plugin-smarthr 0.5.11 → 0.5.13
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 +14 -0
- package/README.md +1 -0
- package/package.json +1 -1
- package/rules/best-practice-for-data-test-attribute/README.md +34 -0
- package/rules/best-practice-for-data-test-attribute/index.js +36 -0
- package/rules/design-system-guideline-prohibit-double-icons/README.md +47 -0
- package/rules/design-system-guideline-prohibit-double-icons/index.js +58 -0
- package/test/best-practice-for-data-test-attribute.js +35 -0
- package/test/design-system-guideline-prohibit-double-icons.js +39 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
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.5.13](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.5.12...v0.5.13) (2024-06-21)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* ButtonやTextLinkコンポーネントにprefix, suffixの両属性を同時に設定できないルールを追加 ([1eb7568](https://github.com/kufu/eslint-plugin-smarthr/commit/1eb75680a1125391106994532b58cc0591853711))
|
|
11
|
+
|
|
12
|
+
### [0.5.12](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.5.11...v0.5.12) (2024-06-10)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* best-practice-for-data-test-attributeを追加 ([#141](https://github.com/kufu/eslint-plugin-smarthr/issues/141)) ([30a8f00](https://github.com/kufu/eslint-plugin-smarthr/commit/30a8f003e1cdb79c709390be0322703e74de284d))
|
|
18
|
+
|
|
5
19
|
### [0.5.11](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.5.9...v0.5.11) (2024-06-06)
|
|
6
20
|
|
|
7
21
|
|
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
- [a11y-replace-unreadable-symbol](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/a11y-replace-unreadable-symbol)
|
|
16
16
|
- [a11y-trigger-has-button](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/a11y-trigger-has-button)
|
|
17
17
|
- [best-practice-for-button-element](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/best-practice-for-button-element)
|
|
18
|
+
- [best-practice-for-data-test-attribute](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/best-practice-for-data-test-attribute)
|
|
18
19
|
- [best-practice-for-date](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/best-practice-for-date)
|
|
19
20
|
- [best-practice-for-layouts](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/best-practice-for-layouts)
|
|
20
21
|
- [best-practice-for-remote-trigger-dialog](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/best-practice-for-remote-trigger-dialog)
|
package/package.json
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# smarthr/best-practice-for-data-test-attribute
|
|
2
|
+
|
|
3
|
+
- テスト用の `data-spec` と `data-testid` 属性の利用を控え、代替手段の検討を推奨するルールです。
|
|
4
|
+
- `data-spec` 等の利用を控えることで、テスト環境標準のメソッドを利用することでアクセシビリティのテストも同時に行えるためテストコードの価値が高まります。
|
|
5
|
+
|
|
6
|
+
## rules
|
|
7
|
+
|
|
8
|
+
```js
|
|
9
|
+
{
|
|
10
|
+
rules: {
|
|
11
|
+
'smarthr/best-practice-for-data-test-attribute': 'warn', // 'error', 'off'
|
|
12
|
+
},
|
|
13
|
+
}
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## ❌ Incorrect
|
|
17
|
+
|
|
18
|
+
```jsx
|
|
19
|
+
<Any data-spec="hoge">ほげ</Any>
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```jsx
|
|
23
|
+
<Any data-testid="hoge">ほげ</Any>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## ✅ Correct
|
|
27
|
+
|
|
28
|
+
```jsx
|
|
29
|
+
<Any data-hoge="true">...</Any>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```jsx
|
|
33
|
+
<Any>...</Any>
|
|
34
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const SCHEMA = []
|
|
2
|
+
|
|
3
|
+
const prohibitAttributies = [
|
|
4
|
+
"data-spec",
|
|
5
|
+
"data-testid"
|
|
6
|
+
]
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @type {import('@typescript-eslint/utils').TSESLint.RuleModule<''>}
|
|
10
|
+
*/
|
|
11
|
+
module.exports = {
|
|
12
|
+
meta: {
|
|
13
|
+
type: 'suggestion',
|
|
14
|
+
schema: SCHEMA,
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
return {
|
|
18
|
+
JSXAttribute: (node) => {
|
|
19
|
+
const hit = prohibitAttributies.find((attr) => attr === node.name.name)
|
|
20
|
+
|
|
21
|
+
if (hit) {
|
|
22
|
+
context.report({
|
|
23
|
+
node,
|
|
24
|
+
message: `テストのために要素を指定するために、${hit} 属性を利用するのではなく、他の方法で要素を指定することを検討してください。
|
|
25
|
+
- 方法1: click_link, click_button等を利用したりすることで、利用しているテスト環境に準じた方法で要素を指定することを検討してください。
|
|
26
|
+
- 参考(Testing Library): https://testing-library.com/docs/queries/about
|
|
27
|
+
- 参考(Capybara): https://rubydoc.info/github/jnicklas/capybara/Capybara/Node/Finders
|
|
28
|
+
- 方法2: テスト環境のメソッド等で要素が指定できない場合はrole属性、name属性、id属性等を利用した方法で要素を指定することを検討してください。
|
|
29
|
+
- 方法3: 上記の方法でも要素が指定できない場合は、'eslint-disable-next-line' 等を利用して、このルールを無効化してください。`,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
module.exports.schema = SCHEMA
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# smarthr/design-system-guideline-prohibit-double-icons
|
|
2
|
+
|
|
3
|
+
- 要素の前後両方にアイコンの使用を禁止するルールです
|
|
4
|
+
- `Button` や `TextLink` において、`prefix` と `suffix` が同時に設定されている場合、エラーとなります。
|
|
5
|
+
- 基本的にアイコンのみが設定される前提のルールになっていますが、文字列などが設定されている場合もエラーとなります。
|
|
6
|
+
- どちらにもアイコンをつけられそうな場合は、アイコン付き(右)(サフィックス)を優先し、アイコン付き(左)(プレフィックス)には指定しないでください。
|
|
7
|
+
|
|
8
|
+
## rules
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
{
|
|
12
|
+
rules: {
|
|
13
|
+
'smarthr/design-system-guideline-prohibit-double-icons': [
|
|
14
|
+
'error', // 'warn', 'off'
|
|
15
|
+
// { checkType: 'always' } /* 'always' || 'allow-spread-attributes' */
|
|
16
|
+
]
|
|
17
|
+
},
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## ❌ Incorrect
|
|
22
|
+
|
|
23
|
+
```jsx
|
|
24
|
+
<Button>hoge</Button>
|
|
25
|
+
<Button suffix={SUFFIX}>hoge</Button>
|
|
26
|
+
<Button prefix="PREFIX">hoge</Button>
|
|
27
|
+
<TextLink>hoge</TextLink>
|
|
28
|
+
<TextLink suffix="SUFFIX">hoge</TextLink>
|
|
29
|
+
<TextLink prefix={PREFIX}>hoge</TextLink>
|
|
30
|
+
<StyledButton>hoge</StyledButton>
|
|
31
|
+
<StyledLink>hoge</StyledLink>
|
|
32
|
+
<Input prefix={PREFIX} suffix={SUFFIX} />
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## ✅ Correct
|
|
36
|
+
|
|
37
|
+
```jsx
|
|
38
|
+
<Button>hoge</Button>
|
|
39
|
+
<Button suffix={SUFFIX}>hoge</Button>
|
|
40
|
+
<Button prefix="PREFIX">hoge</Button>
|
|
41
|
+
<TextLink>hoge</TextLink>
|
|
42
|
+
<TextLink suffix="SUFFIX">hoge</TextLink>
|
|
43
|
+
<TextLink prefix={PREFIX}>hoge</TextLink>
|
|
44
|
+
<StyledButton>hoge</StyledButton>
|
|
45
|
+
<StyledLink>hoge</StyledLink>
|
|
46
|
+
<Input prefix={PREFIX} suffix={SUFFIX} />
|
|
47
|
+
```
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const { generateTagFormatter } = require('../../libs/format_styled_components')
|
|
2
|
+
|
|
3
|
+
const EXPECTED_NAMES = {
|
|
4
|
+
'(Button)$': '(Button)$',
|
|
5
|
+
'(Link)$': '(Link)$',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const UNEXPECTED_NAMES = EXPECTED_NAMES
|
|
9
|
+
|
|
10
|
+
const SCHEMA = []
|
|
11
|
+
|
|
12
|
+
const REGEX_PATTERN = new RegExp(`(${Object.keys(EXPECTED_NAMES).join('|')})`)
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @type {import('@typescript-eslint/utils').TSESLint.RuleModule<''>}
|
|
16
|
+
*/
|
|
17
|
+
module.exports = {
|
|
18
|
+
meta: {
|
|
19
|
+
type: 'problem',
|
|
20
|
+
schema: SCHEMA,
|
|
21
|
+
},
|
|
22
|
+
create(context) {
|
|
23
|
+
return {
|
|
24
|
+
...generateTagFormatter({ context, EXPECTED_NAMES, UNEXPECTED_NAMES }),
|
|
25
|
+
JSXOpeningElement: (node) => {
|
|
26
|
+
const nodeName = node.name.name
|
|
27
|
+
|
|
28
|
+
if (REGEX_PATTERN.test(nodeName)) {
|
|
29
|
+
let prefix = null
|
|
30
|
+
let suffix = null
|
|
31
|
+
|
|
32
|
+
for (const attr of node.attributes) {
|
|
33
|
+
switch (attr.name.name) {
|
|
34
|
+
case 'prefix':
|
|
35
|
+
prefix = attr
|
|
36
|
+
break
|
|
37
|
+
case 'suffix':
|
|
38
|
+
suffix = attr
|
|
39
|
+
break
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if(prefix && suffix) {
|
|
43
|
+
context.report({
|
|
44
|
+
node,
|
|
45
|
+
message: `${nodeName} には prefix と suffix は同時に設定できません。
|
|
46
|
+
- prefix または suffix のみを設定してください。
|
|
47
|
+
- どちらにもアイコンをつけられそうな場合は、アイコン付き(右)(サフィックス)を優先し、アイコン付き(左)(プレフィックス)には指定しないでください。
|
|
48
|
+
- 両方設定したい場合は、'eslint-disable-next-line' 等を利用して、このルールを無効化してください。`,
|
|
49
|
+
})
|
|
50
|
+
break
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
}
|
|
58
|
+
module.exports.schema = SCHEMA
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const rule = require('../rules/best-practice-for-data-test-attribute')
|
|
2
|
+
const RuleTester = require('eslint').RuleTester
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
parserOptions: {
|
|
6
|
+
ecmaVersion: 12,
|
|
7
|
+
ecmaFeatures: {
|
|
8
|
+
experimentalObjectRestSpread: true,
|
|
9
|
+
jsx: true,
|
|
10
|
+
},
|
|
11
|
+
sourceType: 'module',
|
|
12
|
+
},
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const generateErrorMessage = (attr) => `テストのために要素を指定するために、${attr} 属性を利用するのではなく、他の方法で要素を指定することを検討してください。
|
|
16
|
+
- 方法1: click_link, click_button等を利用したりすることで、利用しているテスト環境に準じた方法で要素を指定することを検討してください。
|
|
17
|
+
- 参考(Testing Library): https://testing-library.com/docs/queries/about
|
|
18
|
+
- 参考(Capybara): https://rubydoc.info/github/jnicklas/capybara/Capybara/Node/Finders
|
|
19
|
+
- 方法2: テスト環境のメソッド等で要素が指定できない場合はrole属性、name属性、id属性等を利用した方法で要素を指定することを検討してください。
|
|
20
|
+
- 方法3: 上記の方法でも要素が指定できない場合は、'eslint-disable-next-line' 等を利用して、このルールを無効化してください。`
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
ruleTester.run('best-practice-for-data-test-attribute', rule, {
|
|
24
|
+
valid: [
|
|
25
|
+
{ code: '<Any>ほげ</Any>'},
|
|
26
|
+
{ code: '<Any name="hoge">ほげ</Any>'},
|
|
27
|
+
{ code: '<Any data-any="fuga">ほげ</Any>'},
|
|
28
|
+
],
|
|
29
|
+
invalid: [
|
|
30
|
+
{ code: '<Any data-spec="hijklmn">ほげ</Any>', errors: [{message: generateErrorMessage("data-spec")}] },
|
|
31
|
+
{ code: '<Any data-spec>ほげ</Any>', errors: [{message: generateErrorMessage("data-spec")}] },
|
|
32
|
+
{ code: '<Any data-testid="abcdefg">ほげ</Any>', errors: [{message: generateErrorMessage("data-testid")}] },
|
|
33
|
+
{ code: '<Any data-testid>ほげ</Any>', errors: [{message: generateErrorMessage("data-testid")}] },
|
|
34
|
+
]
|
|
35
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const rule = require('../rules/design-system-guideline-prohibit-double-icons')
|
|
2
|
+
const RuleTester = require('eslint').RuleTester
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
parserOptions: {
|
|
6
|
+
ecmaVersion: 12,
|
|
7
|
+
ecmaFeatures: {
|
|
8
|
+
experimentalObjectRestSpread: true,
|
|
9
|
+
jsx: true,
|
|
10
|
+
},
|
|
11
|
+
sourceType: 'module',
|
|
12
|
+
},
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const generateErrorText = (name) => `${name} には prefix と suffix は同時に設定できません。
|
|
16
|
+
- prefix または suffix のみを設定してください。
|
|
17
|
+
- どちらにもアイコンをつけられそうな場合は、アイコン付き(右)(サフィックス)を優先し、アイコン付き(左)(プレフィックス)には指定しないでください。
|
|
18
|
+
- 両方設定したい場合は、'eslint-disable-next-line' 等を利用して、このルールを無効化してください。`
|
|
19
|
+
|
|
20
|
+
ruleTester.run('design-system-guideline-prohibit-double-icons', rule, {
|
|
21
|
+
valid: [
|
|
22
|
+
{ code: `<Button>hoge</Button>` },
|
|
23
|
+
{ code: `<Button suffix={SUFFIX}>hoge</Button>` },
|
|
24
|
+
{ code: `<Button prefix="PREFIX">hoge</Button>` },
|
|
25
|
+
{ code: `<TextLink>hoge</TextLink>` },
|
|
26
|
+
{ code: `<TextLink suffix="SUFFIX">hoge</TextLink>` },
|
|
27
|
+
{ code: `<TextLink prefix={PREFIX}>hoge</TextLink>` },
|
|
28
|
+
{ code: `<StyledButton>hoge</StyledButton>` },
|
|
29
|
+
{ code: `<StyledLink>hoge</StyledLink>` },
|
|
30
|
+
{ code: `<Input prefix={PREFIX} suffix={SUFFIX} />` },
|
|
31
|
+
],
|
|
32
|
+
invalid: [
|
|
33
|
+
{ code: `<Button suffix={SUFFIX} prefix={PREFIX}>hoge</Button>`, errors: [{message: generateErrorText('Button')}]},
|
|
34
|
+
{ code: `<Button suffix prefix>hoge</Button>`, errors: [{message: generateErrorText('Button')}]},
|
|
35
|
+
{ code: `<StyledButton suffix={undefined} prefix={null}>hoge</StyledButton>`, errors: [{message: generateErrorText('StyledButton')}]},
|
|
36
|
+
{ code: `<Link prefix="PREFIX" suffix="SUFFIX">hoge</Link>`, errors: [{message: generateErrorText('Link')}]},
|
|
37
|
+
{ code: `<StyledLink prefix="PREFIX" suffix="SUFFIX">hoge</StyledLink>`, errors: [{message: generateErrorText('StyledLink')}]},
|
|
38
|
+
]
|
|
39
|
+
})
|