Skip to content

Commit 2871232

Browse files
authored
Merge pull request #40 from NaverPayDev/feat/28
svg 관련 rule 이관
2 parents 7b0a02e + 6f87db0 commit 2871232

19 files changed

+2243
-34
lines changed

.changeset/calm-donuts-enjoy.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@naverpay/eslint-plugin": minor
3+
---
4+
5+
- `@naverpay/svg-manager`와 의존관계가 있는 `optimize-svg-components`, `svg-unique-id` rule 추가
6+
- minimatch 가져오기 누락 수정

.github/workflows/ci.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jobs:
1111
- uses: actions/checkout@v3
1212
- uses: pnpm/action-setup@v3
1313
with:
14-
version: 9
1514
run_install: true
1615
- run: pnpm run prettier
1716
- run: pnpm run lint
@@ -23,6 +22,5 @@ jobs:
2322
- uses: actions/checkout@v3
2423
- uses: pnpm/action-setup@v3
2524
with:
26-
version: 9
2725
run_install: true
2826
- run: pnpm run test

packages/eslint-plugin/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ npm install @naverpay/eslint-plugin -D
3737
| Name | Description | 🔧 |
3838
| :--------------------------------------------------------------- | :---------------------------------------------- | :-- |
3939
| [memo-react-components](docs/memo-react-components.md) | 주어진 경로의 React 컴포넌트들을 자동으로 memoize 합니다. | 🔧 |
40+
| [optimize-svg-components](docs/optimize-svg-components.md) | 주어진 경로의 svg 컴포넌트들을 [svgo](https://github.com/svg/svgo) 기반으로 최적화합니다. | 🔧 |
4041
| [prevent-default-import](docs/prevent-default-import.md) | 패키지 목록을 받아, 해당 패키지 default import를 금지합니다. | 🔧 |
4142
| [sort-exports](docs/sort-exports.md) | 파일에 있는 모든 export 문을 정렬합니다. | 🔧 |
43+
| [svg-unique-id](docs/svg-unique-id.md) | 주어진 경로의 SVG 컴포넌트들에 고유한 id를 부여하는 HOC를 추가합니다. | 🔧 |
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# `@naverpay/optimize-svg-components`
2+
3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
5+
주어진 경로의 SVG 컴포넌트들을 최적화합니다.
6+
7+
## 필수 패키지
8+
9+
`@naverpay/svg-manager` 패키지가 필요합니다.
10+
11+
```sh
12+
npm i @naverpay/svg-manager -D
13+
```
14+
15+
> **왜 필요한가요?**
16+
>
17+
> - `@naverpay/svg-manager`는 SVG 관련 타입과 HOC 등을 정의합니다.
18+
> - 해당 규칙이 불필요한 환경에서 설치되는 것을 방지하고자, 규칙을 사용되는 곳에서만 의존성을 설치합니다.
19+
20+
## 설명
21+
22+
주어진 경로의 svg 컴포넌트들을 [svgo](https://github.com/svg/svgo) 기반으로 최적화합니다.
23+
24+
### SVG 컴포넌트 예시
25+
26+
```tsx
27+
const IconCheckFill = (props: SVGStyleProps) => (
28+
<svg
29+
xmlns="http://www.w3.org/2000/svg"
30+
width={props.width || '100%'}
31+
height={props.height || '100%'}
32+
viewBox={props.viewBox || '0 0 22 22'}
33+
style={props.style || {}}
34+
>
35+
<g fill="none" fillRule="evenodd">
36+
<path
37+
fill={props.fill || '#03c75a'}
38+
d="M11 .5C5.21.5.5 5.21.5 11S5.21 21.5 11 21.5 21.5 16.79 21.5 11 16.79.5 11 .5"
39+
/>
40+
<path
41+
fill="#FFF"
42+
d="M8.754 14.847l-3.027-2.995c-.116-.114-.182-.27-.182-.434 0-.163.066-.32.182-.434.244-.24.635-.24.878 0l2.589 2.56 6.2-6.131c.244-.24.635-.24.878 0 .117.114.182.27.182.434 0 .163-.065.32-.182.434l-6.639 6.566c-.117.115-.275.18-.44.18-.164 0-.322-.065-.439-.18z"
43+
/>
44+
</g>
45+
</svg>
46+
)
47+
```
48+
49+
단, 다음과 같은 특징이 있는 컴포넌트는 최적화에서 제외합니다.
50+
51+
- props로 id가 존재하는 경우
52+
- 내부에 [classnames](https://www.npmjs.com/package/classnames)로 클래스를 정의한 경우
53+
- 요소에 한글을 포함한 경우
54+
- 이미 최적화되었다고 판단된 경우 ([주의사항](#주의사항) 참고)
55+
56+
## 옵션
57+
58+
### `pathGroups: [array]`:
59+
60+
svg 컴포넌트의 경로를 설정합니다
61+
62+
**Properties of the objects**
63+
| property | required | type | description |
64+
|----------------|:--------:|--------|---------------|
65+
| path || string | 그룹에 속할 minimatch pattern |
66+
| excludes | | Array\<string\> | path에서 제외할 파일명 |
67+
68+
### (Optional) `excludes: [array]`:
69+
70+
전체 경로에서 pathGroups에 속하지만 제외하고자 하는 파일 경로를 지정합니다.
71+
72+
```json
73+
"excludes": ["/src/assets/*.stories.tsx"]
74+
```
75+
76+
## 주의사항
77+
78+
최적화 후 아래 주석이 `@autogenerated`됩니다.
79+
해당 주석을 제거하면 최적화 여부를 판단할 수 없으므로 중복하여 린트가 실행될 수 있습니다.
80+
따라서 린트 실행하여 최적화된 컴포넌트에서 자동 생성된 주석은 유지할 것을 권장합니다.
81+
82+
```js
83+
/* @autogenerated optimization completed by @naverpay/optimize-svg-components */
84+
```
85+
86+
## 예시
87+
88+
```json
89+
"@naverpay/optimize-svg-components": [
90+
"warn",
91+
{
92+
"pathGroups": [
93+
{
94+
"path": "/src/assets/*.tsx",
95+
},
96+
{
97+
"path": "/src/components/assets/*.tsx",
98+
"excludes": ["IconExample.tsx"],
99+
}
100+
],
101+
"excludes": ["/src/**/*.stories.tsx"]
102+
}
103+
]
104+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# `@naverpay/svg-unique-id`
2+
3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
5+
주어진 경로의 SVG 컴포넌트들에 고유한 id를 부여하는 HOC를 추가합니다.
6+
7+
## 필수 패키지
8+
9+
`@naverpay/svg-manager` 패키지가 필요합니다.
10+
11+
```sh
12+
npm i @naverpay/svg-manager -D
13+
```
14+
15+
> **왜 필요한가요?**
16+
>
17+
> - `@naverpay/svg-manager`는 SVG 관련 타입과 HOC 등을 정의합니다.
18+
> - 해당 규칙이 불필요한 환경에서 설치되는 것을 방지하고자, 규칙을 사용되는 곳에서만 의존성을 설치합니다.
19+
20+
## 설명
21+
22+
SVG 컴포넌트 내부 요소의 id가 동일할 때, SVG 컴포넌트를 같은 화면에서 여러 번 사용하게 된다면
23+
SVG 내부 요소가 제대로 그려지지 않을 수 있습니다.
24+
id 중복 문제를 해결하기 위해 `<SvgUniqueID />` Wrapper로 SVG 컴포넌트를 감쌉니다.
25+
26+
## 옵션
27+
28+
### `paths: [array]`:
29+
30+
대상 파일 경로
31+
32+
## Examples
33+
34+
```json
35+
"@naverpay/svg-unique-id": [
36+
"warn",
37+
{
38+
"paths": [
39+
"/packages/common-bi/src/assets/*.tsx"
40+
]
41+
}
42+
]
43+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const SVG_OPTIMIZED_COMMENT_CONTENT = '@autogenerated optimization completed by @naverpay/optimize-svg-components'
2+
const SVG_OPTIMIZE_COMPLETED_COMMENT = `/* ${SVG_OPTIMIZED_COMMENT_CONTENT} */`
3+
const PROPS_IDENTIFIER_NAME = 'props'
4+
const FIRST_RANGE = [0, 0]
5+
6+
module.exports = {
7+
PROPS_IDENTIFIER_NAME,
8+
SVG_OPTIMIZED_COMMENT_CONTENT,
9+
SVG_OPTIMIZE_COMPLETED_COMMENT,
10+
FIRST_RANGE,
11+
}

0 commit comments

Comments
 (0)