Skip to content

Commit 32bcb48

Browse files
authored
Merge pull request #1177 from fatfisz/curly-spacing-for-children
`jsx-curly-spacing`: Enable curly spacing check for children Fixes #857
2 parents 8e547ea + d57c115 commit 32bcb48

File tree

4 files changed

+1830
-133
lines changed

4 files changed

+1830
-133
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Finally, enable all of the rules that you would like to use. Use [our preset](#
123123
* [react/jsx-boolean-value](docs/rules/jsx-boolean-value.md): Enforce boolean attributes notation in JSX (fixable)
124124
* [react/jsx-closing-bracket-location](docs/rules/jsx-closing-bracket-location.md): Validate closing bracket location in JSX (fixable)
125125
* [react/jsx-closing-tag-location](docs/rules/jsx-closing-tag-location.md): Validate closing tag location in JSX (fixable)
126-
* [react/jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes (fixable)
126+
* [react/jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes and expressions (fixable)
127127
* [react/jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes (fixable)
128128
* [react/jsx-filename-extension](docs/rules/jsx-filename-extension.md): Restrict file extensions that may contain JSX
129129
* [react/jsx-first-prop-new-line](docs/rules/jsx-first-prop-new-line.md): Enforce position of the first prop in JSX (fixable)

docs/rules/jsx-curly-spacing.md

Lines changed: 138 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,60 @@
1-
# Enforce or disallow spaces inside of curly braces in JSX attributes. (react/jsx-curly-spacing)
1+
# Enforce or disallow spaces inside of curly braces in JSX attributes and expressions. (react/jsx-curly-spacing)
22

33
While formatting preferences are very personal, a number of style guides require or disallow spaces between curly braces.
44

55
**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.
66

77
## Rule Details
88

9-
This rule aims to maintain consistency around the spacing inside of JSX attributes.
9+
This rule aims to maintain consistency around the spacing inside of JSX attributes and expressions inside element children.
1010

1111
It either requires or disallows spaces between those braces and the values inside of them.
1212

13-
### Options
13+
## Rule Options
1414

1515
There are two main options for the rule:
1616

17-
* `"always"` enforces a space inside of curly braces
18-
* `"never"` disallows spaces inside of curly braces (default)
17+
* `{"when": "always"}` enforces a space inside of curly braces
18+
* `{"when": "never"}` disallows spaces inside of curly braces (default)
1919

20-
Depending on your coding conventions, you can choose either option by specifying it in your configuration:
20+
There are also two properties that allow specifying how the rule should work with the attributes (`attributes`) and the expressions (`children`). The possible values are:
2121

22-
```json
23-
"react/jsx-curly-spacing": [2, "always"]
22+
* `true` enables checking for the spacing using the options (default for `attributes`), e.g. `{"attributes": false}` disables checking the attributes
23+
* `false` disables checking for the spacing (default for `children`, for backward compatibility), e.g. `{"children": true}` enables checking the expressions
24+
* an object containing options that override the global options, e.g. `{"when": "always", "children": {"when": "never"}}` enforces a space inside attributes, but disallows spaces inside expressions
25+
26+
### never
27+
28+
When `{"when": "never"}` is set, the following patterns are considered warnings:
29+
30+
```jsx
31+
<Hello name={ firstname } />;
32+
<Hello name={ firstname} />;
33+
<Hello name={firstname } />;
2434
```
2535

26-
#### never
36+
The following patterns are not warnings:
37+
38+
```jsx
39+
<Hello name={firstname} />;
40+
<Hello name={{ firstname: 'John', lastname: 'Doe' }} />;
41+
<Hello name={
42+
firstname
43+
} />;
44+
<Hello>{firstname}</Hello>;
45+
<Hello>{ firstname }</Hello>;
46+
<Hello>{
47+
firstname
48+
}</Hello>;
49+
```
2750

28-
When `"never"` is set, the following patterns are considered warnings:
51+
When `{"when": "never", "children": true}` is set, the following patterns are considered warnings:
2952

3053
```jsx
3154
<Hello name={ firstname } />;
3255
<Hello name={ firstname} />;
3356
<Hello name={firstname } />;
57+
<Hello>{ firstname }</Hello>;
3458
```
3559

3660
The following patterns are not warnings:
@@ -41,11 +65,15 @@ The following patterns are not warnings:
4165
<Hello name={
4266
firstname
4367
} />;
68+
<Hello>{firstname}</Hello>;
69+
<Hello>{
70+
firstname
71+
}</Hello>;
4472
```
4573

46-
#### always
74+
### always
4775

48-
When `"always"` is used, the following patterns are considered warnings:
76+
When `{"when": "always"}` is set, the following patterns are considered warnings:
4977

5078
```jsx
5179
<Hello name={firstname} />;
@@ -61,14 +89,42 @@ The following patterns are not warnings:
6189
<Hello name={
6290
firstname
6391
} />;
92+
<Hello>{ firstname }</Hello>;
93+
<Hello>{firstname}</Hello>;
94+
<Hello>{
95+
firstname
96+
}</Hello>;
97+
```
98+
99+
When `{"when": "always", "children": true}` is set, the following patterns are considered warnings:
100+
101+
```jsx
102+
<Hello name={firstname} />;
103+
<Hello name={ firstname} />;
104+
<Hello name={firstname } />;
105+
<Hello>{firstname}</Hello>;
106+
```
107+
108+
The following patterns are not warnings:
109+
110+
```jsx
111+
<Hello name={ firstname } />;
112+
<Hello name={ {firstname: 'John', lastname: 'Doe'} } />;
113+
<Hello name={
114+
firstname
115+
} />;
116+
<Hello>{ firstname }</Hello>;
117+
<Hello>{
118+
firstname
119+
}</Hello>;
64120
```
65121

66-
#### Braces spanning multiple lines
122+
### Braces spanning multiple lines
67123

68124
By default, braces spanning multiple lines are allowed with either setting. If you want to disallow them you can specify an additional `allowMultiline` property with the value `false`:
69125

70126
```json
71-
"react/jsx-curly-spacing": [2, "never", {"allowMultiline": false}]
127+
"react/jsx-curly-spacing": [2, {"when": "never", "allowMultiline": false}]
72128
```
73129

74130
When `"never"` is used and `allowMultiline` is `false`, the following patterns are considered warnings:
@@ -87,6 +143,11 @@ The following patterns are not warnings:
87143
```jsx
88144
<Hello name={firstname} />;
89145
<Hello name={{ firstname: 'John', lastname: 'Doe' }} />;
146+
<Hello>{firstname}</Hello>;
147+
<Hello>{ firstname }</Hello>;
148+
<Hello>{
149+
firstname
150+
}</Hello>;
90151
```
91152

92153
When `"always"` is used and `allowMultiline` is `false`, the following patterns are considered warnings:
@@ -105,14 +166,39 @@ The following patterns are not warnings:
105166
```jsx
106167
<Hello name={ firstname } />;
107168
<Hello name={ {firstname: 'John', lastname: 'Doe'} } />;
169+
<Hello>{firstname}</Hello>;
170+
<Hello>{ firstname }</Hello>;
171+
<Hello>{
172+
firstname
173+
}</Hello>;
174+
```
175+
176+
When `{"when": "never", "attributes": {"allowMultiline": false}, "children": true}` is set, the following patterns are considered warnings:
177+
178+
```jsx
179+
<Hello name={ firstname } />;
180+
<Hello name={
181+
firstname
182+
} />;
183+
<Hello>{ firstname }</Hello>;
108184
```
109185

110-
#### Granular spacing controls
186+
The following patterns are not warnings:
187+
188+
```jsx
189+
<Hello name={firstname} />;
190+
<Hello>{firstname}</Hello>;
191+
<Hello>{
192+
firstname
193+
}</Hello>;
194+
```
195+
196+
### Granular spacing controls
111197

112198
You can specify an additional `spacing` property that is an object with the following possible values:
113199

114200
```json
115-
"react/jsx-curly-spacing": [2, "always", {"spacing": {
201+
"react/jsx-curly-spacing": [2, {"when": "always", "spacing": {
116202
"objectLiterals": "never"
117203
}}]
118204
```
@@ -135,6 +221,41 @@ When `"never"` is used and `objectLiterals` is `"always"`, the following pattern
135221

136222
Please note that spacing of the object literal curly braces themselves is controlled by the built-in [`object-curly-spacing`](http://eslint.org/docs/rules/object-curly-spacing) rule.
137223

224+
### Shorthand options
225+
226+
To preserve backward compatibility, two additional options are supported:
227+
228+
```json
229+
"react/jsx-curly-spacing": [2, "always"]
230+
```
231+
232+
which is a shorthand for
233+
234+
```json
235+
"react/jsx-curly-spacing": [2, {"when": "always"}]
236+
```
237+
238+
and
239+
240+
```json
241+
"react/jsx-curly-spacing": [2, "never"]
242+
```
243+
244+
which is a shorthand for
245+
246+
```json
247+
"react/jsx-curly-spacing": [2, {"when": "never"}]
248+
```
249+
250+
When using the shorthand options, only attributes will be checked. To specify other options, use another argument:
251+
252+
```json
253+
"react/jsx-curly-spacing": [2, "never", {
254+
"allowMultiline": false,
255+
"spacing": {"objectLiterals": "always"}
256+
}]
257+
```
258+
138259
## When Not To Use It
139260

140-
You can turn this rule off if you are not concerned with the consistency around the spacing inside of JSX attributes.
261+
You can turn this rule off if you are not concerned with the consistency around the spacing inside of JSX attributes or expressions.

0 commit comments

Comments
 (0)