Skip to content
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

prefer-stateless-function w/ decorators #1086

Merged
merged 1 commit into from Apr 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/prefer-stateless-function.md
Expand Up @@ -10,6 +10,7 @@ This rule will check your class based React components for
* instance property other than `this.props` and `this.context`
* extension of `React.PureComponent` (if the `ignorePureComponents` flag is true)
* presence of `ref` attribute in JSX
* the use of decorators
* `render` method that return anything but JSX: `undefined`, `null`, etc. (only in React <15.0.0, see [shared settings](https://github.com/yannickcr/eslint-plugin-react/blob/master/README.md#configuration) for React version configuration)

If none of these elements are found, the rule will warn you to write this component as a pure function.
Expand Down
15 changes: 15 additions & 0 deletions lib/rules/prefer-stateless-function.js
Expand Up @@ -286,11 +286,25 @@ module.exports = {
});
}

/**
* Mark a ClassDeclaration as having used decorators
* @param {ASTNode} node The AST node being checked.
*/
function markDecoratorsAsUsed(node) {
components.set(node, {
useDecorators: true
});
}

return {
ClassDeclaration: function (node) {
if (ignorePureComponents && utils.isPureComponent(node)) {
markSCUAsDeclared(node);
}

if (node.decorators && node.decorators.length) {
markDecoratorsAsUsed(node);
}
},

// Mark `this` destructuring as a usage of `this`
Expand Down Expand Up @@ -378,6 +392,7 @@ module.exports = {
list[component].useRef ||
list[component].invalidReturn ||
list[component].hasChildContextTypes ||
list[component].useDecorators ||
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
) {
continue;
Expand Down
34 changes: 34 additions & 0 deletions tests/lib/rules/prefer-stateless-function.js
Expand Up @@ -268,6 +268,40 @@ ruleTester.run('prefer-stateless-function', rule, {
'};'
].join('\n'),
parser: 'babel-eslint'
}, {
// Uses a decorator
code: [
'@foo',
'class Foo extends React.Component {',
' render() {',
' return <div>{this.props.foo}</div>;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
// Uses a called decorator
code: [
'@foo("bar")',
'class Foo extends React.Component {',
' render() {',
' return <div>{this.props.foo}</div>;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
// Uses multiple decorators
code: [
'@foo',
'@bar()',
'class Foo extends React.Component {',
' render() {',
' return <div>{this.props.foo}</div>;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down