eslint-plugin-smarthr 0.5.11 → 0.5.12

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 CHANGED
@@ -2,6 +2,13 @@
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.12](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.5.11...v0.5.12) (2024-06-10)
6
+
7
+
8
+ ### Features
9
+
10
+ * 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))
11
+
5
12
  ### [0.5.11](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.5.9...v0.5.11) (2024-06-06)
6
13
 
7
14
 
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-smarthr",
3
- "version": "0.5.11",
3
+ "version": "0.5.12",
4
4
  "author": "SmartHR",
5
5
  "license": "MIT",
6
6
  "description": "A sharable ESLint plugin for SmartHR",
@@ -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,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
+ })