Skip to content

Commit

Permalink
Fix props validation in constructor (fixes #254)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Oct 18, 2015
1 parent eb985f5 commit 7cd9b39
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,31 @@ module.exports = function(context) {
return true;
}

/**
* Check if we are in a class constructor
* @return {boolean} true if we are in a class constructor, false if not
*/
function inConstructor() {
var scope = context.getScope();
while (scope) {
if (scope.block && scope.block.parent && scope.block.parent.kind === 'constructor') {
return true;
}
scope = scope.upper;
}
return false;
}

/**
* Retrieve the name of a property node
* @param {ASTNode} node The AST node with the property.
* @return {string} the name of the property or undefined if not found
*/
function getPropertyName(node) {
if (componentUtil.getNode(context, node)) {
if (componentUtil.getNode(context, node) && !inConstructor()) {
if (node.object.name === 'props') {
return void 0;
}
node = node.parent;
}
var property = node.property;
Expand Down Expand Up @@ -400,7 +418,7 @@ module.exports = function(context) {
usedPropTypes.push({
name: name,
allNames: allNames,
node: node.object.name !== 'props' ? node.parent.property : node.property
node: node.object.name !== 'props' && !inConstructor() ? node.parent.property : node.property
});
break;
case 'destructuring':
Expand Down
40 changes: 40 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,19 @@ ruleTester.run('prop-types', rule, {
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'class Hello extends React.Component {',
' constructor(props, context) {',
' super(props, context)',
' this.state = { status: props.source.uri }',
' }',
' static propTypes = {',
' source: PropTypes.object',
' }',
'}'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down Expand Up @@ -1236,6 +1249,33 @@ ruleTester.run('prop-types', rule, {
errors: [
{message: '\'lastname\' is missing in props validation for Hello'}
]
}, {
code: [
'class Hello extends React.Component {',
' constructor(props, context) {',
' super(props, context)',
' this.state = { status: props.source }',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
errors: [
{message: '\'source\' is missing in props validation for Hello'}
]
}, {
code: [
'class Hello extends React.Component {',
' constructor(props, context) {',
' super(props, context)',
' this.state = { status: props.source.uri }',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
errors: [
{message: '\'source\' is missing in props validation for Hello'},
{message: '\'source.uri\' is missing in props validation for Hello'}
]
}
]
});

0 comments on commit 7cd9b39

Please sign in to comment.