Skip to content

Commit f36b0fa

Browse files
authored
Merge pull request #1522 from jomasti/issue-1521
Account for defaultProps in prefer-stateless rule
2 parents 2010205 + 5c6b355 commit f36b0fa

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

docs/rules/prefer-stateless-function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Stateless functional components are simpler than class based components and will
66

77
This rule will check your class based React components for
88

9-
* methods/properties other than `displayName`, `propTypes`, `render` and useless constructor (same detection as ESLint [no-useless-constructor rule](http://eslint.org/docs/rules/no-useless-constructor))
9+
* methods/properties other than `displayName`, `propTypes`, `contextTypes`, `defaultProps`, `render` and useless constructor (same detection as ESLint [no-useless-constructor rule](http://eslint.org/docs/rules/no-useless-constructor))
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

lib/rules/prefer-stateless-function.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,13 @@ module.exports = {
216216
const isDisplayName = name === 'displayName';
217217
const isPropTypes = name === 'propTypes' || name === 'props' && property.typeAnnotation;
218218
const contextTypes = name === 'contextTypes';
219+
const defaultProps = name === 'defaultProps';
219220
const isUselessConstructor =
220221
property.kind === 'constructor' &&
221222
isRedundantSuperCall(property.value.body.body, property.value.params)
222223
;
223224
const isRender = name === 'render';
224-
return !isDisplayName && !isPropTypes && !contextTypes && !isUselessConstructor && !isRender;
225+
return !isDisplayName && !isPropTypes && !contextTypes && !defaultProps && !isUselessConstructor && !isRender;
225226
});
226227
}
227228

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,105 @@ ruleTester.run('prefer-stateless-function', rule, {
496496
}
497497
}
498498
`,
499+
errors: [{
500+
message: 'Component should be written as a pure function'
501+
}]
502+
}, {
503+
code: `
504+
class Foo extends React.Component {
505+
static defaultProps = {
506+
foo: true
507+
}
508+
render() {
509+
const { foo } = this.props;
510+
return foo ? <div /> : null;
511+
}
512+
}
513+
`,
499514
parser: 'babel-eslint',
500515
errors: [{
501516
message: 'Component should be written as a pure function'
502517
}]
518+
}, {
519+
code: `
520+
class Foo extends React.Component {
521+
static get defaultProps() {
522+
return {
523+
foo: true
524+
};
525+
}
526+
render() {
527+
const { foo } = this.props;
528+
return foo ? <div /> : null;
529+
}
530+
}
531+
`,
532+
errors: [{
533+
message: 'Component should be written as a pure function'
534+
}]
535+
}, {
536+
code: `
537+
class Foo extends React.Component {
538+
render() {
539+
const { foo } = this.props;
540+
return foo ? <div /> : null;
541+
}
542+
}
543+
Foo.defaultProps = {
544+
foo: true
545+
};
546+
`,
547+
errors: [{
548+
message: 'Component should be written as a pure function'
549+
}]
550+
}, {
551+
code: `
552+
class Foo extends React.Component {
553+
static contextTypes = {
554+
foo: PropTypes.boolean
555+
}
556+
render() {
557+
const { foo } = this.context;
558+
return foo ? <div /> : null;
559+
}
560+
}
561+
`,
562+
parser: 'babel-eslint',
563+
errors: [{
564+
message: 'Component should be written as a pure function'
565+
}]
566+
}, {
567+
code: `
568+
class Foo extends React.Component {
569+
static get contextTypes() {
570+
return {
571+
foo: PropTypes.boolean
572+
};
573+
}
574+
render() {
575+
const { foo } = this.context;
576+
return foo ? <div /> : null;
577+
}
578+
}
579+
`,
580+
errors: [{
581+
message: 'Component should be written as a pure function'
582+
}]
583+
}, {
584+
code: `
585+
class Foo extends React.Component {
586+
render() {
587+
const { foo } = this.context;
588+
return foo ? <div /> : null;
589+
}
590+
}
591+
Foo.contextTypes = {
592+
foo: PropTypes.boolean
593+
};
594+
`,
595+
errors: [{
596+
message: 'Component should be written as a pure function'
597+
}]
503598
}
504599
]
505600
});

0 commit comments

Comments
 (0)