Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] stop crashing when assigning to propTypes #1932

Merged
merged 1 commit into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/util/propTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ module.exports = function propTypesInstructions(context, components, utils) {
const component = components.get(componentNode);
const declaredPropTypes = component && component.declaredPropTypes || {};
let ignorePropsValidation = component && component.ignorePropsValidation || false;

switch (propTypes && propTypes.type) {
case 'ObjectTypeAnnotation':
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(propTypes, declaredPropTypes);
Expand Down Expand Up @@ -470,13 +469,18 @@ module.exports = function propTypesInstructions(context, components, utils) {
}
}
if (propTypes && propTypes.parent && propTypes.property) {
if (!(propTypes === propTypes.parent.left && propTypes.parent.left.object)) {
ignorePropsValidation = true;
break;
}
const parentProp = context.getSource(propTypes.parent.left.object).replace(/^.*\.propTypes\./, '');
const types = buildReactDeclarationTypes(
propTypes.parent.right,
propTypes.parent.left.object.property.name
parentProp
);

types.name = propTypes.property.name;
types.fullName = propTypes.property.name;
types.fullName = [parentProp, propTypes.property.name].join('.');
types.node = propTypes.property;
curDeclaredPropTypes[propTypes.property.name] = types;
} else {
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4642,6 +4642,29 @@ ruleTester.run('no-unused-prop-types', rule, {
errors: [{
message: '\'age\' PropType is defined but prop is never used'
}]
}, {
code: [
'class Hello extends React.Component {',
' render() {',
' return <div>Hello</div>;',
' }',
'}',
'Hello.propTypes = {',
' a: PropTypes.shape({',
' b: PropTypes.shape({',
' })',
' })',
'};',
'Hello.propTypes.a.b.c = PropTypes.number;'
].join('\n'),
options: [{skipShapeProps: false}],
errors: [{
message: '\'a\' PropType is defined but prop is never used'
}, {
message: '\'a.b\' PropType is defined but prop is never used'
}, {
message: '\'a.b.c\' PropType is defined but prop is never used'
}]
}

/* , {
Expand Down
34 changes: 34 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,24 @@ ruleTester.run('prop-types', rule, {
}
`,
parser: 'babel-eslint'
},
{
code: `
const Slider = props => (
<RcSlider {...props} />
);

Slider.propTypes = RcSlider.propTypes;
`
},
{
code: `
const Slider = props => (
<RcSlider foo={props.bar} />
);

Slider.propTypes = RcSlider.propTypes;
`
}
],

Expand Down Expand Up @@ -2137,6 +2155,22 @@ ruleTester.run('prop-types', rule, {
errors: [{
message: '\'a.b.c\' is missing in props validation'
}]
}, {
code: [
'class Hello extends React.Component {',
' render() {',
' this.props.a.b.c;',
' return <div>Hello</div>;',
' }',
'}',
'Hello.propTypes = {',
' a: PropTypes.shape({})',
'};',
'Hello.propTypes.a.b = PropTypes.shape({});'
].join('\n'),
errors: [{
message: '\'a.b.c\' is missing in props validation'
}]
}, {
code: [
'class Hello extends React.Component {',
Expand Down