eslint-plugin-smarthr 0.2.21 → 0.2.22

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.22](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.21...v0.2.22) (2023-01-27)
6
+
7
+
8
+ ### Features
9
+
10
+ * a11y-anchor-has-href-attribute を追加 ([#57](https://github.com/kufu/eslint-plugin-smarthr/issues/57)) ([83856b1](https://github.com/kufu/eslint-plugin-smarthr/commit/83856b18907772828b69de70c30315b3ff4986c1))
11
+
5
12
  ### [0.2.21](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.20...v0.2.21) (2023-01-19)
6
13
 
7
14
 
package/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # eslint-plugin-smarthr
2
2
 
3
+ - [a11y-anchor-has-href-attribute](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/a11y-anchor-has-href-attribute)
3
4
  - [a11y-clickable-element-has-text](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/a11y-clickable-element-has-text)
4
5
  - [a11y-image-has-alt-attribute](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/a11y-image-has-alt-attribute)
5
6
  - [a11y-input-has-name-attribute](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/a11y-input-has-name-attribute)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-smarthr",
3
- "version": "0.2.21",
3
+ "version": "0.2.22",
4
4
  "author": "SmartHR",
5
5
  "license": "MIT",
6
6
  "description": "A sharable ESLint plugin for SmartHR",
@@ -0,0 +1,35 @@
1
+ # smarthr/a11y-anchor-has-href-attribute
2
+
3
+ - a, Anchor, Link コンポーネントに href 属性を設定することを促すルールです
4
+ - href が設定されていないanchor要素は `遷移先が存在しない無効化されたリンク` という扱いになります
5
+ - URLの変更を行わない場合、責務としても a より button が適切です
6
+ - URL遷移を行う場合、hrefが設定されていないとキーボード操作やコンテキストメニューからの遷移ができなくなります
7
+ - これらの操作は href属性を参照します
8
+ - 無効化されたリンクであることを表したい場合 `href={undefined}` を設定してください
9
+
10
+ ## rules
11
+
12
+ ```js
13
+ {
14
+ rules: {
15
+ 'smarthr/a11y-anchor-has-href-attribute': 'error', // 'warn', 'off'
16
+ },
17
+ }
18
+ ```
19
+
20
+ ## ❌ Incorrect
21
+
22
+ ```jsx
23
+ <a>any</a>
24
+ <XxxAnchor>any</XxxAnchor>
25
+ <XxxLink>any</XxxLink>
26
+ <XxxLink href>any</XxxLink>
27
+ ```
28
+
29
+ ## ✅ Correct
30
+
31
+ ```jsx
32
+ <a href="https://www.google.com/search">any</a>
33
+ <XxxAnchor href={hoge}>any</XxxAnchor>
34
+ <XxxLink href={undefined}>any</XxxLink>
35
+ ```
@@ -0,0 +1,40 @@
1
+ const { generateTagFormatter } = require('../../libs/format_styled_components')
2
+
3
+ const EXPECTED_NAMES = {
4
+ 'Anchor$': 'Anchor$',
5
+ 'Link$': 'Link$',
6
+ '^a$': '(Anchor|Link)$',
7
+ }
8
+
9
+ const REGEX_TARGET = /(Anchor|Link|^a)$/
10
+ const findHref = (a) => a.name?.name == 'href'
11
+
12
+ module.exports = {
13
+ meta: {
14
+ type: 'problem',
15
+ schema: [],
16
+ },
17
+ create(context) {
18
+ return {
19
+ ...generateTagFormatter({ context, EXPECTED_NAMES }),
20
+ JSXOpeningElement: (node) => {
21
+ const nodeName = node.name.name || ''
22
+
23
+ if (nodeName.match(REGEX_TARGET)) {
24
+ const href = node.attributes.find(findHref)
25
+
26
+ if (!href || !href.value) {
27
+ context.report({
28
+ node,
29
+ message: `${nodeName} に href 属性を設定してください。
30
+ - onClickなどでページ遷移する場合、href属性に遷移先のURIを設定してください。Cmd + clickなどのキーボードショートカットに対応出来ます。
31
+ - onClickなどの動作がURLの変更を行わない場合、リンクではなくbuttonでマークアップすることを検討してください。
32
+ - リンクを無効化することを表したい場合、href属性に undefined を設定してください。`,
33
+ })
34
+ }
35
+ }
36
+ },
37
+ }
38
+ },
39
+ }
40
+ module.exports.schema = []
@@ -0,0 +1,56 @@
1
+ const rule = require('../rules/a11y-anchor-has-href-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
+ const generateErrorText = (name) => `${name} に href 属性を設定してください。
16
+ - onClickなどでページ遷移する場合、href属性に遷移先のURIを設定してください。Cmd + clickなどのキーボードショートカットに対応出来ます。
17
+ - onClickなどの動作がURLの変更を行わない場合、リンクではなくbuttonでマークアップすることを検討してください。
18
+ - リンクを無効化することを表したい場合、href属性に undefined を設定してください。`
19
+
20
+ ruleTester.run('a11y-anchor-has-href-attribute', rule, {
21
+ valid: [
22
+ { code: `import styled from 'styled-components'` },
23
+ { code: `import styled, { css } from 'styled-components'` },
24
+ { code: `import { css } from 'styled-components'` },
25
+ { code: 'const HogeAnchor = styled.a``' },
26
+ { code: 'const HogeLink = styled.a``' },
27
+ { code: 'const HogeAnchor = styled(Anchor)``' },
28
+ { code: 'const HogeLink = styled(Link)``' },
29
+ {
30
+ code: `<a href="hoge">ほげ</a>`,
31
+ },
32
+ {
33
+ code: `<a href={hoge}>ほげ</a>`,
34
+ },
35
+ {
36
+ code: `<a href={undefined}>ほげ</a>`,
37
+ },
38
+ {
39
+ code: `<HogeAnchor href={hoge}>ほげ</HogeAnchor>`,
40
+ },
41
+ {
42
+ code: `<Link href="hoge">ほげ</Link>`,
43
+ },
44
+ ],
45
+ invalid: [
46
+ { code: `import hoge from 'styled-components'`, errors: [ { message: `styled-components をimportする際は、名称が"styled" となるようにしてください。例: "import styled from 'styled-components'"` } ] },
47
+ { code: 'const Hoge = styled.a``', errors: [ { message: `Hogeを正規表現 "/(Anchor|Link)$/" がmatchする名称に変更してください` } ] },
48
+ { code: 'const Hoge = styled(Anchor)``', errors: [ { message: `Hogeを正規表現 "/Anchor$/" がmatchする名称に変更してください` } ] },
49
+ { code: 'const Hoge = styled(Link)``', errors: [ { message: `Hogeを正規表現 "/Link$/" がmatchする名称に変更してください` } ] },
50
+ { code: `<a></a>`, errors: [{ message: generateErrorText('a') }] },
51
+ { code: `<a>hoge</a>`, errors: [{ message: generateErrorText('a') }] },
52
+ { code: `<Anchor>hoge</Anchor>`, errors: [{ message: generateErrorText('Anchor') }] },
53
+ { code: `<HogeLink>hoge</HogeLink>`, errors: [{ message: generateErrorText('HogeLink') }] },
54
+ { code: `<HogeLink href>hoge</HogeLink>`, errors: [{ message: generateErrorText('HogeLink') }] },
55
+ ]
56
+ })