Skip to content

jsx-wrap-multilines check arrow functions without block body #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/rules/jsx-wrap-multilines.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Prevent missing parentheses around multiline JSX (jsx-wrap-multilines)

Wrapping multiline JSX in parentheses can improve readability and/or convenience. It optionally takes a second parameter in the form of an object, containing places to apply the rule. By default, `"declaration"`, `"assignment"`, and `"return"` syntax is checked, but these can be explicitly disabled. Any syntax type missing in the object will follow the default behavior (become enabled).
Wrapping multiline JSX in parentheses can improve readability and/or convenience. It optionally takes a second parameter in the form of an object, containing places to apply the rule. By default, `"declaration"`, `"assignment"`, `"return"`, and `"arrow"` syntax is checked, but these can be explicitly disabled. Any syntax type missing in the object will follow the default behavior (become enabled).

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

Expand Down Expand Up @@ -43,4 +43,9 @@ hello = <div>
var world = <div>
<p>World</p>
</div>

// When [1, {arrow: false}]
var hello = () => <div>
<p>World</p>
</div>
```
14 changes: 13 additions & 1 deletion lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
var DEFAULTS = {
declaration: true,
assignment: true,
return: true
return: true,
arrow: true
};

// ------------------------------------------------------------------------------
Expand All @@ -38,6 +39,9 @@ module.exports = {
},
return: {
type: 'boolean'
},
arrow: {
type: 'boolean'
}
},
additionalProperties: false
Expand Down Expand Up @@ -107,6 +111,14 @@ module.exports = {
if (isEnabled('return')) {
check(node.argument);
}
},

'ArrowFunctionExpression:exit': function (node) {
var arrowBody = node.body;

if (isEnabled('arrow') && arrowBody.type !== 'BlockStatement') {
check(arrowBody);
}
}
};

Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/jsx-wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ var ASSIGNMENT_NO_PAREN = '\
<p>Hello</p>\n\
</div>;';

var ARROW_SINGLE_LINE = 'var hello = () => <p>Hello</p>;';

var ARROW_PAREN = '\
var hello = () => (<div>\n\
<p>Hello</p>\n\
</div>);';

var ARROW_NO_PAREN = '\
var hello = () => <div>\n\
<p>Hello</p>\n\
</div>;';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb After your Node 4+ PR this could get cleaned up a little.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call; I'll include this in my PR.


// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -112,6 +124,18 @@ ruleTester.run('jsx-wrap-multilines', rule, {
code: ASSIGNMENT_NO_PAREN,
options: [{assignment: false}],
parserOptions: parserOptions
}, {
code: ARROW_PAREN,
options: [],
parserOptions: parserOptions
}, {
code: ARROW_SINGLE_LINE,
options: [],
parserOptions: parserOptions
}, {
code: ARROW_NO_PAREN,
options: [{arrow: false}],
parserOptions: parserOptions
}
],

Expand Down Expand Up @@ -149,6 +173,12 @@ ruleTester.run('jsx-wrap-multilines', rule, {
parserOptions: parserOptions,
options: [{assignment: true}],
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: ARROW_NO_PAREN,
output: ARROW_PAREN,
parserOptions: parserOptions,
options: [{arrow: true}],
errors: [{message: 'Missing parentheses around multilines JSX'}]
}
]
});