eslint-plugin-smarthr 3.3.0 → 3.3.2
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
|
+
## [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
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* best-practice-for-rest-parametersの誤検知を修正し、エラー文言を条件に合わせて出し分けるように修正 ([#967](https://github.com/kufu/tamatebako/issues/967)) ([cf71e12](https://github.com/kufu/tamatebako/commit/cf71e1256ef6453fe99080829e6969cea88d5a92))
|
|
11
|
+
|
|
12
|
+
## [3.3.1](https://github.com/kufu/tamatebako/compare/eslint-plugin-smarthr-v3.3.0...eslint-plugin-smarthr-v3.3.1) (2025-12-19)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* smarthr/best-practice-for-rest-parametersの誤検知を修正 ([#959](https://github.com/kufu/tamatebako/issues/959)) ([12d2018](https://github.com/kufu/tamatebako/commit/12d20189c5f697b74751169925a063b8c67b4edb))
|
|
18
|
+
|
|
5
19
|
## [3.3.0](https://github.com/kufu/tamatebako/compare/eslint-plugin-smarthr-v3.2.0...eslint-plugin-smarthr-v3.3.0) (2025-12-18)
|
|
6
20
|
|
|
7
21
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-smarthr",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.2",
|
|
4
4
|
"author": "SmartHR",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "A sharable ESLint plugin for SmartHR",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"json5": "^2.2.3"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"typescript-eslint": "^8.
|
|
29
|
+
"typescript-eslint": "^8.50.0"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"eslint": "^9"
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"eslintplugin",
|
|
38
38
|
"smarthr"
|
|
39
39
|
],
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "6b6b572bcc35d4b23cb20ef6b29bedf011e3a881"
|
|
41
41
|
}
|
|
@@ -3,13 +3,16 @@
|
|
|
3
3
|
- 残余引数(rest parameters)の命名規則を設定するルールです
|
|
4
4
|
- https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Functions/rest_parameters
|
|
5
5
|
- 残余引数にはrestという名称を設定することを推奨します
|
|
6
|
-
- よく利用される `props`
|
|
6
|
+
- よく利用される `props` などを利用するとエラーになります
|
|
7
7
|
- コンポーネントが受け取れる属性を定義する際、多用される "Props" 型と勘違いされる可能性を減らすためです
|
|
8
8
|
- 残余引数の時点でコンポーネントが受け取れる属性全てではないことが確定するためpropsの利用を禁止しています
|
|
9
|
-
- `
|
|
9
|
+
- `rest` という名称に揃えることで可読性を向上させることが出来ます
|
|
10
10
|
- 残余引数以外でrestという名称と完全一致する設定することを禁止します
|
|
11
11
|
- restは rest parametersから命名されたjsのイディオムのため、残余引数以外の箇所で利用すると混乱を招くためです
|
|
12
12
|
- `xxxRest` のように他単語と組合されている場合はエラーになりません
|
|
13
|
+
- 残余引数内の属性を直接参照することを禁止します
|
|
14
|
+
- 例: `const hoge = rest.fuga`
|
|
15
|
+
- この条件を守る場合、残余引数がそのスコープ内で関心が薄い引数の集まりになり、可読性が向上します
|
|
13
16
|
|
|
14
17
|
## rules
|
|
15
18
|
|
|
@@ -24,7 +27,7 @@
|
|
|
24
27
|
## ❌ Incorrect
|
|
25
28
|
|
|
26
29
|
```js
|
|
27
|
-
// 残余引数に
|
|
30
|
+
// 残余引数にrest以外の名称が利用されているためNG
|
|
28
31
|
const hoge = (a, b, ...props) => {
|
|
29
32
|
// any
|
|
30
33
|
}
|
|
@@ -40,6 +43,17 @@ const hoge = (a, rest, b) => {
|
|
|
40
43
|
}
|
|
41
44
|
// 引数以外の場合でも混乱するためNG
|
|
42
45
|
const rest = { /* any */ }
|
|
46
|
+
|
|
47
|
+
// 残余引数内の属性を参照しているためNG
|
|
48
|
+
const Component = ({ a, b, ...rest }) => {
|
|
49
|
+
...
|
|
50
|
+
|
|
51
|
+
if (rest.abc) {
|
|
52
|
+
return <Children {...rest} />
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
...
|
|
56
|
+
}
|
|
43
57
|
```
|
|
44
58
|
|
|
45
59
|
## ✅ Correct
|
|
@@ -50,14 +64,22 @@ const rest = { /* any */ }
|
|
|
50
64
|
const hoge = (a, b, ...rest) => {
|
|
51
65
|
// any
|
|
52
66
|
}
|
|
53
|
-
// 残余引数でもprops以外は許容
|
|
54
|
-
const hoge = ({ a, b, ...actionButtonProps }) => {
|
|
55
|
-
// any
|
|
56
|
-
}
|
|
57
67
|
|
|
58
|
-
//
|
|
68
|
+
// 残余引数ではない場合、rest以外の名称を許容
|
|
59
69
|
const hoge = (props) => {
|
|
60
70
|
// any
|
|
61
71
|
}
|
|
62
72
|
const props = { /* any */ }
|
|
73
|
+
|
|
74
|
+
// 残余引数内の属性を参照しないようにコードが書かれているため許容
|
|
75
|
+
// HINT: 残余引数がそのスコープ内で関心が薄い引数の集まりになり、可読性が向上します
|
|
76
|
+
const Component = ({ a, b, abc, ...rest }) => {
|
|
77
|
+
...
|
|
78
|
+
|
|
79
|
+
if (abc) {
|
|
80
|
+
return <Children {...rest} abc={abc} />
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
...
|
|
84
|
+
}
|
|
63
85
|
```
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
const SCHEMA = []
|
|
2
2
|
|
|
3
|
+
const MEMBER_EXPRESSION_REST_REGEX = /^rest\./
|
|
4
|
+
const DETAIL_LINK = `
|
|
5
|
+
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters`
|
|
6
|
+
|
|
3
7
|
/**
|
|
4
8
|
* @type {import('@typescript-eslint/utils').TSESLint.RuleModule<''>}
|
|
5
9
|
*/
|
|
@@ -9,23 +13,36 @@ module.exports = {
|
|
|
9
13
|
schema: SCHEMA,
|
|
10
14
|
},
|
|
11
15
|
create(context) {
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
const actionNotRest = (node) => {
|
|
17
|
+
context.report({
|
|
18
|
+
node,
|
|
19
|
+
message: `残余引数以外に 'rest' という名称を利用しないでください${DETAIL_LINK}
|
|
20
|
+
- 残余引数(rest parameters)と混同する可能性があるため別の名称に修正してください`,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
const actionMemberExpressionName = (node) => {
|
|
24
|
+
if (node.parent.type === 'MemberExpression') {
|
|
25
|
+
return actionMemberExpressionName(node.parent)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (MEMBER_EXPRESSION_REST_REGEX.test(context.sourceCode.getText(node))){
|
|
14
29
|
context.report({
|
|
15
30
|
node,
|
|
16
|
-
message:
|
|
17
|
-
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters
|
|
18
|
-
- 'rest' という名称を推奨します`,
|
|
31
|
+
message: `残余引数内の属性を参照しないでください${DETAIL_LINK}`,
|
|
19
32
|
});
|
|
20
|
-
}
|
|
21
|
-
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
[`RestElement:not([argument.name='rest'])`]: (node) => {
|
|
22
38
|
context.report({
|
|
23
39
|
node,
|
|
24
|
-
message:
|
|
25
|
-
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters
|
|
26
|
-
- 残余引数(rest parameters)と混同する可能性があるため別の名称に修正してください`,
|
|
40
|
+
message: `残余引数には 'rest' という名称を指定してください${DETAIL_LINK}`,
|
|
27
41
|
});
|
|
28
42
|
},
|
|
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,
|
|
29
46
|
}
|
|
30
47
|
},
|
|
31
48
|
}
|
|
@@ -11,29 +11,39 @@ const ruleTester = new RuleTester({
|
|
|
11
11
|
},
|
|
12
12
|
})
|
|
13
13
|
|
|
14
|
-
const
|
|
15
|
-
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters
|
|
14
|
+
const DETAIL_LINK = `
|
|
15
|
+
- 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters`
|
|
16
|
+
const ERROR_REST_NAME = `残余引数には 'rest' という名称を指定してください${DETAIL_LINK}`
|
|
17
|
+
const ERROR_NOT_REST_NAME = `残余引数以外に 'rest' という名称を利用しないでください${DETAIL_LINK}
|
|
19
18
|
- 残余引数(rest parameters)と混同する可能性があるため別の名称に修正してください`
|
|
19
|
+
const ERROR_REST_CHILD_REF = `残余引数内の属性を参照しないでください${DETAIL_LINK}`
|
|
20
20
|
|
|
21
21
|
ruleTester.run('best-practice-for-rest-parameters', rule, {
|
|
22
22
|
valid: [
|
|
23
23
|
{ code: `const hoge = (a, b, ...rest) => {}` },
|
|
24
24
|
{ code: `const hoge = ({ a, b, ...rest }) => {}` },
|
|
25
|
-
{ code: `const hoge = (a, b, ...hoge) => {}` },
|
|
26
|
-
{ code: `const hoge = ({ a, b, ...xxxProps }) => {}` },
|
|
27
25
|
{ code: `const hoge = (props) => {}` },
|
|
28
26
|
{ code: `const props = {}` },
|
|
29
27
|
{ code: `const hogeRest = {}` },
|
|
28
|
+
{ code: `const hoge = { fuga: rest }` },
|
|
29
|
+
{ code: `const hoge = rest` },
|
|
30
|
+
{ code: `const hoge = hoge.rest.fuga` },
|
|
31
|
+
{ code: `const hoge = { ...rest }` },
|
|
32
|
+
{ code: `const hoge = [rest]` },
|
|
33
|
+
{ code: `hoge(rest)` },
|
|
34
|
+
{ code: `<Any {...rest} />` },
|
|
30
35
|
],
|
|
31
36
|
invalid: [
|
|
32
|
-
{ code: `const hoge = (a, b, ...props) => {}`, errors: [ { message:
|
|
33
|
-
{ code: `const hoge = ({ a, b, ...props }) => {}`, errors: [ { message:
|
|
34
|
-
{ code: `const hoge = (rest) => {}`, errors: [ { message:
|
|
35
|
-
{ code: `const hoge = (a, b, rest) => {}`, errors: [ { message:
|
|
36
|
-
{ code: `const
|
|
37
|
+
{ code: `const hoge = (a, b, ...props) => {}`, errors: [ { message: ERROR_REST_NAME } ] },
|
|
38
|
+
{ code: `const hoge = ({ a, b, ...props }) => {}`, errors: [ { message: ERROR_REST_NAME } ] },
|
|
39
|
+
{ code: `const hoge = (rest) => {}`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
40
|
+
{ code: `const hoge = (a, b, rest) => {}`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
41
|
+
{ code: `const hoge = ({ a: rest, b }) => {}`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
42
|
+
{ code: `const rest = {}`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
43
|
+
{ code: `const hoge = { rest }`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
44
|
+
{ code: `const hoge = { rest: fuga }`, errors: [ { message: ERROR_NOT_REST_NAME } ] },
|
|
45
|
+
{ code: `const hoge = rest.hoge`, errors: [ { message: ERROR_REST_CHILD_REF } ] },
|
|
46
|
+
{ code: `const hoge = rest.hoge.fuga`, errors: [ { message: ERROR_REST_CHILD_REF } ] },
|
|
37
47
|
]
|
|
38
48
|
})
|
|
39
49
|
|