Skip to content

Commit fd90dae

Browse files
authored
Merge pull request #1086 from benstepp/bs-stateless-decorators
prefer-stateless-function w/ decorators
2 parents 82648f3 + 3266ab6 commit fd90dae

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

docs/rules/prefer-stateless-function.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This rule will check your class based React components for
1010
* instance property other than `this.props` and `this.context`
1111
* extension of `React.PureComponent` (if the `ignorePureComponents` flag is true)
1212
* presence of `ref` attribute in JSX
13+
* the use of decorators
1314
* `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)
1415

1516
If none of these elements are found, the rule will warn you to write this component as a pure function.

lib/rules/prefer-stateless-function.js

+15
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,25 @@ module.exports = {
286286
});
287287
}
288288

289+
/**
290+
* Mark a ClassDeclaration as having used decorators
291+
* @param {ASTNode} node The AST node being checked.
292+
*/
293+
function markDecoratorsAsUsed(node) {
294+
components.set(node, {
295+
useDecorators: true
296+
});
297+
}
298+
289299
return {
290300
ClassDeclaration: function (node) {
291301
if (ignorePureComponents && utils.isPureComponent(node)) {
292302
markSCUAsDeclared(node);
293303
}
304+
305+
if (node.decorators && node.decorators.length) {
306+
markDecoratorsAsUsed(node);
307+
}
294308
},
295309

296310
// Mark `this` destructuring as a usage of `this`
@@ -378,6 +392,7 @@ module.exports = {
378392
list[component].useRef ||
379393
list[component].invalidReturn ||
380394
list[component].hasChildContextTypes ||
395+
list[component].useDecorators ||
381396
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
382397
) {
383398
continue;

tests/lib/rules/prefer-stateless-function.js

+34
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,40 @@ ruleTester.run('prefer-stateless-function', rule, {
268268
'};'
269269
].join('\n'),
270270
parser: 'babel-eslint'
271+
}, {
272+
// Uses a decorator
273+
code: [
274+
'@foo',
275+
'class Foo extends React.Component {',
276+
' render() {',
277+
' return <div>{this.props.foo}</div>;',
278+
' }',
279+
'}'
280+
].join('\n'),
281+
parser: 'babel-eslint'
282+
}, {
283+
// Uses a called decorator
284+
code: [
285+
'@foo("bar")',
286+
'class Foo extends React.Component {',
287+
' render() {',
288+
' return <div>{this.props.foo}</div>;',
289+
' }',
290+
'}'
291+
].join('\n'),
292+
parser: 'babel-eslint'
293+
}, {
294+
// Uses multiple decorators
295+
code: [
296+
'@foo',
297+
'@bar()',
298+
'class Foo extends React.Component {',
299+
' render() {',
300+
' return <div>{this.props.foo}</div>;',
301+
' }',
302+
'}'
303+
].join('\n'),
304+
parser: 'babel-eslint'
271305
}
272306
],
273307

0 commit comments

Comments
 (0)