Skip to content

Commit 9712cbd

Browse files
committed
Extend tests and docs for jsx-dollar rule
1 parent e66b8a0 commit 9712cbd

File tree

2 files changed

+96
-16
lines changed

2 files changed

+96
-16
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/jsx-dollar.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ function MyComponent({ user }) {
6868
}
6969
```
7070

71+
```tsx
72+
import React from "react";
73+
74+
function MyComponent({ count, total }) {
75+
return <div>Progress: ${count} / ${total}</div>;
76+
// ^ ^
77+
// - Possible unnecessary '$' character before expression.
78+
// - Possible unnecessary '$' character before expression.
79+
}
80+
```
81+
7182
### Passing
7283

7384
```tsx

packages/plugins/eslint-plugin-react-x/src/rules/jsx-dollar.spec.ts

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,54 @@ import rule, { RULE_NAME } from "./jsx-dollar";
66
ruleTester.run(RULE_NAME, rule, {
77
invalid: [
88
{
9+
// Template literal syntax mistakenly used in JSX text
910
code: tsx`
10-
const MyComponent = () => <>Hello \${user.name}</>
11+
const App = () => <>Hello \${user.name}</>
1112
`,
1213
errors: [
1314
{
1415
messageId: "jsxDollar",
15-
column: 35,
16-
endColumn: 36,
16+
column: 27,
17+
endColumn: 28,
1718
endLine: 1,
1819
line: 1,
1920
suggestions: [
2021
{
2122
messageId: "removeDollarSign",
23+
// Should use JSX expression syntax instead
2224
output: tsx`
23-
const MyComponent = () => <>Hello {user.name}</>
25+
const App = () => <>Hello {user.name}</>
2426
`,
2527
},
2628
],
2729
},
2830
],
2931
},
3032
{
33+
// Two dollar signs before expression (report the last one)
34+
code: tsx`
35+
const App = () => <>Hello $\${user.name}</>
36+
`,
37+
errors: [
38+
{
39+
messageId: "jsxDollar",
40+
column: 28,
41+
endColumn: 29,
42+
endLine: 1,
43+
line: 1,
44+
suggestions: [
45+
{
46+
messageId: "removeDollarSign",
47+
output: tsx`
48+
const App = () => <>Hello \${user.name}</>
49+
`,
50+
},
51+
],
52+
},
53+
],
54+
},
55+
{
56+
// Template literal syntax in div element
3157
code: tsx`
3258
const App = (props) => {
3359
return <div>Hello \${props.name}</div>;
@@ -54,6 +80,7 @@ ruleTester.run(RULE_NAME, rule, {
5480
],
5581
},
5682
{
83+
// Template literal syntax at start of text
5784
code: tsx`
5885
const App = (props) => {
5986
return <div>\${props.name} is your name</div>;
@@ -80,6 +107,7 @@ ruleTester.run(RULE_NAME, rule, {
80107
],
81108
},
82109
{
110+
// Template literal syntax in middle of text
83111
code: tsx`
84112
const App = (props) => {
85113
return <div>Hello \${props.name} is your name</div>;
@@ -105,39 +133,80 @@ ruleTester.run(RULE_NAME, rule, {
105133
},
106134
],
107135
},
136+
{
137+
// Multiple template literal syntax errors in single JSX element
138+
code: tsx`
139+
function App({ count, total }) {
140+
return <div>Progress: \${count} / \${total}</div>;
141+
}
142+
`,
143+
errors: [
144+
{
145+
messageId: "jsxDollar",
146+
column: 25,
147+
endColumn: 26,
148+
endLine: 2,
149+
line: 2,
150+
suggestions: [
151+
{
152+
messageId: "removeDollarSign",
153+
// Fix first occurrence only
154+
output: tsx`
155+
function App({ count, total }) {
156+
return <div>Progress: {count} / \${total}</div>;
157+
}
158+
`,
159+
},
160+
],
161+
},
162+
{
163+
messageId: "jsxDollar",
164+
column: 36,
165+
endColumn: 37,
166+
endLine: 2,
167+
line: 2,
168+
suggestions: [
169+
{
170+
messageId: "removeDollarSign",
171+
// Fix second occurrence only
172+
output: tsx`
173+
function App({ count, total }) {
174+
return <div>Progress: \${count} / {total}</div>;
175+
}
176+
`,
177+
},
178+
],
179+
},
180+
],
181+
},
108182
],
109183
valid: [
110184
...allValid,
185+
// Template literal in JavaScript expression - valid usage
111186
tsx`
112-
const MyComponent = () => \`Hello \${user.name}\`
113-
`,
114-
tsx`
115-
const App = (props) => {
116-
return [<div key="1">1</div>]
117-
};
187+
const App = () => \`Hello \${user.name}\`
118188
`,
189+
// Plain dollar sign without template literal syntax
119190
tsx`
120191
const App = (props) => {
121192
return <div>Hello $</div>;
122193
};
123194
`,
195+
// Correct JSX expression syntax
124196
tsx`
125197
const App = (props) => {
126198
return <div>Hello {props.name}</div>;
127199
};
128200
`,
201+
// Dollar sign in template literal inside JSX expression - valid
129202
tsx`
130-
import React from "react";
131-
132-
function MyComponent({ price }) {
203+
function App({ price }) {
133204
// 🟢 Good: This is a legitimate use of the '$' character.
134205
return <div>{\`$\${price}\`}</div>;
135206
}
136207
`,
137208
tsx`
138-
import React from "react";
139-
function AnotherComponent({ price }) {
140-
// 🟢 Good: Another legitimate way to display a price.
209+
function App({ price }) {
141210
return <div>\${price}</div>;
142211
}
143212
`,

0 commit comments

Comments
 (0)