react/jsx-tag-spacing
Enforce spacing around the <, / and > of JSX tags
JSX allows spaces almost anywhere inside a tag, so <Avatar/>, <Avatar /> and < Avatar / > all mean the same thing. This rule picks one way to write them. By default that's <Avatar /> and </p>: a space before />, none after <, and none between / and the bracket next to it.
--fix adds or removes the spaces.
Configure#
Off by default. To turn it on:
[lint.rules]
"react/jsx-tag-spacing" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"react/jsx-tag-spacing" = ["error", {
closingSlash = "never", # "always", "never" or "allow"
# "always", "proportional-always", "never" or "allow"
beforeSelfClosing = "always",
afterOpening = "never", # "always", "allow-multiline", "never" or "allow"
beforeClosing = "allow",
}]Examples#
const small = <Avatar/A space is required before closing bracket>;
const medium = < A space is forbidden after opening bracketAvatar />;
const large = <Avatar / >Whitespace is forbidden between / and >; write />;
const label = <p>Profile< /Whitespace is forbidden between < and /; write </p>;const small = <Avatar />;
const label = <p>Profile</p>;Options#
Each option takes "always", "never" or "allow". "allow" turns that check off. Some take one more value.
| Option | Default | What it checks |
|---|---|---|
closingSlash | "never" | The space between / and > in />, and between < and / in </. |
beforeSelfClosing | "always" | The space before />. Also takes "proportional-always". |
afterOpening | "never" | The space after < and after </. Also takes "allow-multiline". |
beforeClosing | "allow" | The space before the > of an opening or a closing tag. Also takes "proportional-always". |
{ "beforeSelfClosing": "never" }No problemsconst small = <Avatar/>;{ "beforeClosing": "never" }2 problemsconst label = <p A space is forbidden before closing bracket>Profile</p A space is forbidden before closing bracket>;"allow-multiline" works like "never", except that it allows a line break after <.
With "proportional-always", a tag that spans several lines must have its /> or > on a separate line:
{ "beforeSelfClosing": "proportional-always" }1 problemconst avatar = (
<Avatar
src={photo}
alt={name} A newline is required before closing bracket/>
);{ "beforeSelfClosing": "proportional-always" }No problemsconst small = <Avatar src={photo} />;
const avatar = (
<Avatar
src={photo}
alt={name}
/>
);For a tag on one line, beforeSelfClosing then wants a space before />, and beforeClosing wants no space before >:
{ "beforeClosing": "proportional-always" }1 problemconst label = <p className="label" Whitespace is required before closing bracket>Profile</p>;{ "beforeClosing": "proportional-always" }No problemsconst label = <p className="label">Profile</p>;
const title = (
<h1
className="title"
id="profile"
>
Profile
</h1>
);When not to use it#
A formatter such as bun fmt or Prettier already fixes this spacing.