Skip to content

Commit

Permalink
[Fix] jsx-newline: prevent a crash when allowMultilines
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 22, 2023
1 parent ecadb92 commit 28f9a6c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Fixed
* [`jsx-no-leaked-render`]: preserve RHS parens for multiline jsx elements while fixing ([#3623][] @akulsr0)
* [`jsx-key`]: detect conditional returns ([#3630][] @yialo)
* [`jsx-newline`]: prevent a crash when `allowMultilines ([#3633][] @ljharb)

[#3633]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3633
[#3630]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3630
[#3623]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3623
[#3615]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3615
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/jsx-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const messages = {
};

function isMultilined(node) {
return node.loc.start.line !== node.loc.end.line;
return node && node.loc.start.line !== node.loc.end.line;
}

module.exports = {
Expand Down Expand Up @@ -103,7 +103,6 @@ module.exports = {
const isWithoutNewLine = !/\n\s*\n/.test(firstAdjacentSibling.value);

if (isBlockCommentInCurlyBraces(element)) return;

if (
allowMultilines
&& (
Expand Down
71 changes: 71 additions & 0 deletions tests/lib/rules/jsx-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,5 +649,76 @@ new RuleTester({ parserOptions }).run('jsx-newline', rule, {
],
options: [{ prevent: true, allowMultilines: true }],
},
{
code: `
const frag: DocumentFragment = (
<Fragment>
<sni-sequence-editor-tool
name="forward"
direction="forward"
type="control"
onClick={ () => this.onClickNavigate('forward') }
/>
<sni-sequence-editor-tool
name="rotate"
direction="left"
type="control"
onClick={ () => this.onClickNavigate('left') }
/>
<sni-sequence-editor-tool
name="rotate"
direction="right"
type="control"
onClick={ (): void => this.onClickNavigate('right') }
/>
<div className="sni-sequence-editor-control-panel__delete" data-name="delete" onClick={ this.onDeleteCommand } />
{
...Array.from(this.children)
}
</Fragment>
)
`,
output: `
const frag: DocumentFragment = (
<Fragment>
<sni-sequence-editor-tool
name="forward"
direction="forward"
type="control"
onClick={ () => this.onClickNavigate('forward') }
/>
<sni-sequence-editor-tool
name="rotate"
direction="left"
type="control"
onClick={ () => this.onClickNavigate('left') }
/>
${' '}
<sni-sequence-editor-tool
name="rotate"
direction="right"
type="control"
onClick={ (): void => this.onClickNavigate('right') }
/>
<div className="sni-sequence-editor-control-panel__delete" data-name="delete" onClick={ this.onDeleteCommand } />
{
...Array.from(this.children)
}
</Fragment>
)
`,
features: ['types'],
options: [{ prevent: true, allowMultilines: true }],
errors: [
{ messageId: 'allowMultilines', line: 10 },
{ messageId: 'prevent', line: 26 },
],
},
]),
});

0 comments on commit 28f9a6c

Please sign in to comment.