-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yannick Croissant
committed
Dec 28, 2014
1 parent
00ca2e6
commit 4b49bd0
Showing
6 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Prevent missing parentheses around multilines JSX (wrap-multilines) | ||
|
||
Wrapping multilines JSX in parentheses can improve readability and/or convenience. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
var Hello = React.createClass({ | ||
render: function() { | ||
return <div> | ||
<p>Hello {this.props.name}</p> | ||
</div>; | ||
} | ||
}); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
var singleLineJSX = <p>Hello</p> | ||
|
||
var Hello = React.createClass({ | ||
render: function() { | ||
return ( | ||
<div> | ||
<p>Hello {this.props.name}</p> | ||
</div> | ||
); | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @fileoverview Prevent missing parentheses around multilines JSX | ||
* @author Yannick Croissant | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
function isParenthesised(node) { | ||
var previousToken = context.getTokenBefore(node); | ||
var nextToken = context.getTokenAfter(node); | ||
|
||
return previousToken && nextToken && | ||
previousToken.value === '(' && previousToken.range[1] <= node.range[0] && | ||
nextToken.value === ')' && nextToken.range[0] >= node.range[1]; | ||
} | ||
|
||
function isMultilines(node) { | ||
return node.loc.start.line !== node.loc.end.line; | ||
} | ||
|
||
function check(node) { | ||
if (!node || node.type !== 'XJSElement') { | ||
return; | ||
} | ||
|
||
if (!isParenthesised(node) && isMultilines(node)) { | ||
context.report(node, 'Missing parentheses around multilines JSX'); | ||
} | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
'VariableDeclarator': function(node) { | ||
check(node.init); | ||
}, | ||
|
||
'AssignmentExpression': function(node) { | ||
check(node.right); | ||
}, | ||
|
||
'ReturnStatement': function(node) { | ||
check(node.argument); | ||
} | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @fileoverview Prevent missing parentheses around multilines JSX | ||
* @author Yannick Croissant | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var eslint = require('eslint').linter; | ||
var ESLintTester = require('eslint-tester'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var eslintTester = new ESLintTester(eslint); | ||
eslintTester.addRuleTest('lib/rules/wrap-multilines', { | ||
|
||
valid: [ | ||
{ | ||
code: 'var Hello = React.createClass({render: function() {return <p>Hello {this.props.name}</p>;}});', | ||
settings: { | ||
ecmascript: 6, | ||
jsx: true | ||
} | ||
}, { | ||
code: 'var Hello = React.createClass({render: function() {return (\n<div>\n<p>Hello {this.props.name}</p>\n</div>\n);}});', | ||
settings: { | ||
ecmascript: 6, | ||
jsx: true | ||
} | ||
}, { | ||
code: 'var hello = <p>Hello</p>;', | ||
settings: { | ||
ecmascript: 6, | ||
jsx: true | ||
} | ||
}, { | ||
code: 'var hello = (\n<div>\n<p>Hello</p>\n</div>\n);', | ||
settings: { | ||
ecmascript: 6, | ||
jsx: true | ||
} | ||
}, { | ||
code: 'var hello; hello = (\n<div>\n<p>Hello</p>\n</div>\n);', | ||
settings: { | ||
ecmascript: 6, | ||
jsx: true | ||
} | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'var Hello = React.createClass({render: function() {return <div>\n<p>Hello {this.props.name}</p>\n</div>;}});', | ||
settings: { | ||
ecmascript: 6, | ||
jsx: true | ||
}, | ||
errors: [{ | ||
message: 'Missing parentheses around multilines JSX' | ||
}] | ||
}, { | ||
code: 'var hello = <div>\n<p>Hello</p>\n</div>;', | ||
settings: { | ||
ecmascript: 6, | ||
jsx: true | ||
}, | ||
errors: [{ | ||
message: 'Missing parentheses around multilines JSX' | ||
}] | ||
}, { | ||
code: 'var hello; hello = <div>\n<p>Hello</p>\n</div>;', | ||
settings: { | ||
ecmascript: 6, | ||
jsx: true | ||
}, | ||
errors: [{ | ||
message: 'Missing parentheses around multilines JSX' | ||
}] | ||
} | ||
] | ||
}); |