Skip to content

Commit 21e4857

Browse files
committed
Add auto fix for wrap-multiline
1 parent 3371be2 commit 21e4857

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Finally, enable all of the rules that you would like to use.
151151
* [require-extension](docs/rules/require-extension.md): Restrict file extensions that may be required
152152
* [self-closing-comp](docs/rules/self-closing-comp.md): Prevent extra closing tags for components without children
153153
* [sort-comp](docs/rules/sort-comp.md): Enforce component methods order
154-
* [wrap-multilines](docs/rules/wrap-multilines.md): Prevent missing parentheses around multilines JSX
154+
* [wrap-multilines](docs/rules/wrap-multilines.md): Prevent missing parentheses around multilines JSX (fixable)
155155

156156
## React Native
157157

docs/rules/wrap-multilines.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

5+
**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.
6+
57
## Rule Details
68

79
The following patterns are considered warnings:

lib/rules/wrap-multilines.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var DEFAULTS = {
2020

2121
module.exports = function(context) {
2222

23+
var sourceCode = context.getSourceCode();
24+
2325
function isParenthesised(node) {
2426
var previousToken = context.getTokenBefore(node);
2527
var nextToken = context.getTokenAfter(node);
@@ -39,7 +41,13 @@ module.exports = function(context) {
3941
}
4042

4143
if (!isParenthesised(node) && isMultilines(node)) {
42-
context.report(node, 'Missing parentheses around multilines JSX');
44+
context.report({
45+
node: node,
46+
message: 'Missing parentheses around multilines JSX',
47+
fix: function(fixer) {
48+
return fixer.replaceText(node, '(' + sourceCode.getText(node) + ')');
49+
}
50+
});
4351
}
4452
}
4553

tests/lib/rules/wrap-multilines.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ var RETURN_SINGLE_LINE = '\
3232
var RETURN_PAREN = '\
3333
var Hello = React.createClass({\
3434
render: function() {\
35-
return (\n\
36-
<div>\n\
37-
<p>Hello {this.props.name}</p>\n\
38-
</div>\n\
39-
);\
35+
return (<div>\n\
36+
<p>Hello {this.props.name}</p>\n\
37+
</div>);\
4038
}\
4139
});';
4240

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

5452
var DECLARATION_PAREN = '\
55-
var hello = (\n\
56-
<div>\n\
57-
<p>Hello</p>\n\
58-
</div>\n\
59-
);';
53+
var hello = (<div>\n\
54+
<p>Hello</p>\n\
55+
</div>);';
6056

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

6864
var ASSIGNMENT_PAREN = '\
6965
var hello;\
70-
hello = (\n\
71-
<div>\n\
72-
<p>Hello</p>\n\
73-
</div>\n\
74-
);';
66+
hello = (<div>\n\
67+
<p>Hello</p>\n\
68+
</div>);';
7569

7670
var ASSIGNMENT_NO_PAREN = '\
7771
var hello;\
@@ -124,28 +118,34 @@ ruleTester.run('wrap-multilines', rule, {
124118
invalid: [
125119
{
126120
code: RETURN_NO_PAREN,
121+
output: RETURN_PAREN,
127122
parserOptions: parserOptions,
128123
errors: [{message: 'Missing parentheses around multilines JSX'}]
129124
}, {
130125
code: RETURN_NO_PAREN,
126+
output: RETURN_PAREN,
131127
parserOptions: parserOptions,
132128
options: [{return: true}],
133129
errors: [{message: 'Missing parentheses around multilines JSX'}]
134130
}, {
135131
code: DECLARATION_NO_PAREN,
132+
output: DECLARATION_PAREN,
136133
parserOptions: parserOptions,
137134
errors: [{message: 'Missing parentheses around multilines JSX'}]
138135
}, {
139136
code: DECLARATION_NO_PAREN,
137+
output: DECLARATION_PAREN,
140138
parserOptions: parserOptions,
141139
options: [{declaration: true}],
142140
errors: [{message: 'Missing parentheses around multilines JSX'}]
143141
}, {
144142
code: ASSIGNMENT_NO_PAREN,
143+
output: ASSIGNMENT_PAREN,
145144
parserOptions: parserOptions,
146145
errors: [{message: 'Missing parentheses around multilines JSX'}]
147146
}, {
148147
code: ASSIGNMENT_NO_PAREN,
148+
output: ASSIGNMENT_PAREN,
149149
parserOptions: parserOptions,
150150
options: [{assignment: true}],
151151
errors: [{message: 'Missing parentheses around multilines JSX'}]

0 commit comments

Comments
 (0)