JS.REACT.JSX.WRAP.MULTILINES

Prevent missing parentheses around multilines JSX

Wrapping multiline JSX in parentheses can improve readability and/or convenience.

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

Rule Details

This rule optionally takes a second parameter in the form of an object, containing places to apply the rule. By default, all the syntax listed below will be checked except the conditions out of declaration or assignment, logical expressions and JSX attributes, but these can be explicitly disabled. Any syntax type missing in the object will follow the default behavior displayed below.

コピー
{
  "declaration": "parens",
  "assignment": "parens",
  "return": "parens",
  "arrow": "parens",
  "condition": "ignore",
  "logical": "ignore",
  "prop": "ignore"
}

Note: conditions are checked by default in declarations or assignments.

Examples of incorrect code for this rule, when configured with parens or parens-new-line:

コピー
var Hello = createReactClass({
  render: function() {
    return <div>
      <p>Hello {this.props.name}</p>
    </div>;
  }
});

Examples of incorrect code for this rule, when configured with parens-new-line:

コピー
var Hello = createReactClass({
  render: function() {
    return (<div>
      <p>Hello {this.props.name}</p>
    </div>);
  }
});

Examples of correct code for this rule:

コピー
var singleLineJSX = <p>Hello</p>

var Hello = createReactClass({
  render: function() {
    return (
      <div>
        <p>Hello {this.props.name}</p>
      </div>
    );
  }
});

declaration

Examples of incorrect code for this rule, when configured with { declaration: "parens" }:

コピー
var hello = <div>
  <p>Hello</p>
</div>;

Examples of correct code for this rule, when configured with { declaration: "parens" }:

コピー
var hello = (
  <div>
    <p>Hello</p>
  </div>
);
コピー
var hello = (<div>
  <p>Hello</p>
</div>);

Examples of incorrect code for this rule, when configured with { declaration: "parens-new-line" }:

コピー
var hello = <div>
  <p>Hello</p>
</div>;
コピー
var hello = (<div>
  <p>Hello</p>
</div>);

Examples of correct code for this rule, when configured with { declaration: "parens-new-line" }.

コピー
var hello = (
  <div>
    <p>Hello</p>
  </div>
);

assignment

Examples of incorrect code for this rule, when configured with { assignment: "parens" }.

コピー
var hello;
hello = <div>
  <p>Hello</p>
</div>;

Examples of correct code for this rule, when configured with { assignment: "parens" }.

コピー
var hello;
hello = (
  <div>
    <p>Hello</p>
  </div>
);
コピー
var hello;
hello = (<div>
  <p>Hello</p>
</div>);

Examples of incorrect code for this rule, when configured with { assignment: "parens-new-line" }.

コピー
var hello;
hello = <div>
  <p>Hello</p>
</div>;
コピー
var hello;
hello = (<div>
  <p>Hello</p>
</div>);

Examples of correct code for this rule, when configured with { assignment: "parens-new-line" }.

コピー
var hello;
hello = (
  <div>
    <p>Hello</p>
  </div>
);

return

Examples of incorrect code for this rule, when configured with { return: "parens" }.

コピー
function hello() {
  return <div>
    <p>Hello</p>
  </div>;
}

Examples of correct code for this rule, when configured with { return: "parens" }.

コピー
function hello() {
  return (
    <div>
      <p>Hello</p>
    </div>
  );
}
コピー
function hello() {
  return (<div>
    <p>Hello</p>
  </div>);
}

Examples of incorrect code for this rule, when configured with { return: "parens-new-line" }.

コピー
function hello() {
  return <div>
    <p>Hello</p>
  </div>;
}
コピー
function hello() {
  return (<div>
    <p>Hello</p>
  </div>);
}

Examples of correct code for this rule, when configured with { return: "parens-new-line" }.

コピー
function hello() {
  return (
    <div>
      <p>Hello</p>
    </div>
  );
}

arrow

Examples of incorrect code for this rule, when configured with { arrow: "parens" }.

コピー
var hello = () => <div>
  <p>World</p>
</div>;

Examples of correct code for this rule, when configured { arrow: "parens" }.

コピー
var hello = () => (
  <div>
    <p>World</p>
  </div>
);
コピー
var hello = () => (<div>
  <p>World</p>
</div>);

Examples of incorrect code for this rule, when configured with { arrow: "parens-new-line" }.

コピー
var hello = () => <div>
  <p>World</p>
</div>;
コピー
var hello = () => (<div>
  <p>World</p>
</div>);

Examples of correct code for this rule, when configured with { arrow: "parens-new-line" }.

コピー
var hello = () => (
  <div>
    <p>World</p>
  </div>
);

condition

Examples of incorrect code for this rule, when configured with { condition: "parens" }.

コピー
<div>
  {foo ? <div>
      <p>Hello</p>
    </div> : null}
</div>

Examples of correct code for this rule, when configured with { condition: "parens" }.

コピー
<div>
  {foo ? (<div>
      <p>Hello</p>
  </div>) : null}
</div>
コピー
<div>
  {foo ? (
    <div>
      <p>Hello</p>
    </div>
  ): null}
</div>

Examples of incorrect code for this rule, when configured with { condition: "parens-new-line" }.

コピー
<div>
  {foo ? <div>
      <p>Hello</p>
    </div> : null}
</div>
コピー
<div>
  {foo ? (<div>
      <p>Hello</p>
  </div>) : null}
</div>

Examples of correct code for this rule, when configured with { condition: "parens-new-line" }.

コピー
<div>
  {foo ? (
    <div>
      <p>Hello</p>
    </div>
  ): null}
</div>

logical

Examples of incorrect code for this rule, when configured with { logical: "parens" }.

コピー
<div>
  {foo &&
    <div>
      <p>Hello World</p>
    </div>
  }
</div>

Examples of correct code for this rule, when configured with { logical: "parens" }.

コピー
<div>
  {foo &&
    (<div>
      <p>Hello World</p>
    </div>)
  }
</div>
コピー
<div>
  {foo && (
    <div>
      <p>Hello World</p>
    </div>
  )}
</div>

Examples of incorrect code for this rule, when configured with { logical: "parens-new-line" }.

コピー
<div>
  {foo &&
    <div>
      <p>Hello World</p>
    </div>
  }
</div>
コピー
<div>
  {foo &&
    (<div>
      <p>Hello World</p>
    </div>)
  }
</div>

Examples of correct code for this rule, when configured with { logical: "parens-new-line" }.

コピー
<div>
  {foo && (
    <div>
      <p>Hello World</p>
    </div>
  )}
</div>

prop

Examples of incorrect code for this rule, when configured with { prop: "parens" }.

コピー
<div foo={<div>
    <p>Hello</p>
  </div>}>
  <p>Hello</p>
</div>;

Examples of correct code for this rule, when configured with { prop: "parens" }.

コピー
<div foo={(<div>
    <p>Hello</p>
  </div>)}>
  <p>Hello</p>
</div>;
コピー
<div foo={(
  <div>
    <p>Hello</p>
  </div>
)}>
  <p>Hello</p>
</div>;

Examples of incorrect code for this rule, when configured with { prop: "parens-new-line" }.

コピー
<div foo={<div>
    <p>Hello</p>
  </div>}>
  <p>Hello</p>
</div>;
コピー
<div foo={(<div>
    <p>Hello</p>
  </div>)}>
  <p>Hello</p>
</div>;

Examples of correct code for this rule, when configured with { prop: "parens-new-line" }.

コピー
<div foo={(
  <div>
    <p>Hello</p>
  </div>
)}>
  <p>Hello</p>
</div>;

The content on this page is adapted from the ESLint User Guide. Copyright © OpenJS Foundation and other contributors, www.openjsf.org. All rights reserved. https://eslint.org/docs/rules/