Skip to content

Commit 000d803

Browse files
catalog: add wrong svg attribute naming in JSX
fix #662
1 parent 506c9df commit 000d803

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

website/catalog/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ Feel free to join our [Discord](https://discord.gg/4YZjf6htSQ) channel and ask @
5050
* [Unnecessary `useState` Type](/catalog/tsx/#unnecessary-usestate-type)
5151
* [Reverse React Compiler™](/catalog/tsx/#reverse-react-compilertm)
5252
* [Avoid nested links](/catalog/tsx/#avoid-nested-links)
53+
* [Rename SVG Attribute](/catalog/tsx/#rename-svg-attribute)
5354
* [YAML](/catalog/yaml/)
5455
* [Find key/value and show message](/catalog/yaml/#find-key-value-and-show-message-using-those-key-vals)

website/catalog/tsx/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ In order to reduce rule duplication, you can use the [`languageGlobs`](/referenc
1414
<!--@include: ./rewrite-mobx-component.md-->
1515
<!--@include: ./unnecessary-react-hook.md-->
1616
<!--@include: ./reverse-react-compiler.md-->
17-
<!--@include: ./avoid-nested-links.md-->
17+
<!--@include: ./avoid-nested-links.md-->
18+
<!--@include: ./rename-svg-attribute.md-->
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## Rename SVG Attribute <Badge type="tip" text="Has Fix" />
2+
3+
4+
* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InRzeCIsInF1ZXJ5IjoiIiwicmV3cml0ZSI6IiIsInN0cmljdG5lc3MiOiJyZWxheGVkIiwic2VsZWN0b3IiOiIiLCJjb25maWciOiJpZDogcmV3cml0ZS1zdmctYXR0cmlidXRlXG5sYW5ndWFnZTogdHN4XG5ydWxlOlxuICBwYXR0ZXJuOiAkUFJPUFxuICByZWdleDogKFthLXpdKyktKFthLXpdKVxuICBraW5kOiBwcm9wZXJ0eV9pZGVudGlmaWVyXG4gIGluc2lkZTpcbiAgICBraW5kOiBqc3hfYXR0cmlidXRlXG50cmFuc2Zvcm06XG4gIE5FV19QUk9QOlxuICAgIGNvbnZlcnQ6XG4gICAgICBzb3VyY2U6ICRQUk9QXG4gICAgICB0b0Nhc2U6IGNhbWVsQ2FzZVxuZml4OiAkTkVXX1BST1AiLCJzb3VyY2UiOiJjb25zdCBlbGVtZW50ID0gKFxuICA8c3ZnIHdpZHRoPVwiMTAwXCIgaGVpZ2h0PVwiMTAwXCIgdmlld0JveD1cIjAgMCAxMDAgMTAwXCI+XG4gICAgPHBhdGggZD1cIk0xMCAyMCBMMzAgNDBcIiBzdHJva2UtbGluZWNhcD1cInJvdW5kXCIgZmlsbC1vcGFjaXR5PVwiMC41XCIgLz5cbiAgPC9zdmc+XG4pIn0=)
5+
6+
### Description
7+
8+
[SVG](https://en.wikipedia.org/wiki/SVG)(Scalable Vector Graphics)s' hyphenated names are not compatible with JSX syntax in React. JSX requires [camelCase naming](https://react.dev/learn/writing-markup-with-jsx#3-camelcase-salls-most-of-the-things) for attributes.
9+
For example, an SVG attribute like `stroke-linecap` needs to be renamed to `strokeLinecap` to work correctly in React.
10+
11+
### YAML
12+
```yaml
13+
id: rewrite-svg-attribute
14+
language: tsx
15+
rule:
16+
pattern: $PROP # capture in metavar
17+
regex: ([a-z]+)-([a-z]) # hyphenated name
18+
kind: property_identifier
19+
inside:
20+
kind: jsx_attribute # in JSX attribute
21+
transform:
22+
NEW_PROP: # new property name
23+
convert: # use ast-grep's convert
24+
source: $PROP
25+
toCase: camelCase # to camelCase naming
26+
fix: $NEW_PROP
27+
```
28+
29+
### Example
30+
```tsx {3}
31+
const element = (
32+
<svg width="100" height="100" viewBox="0 0 100 100">
33+
<path d="M10 20 L30 40" stroke-linecap="round" fill-opacity="0.5" />
34+
</svg>
35+
)
36+
```
37+
38+
### Diff
39+
```ts
40+
const element = (
41+
<svg width="100" height="100" viewBox="0 0 100 100">
42+
<path d="M10 20 L30 40" stroke-linecap="round" fill-opacity="0.5" /> // [!code --]
43+
<path d="M10 20 L30 40" strokeLinecap="round" fillOpacity="0.5" /> // [!code ++]
44+
</svg>
45+
)
46+
```
47+
48+
### Contributed by
49+
Inspired by [SVG Renamer](https://admondtamang.medium.com/introducing-svg-renamer-your-solution-for-react-svg-attributes-26503382d5a8)

0 commit comments

Comments
 (0)