eslint-plugin-smarthr 0.5.12 → 0.5.13

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
+ ### [0.5.13](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.5.12...v0.5.13) (2024-06-21)
6
+
7
+
8
+ ### Features
9
+
10
+ * ButtonやTextLinkコンポーネントにprefix, suffixの両属性を同時に設定できないルールを追加 ([1eb7568](https://github.com/kufu/eslint-plugin-smarthr/commit/1eb75680a1125391106994532b58cc0591853711))
11
+
5
12
  ### [0.5.12](https://github.com/kufu/eslint-plugin-smarthr/compare/v0.5.11...v0.5.12) (2024-06-10)
6
13
 
7
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-smarthr",
3
- "version": "0.5.12",
3
+ "version": "0.5.13",
4
4
  "author": "SmartHR",
5
5
  "license": "MIT",
6
6
  "description": "A sharable ESLint plugin for SmartHR",
@@ -0,0 +1,47 @@
1
+ # smarthr/design-system-guideline-prohibit-double-icons
2
+
3
+ - 要素の前後両方にアイコンの使用を禁止するルールです
4
+ - `Button` や `TextLink` において、`prefix` と `suffix` が同時に設定されている場合、エラーとなります。
5
+ - 基本的にアイコンのみが設定される前提のルールになっていますが、文字列などが設定されている場合もエラーとなります。
6
+ - どちらにもアイコンをつけられそうな場合は、アイコン付き(右)(サフィックス)を優先し、アイコン付き(左)(プレフィックス)には指定しないでください。
7
+
8
+ ## rules
9
+
10
+ ```js
11
+ {
12
+ rules: {
13
+ 'smarthr/design-system-guideline-prohibit-double-icons': [
14
+ 'error', // 'warn', 'off'
15
+ // { checkType: 'always' } /* 'always' || 'allow-spread-attributes' */
16
+ ]
17
+ },
18
+ }
19
+ ```
20
+
21
+ ## ❌ Incorrect
22
+
23
+ ```jsx
24
+ <Button>hoge</Button>
25
+ <Button suffix={SUFFIX}>hoge</Button>
26
+ <Button prefix="PREFIX">hoge</Button>
27
+ <TextLink>hoge</TextLink>
28
+ <TextLink suffix="SUFFIX">hoge</TextLink>
29
+ <TextLink prefix={PREFIX}>hoge</TextLink>
30
+ <StyledButton>hoge</StyledButton>
31
+ <StyledLink>hoge</StyledLink>
32
+ <Input prefix={PREFIX} suffix={SUFFIX} />
33
+ ```
34
+
35
+ ## ✅ Correct
36
+
37
+ ```jsx
38
+ <Button>hoge</Button>
39
+ <Button suffix={SUFFIX}>hoge</Button>
40
+ <Button prefix="PREFIX">hoge</Button>
41
+ <TextLink>hoge</TextLink>
42
+ <TextLink suffix="SUFFIX">hoge</TextLink>
43
+ <TextLink prefix={PREFIX}>hoge</TextLink>
44
+ <StyledButton>hoge</StyledButton>
45
+ <StyledLink>hoge</StyledLink>
46
+ <Input prefix={PREFIX} suffix={SUFFIX} />
47
+ ```
@@ -0,0 +1,58 @@
1
+ const { generateTagFormatter } = require('../../libs/format_styled_components')
2
+
3
+ const EXPECTED_NAMES = {
4
+ '(Button)$': '(Button)$',
5
+ '(Link)$': '(Link)$',
6
+ }
7
+
8
+ const UNEXPECTED_NAMES = EXPECTED_NAMES
9
+
10
+ const SCHEMA = []
11
+
12
+ const REGEX_PATTERN = new RegExp(`(${Object.keys(EXPECTED_NAMES).join('|')})`)
13
+
14
+ /**
15
+ * @type {import('@typescript-eslint/utils').TSESLint.RuleModule<''>}
16
+ */
17
+ module.exports = {
18
+ meta: {
19
+ type: 'problem',
20
+ schema: SCHEMA,
21
+ },
22
+ create(context) {
23
+ return {
24
+ ...generateTagFormatter({ context, EXPECTED_NAMES, UNEXPECTED_NAMES }),
25
+ JSXOpeningElement: (node) => {
26
+ const nodeName = node.name.name
27
+
28
+ if (REGEX_PATTERN.test(nodeName)) {
29
+ let prefix = null
30
+ let suffix = null
31
+
32
+ for (const attr of node.attributes) {
33
+ switch (attr.name.name) {
34
+ case 'prefix':
35
+ prefix = attr
36
+ break
37
+ case 'suffix':
38
+ suffix = attr
39
+ break
40
+ }
41
+
42
+ if(prefix && suffix) {
43
+ context.report({
44
+ node,
45
+ message: `${nodeName} には prefix と suffix は同時に設定できません。
46
+ - prefix または suffix のみを設定してください。
47
+ - どちらにもアイコンをつけられそうな場合は、アイコン付き(右)(サフィックス)を優先し、アイコン付き(左)(プレフィックス)には指定しないでください。
48
+ - 両方設定したい場合は、'eslint-disable-next-line' 等を利用して、このルールを無効化してください。`,
49
+ })
50
+ break
51
+ }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ },
57
+ }
58
+ module.exports.schema = SCHEMA
@@ -0,0 +1,39 @@
1
+ const rule = require('../rules/design-system-guideline-prohibit-double-icons')
2
+ const RuleTester = require('eslint').RuleTester
3
+
4
+ const ruleTester = new RuleTester({
5
+ parserOptions: {
6
+ ecmaVersion: 12,
7
+ ecmaFeatures: {
8
+ experimentalObjectRestSpread: true,
9
+ jsx: true,
10
+ },
11
+ sourceType: 'module',
12
+ },
13
+ })
14
+
15
+ const generateErrorText = (name) => `${name} には prefix と suffix は同時に設定できません。
16
+ - prefix または suffix のみを設定してください。
17
+ - どちらにもアイコンをつけられそうな場合は、アイコン付き(右)(サフィックス)を優先し、アイコン付き(左)(プレフィックス)には指定しないでください。
18
+ - 両方設定したい場合は、'eslint-disable-next-line' 等を利用して、このルールを無効化してください。`
19
+
20
+ ruleTester.run('design-system-guideline-prohibit-double-icons', rule, {
21
+ valid: [
22
+ { code: `<Button>hoge</Button>` },
23
+ { code: `<Button suffix={SUFFIX}>hoge</Button>` },
24
+ { code: `<Button prefix="PREFIX">hoge</Button>` },
25
+ { code: `<TextLink>hoge</TextLink>` },
26
+ { code: `<TextLink suffix="SUFFIX">hoge</TextLink>` },
27
+ { code: `<TextLink prefix={PREFIX}>hoge</TextLink>` },
28
+ { code: `<StyledButton>hoge</StyledButton>` },
29
+ { code: `<StyledLink>hoge</StyledLink>` },
30
+ { code: `<Input prefix={PREFIX} suffix={SUFFIX} />` },
31
+ ],
32
+ invalid: [
33
+ { code: `<Button suffix={SUFFIX} prefix={PREFIX}>hoge</Button>`, errors: [{message: generateErrorText('Button')}]},
34
+ { code: `<Button suffix prefix>hoge</Button>`, errors: [{message: generateErrorText('Button')}]},
35
+ { code: `<StyledButton suffix={undefined} prefix={null}>hoge</StyledButton>`, errors: [{message: generateErrorText('StyledButton')}]},
36
+ { code: `<Link prefix="PREFIX" suffix="SUFFIX">hoge</Link>`, errors: [{message: generateErrorText('Link')}]},
37
+ { code: `<StyledLink prefix="PREFIX" suffix="SUFFIX">hoge</StyledLink>`, errors: [{message: generateErrorText('StyledLink')}]},
38
+ ]
39
+ })