eslint-plugin-smarthr 3.3.2 → 3.4.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
|
+
## [3.4.0](https://github.com/kufu/tamatebako/compare/eslint-plugin-smarthr-v3.3.2...eslint-plugin-smarthr-v3.4.0) (2025-12-24)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* best-practice-for-rest-parameters の残余引数の命名規則をrestもしくはxxxRestにすることを促すように修正 ([#973](https://github.com/kufu/tamatebako/issues/973)) ([3824818](https://github.com/kufu/tamatebako/commit/3824818a68d0ab6b2b2dc04ff5292f9d8be03d9b))
|
|
11
|
+
|
|
5
12
|
## [3.3.2](https://github.com/kufu/tamatebako/compare/eslint-plugin-smarthr-v3.3.1...eslint-plugin-smarthr-v3.3.2) (2025-12-23)
|
|
6
13
|
|
|
7
14
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-smarthr",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.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": "
|
|
40
|
+
"gitHead": "25b508c89653cc0e754e091f83d8b31346afb08e"
|
|
41
41
|
}
|
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
- コンポーネントが受け取れる属性を定義する際、多用される "Props" 型と勘違いされる可能性を減らすためです
|
|
8
8
|
- 残余引数の時点でコンポーネントが受け取れる属性全てではないことが確定するためpropsの利用を禁止しています
|
|
9
9
|
- `rest` という名称に揃えることで可読性を向上させることが出来ます
|
|
10
|
-
-
|
|
10
|
+
- restがすでに利用されており設定出来ない場合 `xxxRest` というフォーマットが利用出来ます
|
|
11
|
+
- 残余引数以外でrest、もしくはxxxRestというフォーマットの名称を設定することを禁止します
|
|
11
12
|
- restは rest parametersから命名されたjsのイディオムのため、残余引数以外の箇所で利用すると混乱を招くためです
|
|
12
|
-
- `xxxRest` のように他単語と組合されている場合はエラーになりません
|
|
13
13
|
- 残余引数内の属性を直接参照することを禁止します
|
|
14
14
|
- 例: `const hoge = rest.fuga`
|
|
15
|
+
- 例: `const { hoge } = rest`
|
|
15
16
|
- この条件を守る場合、残余引数がそのスコープ内で関心が薄い引数の集まりになり、可読性が向上します
|
|
16
17
|
|
|
17
18
|
## rules
|
|
@@ -27,7 +28,7 @@
|
|
|
27
28
|
## ❌ Incorrect
|
|
28
29
|
|
|
29
30
|
```js
|
|
30
|
-
// 残余引数にrest以外の名称が利用されているためNG
|
|
31
|
+
// 残余引数にrest、xxxRest以外の名称が利用されているためNG
|
|
31
32
|
const hoge = (a, b, ...props) => {
|
|
32
33
|
// any
|
|
33
34
|
}
|
|
@@ -37,15 +38,15 @@ const hoge = ({ a, b, ...props }) => {
|
|
|
37
38
|
// any
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
//
|
|
41
|
-
const hoge = (a,
|
|
41
|
+
// 残余引数ではない箇所で残余引数と勘違いしかねない命名がされているためNG
|
|
42
|
+
const hoge = (a, anyRest, b) => {
|
|
42
43
|
// any
|
|
43
44
|
}
|
|
44
45
|
// 引数以外の場合でも混乱するためNG
|
|
45
|
-
const
|
|
46
|
+
const hogeRest = { /* any */ }
|
|
46
47
|
|
|
47
48
|
// 残余引数内の属性を参照しているためNG
|
|
48
|
-
const
|
|
49
|
+
const ComponentA = ({ a, b, ...rest }) => {
|
|
49
50
|
...
|
|
50
51
|
|
|
51
52
|
if (rest.abc) {
|
|
@@ -54,6 +55,18 @@ const Component = ({ a, b, ...rest }) => {
|
|
|
54
55
|
|
|
55
56
|
...
|
|
56
57
|
}
|
|
58
|
+
const ComponentB = ({ a, b, ...rest }) => {
|
|
59
|
+
...
|
|
60
|
+
|
|
61
|
+
// 残余引数を構造分解することもNG
|
|
62
|
+
const { abc } = rest
|
|
63
|
+
|
|
64
|
+
if (abc) {
|
|
65
|
+
return <Children {...rest} />
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
...
|
|
69
|
+
}
|
|
57
70
|
```
|
|
58
71
|
|
|
59
72
|
## ✅ Correct
|
|
@@ -64,6 +77,9 @@ const Component = ({ a, b, ...rest }) => {
|
|
|
64
77
|
const hoge = (a, b, ...rest) => {
|
|
65
78
|
// any
|
|
66
79
|
}
|
|
80
|
+
const fuga = ({ a, b, ...anyRest }) => {
|
|
81
|
+
// any
|
|
82
|
+
}
|
|
67
83
|
|
|
68
84
|
// 残余引数ではない場合、rest以外の名称を許容
|
|
69
85
|
const hoge = (props) => {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const SCHEMA = []
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const REST_REGEX = /(^r|R)est$/
|
|
4
|
+
const MEMBER_EXPRESSION_REST_REGEX = /^(r|[a-zA-Z0-9_]+R)est\./
|
|
4
5
|
const DETAIL_LINK = `
|
|
5
6
|
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters`
|
|
6
7
|
|
|
@@ -16,9 +17,9 @@ module.exports = {
|
|
|
16
17
|
const actionNotRest = (node) => {
|
|
17
18
|
context.report({
|
|
18
19
|
node,
|
|
19
|
-
message: `残余引数以外に
|
|
20
|
+
message: `残余引数以外に ${REST_REGEX} とマッチする名称を利用しないでください${DETAIL_LINK}
|
|
20
21
|
- 残余引数(rest parameters)と混同する可能性があるため別の名称に修正してください`,
|
|
21
|
-
})
|
|
22
|
+
})
|
|
22
23
|
}
|
|
23
24
|
const actionMemberExpressionName = (node) => {
|
|
24
25
|
if (node.parent.type === 'MemberExpression') {
|
|
@@ -29,20 +30,32 @@ module.exports = {
|
|
|
29
30
|
context.report({
|
|
30
31
|
node,
|
|
31
32
|
message: `残余引数内の属性を参照しないでください${DETAIL_LINK}`,
|
|
32
|
-
})
|
|
33
|
+
})
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
return {
|
|
37
|
-
[`
|
|
38
|
+
[`ObjectPattern[properties.length=1]>RestElement`]: (node) => {
|
|
38
39
|
context.report({
|
|
39
40
|
node,
|
|
40
|
-
message:
|
|
41
|
-
})
|
|
41
|
+
message: `意味のない残余引数のため、単一の引数に変更してください${DETAIL_LINK}`,
|
|
42
|
+
})
|
|
43
|
+
},
|
|
44
|
+
[`RestElement:not([argument.name=${REST_REGEX}])`]: (node) => {
|
|
45
|
+
context.report({
|
|
46
|
+
node,
|
|
47
|
+
message: `残余引数には ${REST_REGEX} とマッチする名称を指定してください${DETAIL_LINK}`,
|
|
48
|
+
})
|
|
49
|
+
},
|
|
50
|
+
[`:not(:matches(RestElement,JSXSpreadAttribute,JSXSpreadAttribute>TSAsExpression,SpreadElement,SpreadElement>TSAsExpression,MemberExpression,VariableDeclarator,ArrayExpression,CallExpression,ObjectPattern>Property,ObjectExpression>Property))>Identifier[name=${REST_REGEX}]`]: actionNotRest,
|
|
51
|
+
[`:matches(VariableDeclarator[id.name=${REST_REGEX}],ObjectPattern>Property[value.name=${REST_REGEX}],ObjectExpression>Property[key.name=${REST_REGEX}])`]: actionNotRest,
|
|
52
|
+
[`MemberExpression[object.name=${REST_REGEX}]`]: actionMemberExpressionName,
|
|
53
|
+
[`VariableDeclarator[id.type='ObjectPattern'][init.name=${REST_REGEX}]`]: (node) => {
|
|
54
|
+
context.report({
|
|
55
|
+
node,
|
|
56
|
+
message: `残余引数内の属性を参照しないでください${DETAIL_LINK}`,
|
|
57
|
+
})
|
|
42
58
|
},
|
|
43
|
-
[`:not(:matches(RestElement,JSXSpreadAttribute,JSXSpreadAttribute>TSAsExpression,SpreadElement,SpreadElement>TSAsExpression,MemberExpression,VariableDeclarator,ArrayExpression,CallExpression,ObjectPattern>Property,ObjectExpression>Property))>Identifier[name='rest']`]: actionNotRest,
|
|
44
|
-
[`:matches(VariableDeclarator[id.name='rest'],ObjectPattern>Property[value.name='rest'],ObjectExpression>Property[key.name='rest'])`]: actionNotRest,
|
|
45
|
-
[`MemberExpression[object.name='rest']`]: actionMemberExpressionName,
|
|
46
59
|
}
|
|
47
60
|
},
|
|
48
61
|
}
|
|
@@ -13,37 +13,40 @@ const ruleTester = new RuleTester({
|
|
|
13
13
|
|
|
14
14
|
const DETAIL_LINK = `
|
|
15
15
|
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters`
|
|
16
|
-
const ERROR_REST_NAME = `残余引数には
|
|
17
|
-
const ERROR_NOT_REST_NAME = `残余引数以外に
|
|
16
|
+
const ERROR_REST_NAME = `残余引数には /(^r|R)est$/ とマッチする名称を指定してください${DETAIL_LINK}`
|
|
17
|
+
const ERROR_NOT_REST_NAME = `残余引数以外に /(^r|R)est$/ とマッチする名称を利用しないでください${DETAIL_LINK}
|
|
18
18
|
- 残余引数(rest parameters)と混同する可能性があるため別の名称に修正してください`
|
|
19
19
|
const ERROR_REST_CHILD_REF = `残余引数内の属性を参照しないでください${DETAIL_LINK}`
|
|
20
20
|
|
|
21
21
|
ruleTester.run('best-practice-for-rest-parameters', rule, {
|
|
22
22
|
valid: [
|
|
23
|
+
{ code: `const hoge = (...rest) => {}` },
|
|
23
24
|
{ code: `const hoge = (a, b, ...rest) => {}` },
|
|
24
|
-
{ code: `const hoge = ({ a, b, ...
|
|
25
|
+
{ code: `const hoge = ({ a, b, ...anyRest }) => {}` },
|
|
25
26
|
{ code: `const hoge = (props) => {}` },
|
|
26
27
|
{ code: `const props = {}` },
|
|
27
|
-
{ code: `const hogeRest = {}` },
|
|
28
28
|
{ code: `const hoge = { fuga: rest }` },
|
|
29
|
-
{ code: `const hoge =
|
|
29
|
+
{ code: `const hoge = hogeRest` },
|
|
30
30
|
{ code: `const hoge = hoge.rest.fuga` },
|
|
31
|
-
{ code: `const hoge = { ...
|
|
31
|
+
{ code: `const hoge = { ...anyRest }` },
|
|
32
32
|
{ code: `const hoge = [rest]` },
|
|
33
|
-
{ code: `hoge(
|
|
33
|
+
{ code: `hoge(fugaRest)` },
|
|
34
34
|
{ code: `<Any {...rest} />` },
|
|
35
35
|
],
|
|
36
36
|
invalid: [
|
|
37
|
-
{ code: `const hoge = (
|
|
38
|
-
{ code: `const hoge = (
|
|
37
|
+
{ code: `const hoge = ({ ...rest }) => {}`, errors: [ { message: `意味のない残余引数のため、単一の引数に変更してください${DETAIL_LINK}` } ] },
|
|
38
|
+
{ code: `const hoge = (a, b, ...any) => {}`, errors: [ { message: ERROR_REST_NAME } ] },
|
|
39
|
+
{ code: `const hoge = ({ a, b, ...restHoge }) => {}`, errors: [ { message: ERROR_REST_NAME } ] },
|
|
39
40
|
{ code: `const hoge = (rest) => {}`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
41
|
+
{ code: `const hogeRest = {}`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
40
42
|
{ code: `const hoge = (a, b, rest) => {}`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
41
|
-
{ code: `const hoge = ({ a:
|
|
43
|
+
{ code: `const hoge = ({ a: anyRest, b }) => {}`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
42
44
|
{ code: `const rest = {}`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
43
|
-
{ code: `const hoge = {
|
|
45
|
+
{ code: `const hoge = { hogeRest }`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
44
46
|
{ code: `const hoge = { rest: fuga }`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
45
47
|
{ code: `const hoge = rest.hoge`, errors: [ { message: ERROR_REST_CHILD_REF } ] },
|
|
46
|
-
{ code: `const hoge =
|
|
48
|
+
{ code: `const hoge = anyRest.hoge.fuga`, errors: [ { message: ERROR_REST_CHILD_REF } ] },
|
|
49
|
+
{ code: `const { any } = rest`, errors: [ { message: ERROR_REST_CHILD_REF } ] },
|
|
47
50
|
]
|
|
48
51
|
})
|
|
49
52
|
|