eslint-plugin-smarthr 0.2.13 → 0.2.14

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,12 +2,17 @@
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
+ ### [0.2.14](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.13...v0.2.14) (2022-12-13)
6
+
7
+ ### Features
8
+ * trim-propsルールを追加 ([#44](https://github.com/kufu/eslint-plugin-smarthr/pull/44))
9
+
5
10
  ### [0.2.13](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.12...v0.2.13) (2022-12-07)
6
11
 
7
12
 
8
13
  ### Bug Fixes
9
14
 
10
- * redundant-nameのarrowedNameなどで利用するファイルパスが削られすぎていたため、指定が行いにくい問題を修正する ([4d45281](https://github.com/kufu/eslint-plugin-smarthr/commit/4d45281c383a9996437f3603dd954548f428cd19))
15
+ * redundant-nameのarrowedNameなどで利用するファイルパスが削られすぎていたため、指定が行いにくい問題を修正する ([#43](https://github.com/kufu/eslint-plugin-smarthr/issues/43)) ([6de9618](https://github.com/kufu/eslint-plugin-smarthr/commit/6de961831a9f9e0e93eeeebb80e56ecb60d9a2ff))
11
16
 
12
17
  ### [0.2.12](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.2.11...v0.2.12) (2022-12-07)
13
18
 
package/README.md CHANGED
@@ -17,3 +17,4 @@
17
17
  - [require-declaration](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/require-declaration)
18
18
  - [require-export](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/require-export)
19
19
  - [require-import](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/require-import)
20
+ - [trim-props](https://github.com/kufu/eslint-plugin-smarthr/tree/main/rules/trim-props)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-smarthr",
3
- "version": "0.2.13",
3
+ "version": "0.2.14",
4
4
  "author": "SmartHR",
5
5
  "license": "MIT",
6
6
  "description": "A sharable ESLint plugin for SmartHR",
@@ -0,0 +1,30 @@
1
+ # smarthr/trim-props
2
+
3
+ - このルールはCLIオプションの`--fix`で自動的に修正可能です
4
+ - 文字列型のpropsについて、先頭末尾に空白文字を含む文字列の設定を禁止させたい場合に利用します
5
+
6
+ ## rules
7
+
8
+ ```js
9
+ {
10
+ rules: {
11
+ 'smarthr/trim-props': 'error', // 'warn', 'off',
12
+ },
13
+ }
14
+ ```
15
+
16
+ ## ❌ Incorrect
17
+
18
+ ```jsx
19
+ <a href=" https://www.google.com">google</a>
20
+ <img src={"/sample.jpg "} alt={" sample "} />
21
+ <div data-spec=" info-area ">....</div>
22
+ ```
23
+
24
+ ## ✅ Correct
25
+
26
+ ```jsx
27
+ <a href="https://www.google.com">google</a>
28
+ <img src={"/sample.jpg"} alt={"sample"} />
29
+ <div data-spec="info-area">....</div>
30
+ ```
@@ -0,0 +1,37 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: 'suggestion',
4
+ messages: {
5
+ 'trim-props': '{{ message }}',
6
+ },
7
+ schema: [],
8
+ fixable: 'whitespace',
9
+ },
10
+ create(context) {
11
+ return {
12
+ JSXOpeningElement: (node) =>
13
+ node.attributes.reduce((prev, current) => {
14
+ const attribute = current.value?.type === 'JSXExpressionContainer' ? current.value.expression : current.value
15
+ const props = attribute?.value
16
+
17
+ if (typeof props !== 'string') return prev
18
+
19
+ if (props.match(/(^\s+|\s+$)/)) {
20
+ return context.report({
21
+ node,
22
+ loc: current.loc,
23
+ messageId: 'trim-props',
24
+ data: {
25
+ message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください',
26
+ },
27
+ fix(fixer) {
28
+ return fixer.replaceTextRange([attribute.range[0] + 1, attribute.range[1] - 1], props.trim())
29
+ },
30
+ })
31
+ }
32
+ return prev
33
+ }, []),
34
+ }
35
+ },
36
+ }
37
+ module.exports.schema = []
@@ -0,0 +1,78 @@
1
+ const rule = require('../rules/trim-props')
2
+ const RuleTester = require('eslint').RuleTester
3
+
4
+ const ruleTester = new RuleTester({
5
+ parserOptions: {
6
+ ecmaFeatures: {
7
+ jsx: true,
8
+ },
9
+ },
10
+ })
11
+
12
+ ruleTester.run('trim-props', rule, {
13
+ valid: [
14
+ { code: '<a href="https://www.google.com">google</a>' },
15
+ { code: '<a href={"https://www.google.com"}>google</a>' },
16
+ { code: '<img src="/sample.jpg" alt="sample" />' },
17
+ { code: '<img src={"/sample.jpg"} alt={"sample"} />' },
18
+ { code: '<div data-spec="info-area">....</div>' },
19
+ ],
20
+ invalid: [
21
+ {
22
+ code: '<a href=" https://www.google.com">google</a>',
23
+ output: '<a href="https://www.google.com">google</a>',
24
+ errors: [{ message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' }],
25
+ },
26
+ {
27
+ code: '<a href="https://www.google.com ">google</a>',
28
+ output: '<a href="https://www.google.com">google</a>',
29
+ errors: [{ message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' }],
30
+ },
31
+ {
32
+ code: '<a href=" https://www.google.com ">google</a>',
33
+ output: '<a href="https://www.google.com">google</a>',
34
+ errors: [{ message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' }],
35
+ },
36
+ {
37
+ code: '<a href={" https://www.google.com"}>google</a>',
38
+ output: '<a href={"https://www.google.com"}>google</a>',
39
+ errors: [{ message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' }],
40
+ },
41
+ {
42
+ code: '<a href={"https://www.google.com "}>google</a>',
43
+ output: '<a href={"https://www.google.com"}>google</a>',
44
+ errors: [{ message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' }],
45
+ },
46
+ {
47
+ code: '<a href={" https://www.google.com "}>google</a>',
48
+ output: '<a href={"https://www.google.com"}>google</a>',
49
+ errors: [{ message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' }],
50
+ },
51
+ {
52
+ code: '<img src=" /sample.jpg" alt="sample " />',
53
+ output: '<img src="/sample.jpg" alt="sample" />',
54
+ errors: [
55
+ { message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' },
56
+ { message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' },
57
+ ],
58
+ },
59
+ {
60
+ code: '<img src={" /sample.jpg"} alt={"sample "} />',
61
+ output: '<img src={"/sample.jpg"} alt={"sample"} />',
62
+ errors: [
63
+ { message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' },
64
+ { message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' },
65
+ ],
66
+ },
67
+ {
68
+ code: '<div data-spec=" info-area ">....</div>',
69
+ output: '<div data-spec="info-area">....</div>',
70
+ errors: [{ message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' }],
71
+ },
72
+ {
73
+ code: '<div data-spec={" info-area "}>....</div>',
74
+ output: '<div data-spec={"info-area"}>....</div>',
75
+ errors: [{ message: '属性に設定している文字列から先頭、末尾の空白文字を削除してください' }],
76
+ },
77
+ ],
78
+ })