-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New rule: Report usages of class methods besides render returning JSX #1580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jomasti
wants to merge
5
commits into
jsx-eslint:master
Choose a base branch
from
jomasti:issue-578
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8dd536e
Add first iteration of prefer-separate-components
jomasti 822a72d
Catch Array#map returning JSX
jomasti bdcb2d7
Check ES5 components in prefer-separate-components
jomasti bf92279
Add createElement tests for prefer-separate rule
jomasti 387d0f9
Document prefer-separate-components rule
jomasti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,95 @@ | ||
# Report usages of class methods besides render returning JSX (react/prefer-separate-components) | ||
|
||
If component functions other than `render` are returning JSX, they should be moved into separate components. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
renderBar() { | ||
return <span>bar</span>; | ||
} | ||
render() { | ||
return ( | ||
<div> | ||
foo | ||
{this.renderBar()} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class Foo extends React.Component { | ||
renderBar() { | ||
return React.createElement('span', null, 'bar'); | ||
} | ||
render() { | ||
return React.createElement( | ||
'div', | ||
null, | ||
'foo', | ||
this.renderBar() | ||
); | ||
} | ||
} | ||
|
||
class Foo extends React.Component { | ||
renderBars() { | ||
const bars = ['bar', 'bar', 'bar']; | ||
return bars.map((bar) => <span>{bar}</span>); | ||
} | ||
render() { | ||
return ( | ||
<div> | ||
foo | ||
{this.renderBars()} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class Foo extends React.Component { | ||
renderBars() { | ||
const bars = ['bar', 'bar', 'bar']; | ||
return bars.map((bar) => React.createElement('span', null, bar)); | ||
} | ||
render() { | ||
return React.createElement( | ||
'div', | ||
null, | ||
'foo', | ||
this.renderBars() | ||
); | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
render() { | ||
return <div>foo</div>; | ||
} | ||
} | ||
|
||
class Foo extends React.Component { | ||
render() { | ||
return React.createElement('div', null, 'foo'); | ||
} | ||
} | ||
|
||
createReactClass({ | ||
render() { | ||
return <div>foo</div>; | ||
} | ||
}); | ||
|
||
createReactClass({ | ||
render() { | ||
return React.createElement('div', null, 'foo'); | ||
} | ||
}); | ||
``` |
This file contains hidden or 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 hidden or 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,53 @@ | ||
/** | ||
* @fileoverview Report usages of class methods besides render returning JSX | ||
*/ | ||
'use strict'; | ||
|
||
const Components = require('../util/Components'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Report usages of class methods besides render returning JSX', | ||
category: 'Best Practices', | ||
recommended: false | ||
}, | ||
schema: [] | ||
}, | ||
|
||
create: Components.detect((context, components, utils) => { | ||
function report(node) { | ||
context.report({ | ||
node, | ||
message: '{{ function }} should be moved into a separate component.', | ||
data: { | ||
function: node.key.name | ||
} | ||
}); | ||
} | ||
|
||
return { | ||
MethodDefinition: function(node) { | ||
if (!utils.getParentES6Component()) { | ||
return; | ||
} | ||
if (node.key.name !== 'render' && utils.isReturningJSX(node)) { | ||
report(node); | ||
} | ||
}, | ||
|
||
Property: function(node) { | ||
if (!utils.getParentES5Component) { | ||
return; | ||
} | ||
if (node.key.name !== 'render' && utils.isReturningJSX(node)) { | ||
report(node); | ||
} | ||
} | ||
}; | ||
}) | ||
}; |
This file contains hidden or 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 hidden or 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,198 @@ | ||
/** | ||
* @fileoverview Report usages of class methods besides render returning JSX | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/prefer-separate-components'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const parserOptions = { | ||
ecmaVersion: 8, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
jsx: true | ||
} | ||
}; | ||
|
||
const ruleTester = new RuleTester({parserOptions}); | ||
ruleTester.run('prefer-separate-components', rule, { | ||
valid: [{ | ||
code: ` | ||
class Foo extends React.Component { | ||
render() { | ||
return <div>foo</div>; | ||
} | ||
} | ||
` | ||
}, { | ||
code: ` | ||
class Foo extends React.Component { | ||
render() { | ||
return React.createElement('div', null, 'foo'); | ||
} | ||
} | ||
` | ||
}, { | ||
code: ` | ||
createReactClass({ | ||
render() { | ||
return <div>foo</div>; | ||
} | ||
}); | ||
` | ||
}, { | ||
code: ` | ||
createReactClass({ | ||
render() { | ||
return React.createElement('div', null, 'foo'); | ||
} | ||
}); | ||
` | ||
}], | ||
invalid: [{ | ||
code: ` | ||
class Foo extends React.Component { | ||
renderBar() { | ||
return <span>bar</span>; | ||
} | ||
render() { | ||
return ( | ||
<div> | ||
foo | ||
{this.renderBar()} | ||
</div> | ||
); | ||
} | ||
} | ||
`, | ||
errors: 1 | ||
}, { | ||
code: ` | ||
class Foo extends React.Component { | ||
renderBar() { | ||
return React.createElement('span', null, 'bar'); | ||
} | ||
render() { | ||
return React.createElement( | ||
'div', | ||
null, | ||
'foo', | ||
this.renderBar() | ||
); | ||
} | ||
} | ||
`, | ||
errors: 1 | ||
}, { | ||
code: ` | ||
class Foo extends React.Component { | ||
renderBars() { | ||
const bars = ['bar', 'bar', 'bar']; | ||
return bars.map((bar) => <span>{bar}</span>); | ||
} | ||
render() { | ||
return ( | ||
<div> | ||
foo | ||
{this.renderBars()} | ||
</div> | ||
); | ||
} | ||
} | ||
`, | ||
errors: 1 | ||
}, { | ||
code: ` | ||
class Foo extends React.Component { | ||
renderBars() { | ||
const bars = ['bar', 'bar', 'bar']; | ||
return bars.map((bar) => React.createElement('span', null, bar)); | ||
} | ||
render() { | ||
return React.createElement( | ||
'div', | ||
null, | ||
'foo', | ||
this.renderBars() | ||
); | ||
} | ||
} | ||
`, | ||
errors: 1 | ||
}, { | ||
code: ` | ||
createReactClass({ | ||
renderBars() { | ||
const bars = ['bar', 'bar', 'bar']; | ||
return bars.map((bar) => <span>{bar}</span>); | ||
}, | ||
render() { | ||
return ( | ||
<div> | ||
foo | ||
{this.renderBars()} | ||
</div> | ||
); | ||
} | ||
}); | ||
`, | ||
errors: 1 | ||
}, { | ||
code: ` | ||
createReactClass({ | ||
renderBars() { | ||
const bars = ['bar', 'bar', 'bar']; | ||
return bars.map((bar) => React.createElement('span', null, bar)); | ||
}, | ||
render() { | ||
return React.createElement( | ||
'div', | ||
null, | ||
'foo', | ||
this.renderBars() | ||
); | ||
} | ||
}); | ||
`, | ||
errors: 1 | ||
}, { | ||
code: ` | ||
createReactClass({ | ||
renderBar() { | ||
return <span>bar</span>; | ||
}, | ||
render() { | ||
return ( | ||
<div> | ||
foo | ||
{this.renderBar()} | ||
</div> | ||
); | ||
} | ||
}); | ||
`, | ||
errors: 1 | ||
}, { | ||
code: ` | ||
createReactClass({ | ||
renderBar() { | ||
return React.createElement('span', null, 'bar'); | ||
}, | ||
render() { | ||
return React.createElement( | ||
'div', | ||
null, | ||
'foo', | ||
this.renderBar() | ||
); | ||
} | ||
}); | ||
`, | ||
errors: 1 | ||
}] | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm concerned about this approach; there's other things that have a
.map
method. How can we know this is an array?