eslint-plugin-smarthr 0.2.20 → 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,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.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
+
12
+ ### [0.2.21](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.20...v0.2.21) (2023-01-19)
13
+
14
+
15
+ ### Features
16
+
17
+ * a11y系ruleのコンポーネント名チェックが漏れているパターンが存在したため調整 ([#56](https://github.com/kufu/eslint-plugin-smarthr/issues/56)) ([e628426](https://github.com/kufu/eslint-plugin-smarthr/commit/e628426596e97548580780632046c2154583af3e))
18
+
5
19
  ### [0.2.20](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.19...v0.2.20) (2023-01-18)
6
20
 
7
21
 
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)
@@ -1,68 +1,71 @@
1
- const getExtendedComponentName = (node) => {
2
- if (!node.parent) {
3
- return null
4
- }
1
+ const STYLED_COMPONENTS_METHOD = 'styled'
2
+ const STYLED_COMPONENTS = `${STYLED_COMPONENTS_METHOD}-components`
5
3
 
6
- return node.parent.id?.name || getExtendedComponentName(node.parent)
7
- }
8
- const getBaseComponentName = (node) => {
9
- if (!node) {
10
- return null
11
- }
4
+ const findInvalidImportNameNode = (s) => s.type === 'ImportDefaultSpecifier' && s.local.name !== STYLED_COMPONENTS_METHOD
12
5
 
13
- if (node.type === 'CallExpression') {
14
- if (node.callee.name === 'styled') {
15
- return node.arguments[0].name
16
- }
17
- if (node.callee.object?.name === 'styled') {
18
- return node.callee.property.name
19
- }
20
- }
6
+ const generateTagFormatter = ({ context, EXPECTED_NAMES }) => {
7
+ const entriesesTagNames = Object.entries(EXPECTED_NAMES).map(([b, e]) => [ new RegExp(b), new RegExp(e) ])
21
8
 
22
- if (node?.object?.name === 'styled') {
23
- return node.property.name
24
- }
9
+ return {
10
+ ImportDeclaration: (node) => {
11
+ if (node.source.value !== STYLED_COMPONENTS) {
12
+ return
13
+ }
25
14
 
26
- return getBaseComponentName(node.parent)
27
- }
15
+ const invalidNameNode = node.specifiers.find(findInvalidImportNameNode)
28
16
 
29
- const generateTagFormatter = ({ context, EXPECTED_NAMES }) => ({
30
- ImportDeclaration: (node) => {
31
- if (node.source.value !== 'styled-components') {
32
- return
33
- }
17
+ if (invalidNameNode) {
18
+ context.report({
19
+ node: invalidNameNode,
20
+ message: `${STYLED_COMPONENTS} をimportする際は、名称が"${STYLED_COMPONENTS_METHOD}" となるようにしてください。例: "import ${STYLED_COMPONENTS_METHOD} from '${STYLED_COMPONENTS}'"`,
21
+ });
22
+ }
23
+ },
24
+ VariableDeclarator: (node) => {
25
+ if (!node.init) {
26
+ return
27
+ }
34
28
 
35
- const invalidNameNode = node.specifiers.find((s) => s.type === 'ImportDefaultSpecifier' && s.local.name !== 'styled')
29
+ const tag = node.init.tag || node.init
36
30
 
37
- if (invalidNameNode) {
38
- context.report({
39
- node: invalidNameNode,
40
- message: "styled-components をimportする際は、名称が`styled` となるようにしてください。例: `import styled from 'styled-components'`",
41
- });
42
- }
43
- },
44
- TaggedTemplateExpression: (node) => {
45
- const extended = getExtendedComponentName(node)
31
+ let base = null
46
32
 
47
- if (extended) {
48
- const base = getBaseComponentName(node.tag)
33
+ if (tag.object?.name === STYLED_COMPONENTS_METHOD) {
34
+ base = tag.property.name
35
+ } else if (tag.callee) {
36
+ const callee = tag.callee
37
+
38
+ switch (STYLED_COMPONENTS_METHOD) {
39
+ case callee.name: {
40
+ const arg = tag.arguments[0]
41
+ base = arg.name || arg.value
42
+ break
43
+ }
44
+ case callee.callee?.name: {
45
+ const arg = callee.arguments[0]
46
+ base = arg.name || arg.value
47
+ break
48
+ }
49
+ case callee.object?.name:
50
+ base = callee.property.name
51
+ break
52
+ }
53
+ }
49
54
 
50
55
  if (base) {
51
- Object.entries(EXPECTED_NAMES).forEach(([b, e]) => {
52
- if (base.match(new RegExp(b))) {
53
- const extendedregex = new RegExp(e)
56
+ const extended = node.id.name
54
57
 
55
- if (!extended.match(extendedregex)) {
56
- context.report({
57
- node: node.parent,
58
- message: `${extended}を正規表現 "${extendedregex.toString()}" がmatchする名称に変更してください`,
59
- });
60
- }
58
+ entriesesTagNames.forEach(([b, e]) => {
59
+ if (base.match(b) && !extended.match(e)) {
60
+ context.report({
61
+ node,
62
+ message: `${extended}を正規表現 "${e.toString()}" がmatchする名称に変更してください`,
63
+ });
61
64
  }
62
65
  })
63
66
  }
64
- }
65
- },
66
- })
67
+ },
68
+ }
69
+ }
67
70
 
68
71
  module.exports = { generateTagFormatter }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-smarthr",
3
- "version": "0.2.20",
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
+ })
@@ -27,6 +27,9 @@ ruleTester.run('a11y-clickable-element-has-text', rule, {
27
27
  { code: 'const HogeButton = styled(Button)``' },
28
28
  { code: 'const FugaAnchor = styled(HogeAnchor)``' },
29
29
  { code: 'const FugaSmartHRLogo = styled(SmartHRLogo)``' },
30
+ { code: 'const HogeAnchor = styled.a(() => ``)' },
31
+ { code: 'const HogeAnchor = styled("a")(() => ``)' },
32
+ { code: 'const HogeAnchor = styled(Anchor)(() => ``)' },
30
33
  {
31
34
  code: `<a>ほげ</a>`,
32
35
  },
@@ -110,7 +113,7 @@ ruleTester.run('a11y-clickable-element-has-text', rule, {
110
113
  },
111
114
  ],
112
115
  invalid: [
113
- { code: `import hoge from 'styled-components'`, errors: [ { message: "styled-components をimportする際は、名称が`styled` となるようにしてください。例: `import styled from 'styled-components'`" } ] },
116
+ { code: `import hoge from 'styled-components'`, errors: [ { message: `styled-components をimportする際は、名称が"styled" となるようにしてください。例: "import styled from 'styled-components'"` } ] },
114
117
  { code: 'const Hoge = styled.a``', errors: [ { message: `Hogeを正規表現 "/(Anchor|Link)$/" がmatchする名称に変更してください` } ] },
115
118
  { code: 'const Hoge = styled.button``', errors: [ { message: `Hogeを正規表現 "/Button$/" がmatchする名称に変更してください` } ] },
116
119
  { code: 'const Hoge = styled(Anchor)``', errors: [ { message: `Hogeを正規表現 "/Anchor$/" がmatchする名称に変更してください` } ] },
@@ -119,6 +122,10 @@ ruleTester.run('a11y-clickable-element-has-text', rule, {
119
122
  { code: 'const Fuga = styled(HogeAnchor)``', errors: [ { message: `Fugaを正規表現 "/Anchor$/" がmatchする名称に変更してください` } ] },
120
123
  { code: 'const Fuga = styled(HogeAnchor)``', errors: [ { message: `Fugaを正規表現 "/Anchor$/" がmatchする名称に変更してください` } ] },
121
124
  { code: 'const Fuga = styled(SmartHRLogo)``', errors: [ { message: `Fugaを正規表現 "/SmartHRLogo$/" がmatchする名称に変更してください` } ] },
125
+ { code: 'const Piyo = styled.a(() => ``)', errors: [ { message: `Piyoを正規表現 "/(Anchor|Link)$/" がmatchする名称に変更してください` } ] },
126
+ { code: 'const Piyo = styled("a")(() => ``)', errors: [ { message: `Piyoを正規表現 "/(Anchor|Link)$/" がmatchする名称に変更してください` } ] },
127
+ { code: 'const Piyo = styled("a")``', errors: [ { message: `Piyoを正規表現 "/(Anchor|Link)$/" がmatchする名称に変更してください` } ] },
128
+ { code: 'const Piyo = styled(Anchor)(() => ``)', errors: [ { message: `Piyoを正規表現 "/Anchor$/" がmatchする名称に変更してください` } ] },
122
129
  {
123
130
  code: `<a><img src="hoge.jpg" /></a>`,
124
131
  errors: [{ message: defaultErrorMessage }]
@@ -33,7 +33,7 @@ ruleTester.run('a11y-image-has-alt-attribute', rule, {
33
33
  { code: '<svg><image /></svg>' },
34
34
  ],
35
35
  invalid: [
36
- { code: `import hoge from 'styled-components'`, errors: [ { message: "styled-components をimportする際は、名称が`styled` となるようにしてください。例: `import styled from 'styled-components'`" } ] },
36
+ { code: `import hoge from 'styled-components'`, errors: [ { message: `styled-components をimportする際は、名称が"styled" となるようにしてください。例: "import styled from 'styled-components'"` } ] },
37
37
  { code: 'const Hoge = styled.img``', errors: [ { message: `Hogeを正規表現 "/(Img|Image|Icon)$/" がmatchする名称に変更してください` } ] },
38
38
  { code: 'const Hoge = styled.svg``', errors: [ { message: `Hogeを正規表現 "/(Img|Image|Icon)$/" がmatchする名称に変更してください` } ] },
39
39
  { code: 'const Hoge = styled(Icon)``', errors: [ { message: `Hogeを正規表現 "/Icon$/" がmatchする名称に変更してください` } ] },
@@ -33,7 +33,7 @@ ruleTester.run('a11y-input-has-name-attribute', rule, {
33
33
  { code: '<Select name="hoge[0][Fuga]" />' },
34
34
  ],
35
35
  invalid: [
36
- { code: `import hoge from 'styled-components'`, errors: [ { message: "styled-components をimportする際は、名称が`styled` となるようにしてください。例: `import styled from 'styled-components'`" } ] },
36
+ { code: `import hoge from 'styled-components'`, errors: [ { message: `styled-components をimportする際は、名称が"styled" となるようにしてください。例: "import styled from 'styled-components'"` } ] },
37
37
  { code: 'const Hoge = styled.input``', errors: [ { message: `Hogeを正規表現 "/Input$/" がmatchする名称に変更してください` } ] },
38
38
  { code: 'const Hoge = styled.Input``', errors: [ { message: `Hogeを正規表現 "/Input$/" がmatchする名称に変更してください` } ] },
39
39
  { code: 'const Hoge = styled(RadioButton)``', errors: [ { message: `Hogeを正規表現 "/RadioButton$/" がmatchする名称に変更してください` } ] },
@@ -43,14 +43,14 @@ ruleTester.run('a11y-prohibit-input-placeholder', rule, {
43
43
  { code: `<ComboBox placeholder="hoge" dropdownHelpMessage="fuga" />` },
44
44
  ],
45
45
  invalid: [
46
- { code: `import hoge from 'styled-components'`, errors: [ { message: "styled-components をimportする際は、名称が`styled` となるようにしてください。例: `import styled from 'styled-components'`" } ] },
46
+ { code: `import hoge from 'styled-components'`, errors: [ { message: `styled-components をimportする際は、名称が"styled" となるようにしてください。例: "import styled from 'styled-components'"` } ] },
47
47
  { code: 'const Hoge = styled.input``', errors: [ { message: `Hogeを正規表現 "/Input$/" がmatchする名称に変更してください` } ] },
48
48
  { code: 'const Hoge = styled(StyledInput)``', errors: [ { message: `Hogeを正規表現 "/Input$/" がmatchする名称に変更してください` } ] },
49
49
  { code: 'const Hoge = styled.textarea``', errors: [ { message: `Hogeを正規表現 "/Textarea$/" がmatchする名称に変更してください` } ] },
50
50
  { code: 'const Hoge = styled(StyledTextarea)``', errors: [ { message: `Hogeを正規表現 "/Textarea$/" がmatchする名称に変更してください` } ] },
51
51
  { code: 'const Hoge = styled(FieldSet)``', errors: [ { message: `Hogeを正規表現 "/FieldSet$/" がmatchする名称に変更してください` } ] },
52
52
  { code: 'const Hoge = styled(ComboBox)``', errors: [ { message: `Hogeを正規表現 "/ComboBox$/" がmatchする名称に変更してください` } ] },
53
- {
53
+ {
54
54
  code: 'const Hoge = styled(SearchInput)``',
55
55
  errors: [
56
56
  { message: `Hogeを正規表現 "/Input$/" がmatchする名称に変更してください` },
@@ -32,7 +32,7 @@ ruleTester.run('a11y-trigger-has-button', rule, {
32
32
  { code: '<DropdownTrigger>{hoge}</DropdownTrigger>' },
33
33
  ],
34
34
  invalid: [
35
- { code: `import hoge from 'styled-components'`, errors: [ { message: "styled-components をimportする際は、名称が`styled` となるようにしてください。例: `import styled from 'styled-components'`" } ] },
35
+ { code: `import hoge from 'styled-components'`, errors: [ { message: `styled-components をimportする際は、名称が"styled" となるようにしてください。例: "import styled from 'styled-components'"` } ] },
36
36
  { code: 'const Hoge = styled.button``', errors: [ { message: `Hogeを正規表現 "/Button$/" がmatchする名称に変更してください` } ] },
37
37
  { code: 'const Hoge = styled.a``', errors: [ { message: `Hogeを正規表現 "/(Anchor|Link)$/" がmatchする名称に変更してください` } ] },
38
38
  { code: 'const Hoge = styled(Button)``', errors: [ { message: `Hogeを正規表現 "/Button$/" がmatchする名称に変更してください` } ] },