eslint-plugin-smarthr 2.2.1 → 2.3.0

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
+ ## [2.3.0](https://github.com/kufu/tamatebako/compare/eslint-plugin-smarthr-v2.2.1...eslint-plugin-smarthr-v2.3.0) (2025-12-01)
6
+
7
+
8
+ ### Features
9
+
10
+ * best-practice-for-layout にHeading, FormControl, Fieldsetのタイトル部分でLayout系コンポーネントの仕様を制限するチェックを追加 ([#906](https://github.com/kufu/tamatebako/issues/906)) ([631aeca](https://github.com/kufu/tamatebako/commit/631aecaa20c834b6d603e31af95f2aaee9694bd0))
11
+
5
12
  ## [2.2.1](https://github.com/kufu/tamatebako/compare/eslint-plugin-smarthr-v2.2.0...eslint-plugin-smarthr-v2.2.1) (2025-11-18)
6
13
 
7
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-smarthr",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "author": "SmartHR",
5
5
  "license": "MIT",
6
6
  "description": "A sharable ESLint plugin for SmartHR",
@@ -37,5 +37,5 @@
37
37
  "eslintplugin",
38
38
  "smarthr"
39
39
  ],
40
- "gitHead": "a730418d8a507045795ce6140a58b5aea18c818b"
40
+ "gitHead": "18f89a924a8920722a1e71982a4d117db0c6a073"
41
41
  }
@@ -15,13 +15,28 @@
15
15
  ## ❌ Incorrect
16
16
 
17
17
  ```jsx
18
- // 子が複数無いためエラー
18
+ // Cluster, Stackは子要素が複数存在する場合に利用するべきもののため
19
+ // 要素が単一の場合エラーになる
19
20
  <Cluster>
20
- <Any />
21
+ <div>
22
+ hoge
23
+ </div>
21
24
  </Cluster>
22
25
  <StyledStack>
23
- {flg ? 'a' : 'b'}
26
+ <Any />
24
27
  </StyledStack>
28
+
29
+ // Heading, FormControlのlabel, Fieldsetのlegendにsmarthr-ui/Layoutsに属するコンポーネントを設置するとエラー
30
+ <Heading><Cluster><AnyIcon /><Text /></Cluster></Heading>
31
+ <FormControl label={{
32
+ text: <Text prefixIcon={<AnyIcon />}>hoge</Cluster>
33
+ }} />
34
+ <Fieldset legend={
35
+ <Stack>
36
+ <Text />
37
+ <SubText />
38
+ </Stack>
39
+ } />
25
40
  ```
26
41
 
27
42
  ## ✅ Correct
@@ -50,4 +65,33 @@
50
65
  <Any />
51
66
  <Any />
52
67
  </Cluster>
68
+
69
+ // Heading, FormControlのlabel, FieldsetのlegendにIconを設定したい場合はicon属性を利用する
70
+ <Heading icon={<AnyIcon />}><Text /></Heading>
71
+ <FormControl label={{
72
+ text: <Text />,
73
+ icon: <AnyIcon />,
74
+ }} />
75
+ // Stackはas="span"、もしくはforwardedAs="span"を指定すれば利用できる
76
+ <Fieldset legend={
77
+ <Stack as="span">
78
+ <Text />
79
+ <SubText />
80
+ </Stack>
81
+ } />
82
+ <FormControl label={{
83
+ text: (
84
+ <AnyStack forwardedAs="span">
85
+ <Text />
86
+ <SubText />
87
+ </AnyStack>
88
+ ),
89
+ }} />
90
+
91
+ // FormControl、Fieldsetで見出しの右側の領域に要素を設置する場合、statusLabels, subActionAreaを利用する
92
+ <FormControl
93
+ label={<Text />}
94
+ statusLabels={<RequiredLabel />}
95
+ subActionArea={<HelpLink />}
96
+ />
53
97
  ```
@@ -1,8 +1,16 @@
1
1
  const MULTI_CHILDREN_REGEX = /(Cluster|Stack)$/
2
-
3
2
  const REGEX_NLSP = /^\s*\n+\s*$/
4
3
  const FLEX_END_REGEX = /^(flex-)?end$/
5
4
 
5
+ const TARGET_INVALID_COMPONENT_REGEX = /(Center|Cluster|Container|Reel|Sidebar)$/
6
+ const INVALID_ELEMENT = `JSXOpeningElement[name.name=${TARGET_INVALID_COMPONENT_REGEX}]`
7
+ const HEADING_ELEMENT = 'JSXElement[openingElement.name.name=/Heading$/]'
8
+ const STACK_ELEMENT_NOT_SPAN = 'JSXOpeningElement[name.name=/Stack$/]:not(:has(JSXAttribute[name.name=/^(as|forwardedAs)$/][value.value="span"]))'
9
+ const FORM_CONTROL_LABEL_ATTRIBUTE = 'JSXOpeningElement[name.name=/FormControl$/] JSXAttribute[name.name="label"]'
10
+ const FIELDSET_LEGEND_ATTRIBUTE = 'JSXOpeningElement[name.name=/Fieldset$/] JSXAttribute[name.name="legend"]'
11
+ const ICON_ELEMENT_WITH_TEXT = `JSXOpeningElement[name.name=/Icon$/]:has(JSXAttribute[name.name="text"])`
12
+ const TEXT_ELEMENT_WITH_PREFIX = 'JSXOpeningElement[name.name=/Text$/]:has(JSXAttribute[name.name="prefixIcon"])'
13
+
6
14
  const filterFalsyJSXText = (cs) => cs.filter(checkFalsyJSXText)
7
15
  const checkFalsyJSXText = (c) => (
8
16
  !(
@@ -55,39 +63,35 @@ module.exports = {
55
63
  },
56
64
  create(context) {
57
65
  return {
58
- JSXOpeningElement: (node) => {
66
+ [`JSXOpeningElement[selfClosing=false][name.name=${MULTI_CHILDREN_REGEX}]`]: (node) => {
59
67
  const nodeName = node.name.name;
68
+ const matcher = nodeName.match(MULTI_CHILDREN_REGEX)
69
+ const layoutType = matcher[1]
70
+ let justifyAttr = null
71
+ let alignAttr = null
72
+ let gapAttr = null
60
73
 
61
- if (nodeName && !node.selfClosing) {
62
- const matcher = nodeName.match(MULTI_CHILDREN_REGEX)
63
-
64
- if (matcher) {
65
- const layoutType = matcher[1]
66
- let justifyAttr = null
67
- let alignAttr = null
68
- let gapAttr = null
69
-
70
- node.attributes.forEach((a) => {
71
- switch (a.name?.name) {
72
- case 'justify':
73
- justifyAttr = a
74
- break
75
- case 'align':
76
- alignAttr = a
77
- break
78
- case 'gap':
79
- gapAttr = a
80
- break
81
- }
82
- })
74
+ node.attributes.forEach((a) => {
75
+ switch (a.name?.name) {
76
+ case 'justify':
77
+ justifyAttr = a
78
+ break
79
+ case 'align':
80
+ alignAttr = a
81
+ break
82
+ case 'gap':
83
+ gapAttr = a
84
+ break
85
+ }
86
+ })
83
87
 
84
- if (layoutType === 'Stack') {
85
- if (alignAttr && FLEX_END_REGEX.test(alignAttr.value.value)) {
86
- return
87
- } else if (gapAttr?.value.type === 'JSXExpressionContainer' && gapAttr.value.expression.value === 0) {
88
- context.report({
89
- node,
90
- message: `${nodeName} に "gap={0}" が指定されており、smarthr-ui/${layoutType} の利用方法として誤っている可能性があります。以下の修正方法を検討してください。
88
+ if (layoutType === 'Stack') {
89
+ if (alignAttr && FLEX_END_REGEX.test(alignAttr.value.value)) {
90
+ return
91
+ } else if (gapAttr?.value.type === 'JSXExpressionContainer' && gapAttr.value.expression.value === 0) {
92
+ context.report({
93
+ node,
94
+ message: `${nodeName} に "gap={0}" が指定されており、smarthr-ui/${layoutType} の利用方法として誤っている可能性があります。以下の修正方法を検討してください。
91
95
  - 方法1: 子要素を一つにまとめられないか検討してください
92
96
  - 例: "<Stack gap={0}><p>hoge</p><p>fuga</p></Stack>" を "<p>hoge<br />fuga</p>" にするなど
93
97
  - 方法2: 子要素のstyleを確認しgap属性を0以外にできないか検討してください
@@ -95,32 +99,86 @@ module.exports = {
95
99
  - 方法3: 別要素でマークアップし直すか、${nodeName}を削除してください
96
100
  - 親要素に smarthr-ui/Cluster, smarthr-ui/Stack などが存在している場合、div・spanなどで1要素にまとめる必要がある場合があります
97
101
  - as, forwardedAsなどでSectioningContent系要素に変更している場合、対応するsmarthr-ui/Section, Aside, Nav, Article のいずれかに差し替えてください`
98
- })
99
- }
100
- }
102
+ })
103
+ }
104
+ }
101
105
 
102
- const children = node.parent.children.filter(checkFalsyJSXText)
106
+ const children = node.parent.children.filter(checkFalsyJSXText)
103
107
 
104
- if (children.length === 1) {
105
- if (justifyAttr && FLEX_END_REGEX.test(justifyAttr.value.value)) {
106
- return
107
- }
108
+ if (children.length === 1) {
109
+ if (justifyAttr && FLEX_END_REGEX.test(justifyAttr.value.value)) {
110
+ return
111
+ }
108
112
 
109
- if (searchChildren(children[0])) {
110
- context.report({
111
- node,
112
- message:
113
- (justifyAttr?.value.value === 'center' || alignAttr?.value.value === 'center')
114
- ? `${nodeName} は smarthr-ui/${layoutType} ではなく smarthr-ui/Center でマークアップしてください`
115
- : `${nodeName}には子要素が一つしか無いため、${layoutType}でマークアップする意味がありません。
113
+ if (searchChildren(children[0])) {
114
+ context.report({
115
+ node,
116
+ message:
117
+ (justifyAttr?.value.value === 'center' || alignAttr?.value.value === 'center')
118
+ ? `${nodeName} は smarthr-ui/${layoutType} ではなく smarthr-ui/Center でマークアップしてください`
119
+ : `${nodeName}には子要素が一つしか無いため、${layoutType}でマークアップする意味がありません。
116
120
  - styleを確認し、div・spanなど、別要素でマークアップし直すか、${nodeName}を削除してください
117
121
  - as, forwardedAsなどでSectioningContent系要素に変更している場合、対応するsmarthr-ui/Section, Aside, Nav, Article のいずれかに差し替えてください`
118
- })
119
- }
120
- }
122
+ })
121
123
  }
122
124
  }
123
125
  },
126
+ [`${HEADING_ELEMENT} ${INVALID_ELEMENT}`]: (node) => {
127
+ const component = node.name.name.match(TARGET_INVALID_COMPONENT_REGEX)[1]
128
+
129
+ context.report({
130
+ node,
131
+ message: `Headingの子孫に${component}を置くことはできません。Headingの外で${component}を使用するようにマークアップを修正してください。`
132
+ })
133
+ },
134
+ [`${HEADING_ELEMENT} ${STACK_ELEMENT_NOT_SPAN}`]: (node) => {
135
+ context.report({
136
+ node,
137
+ message: 'Headingの子孫にStackを置く場合、as属性、もしくはforwardedAs属性に `span` を指定してください',
138
+ })
139
+ },
140
+ [`${HEADING_ELEMENT} :matches(${ICON_ELEMENT_WITH_TEXT},${TEXT_ELEMENT_WITH_PREFIX})`]: (node) => {
141
+ context.report({
142
+ node,
143
+ message: 'HeadingにIconを設定する場合 <Heading icon={<XxxIcon />}></Heading> のようにicon属性を利用してください',
144
+ })
145
+ },
146
+ [`${FORM_CONTROL_LABEL_ATTRIBUTE} ${INVALID_ELEMENT}`]: (node) => {
147
+ context.report({
148
+ node,
149
+ message: `FormControlのlabel属性に${node.name.name.match(TARGET_INVALID_COMPONENT_REGEX)[1]}を置くことはできません。ラベル用テキスト以外をstatusLabels、subActionArea、もしくはlabel属性のObjectとして '{ text: テキスト, icon: <XxxIcon /> }'に置き換えてください。`,
150
+ })
151
+ },
152
+ [`${FORM_CONTROL_LABEL_ATTRIBUTE} ${STACK_ELEMENT_NOT_SPAN}`]: (node) => {
153
+ context.report({
154
+ node,
155
+ message: 'FormControlのlabel属性にStackを置く場合、as属性、もしくはforwardedAs属性に `span` を指定してください',
156
+ })
157
+ },
158
+ [`${FORM_CONTROL_LABEL_ATTRIBUTE} :matches(${ICON_ELEMENT_WITH_TEXT},${TEXT_ELEMENT_WITH_PREFIX})`]: (node) => {
159
+ context.report({
160
+ node,
161
+ message: "FormControlのlabel属性にアイコンを設定する場合 <FormControl label={{ text: 'テキスト', icon: <XxxIcon /> }} /> のようにlabel.icon属性を利用してください",
162
+ })
163
+ },
164
+ [`${FIELDSET_LEGEND_ATTRIBUTE} ${INVALID_ELEMENT}`]: (node) => {
165
+ context.report({
166
+ node,
167
+ message: `Fieldsetのlegend属性に${node.name.name.match(TARGET_INVALID_COMPONENT_REGEX)[1]}を置くことはできません。ラベル用テキスト以外をstatusLabels、subActionArea、もしくはlabel属性のObjectとして '{ text: テキスト, icon: <XxxIcon /> }'に置き換えてください。`,
168
+ })
169
+ },
170
+ [`${FIELDSET_LEGEND_ATTRIBUTE} ${STACK_ELEMENT_NOT_SPAN}`]: (node) => {
171
+ context.report({
172
+ node,
173
+ message: 'Fieldsetのlegend属性にStackを置く場合、as属性、もしくはforwardedAs属性に `span` を指定してください',
174
+ })
175
+ },
176
+ [`${FIELDSET_LEGEND_ATTRIBUTE} :matches(${ICON_ELEMENT_WITH_TEXT},${TEXT_ELEMENT_WITH_PREFIX})`]: (node) => {
177
+ context.report({
178
+ node,
179
+ message: "Fieldsetのlegend属性にアイコンを設定する場合 <Fieldset legend={{ text: 'テキスト', icon: <XxxIcon /> }} /> のようにlegend.icon属性を利用してください",
180
+ })
181
+ },
124
182
  }
125
183
  },
126
184
  }
@@ -58,6 +58,12 @@ ruleTester.run('best-practice-for-button-element', rule, {
58
58
  { code: `<Cluster>{a}</Cluster>` },
59
59
  { code: `<Cluster>{a.b}</Cluster>` },
60
60
  { code: `<Cluster>{a ? b : c}</Cluster>` },
61
+ { code: `<Heading><Stack as="span"><Hoge /><Fuga /></Stack></Heading>` },
62
+ { code: `<AnyHeading icon={<AnyIcon />}>hoge</AnyHeading>` },
63
+ { code: `<FormControl label={<AnyStack as="span"><span>a</span><span>b</span></AnyStack>} />` },
64
+ { code: `<AnyFormControl label={{ text: 'hoge', icon: <AnyIcon /> }} />` },
65
+ { code: `<AnyFieldset legend={{ text: <AnyStack as="span"><span>a</span><span>b</span></AnyStack> }} />` },
66
+ { code: `<Fieldset legend={{ text: 'hoge', icon: <AnyIcon /> }} />` },
61
67
  ],
62
68
  invalid: [
63
69
  { code: `<Stack><Hoge /></Stack>`, errors: [ { message: errorMessage('Stack', 'Stack') } ] },
@@ -90,6 +96,18 @@ ruleTester.run('best-practice-for-button-element', rule, {
90
96
  - 方法3: 別要素でマークアップし直すか、HogeStackを削除してください
91
97
  - 親要素に smarthr-ui/Cluster, smarthr-ui/Stack などが存在している場合、div・spanなどで1要素にまとめる必要がある場合があります
92
98
  - as, forwardedAsなどでSectioningContent系要素に変更している場合、対応するsmarthr-ui/Section, Aside, Nav, Article のいずれかに差し替えてください` } ] },
99
+ { code: `<Heading><Cluster><Hoge /><Fuga /></Cluster></Heading>`, errors: [ { message: `Headingの子孫にClusterを置くことはできません。Headingの外でClusterを使用するようにマークアップを修正してください。` } ] },
100
+ { code: `<AnyHeading><AnyStack><Hoge /><Fuga /></AnyStack></AnyHeading>`, errors: [ { message: `Headingの子孫にStackを置く場合、as属性、もしくはforwardedAs属性に \`span\` を指定してください` } ] },
101
+ { code: `<Heading><AnyIcon text="hoge" /></Heading>`, errors: [ { message: `HeadingにIconを設定する場合 <Heading icon={<XxxIcon />}></Heading> のようにicon属性を利用してください` } ] },
102
+ { code: `<AnyHeading><Text prefixIcon={<SomeIcon />}>hoge</Text></AnyHeading>`, errors: [ { message: `HeadingにIconを設定する場合 <Heading icon={<XxxIcon />}></Heading> のようにicon属性を利用してください` } ] },
103
+ { code: `<AnyFormControl label={{ text: <Cluster><Hoge /><Fuga /></Cluster> }} />`, errors: [ { message: `FormControlのlabel属性にClusterを置くことはできません。ラベル用テキスト以外をstatusLabels、subActionArea、もしくはlabel属性のObjectとして '{ text: テキスト, icon: <XxxIcon /> }'に置き換えてください。` } ] },
104
+ { code: `<FormControl label={<AnyStack><Hoge /><Fuga /></AnyStack>} />`, errors: [ { message: `FormControlのlabel属性にStackを置く場合、as属性、もしくはforwardedAs属性に \`span\` を指定してください` } ] },
105
+ { code: `<AnyFormControl label={{ text: <AnyIcon text="hoge" /> }} />`, errors: [ { message: `FormControlのlabel属性にアイコンを設定する場合 <FormControl label={{ text: 'テキスト', icon: <XxxIcon /> }} /> のようにlabel.icon属性を利用してください` } ] },
106
+ { code: `<FormControl label={{ text: <Text prefixIcon={<SomeIcon /> }>hoge</Text>}} />`, errors: [ { message: `FormControlのlabel属性にアイコンを設定する場合 <FormControl label={{ text: 'テキスト', icon: <XxxIcon /> }} /> のようにlabel.icon属性を利用してください` } ] },
107
+ { code: `<Fieldset legend={<Cluster><Hoge /><Fuga /></Cluster>} />`, errors: [ { message: `Fieldsetのlegend属性にClusterを置くことはできません。ラベル用テキスト以外をstatusLabels、subActionArea、もしくはlabel属性のObjectとして '{ text: テキスト, icon: <XxxIcon /> }'に置き換えてください。` } ] },
108
+ { code: `<AnyFieldset legend={{ text: <AnyStack><Hoge /><Fuga /></AnyStack> }} />`, errors: [ { message: `Fieldsetのlegend属性にStackを置く場合、as属性、もしくはforwardedAs属性に \`span\` を指定してください` } ] },
109
+ { code: `<Fieldset legend={<AnyIcon text="hoge" />} />`, errors: [ { message: `Fieldsetのlegend属性にアイコンを設定する場合 <Fieldset legend={{ text: 'テキスト', icon: <XxxIcon /> }} /> のようにlegend.icon属性を利用してください` } ] },
110
+ { code: `<AnyFieldset legend={{ text: <Text prefixIcon={<SomeIcon />}>hoge</Text>}} />`, errors: [ { message: `Fieldsetのlegend属性にアイコンを設定する場合 <Fieldset legend={{ text: 'テキスト', icon: <XxxIcon /> }} /> のようにlegend.icon属性を利用してください` } ] },
93
111
  ]
94
112
  })
95
113