Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bf498a2

Browse files
committedOct 29, 2017
Fix no-unused-prop-types arrow callbacks
Is some rare cases props are used in arrow callbacks to things like setState. By handling CallExpression correctly the existing code can already handle the props. Fix #1506
1 parent 17a7e47 commit bf498a2

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed
 

‎lib/rules/no-unused-prop-types.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,12 @@ module.exports = {
607607
let allNames;
608608
let properties;
609609
switch (node.type) {
610+
case 'CallExpression':
610611
case 'MemberExpression':
611612
name = getPropertyName(node);
612613
if (name) {
613614
allNames = parentNames.concat(name);
614-
if (node.parent.type === 'MemberExpression') {
615+
if (node.parent.type === 'MemberExpression' || node.parent.type === 'CallExpression') {
615616
markPropTypesAsUsed(node.parent, allNames);
616617
}
617618
// Do not mark computed props as used.

‎tests/lib/rules/no-unused-prop-types.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,10 @@ ruleTester.run('no-unused-prop-types', rule, {
829829
type PropsA = { a: string }
830830
type PropsB = { b: string }
831831
type Props = PropsA & PropsB;
832-
832+
833833
class MyComponent extends React.Component {
834834
props: Props;
835-
835+
836836
render() {
837837
return <div>{this.props.a} - {this.props.b}</div>
838838
}
@@ -848,7 +848,7 @@ ruleTester.run('no-unused-prop-types', rule, {
848848
849849
class Bar extends React.Component {
850850
props: Props & PropsC;
851-
851+
852852
render() {
853853
return <div>{this.props.foo} - {this.props.bar} - {this.props.zap}</div>
854854
}
@@ -864,7 +864,7 @@ ruleTester.run('no-unused-prop-types', rule, {
864864
865865
class Bar extends React.Component {
866866
props: Props & PropsC;
867-
867+
868868
render() {
869869
return <div>{this.props.foo} - {this.props.bar} - {this.props.zap}</div>
870870
}
@@ -876,12 +876,12 @@ ruleTester.run('no-unused-prop-types', rule, {
876876
type PropsB = { foo: string };
877877
type PropsC = { bar: string };
878878
type Props = PropsB & {
879-
zap: string
879+
zap: string
880880
};
881881
882882
class Bar extends React.Component {
883883
props: Props & PropsC;
884-
884+
885885
render() {
886886
return <div>{this.props.foo} - {this.props.bar} - {this.props.zap}</div>
887887
}
@@ -893,12 +893,12 @@ ruleTester.run('no-unused-prop-types', rule, {
893893
type PropsB = { foo: string };
894894
type PropsC = { bar: string };
895895
type Props = {
896-
zap: string
896+
zap: string
897897
} & PropsB;
898898
899899
class Bar extends React.Component {
900900
props: Props & PropsC;
901-
901+
902902
render() {
903903
return <div>{this.props.foo} - {this.props.bar} - {this.props.zap}</div>
904904
}
@@ -2110,6 +2110,24 @@ ruleTester.run('no-unused-prop-types', rule, {
21102110
].join('\n'),
21112111
parser: 'babel-eslint',
21122112
options: [{skipShapeProps: false}]
2113+
}, {
2114+
// issue #1506
2115+
code: [
2116+
'class MyComponent extends React.Component {',
2117+
' onFoo() {',
2118+
' this.setState((prevState, props) => {',
2119+
' props.doSomething();',
2120+
' });',
2121+
' }',
2122+
' render() {',
2123+
' return (',
2124+
' <div onClick={this.onFoo}>Test</div>',
2125+
' );',
2126+
' }',
2127+
'}'
2128+
].join('\n'),
2129+
parser: 'babel-eslint',
2130+
options: [{skipShapeProps: false}]
21132131
}, {
21142132
// issue #106
21152133
code: `
@@ -2796,10 +2814,10 @@ ruleTester.run('no-unused-prop-types', rule, {
27962814
type PropsA = { a: string }
27972815
type PropsB = { b: string }
27982816
type Props = PropsA & PropsB;
2799-
2817+
28002818
class MyComponent extends React.Component {
28012819
props: Props;
2802-
2820+
28032821
render() {
28042822
return <div>{this.props.a}</div>
28052823
}
@@ -2818,7 +2836,7 @@ ruleTester.run('no-unused-prop-types', rule, {
28182836
28192837
class Bar extends React.Component {
28202838
props: Props & PropsC;
2821-
2839+
28222840
render() {
28232841
return <div>{this.props.foo} - {this.props.bar}</div>
28242842
}
@@ -2833,12 +2851,12 @@ ruleTester.run('no-unused-prop-types', rule, {
28332851
type PropsB = { foo: string };
28342852
type PropsC = { bar: string };
28352853
type Props = PropsB & {
2836-
zap: string
2854+
zap: string
28372855
};
28382856
28392857
class Bar extends React.Component {
28402858
props: Props & PropsC;
2841-
2859+
28422860
render() {
28432861
return <div>{this.props.foo} - {this.props.bar}</div>
28442862
}
@@ -2853,12 +2871,12 @@ ruleTester.run('no-unused-prop-types', rule, {
28532871
type PropsB = { foo: string };
28542872
type PropsC = { bar: string };
28552873
type Props = {
2856-
zap: string
2874+
zap: string
28572875
} & PropsB;
28582876
28592877
class Bar extends React.Component {
28602878
props: Props & PropsC;
2861-
2879+
28622880
render() {
28632881
return <div>{this.props.foo} - {this.props.bar}</div>
28642882
}

0 commit comments

Comments
 (0)
Please sign in to comment.