Skip to content

Commit 0220022

Browse files
authored
Merge pull request #886 from snowypowers/master
[new] jsx-first-prop-new-line: autofix
2 parents 16fe501 + a37687e commit 0220022

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Finally, enable all of the rules that you would like to use. Use [our preset](#
117117
* [react/jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes (fixable)
118118
* [react/jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes (fixable)
119119
* [react/jsx-filename-extension](docs/rules/jsx-filename-extension.md): Restrict file extensions that may contain JSX
120-
* [react/jsx-first-prop-new-line](docs/rules/jsx-first-prop-new-line.md): Enforce position of the first prop in JSX
120+
* [react/jsx-first-prop-new-line](docs/rules/jsx-first-prop-new-line.md): Enforce position of the first prop in JSX (fixable)
121121
* [react/jsx-handler-names](docs/rules/jsx-handler-names.md): Enforce event handler naming conventions in JSX
122122
* [react/jsx-indent](docs/rules/jsx-indent.md): Validate JSX indentation
123123
* [react/jsx-indent-props](docs/rules/jsx-indent-props.md): Validate props indentation in JSX (fixable)

docs/rules/jsx-first-prop-new-line.md

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

33
Ensure correct position of the first property.
44

5+
**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line. However, fix does not include indentation. Please rerun lint to correct those errors.
6+
57
## Rule Details
68

79
This rule checks whether the first property of all JSX elements is correctly placed. There are three possible configurations:

lib/rules/jsx-first-prop-new-line.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
category: 'Stylistic Issues',
1616
recommended: false
1717
},
18+
fixable: 'code',
1819

1920
schema: [{
2021
enum: ['always', 'never', 'multiline', 'multiline-multiprop']
@@ -35,20 +36,27 @@ module.exports = {
3536
(configuration === 'multiline-multiprop' && isMultilineJSX(node) && node.attributes.length > 1) ||
3637
(configuration === 'always')
3738
) {
38-
node.attributes.forEach(function(decl) {
39+
node.attributes.some(function(decl) {
3940
if (decl.loc.start.line === node.loc.start.line) {
4041
context.report({
4142
node: decl,
42-
message: 'Property should be placed on a new line'
43+
message: 'Property should be placed on a new line',
44+
fix: function(fixer) {
45+
return fixer.replaceTextRange([node.name.end, decl.start], '\n');
46+
}
4347
});
4448
}
49+
return true;
4550
});
4651
} else if (configuration === 'never' && node.attributes.length > 0) {
4752
var firstNode = node.attributes[0];
4853
if (node.loc.start.line < firstNode.loc.start.line) {
4954
context.report({
5055
node: firstNode,
51-
message: 'Property should be placed on the same line as the component declaration'
56+
message: 'Property should be placed on the same line as the component declaration',
57+
fix: function(fixer) {
58+
return fixer.replaceTextRange([node.name.end, firstNode.start], ' ');
59+
}
5260
});
5361
return;
5462
}

tests/lib/rules/jsx-first-prop-new-line.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
152152

153153
invalid: [
154154
{
155-
code: '<Foo prop="one" />',
155+
code: '<Foo propOne="one" propTwo="two" />',
156+
output: [
157+
'<Foo',
158+
'propOne="one" propTwo="two" />'
159+
].join('\n'),
156160
options: ['always'],
157161
errors: [{message: 'Property should be placed on a new line'}],
158162
parser: parserOptions
@@ -163,15 +167,26 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
163167
' propTwo="two"',
164168
'/>'
165169
].join('\n'),
170+
output: [
171+
'<Foo',
172+
'propOne="one"',
173+
' propTwo="two"',
174+
'/>'
175+
].join('\n'),
166176
options: ['always'],
167177
errors: [{message: 'Property should be placed on a new line'}],
168178
parser: parserOptions
169179
},
170180
{
171181
code: [
172182
'<Foo',
173-
' propOne="one"',
174-
' propTwo="two"',
183+
' propOne="one"',
184+
' propTwo="two"',
185+
'/>'
186+
].join('\n'),
187+
output: [
188+
'<Foo propOne="one"',
189+
' propTwo="two"',
175190
'/>'
176191
].join('\n'),
177192
options: ['never'],
@@ -183,6 +198,11 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
183198
'<Foo prop={{',
184199
'}} />'
185200
].join('\n'),
201+
output: [
202+
'<Foo',
203+
'prop={{',
204+
'}} />'
205+
].join('\n'),
186206
options: ['multiline'],
187207
errors: [{message: 'Property should be placed on a new line'}],
188208
parser: parserOptions
@@ -192,6 +212,11 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
192212
'<Foo bar={{',
193213
'}} baz />'
194214
].join('\n'),
215+
output: [
216+
'<Foo',
217+
'bar={{',
218+
'}} baz />'
219+
].join('\n'),
195220
options: ['multiline-multiprop'],
196221
errors: [{message: 'Property should be placed on a new line'}],
197222
parser: parserOptions

0 commit comments

Comments
 (0)