Skip to content

Commit

Permalink
Merge pull request #886 from snowypowers/master
Browse files Browse the repository at this point in the history
[new] jsx-first-prop-new-line: autofix
  • Loading branch information
ljharb authored Nov 4, 2016
2 parents 16fe501 + a37687e commit 0220022
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Finally, enable all of the rules that you would like to use. Use [our preset](#
* [react/jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes (fixable)
* [react/jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes (fixable)
* [react/jsx-filename-extension](docs/rules/jsx-filename-extension.md): Restrict file extensions that may contain JSX
* [react/jsx-first-prop-new-line](docs/rules/jsx-first-prop-new-line.md): Enforce position of the first prop in JSX
* [react/jsx-first-prop-new-line](docs/rules/jsx-first-prop-new-line.md): Enforce position of the first prop in JSX (fixable)
* [react/jsx-handler-names](docs/rules/jsx-handler-names.md): Enforce event handler naming conventions in JSX
* [react/jsx-indent](docs/rules/jsx-indent.md): Validate JSX indentation
* [react/jsx-indent-props](docs/rules/jsx-indent-props.md): Validate props indentation in JSX (fixable)
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/jsx-first-prop-new-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Ensure correct position of the first property.

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

## Rule Details

This rule checks whether the first property of all JSX elements is correctly placed. There are three possible configurations:
Expand Down
14 changes: 11 additions & 3 deletions lib/rules/jsx-first-prop-new-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
category: 'Stylistic Issues',
recommended: false
},
fixable: 'code',

schema: [{
enum: ['always', 'never', 'multiline', 'multiline-multiprop']
Expand All @@ -35,20 +36,27 @@ module.exports = {
(configuration === 'multiline-multiprop' && isMultilineJSX(node) && node.attributes.length > 1) ||
(configuration === 'always')
) {
node.attributes.forEach(function(decl) {
node.attributes.some(function(decl) {
if (decl.loc.start.line === node.loc.start.line) {
context.report({
node: decl,
message: 'Property should be placed on a new line'
message: 'Property should be placed on a new line',
fix: function(fixer) {
return fixer.replaceTextRange([node.name.end, decl.start], '\n');
}
});
}
return true;
});
} else if (configuration === 'never' && node.attributes.length > 0) {
var firstNode = node.attributes[0];
if (node.loc.start.line < firstNode.loc.start.line) {
context.report({
node: firstNode,
message: 'Property should be placed on the same line as the component declaration'
message: 'Property should be placed on the same line as the component declaration',
fix: function(fixer) {
return fixer.replaceTextRange([node.name.end, firstNode.start], ' ');
}
});
return;
}
Expand Down
31 changes: 28 additions & 3 deletions tests/lib/rules/jsx-first-prop-new-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ ruleTester.run('jsx-first-prop-new-line', rule, {

invalid: [
{
code: '<Foo prop="one" />',
code: '<Foo propOne="one" propTwo="two" />',
output: [
'<Foo',
'propOne="one" propTwo="two" />'
].join('\n'),
options: ['always'],
errors: [{message: 'Property should be placed on a new line'}],
parser: parserOptions
Expand All @@ -163,15 +167,26 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
' propTwo="two"',
'/>'
].join('\n'),
output: [
'<Foo',
'propOne="one"',
' propTwo="two"',
'/>'
].join('\n'),
options: ['always'],
errors: [{message: 'Property should be placed on a new line'}],
parser: parserOptions
},
{
code: [
'<Foo',
' propOne="one"',
' propTwo="two"',
' propOne="one"',
' propTwo="two"',
'/>'
].join('\n'),
output: [
'<Foo propOne="one"',
' propTwo="two"',
'/>'
].join('\n'),
options: ['never'],
Expand All @@ -183,6 +198,11 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
'<Foo prop={{',
'}} />'
].join('\n'),
output: [
'<Foo',
'prop={{',
'}} />'
].join('\n'),
options: ['multiline'],
errors: [{message: 'Property should be placed on a new line'}],
parser: parserOptions
Expand All @@ -192,6 +212,11 @@ ruleTester.run('jsx-first-prop-new-line', rule, {
'<Foo bar={{',
'}} baz />'
].join('\n'),
output: [
'<Foo',
'bar={{',
'}} baz />'
].join('\n'),
options: ['multiline-multiprop'],
errors: [{message: 'Property should be placed on a new line'}],
parser: parserOptions
Expand Down

0 comments on commit 0220022

Please sign in to comment.