Skip to content

Commit

Permalink
Fix prefer-stateless-function to allow components with childContextTy…
Browse files Browse the repository at this point in the history
…pes (fixes #853)
  • Loading branch information
yannickcr committed Sep 22, 2016
1 parent 644b2ee commit 38ff4f1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/rules/prefer-stateless-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ module.exports = {
});
};

/**
* Mark childContextTypes as declared
* @param {ASTNode} node The AST node being checked.
*/
var markChildContextTypesAsDeclared = function (node) {
components.set(node, {
hasChildContextTypes: true
});
};

/**
* Mark a setState as used
* @param {ASTNode} node The AST node being checked.
Expand Down Expand Up @@ -302,9 +312,17 @@ module.exports = {

// Mark `this` usage
MemberExpression: function(node) {
// Ignore calls to `this.props` and `this.context`
if (node.object.type !== 'ThisExpression') {
if (node.property && node.property.name === 'childContextTypes') {
var component = utils.getRelatedComponent(node);
if (!component) {
return;
}
markChildContextTypesAsDeclared(component.node);
return;
}
return;
// Ignore calls to `this.props` and `this.context`
} else if (
(node.property.name || node.property.value) === 'props' ||
(node.property.name || node.property.value) === 'context'
Expand Down Expand Up @@ -358,6 +376,7 @@ module.exports = {
list[component].useThis ||
list[component].useRef ||
list[component].invalidReturn ||
list[component].hasChildContextTypes ||
(!utils.isES5Component(list[component].node) && !utils.isES6Component(list[component].node))
) {
continue;
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/prefer-stateless-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ ruleTester.run('prefer-stateless-function', rule, {
');'
].join('\n'),
parser: 'babel-eslint'
}, {
// Has childContextTypes
code: [
'class Foo extends React.Component {',
' render() {',
' return <div>{this.props.children}</div>;',
' }',
'}',
'Foo.childContextTypes = {',
' color: React.PropTypes.string',
'};'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit 38ff4f1

Please sign in to comment.