-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbest-practice-for-interactive-element.js
More file actions
91 lines (86 loc) · 5.23 KB
/
best-practice-for-interactive-element.js
File metadata and controls
91 lines (86 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const rule = require('../rules/best-practice-for-interactive-element')
const RuleTester = require('eslint').RuleTester
const ruleTester = new RuleTester({
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
})
const INTERACTIVE_COMPONENT_NAMES = `(${[
'(ActionDialogWith|RemoteDialog)Trigger(s)?',
'(B|b)utton(s)?',
'(Check|Combo)(B|b)ox(es|s)?',
'(Date(timeLocal)?|Time|Month|Wareki)Picker(s)?',
'(F|f)orm(Control|Group|Dialog)?(s)?',
'(I|i)nput(File)?(s)?',
'(L|l)egend(s)$',
'(S|s)elect(s)?',
'(T|t)extarea(s)?',
'Accordion(Panel)?(s)?',
'Anchor',
'DisclosureTrigger?',
'DropZone(s)?',
'Field(S|s)et(s)?',
'FilterDropdown(s)?',
'Link(s)?',
'Pagination(s)?',
'RadioButton(Panel)?(s)?',
'RemoteTrigger(.+)Dialog(s)?',
'RightFixedNote(s)?',
'SegmentedControl(s)?',
'SideNav(s)?',
'Switch(s)?',
'TabItem(s)?',
'^a',
'^details',
'^dialog',
'^option',
'^summary',
].join('|')})$`
const INTERACTIVE_ON_REGEX = /^on(Change|Input|Focus|Blur|(Double)?Click|Key(Down|Up|Press)|Mouse(Enter|Over|Down|Up|Leave)|Select|Submit)$/
const interactiveError = (name) => `${name}にrole属性は指定しないでください。
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-interactive-element`
const uninteractiveError = (name) => `${name}にデフォルトで用意されているonXxx形式の属性は設定しないでください
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-interactive-element
- 対応方法1: 対象の属性がコンポーネント内の特定のインタラクティブな要素に設定される場合、名称を具体的なものに変更してください
- 属性名を"${INTERACTIVE_ON_REGEX}"に一致しないものに変更してください
- 例: 対象コンポーネント内に '追加ボタン' が存在する場合、'onClick' という属性名を 'onClickAddButton' に変更する
- 対応方法2: 子要素で発生したイベントを受け取ること(delegate)が目的でonXxx属性を設定している場合、イベントハンドラがdelegateを目的としている事がわかるように修正してください
- 修正例1: "onClick={onClick}" を設定している場合、 "onClick={onDelegateClick}" のようにDelegate, もしくはdelegateを含む名称に変更する
- 修正例2: "onClick={(e) => { ... }}" を設定している場合、 "onClick={(delegateEvent) => { ... }}" のように引数をdelegate, もしくはDelegateを含む名称に変更する
- 対応方法3: 対象の属性が設定されているコンポーネントがインタラクティブなコンポーネントの場合、名称を調整してください
- "${new RegExp(`(${INTERACTIVE_COMPONENT_NAMES})`)}" の正規表現にmatchするコンポーネントに変更、もしくは名称を調整してください
- 対応方法4: インタラクティブな親要素、もしくは子要素が存在する場合、onXxx属性を移動して設定することを検討してください`
ruleTester.run('best-practice-for-interactive-element', rule, {
valid: [
{ code: `<button>...</button>` },
{ code: `<InteractiveComponent>...</InteractiveComponent>`, options: [{ additionalInteractiveComponentRegex: ['^InteractiveComponent%'] }] },
{ code: `<CrewDetail onChangeName={onChange} />` },
{ code: `<Stack as="form" onSubmit={onSubmit} />` },
{ code: `<Stack any={<Button onClick={onClick} />} />` },
{ code: `<Stack onSubmit={onDelegateSubmit} />` },
{ code: `<Stack onSubmit={hoge.fuga.delegateAny.piyo} />` },
{ code: `<Stack onSubmit={(a, delegateEvent, b) => {}} />` },
{ code: `<HogeCheckbox role="switch" />` },
{ code: `<HogeInput role="switch" />` },
{ code: `<input role="combobox" />` },
{ code: `<FugaButton role="option" />` },
{ code: `<FugaButton role="menuitem" />` },
],
invalid: [
{ code: `<button role="presentation">...</button>`, errors: [{ message: interactiveError('button') }] },
{ code: `<Hoge as="form" role="menu" />`, errors: [{ message: `<Hoge as="form">にrole属性は指定しないでください。
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-interactive-element` }] },
{ code: `<FormControl role="menu" />`, errors: [{ message: interactiveError('FormControl') }] },
{ code: `<InteractiveComponent role="group">...</InteractiveComponent>`, options: [{ additionalInteractiveComponentRegex: ['^Interactive'] }], errors: [{ message: interactiveError('InteractiveComponent') }] },
{ code: `<CrewDetail onChange={onChange} />`, errors: [{ message: uninteractiveError('CrewDetail') }] },
{ code: `<Stack onSubmit={onSubmit} />`, errors: [{ message: uninteractiveError('Stack') }] },
{ code: `<HogeCheckbox role="any" />`, errors: [{ message: interactiveError('HogeCheckbox') }] },
{ code: `<HogeInput role="any" />`, errors: [{ message: interactiveError('HogeInput') }] },
{ code: `<input role="any" />`, errors: [{ message: interactiveError('input') }] },
{ code: `<FugaButton role="any" />`, errors: [{ message: interactiveError('FugaButton') }] },
]
})