eslint-plugin-smarthr 0.2.23 → 0.2.25

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,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.2.25](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.24...v0.2.25) (2023-04-04)
6
+
7
+
8
+ ### Features
9
+
10
+ * best-practice-for-remote-trigger-action-dialog ルールを追加 ([#60](https://github.com/kufu/eslint-plugin-smarthr/issues/60)) ([d2bbeef](https://github.com/kufu/eslint-plugin-smarthr/commit/d2bbeeff4d2f48133b2b44f956985cf18aae9800))
11
+
12
+ ### [0.2.24](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.23...v0.2.24) (2023-03-10)
13
+
14
+
15
+ ### Features
16
+
17
+ * next.js, react-routerのLinkコンポーネントに対応するオプションを追加 ([#59](https://github.com/kufu/eslint-plugin-smarthr/issues/59)) ([88996e8](https://github.com/kufu/eslint-plugin-smarthr/commit/88996e88dfd4c14bfaaa36594663737d47f1f029))
18
+
5
19
  ### [0.2.23](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.22...v0.2.23) (2023-03-09)
6
20
 
7
21
  ### [0.2.22](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.21...v0.2.22) (2023-01-27)
package/README.md CHANGED
@@ -7,6 +7,7 @@
7
7
  - [a11y-prohibit-input-placeholder](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/a11y-prohibit-input-placeholder)
8
8
  - [a11y-trigger-has-button](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/a11y-trigger-has-button)
9
9
  - [best-practice-for-date](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/best-practice-for-date)
10
+ - [best-practice-for-remote-trigger-action-dialog](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/best-practice-for-remote-trigger-action-dialog)
10
11
  - [format-import-path](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/format-import-path)
11
12
  - [format-translate-component](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/format-translate-component)
12
13
  - [jsx-start-with-spread-attributes](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/jsx-start-with-spread-attributes)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-smarthr",
3
- "version": "0.2.23",
3
+ "version": "0.2.25",
4
4
  "author": "SmartHR",
5
5
  "license": "MIT",
6
6
  "description": "A sharable ESLint plugin for SmartHR",
@@ -12,7 +12,13 @@
12
12
  ```js
13
13
  {
14
14
  rules: {
15
- 'smarthr/a11y-anchor-has-href-attribute': 'error', // 'warn', 'off'
15
+ 'smarthr/a11y-anchor-has-href-attribute': [
16
+ 'error', // 'warn', 'off'
17
+ // {
18
+ // nextjs: true,
19
+ // react_router: true,
20
+ // },
21
+ ]
16
22
  },
17
23
  }
18
24
  ```
@@ -32,4 +38,10 @@
32
38
  <a href="https://www.google.com/search">any</a>
33
39
  <XxxAnchor href={hoge}>any</XxxAnchor>
34
40
  <XxxLink href={undefined}>any</XxxLink>
41
+
42
+ // nextjs: true
43
+ <Link href={hoge}><a>any</a></Link>
44
+
45
+ // react_router: true
46
+ <Link to={hoge}>any</Link>
35
47
  ```
@@ -7,34 +7,84 @@ const EXPECTED_NAMES = {
7
7
  }
8
8
 
9
9
  const REGEX_TARGET = /(Anchor|Link|^a)$/
10
- const findHref = (a) => a.name?.name == 'href'
10
+ const check = (node, option) => {
11
+ let result = baseCheck(node)
12
+
13
+ if (
14
+ result && (
15
+ (option.nextjs && !nextCheck(node)) ||
16
+ (option.react_router && !reactRouterCheck(node))
17
+ )
18
+ ) {
19
+ result = null
20
+ }
21
+
22
+ return result
23
+ }
24
+ const baseCheck = (node) => {
25
+ const nodeName = node.name.name || ''
26
+
27
+ if (nodeName.match(REGEX_TARGET)) {
28
+ const href = node.attributes.find((a) => a.name?.name == 'href')
29
+
30
+ if (!href || !href.value) {
31
+ return nodeName
32
+ }
33
+ }
34
+
35
+ return false
36
+ }
37
+ const nextCheck = (node) => {
38
+ // HINT: next/link で `Link>a` という構造がありえるので直上のJSXElementを調べる
39
+ const target = node.parent.parent.openingElement
40
+
41
+ if (target) {
42
+ return baseCheck(target)
43
+ }
44
+
45
+ return false
46
+ }
47
+ const reactRouterCheck = (node) => {
48
+ const href = node.attributes.find((a) => a.name?.name == 'to')
49
+
50
+ return !href || !href.value
51
+ }
52
+
53
+ const SCHEMA = [
54
+ {
55
+ type: 'object',
56
+ properties: {
57
+ nextjs: { type: 'boolean' },
58
+ react_router: { type: 'boolean' },
59
+ },
60
+ additionalProperties: false,
61
+ }
62
+ ]
11
63
 
12
64
  module.exports = {
13
65
  meta: {
14
66
  type: 'problem',
15
- schema: [],
67
+ schema: SCHEMA,
16
68
  },
17
69
  create(context) {
70
+ const option = context.options[0] || {}
71
+
18
72
  return {
19
73
  ...generateTagFormatter({ context, EXPECTED_NAMES }),
20
74
  JSXOpeningElement: (node) => {
21
- const nodeName = node.name.name || ''
22
-
23
- if (nodeName.match(REGEX_TARGET)) {
24
- const href = node.attributes.find(findHref)
75
+ const nodeName = check(node, option)
25
76
 
26
- if (!href || !href.value) {
27
- context.report({
28
- node,
29
- message: `${nodeName} に href 属性を設定してください。
77
+ if (nodeName) {
78
+ context.report({
79
+ node,
80
+ message: `${nodeName} に href 属性を設定してください。
30
81
  - onClickなどでページ遷移する場合、href属性に遷移先のURIを設定してください。Cmd + clickなどのキーボードショートカットに対応出来ます。
31
82
  - onClickなどの動作がURLの変更を行わない場合、リンクではなくbuttonでマークアップすることを検討してください。
32
83
  - リンクを無効化することを表したい場合、href属性に undefined を設定してください。`,
33
- })
34
- }
84
+ })
35
85
  }
36
86
  },
37
87
  }
38
88
  },
39
89
  }
40
- module.exports.schema = []
90
+ module.exports.schema = SCHEMA
@@ -0,0 +1,31 @@
1
+ # smarthr/best-practice-for-remote-trigger-action-dialog
2
+
3
+ - RemoteDialogTrigger, RemoteTriggerActionDialog のベストプラクティスをチェックするルールです
4
+ - `id` もしくは `targetId` にリテラルな文字列以外が設定できなくなります
5
+ - 変数なども使えません
6
+ - これは対応するTriggerとDialogをわかりやすくするためです(検索などが容易になります)
7
+
8
+ ## rules
9
+
10
+ ```js
11
+ {
12
+ rules: {
13
+ 'smarthr/best-practice-for-remote-trigger-action-dialog': 'error', // 'warn', 'off'
14
+ },
15
+ }
16
+ ```
17
+
18
+ ## ❌ Incorrect
19
+
20
+ ```js
21
+ <RemoteDialogTrigger targetId={id}>open.</RemoteDialogTrigger>
22
+ <RemoteTriggerActionDialog {...args} id={'hoge'}>content.</RemoteTriggerActionDialog>
23
+ ```
24
+
25
+ ## ✅ Correct
26
+
27
+
28
+ ```js
29
+ <RemoteDialogTrigger targetId="help_dialog">open.</RemoteDialogTrigger>
30
+ <RemoteTriggerActionDialog {...args} id="help_dialog">content.</RemoteTriggerActionDialog>
31
+ ```
@@ -0,0 +1,41 @@
1
+ const { generateTagFormatter } = require('../../libs/format_styled_components');
2
+
3
+ const EXPECTED_NAMES = {
4
+ 'RemoteDialogTrigger$': 'RemoteDialogTrigger$',
5
+ 'RemoteTrigger(.+)Dialog$': 'RemoteTrigger(.+)Dialog$',
6
+ }
7
+
8
+ const REGEX_REMOTE_TRIGGER_DIALOG = /RemoteTrigger(.+)Dialog$/
9
+ const REGEX_REMOTE_DIALOG_TRIGGER = /RemoteDialogTrigger$/
10
+
11
+ module.exports = {
12
+ meta: {
13
+ type: 'suggestion',
14
+ schema: [],
15
+ },
16
+ create(context) {
17
+ return {
18
+ ...generateTagFormatter({ context, EXPECTED_NAMES }),
19
+ JSXOpeningElement: (node) => {
20
+ const nodeName = node.name.name || '';
21
+
22
+ const regexRemoteTriggerDialog = nodeName.match(REGEX_REMOTE_TRIGGER_DIALOG)
23
+
24
+ if (regexRemoteTriggerDialog || nodeName.match(REGEX_REMOTE_DIALOG_TRIGGER)) {
25
+ const attrName = regexRemoteTriggerDialog ? 'id' : 'targetId'
26
+ const id = node.attributes.find((a) => a.name?.name === attrName)
27
+
28
+ if (id.value.type !== 'Literal') {
29
+ context.report({
30
+ node: id,
31
+ message: `${nodeName}の${attrName}属性には直接文字列を指定してください。
32
+ - 変数などは利用できません(これは関連するTriggerとDialogを検索しやすくするためです)
33
+ - RemoteTriggerActionDialogはループやDropdown内にTriggerが存在する場合に利用してください
34
+ - ループやDropdown以外にTriggerが設定されている場合、TriggerAndActionDialogを利用してください`,
35
+ })
36
+ }
37
+ }
38
+ }
39
+ }
40
+ },
41
+ }
@@ -41,6 +41,14 @@ ruleTester.run('a11y-anchor-has-href-attribute', rule, {
41
41
  {
42
42
  code: `<Link href="hoge">ほげ</Link>`,
43
43
  },
44
+ {
45
+ code: `<Link href="hoge"><a>ほげ</a></Link>`,
46
+ options: [{ nextjs: true }],
47
+ },
48
+ {
49
+ code: `<Link to="hoge">ほげ</Link>`,
50
+ options: [{ react_router: true }],
51
+ },
44
52
  ],
45
53
  invalid: [
46
54
  { code: `import hoge from 'styled-components'`, errors: [ { message: `styled-components をimportする際は、名称が"styled" となるようにしてください。例: "import styled from 'styled-components'"` } ] },
@@ -52,5 +60,8 @@ ruleTester.run('a11y-anchor-has-href-attribute', rule, {
52
60
  { code: `<Anchor>hoge</Anchor>`, errors: [{ message: generateErrorText('Anchor') }] },
53
61
  { code: `<HogeLink>hoge</HogeLink>`, errors: [{ message: generateErrorText('HogeLink') }] },
54
62
  { code: `<HogeLink href>hoge</HogeLink>`, errors: [{ message: generateErrorText('HogeLink') }] },
63
+ { code: `<HogeLink href="hoge"><a>hoge</a></HogeLink>`, errors: [{ message: generateErrorText('a') }] },
64
+ { code: `<HogeLink><a>hoge</a></HogeLink>`, options: [{ nextjs: true }], errors: [{ message: generateErrorText('a') }] },
65
+ { code: `<HogeLink to="hoge">hoge</HogeLink>`, errors: [{ message: generateErrorText('HogeLink') }] },
55
66
  ]
56
67
  })
@@ -0,0 +1,46 @@
1
+ const rule = require('../rules/best-practice-for-remote-trigger-action-dialog')
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('best-practice-for-remote-trigger-action-dialog', rule, {
16
+ valid: [
17
+ { code: `import styled from 'styled-components'` },
18
+ { code: 'const HogeRemoteDialogTrigger = styled(RemoteDialogTrigger)``' },
19
+ { code: 'const RemoteTriggerHogeDialog = styled(RemoteTriggerActionDialog)``' },
20
+ { code: '<RemoteDialogTrigger targetId="hoge">open.</RemoteDialogTrigger>' },
21
+ { code: '<StyledRemoteDialogTrigger targetId="fuga">open.</StyledRemoteDialogTrigger>' },
22
+ { code: '<RemoteTriggerActionDialog {...args} id="hoge">content.</RemoteTriggerActionDialog>' },
23
+ { code: '<RemoteTriggerHogeDialog {...args} id="hoge">content.</RemoteTriggerHogeDialog>' },
24
+ ],
25
+ invalid: [
26
+ { code: `import hoge from 'styled-components'`, errors: [ { message: `styled-components をimportする際は、名称が"styled" となるようにしてください。例: "import styled from 'styled-components'"` } ] },
27
+ { code: 'const Hoge = styled(RemoteDialogTrigger)``', errors: [ { message: 'Hogeを正規表現 "/RemoteDialogTrigger$/" がmatchする名称に変更してください' } ] },
28
+ { code: 'const Fuga = styled(RemoteTriggerActionDialog)``', errors: [ { message: 'Fugaを正規表現 "/RemoteTrigger(.+)Dialog$/" がmatchする名称に変更してください' } ] },
29
+ { code: '<RemoteDialogTrigger targetId={hoge}>open.</RemoteDialogTrigger>', errors: [ { message: `RemoteDialogTriggerのtargetId属性には直接文字列を指定してください。
30
+ - 変数などは利用できません(これは関連するTriggerとDialogを検索しやすくするためです)
31
+ - RemoteTriggerActionDialogはループやDropdown内にTriggerが存在する場合に利用してください
32
+ - ループやDropdown以外にTriggerが設定されている場合、TriggerAndActionDialogを利用してください` } ] },
33
+ { code: '<StyledRemoteDialogTrigger targetId={"fuga"}>open.</StyledRemoteDialogTrigger>', errors: [ { message: `StyledRemoteDialogTriggerのtargetId属性には直接文字列を指定してください。
34
+ - 変数などは利用できません(これは関連するTriggerとDialogを検索しやすくするためです)
35
+ - RemoteTriggerActionDialogはループやDropdown内にTriggerが存在する場合に利用してください
36
+ - ループやDropdown以外にTriggerが設定されている場合、TriggerAndActionDialogを利用してください` } ] },
37
+ { code: '<RemoteTriggerHogeDialog {...args} id={hoge}>content.</RemoteTriggerHogeDialog>', errors: [ { message: `RemoteTriggerHogeDialogのid属性には直接文字列を指定してください。
38
+ - 変数などは利用できません(これは関連するTriggerとDialogを検索しやすくするためです)
39
+ - RemoteTriggerActionDialogはループやDropdown内にTriggerが存在する場合に利用してください
40
+ - ループやDropdown以外にTriggerが設定されている場合、TriggerAndActionDialogを利用してください` } ] },
41
+ { code: '<StyldRemoteTriggerActionDialog {...args} id={"fuga"}>content.</StyldRemoteTriggerActionDialog>', errors: [ { message: `StyldRemoteTriggerActionDialogのid属性には直接文字列を指定してください。
42
+ - 変数などは利用できません(これは関連するTriggerとDialogを検索しやすくするためです)
43
+ - RemoteTriggerActionDialogはループやDropdown内にTriggerが存在する場合に利用してください
44
+ - ループやDropdown以外にTriggerが設定されている場合、TriggerAndActionDialogを利用してください` } ] },
45
+ ]
46
+ })