Skip to content

Commit 0fa0761

Browse files
committed
Added test & working
1 parent e77c97f commit 0fa0761

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/rules/prop-types.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ module.exports = {
121121
return false;
122122
}
123123

124+
/**
125+
* Check if we are in a class constructor
126+
* @return {boolean} true if we are in a class constructor, false if not
127+
*/
128+
function inShouldComponentUpdate() {
129+
let scope = context.getScope();
130+
while (scope) {
131+
if (
132+
scope.block && scope.block.parent &&
133+
scope.block.parent.key && scope.block.parent.key.name === 'shouldComponentUpdate'
134+
) {
135+
return true;
136+
}
137+
scope = scope.upper;
138+
}
139+
return false;
140+
}
141+
124142
/**
125143
* Checks if a prop is being assigned a value props.bar = 'bar'
126144
* @param {ASTNode} node The AST node being checked.
@@ -146,7 +164,7 @@ module.exports = {
146164
node.object.type === 'ThisExpression' && node.property.name === 'props'
147165
);
148166
const isStatelessFunctionUsage = node.object.name === 'props' && !isAssignmentToProp(node);
149-
const isNextPropsUsage = node.object.name === 'nextProps' && inComponentWillReceiveProps();
167+
const isNextPropsUsage = node.object.name === 'nextProps' && (inComponentWillReceiveProps() || inShouldComponentUpdate());
150168
return isClassUsage || isStatelessFunctionUsage || isNextPropsUsage;
151169
}
152170

@@ -549,7 +567,9 @@ module.exports = {
549567
const isInClassComponent = utils.getParentES6Component() || utils.getParentES5Component();
550568
const isNotInConstructor = !inConstructor();
551569
const isNotInComponentWillReceiveProps = !inComponentWillReceiveProps();
552-
if (isDirectProp && isInClassComponent && isNotInConstructor && isNotInComponentWillReceiveProps) {
570+
const isNotInShouldComponentUpdate = !inShouldComponentUpdate();
571+
if (isDirectProp && isInClassComponent && isNotInConstructor && isNotInComponentWillReceiveProps
572+
&& isNotInShouldComponentUpdate) {
553573
return void 0;
554574
}
555575
if (!isDirectProp) {

tests/lib/rules/prop-types.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3068,6 +3068,29 @@ ruleTester.run('prop-types', rule, {
30683068
errors: [
30693069
{message: '\'foo\' is missing in props validation'}
30703070
]
3071+
}, {
3072+
code: [
3073+
'class Hello extends Component {',
3074+
' static propTypes = forbidExtraProps({',
3075+
' bar: PropTypes.func',
3076+
' })',
3077+
' shouldComponentUpdate(nextProps) {',
3078+
' if (nextProps.foo) {',
3079+
' return;',
3080+
' }',
3081+
' }',
3082+
' render() {',
3083+
' return <div bar={this.props.bar} />;',
3084+
' }',
3085+
'}'
3086+
].join('\n'),
3087+
parser: 'babel-eslint',
3088+
settings: Object.assign({}, settings, {
3089+
propWrapperFunctions: ['forbidExtraProps']
3090+
}),
3091+
errors: [
3092+
{message: '\'foo\' is missing in props validation'}
3093+
]
30713094
}, {
30723095
code: [
30733096
'class Hello extends Component {',

0 commit comments

Comments
 (0)