eslint-plugin-smarthr 0.2.23 → 0.2.24

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.2.24](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.23...v0.2.24) (2023-03-10)
6
+
7
+
8
+ ### Features
9
+
10
+ * 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))
11
+
5
12
  ### [0.2.23](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.22...v0.2.23) (2023-03-09)
6
13
 
7
14
  ### [0.2.22](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.21...v0.2.22) (2023-01-27)
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.24",
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
@@ -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
  })