Skip to content

Commit

Permalink
Add auto fix for wrap-multiline
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Jan 18, 2016
1 parent 3371be2 commit 21e4857
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Finally, enable all of the rules that you would like to use.
* [require-extension](docs/rules/require-extension.md): Restrict file extensions that may be required
* [self-closing-comp](docs/rules/self-closing-comp.md): Prevent extra closing tags for components without children
* [sort-comp](docs/rules/sort-comp.md): Enforce component methods order
* [wrap-multilines](docs/rules/wrap-multilines.md): Prevent missing parentheses around multilines JSX
* [wrap-multilines](docs/rules/wrap-multilines.md): Prevent missing parentheses around multilines JSX (fixable)

## React Native

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/wrap-multilines.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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).

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

## Rule Details

The following patterns are considered warnings:
Expand Down
10 changes: 9 additions & 1 deletion lib/rules/wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var DEFAULTS = {

module.exports = function(context) {

var sourceCode = context.getSourceCode();

function isParenthesised(node) {
var previousToken = context.getTokenBefore(node);
var nextToken = context.getTokenAfter(node);
Expand All @@ -39,7 +41,13 @@ module.exports = function(context) {
}

if (!isParenthesised(node) && isMultilines(node)) {
context.report(node, 'Missing parentheses around multilines JSX');
context.report({
node: node,
message: 'Missing parentheses around multilines JSX',
fix: function(fixer) {
return fixer.replaceText(node, '(' + sourceCode.getText(node) + ')');
}
});
}
}

Expand Down
30 changes: 15 additions & 15 deletions tests/lib/rules/wrap-multilines.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ var RETURN_SINGLE_LINE = '\
var RETURN_PAREN = '\
var Hello = React.createClass({\
render: function() {\
return (\n\
<div>\n\
<p>Hello {this.props.name}</p>\n\
</div>\n\
);\
return (<div>\n\
<p>Hello {this.props.name}</p>\n\
</div>);\
}\
});';

Expand All @@ -52,11 +50,9 @@ var RETURN_NO_PAREN = '\
var DECLARATION_SINGLE_LINE = 'var hello = <p>Hello</p>;';

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

var DECLARATION_NO_PAREN = '\
var hello = <div>\n\
Expand All @@ -67,11 +63,9 @@ var ASSIGNMENT_SINGLE_LINE = 'var hello; hello = <p>Hello</p>;';

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

var ASSIGNMENT_NO_PAREN = '\
var hello;\
Expand Down Expand Up @@ -124,28 +118,34 @@ ruleTester.run('wrap-multilines', rule, {
invalid: [
{
code: RETURN_NO_PAREN,
output: RETURN_PAREN,
parserOptions: parserOptions,
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: RETURN_NO_PAREN,
output: RETURN_PAREN,
parserOptions: parserOptions,
options: [{return: true}],
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: DECLARATION_NO_PAREN,
output: DECLARATION_PAREN,
parserOptions: parserOptions,
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: DECLARATION_NO_PAREN,
output: DECLARATION_PAREN,
parserOptions: parserOptions,
options: [{declaration: true}],
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: ASSIGNMENT_NO_PAREN,
output: ASSIGNMENT_PAREN,
parserOptions: parserOptions,
errors: [{message: 'Missing parentheses around multilines JSX'}]
}, {
code: ASSIGNMENT_NO_PAREN,
output: ASSIGNMENT_PAREN,
parserOptions: parserOptions,
options: [{assignment: true}],
errors: [{message: 'Missing parentheses around multilines JSX'}]
Expand Down

0 comments on commit 21e4857

Please sign in to comment.