Skip to content

Commit 38ff4f1

Browse files
committed
Fix prefer-stateless-function to allow components with childContextTypes (fixes #853)
1 parent 644b2ee commit 38ff4f1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/rules/prefer-stateless-function.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ module.exports = {
235235
});
236236
};
237237

238+
/**
239+
* Mark childContextTypes as declared
240+
* @param {ASTNode} node The AST node being checked.
241+
*/
242+
var markChildContextTypesAsDeclared = function (node) {
243+
components.set(node, {
244+
hasChildContextTypes: true
245+
});
246+
};
247+
238248
/**
239249
* Mark a setState as used
240250
* @param {ASTNode} node The AST node being checked.
@@ -302,9 +312,17 @@ module.exports = {
302312

303313
// Mark `this` usage
304314
MemberExpression: function(node) {
305-
// Ignore calls to `this.props` and `this.context`
306315
if (node.object.type !== 'ThisExpression') {
316+
if (node.property && node.property.name === 'childContextTypes') {
317+
var component = utils.getRelatedComponent(node);
318+
if (!component) {
319+
return;
320+
}
321+
markChildContextTypesAsDeclared(component.node);
322+
return;
323+
}
307324
return;
325+
// Ignore calls to `this.props` and `this.context`
308326
} else if (
309327
(node.property.name || node.property.value) === 'props' ||
310328
(node.property.name || node.property.value) === 'context'
@@ -358,6 +376,7 @@ module.exports = {
358376
list[component].useThis ||
359377
list[component].useRef ||
360378
list[component].invalidReturn ||
379+
list[component].hasChildContextTypes ||
361380
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
362381
) {
363382
continue;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,19 @@ ruleTester.run('prefer-stateless-function', rule, {
255255
');'
256256
].join('\n'),
257257
parser: 'babel-eslint'
258+
}, {
259+
// Has childContextTypes
260+
code: [
261+
'class Foo extends React.Component {',
262+
' render() {',
263+
' return <div>{this.props.children}</div>;',
264+
' }',
265+
'}',
266+
'Foo.childContextTypes = {',
267+
' color: React.PropTypes.string',
268+
'};'
269+
].join('\n'),
270+
parser: 'babel-eslint'
258271
}
259272
],
260273

0 commit comments

Comments
 (0)