eslint-plugin-smarthr 3.3.1 → 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,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.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
+
5
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)
6
13
 
7
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-smarthr",
3
- "version": "3.3.1",
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.49.0"
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": "7390a378bca7e0f44fa90cb2a983cc807dc6c403"
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
- - `xxxProps` のように他単語と組合されている場合はエラーになりません
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
- // 残余引数にpropsという名称が設定されているためNG
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
- // 残余引数以外の場合はpropsを許容
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,26 +13,36 @@ module.exports = {
9
13
  schema: SCHEMA,
10
14
  },
11
15
  create(context) {
12
- const restAction = (node) => {
16
+ const actionNotRest = (node) => {
13
17
  context.report({
14
18
  node,
15
- message: `残余引数以外に 'rest' という名称を利用しないでください
16
- - 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters
19
+ message: `残余引数以外に 'rest' という名称を利用しないでください${DETAIL_LINK}
17
20
  - 残余引数(rest parameters)と混同する可能性があるため別の名称に修正してください`,
18
21
  });
19
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))){
29
+ context.report({
30
+ node,
31
+ message: `残余引数内の属性を参照しないでください${DETAIL_LINK}`,
32
+ });
33
+ }
34
+ }
20
35
 
21
36
  return {
22
- [`RestElement[argument.name='props']`]: (node) => {
37
+ [`RestElement:not([argument.name='rest'])`]: (node) => {
23
38
  context.report({
24
39
  node,
25
- message: `残余引数には 'props' という名称を利用しないでください
26
- - 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters
27
- - 'rest' という名称を推奨します`,
40
+ message: `残余引数には 'rest' という名称を指定してください${DETAIL_LINK}`,
28
41
  });
29
42
  },
30
- [`:not(:matches(RestElement,JSXSpreadAttribute,ObjectPattern>Property,ObjectExpression>Property))>Identifier[name='rest']`]: restAction,
31
- [`:matches(ObjectPattern>Property[value.name='rest'],ObjectExpression>Property[key.name='rest'])`]: restAction,
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,
32
46
  }
33
47
  },
34
48
  }
@@ -11,34 +11,39 @@ const ruleTester = new RuleTester({
11
11
  },
12
12
  })
13
13
 
14
- const ERROR_PROPS = `残余引数には 'props' という名称を利用しないでください
15
- - 詳細: https://github.com/kufu/tamatebako/tree/master/packages/eslint-plugin-smarthr/rules/best-practice-for-rest-parameters
16
- - 'rest' という名称を推奨します`
17
- const ERROR_REST = `残余引数以外に 'rest' という名称を利用しないでください
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 = {}` },
30
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)` },
31
34
  { code: `<Any {...rest} />` },
32
35
  ],
33
36
  invalid: [
34
- { code: `const hoge = (a, b, ...props) => {}`, errors: [ { message: ERROR_PROPS } ] },
35
- { code: `const hoge = ({ a, b, ...props }) => {}`, errors: [ { message: ERROR_PROPS } ] },
36
- { code: `const hoge = (rest) => {}`, errors: [ { message: ERROR_REST } ] },
37
- { code: `const hoge = (a, b, rest) => {}`, errors: [ { message: ERROR_REST } ] },
38
- { code: `const hoge = ({ a: rest, b }) => {}`, errors: [ { message: ERROR_REST } ] },
39
- { code: `const rest = {}`, errors: [ { message: ERROR_REST } ] },
40
- { code: `const hoge = { rest }`, errors: [ { message: ERROR_REST } ] },
41
- { code: `const hoge = { rest: fuga }`, errors: [ { message: ERROR_REST } ] },
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 } ] },
42
47
  ]
43
48
  })
44
49