Skip to content

Commit

Permalink
Merge pull request #791 from Lingvokot/790-stateless-multiline-wrap
Browse files Browse the repository at this point in the history
jsx-wrap-multilines check arrow functions without block body (fixes #790)
  • Loading branch information
yannickcr authored Apr 23, 2017
2 parents 77048c8 + 70c8f4f commit b5856ca
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
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 @@ -13,7 +13,8 @@ var has = require('has');
var DEFAULTS = {
declaration: true,
assignment: true,
return: true
return: true,
arrow: true
};

// ------------------------------------------------------------------------------
Expand All @@ -40,6 +41,9 @@ module.exports = {
},
return: {
type: 'boolean'
},
arrow: {
type: 'boolean'
}
},
additionalProperties: false
Expand Down Expand Up @@ -121,6 +125,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 @@ -118,6 +118,18 @@ var ASSIGNMENT_NO_PAREN = `
</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>;';

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -177,6 +189,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 @@ -230,6 +254,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'}]
}
]
});

0 comments on commit b5856ca

Please sign in to comment.