Skip to content

Commit 144d92a

Browse files
committed
Add tests and fix the function that was copied over
1 parent 6c733ff commit 144d92a

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const has = require('has');
1111
const Components = require('../util/Components');
1212
const variable = require('../util/variable');
1313
const annotations = require('../util/annotations');
14+
const versionUtil = require('../util/version');
1415

1516
// ------------------------------------------------------------------------------
1617
// Constants
@@ -134,17 +135,23 @@ module.exports = {
134135
}
135136

136137
/**
137-
* Resolve the type annotation for a given node.
138-
* Flow annotations are sometimes wrapped in outer `TypeAnnotation`
139-
* and `NullableTypeAnnotation` nodes which obscure the annotation we're
140-
* interested in.
141-
* This method also resolves type aliases where possible.
138+
* Resolve the type annotation for a given class declaration node with superTypeParameters.
142139
*
143140
* @param {ASTNode} node The annotation or a node containing the type annotation.
144141
* @returns {ASTNode} The resolved type annotation for the node.
145142
*/
146-
function resolveTypeAnnotation(node) {
147-
let annotation = node.typeAnnotation || node;
143+
function resolveSuperParameterPropsType(node) {
144+
let propsParameterPosition;
145+
try {
146+
// Flow <=0.52 had 3 required TypedParameters of which the second one is the Props.
147+
// Flow >=0.53 has 2 optional TypedParameters of which the first one is the Props.
148+
propsParameterPosition = versionUtil.testFlowVersion(context, '0.53.0') ? 0 : 1;
149+
} catch (e) {
150+
// In case there is no flow version defined, we can safely assume that when there are 3 Props we are dealing with version <= 0.52
151+
propsParameterPosition = node.superTypeParameters.params.length <= 2 ? 0 : 1;
152+
}
153+
154+
let annotation = node.superTypeParameters.params[propsParameterPosition];
148155
while (annotation && (annotation.type === 'TypeAnnotation' || annotation.type === 'NullableTypeAnnotation')) {
149156
annotation = annotation.typeAnnotation;
150157
}

lib/rules/prop-types.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Components = require('../util/Components');
1212
const variable = require('../util/variable');
1313
const annotations = require('../util/annotations');
1414
const versionUtil = require('../util/version');
15+
1516
// ------------------------------------------------------------------------------
1617
// Constants
1718
// ------------------------------------------------------------------------------

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

+40
Original file line numberDiff line numberDiff line change
@@ -2069,6 +2069,30 @@ ruleTester.run('no-unused-prop-types', rule, {
20692069
};
20702070
`,
20712071
parser: 'babel-eslint'
2072+
}, {
2073+
code: `
2074+
type Person = {
2075+
firstname: string
2076+
}
2077+
class MyComponent extends React.Component<void, Props, void> {
2078+
render() {
2079+
return <div>Hello {this.props.firstname}</div>
2080+
}
2081+
}
2082+
`,
2083+
parser: 'babel-eslint'
2084+
}, {
2085+
code: `
2086+
type Person = {
2087+
firstname: string
2088+
}
2089+
class MyComponent extends React.Component<Props> {
2090+
render() {
2091+
return <div>Hello {this.props.firstname}</div>
2092+
}
2093+
}
2094+
`,
2095+
parser: 'babel-eslint'
20722096
}
20732097
],
20742098

@@ -3469,6 +3493,22 @@ ruleTester.run('no-unused-prop-types', rule, {
34693493
errors: [{
34703494
message: '\'aProp\' PropType is defined but prop is never used'
34713495
}]
3496+
}, {
3497+
code: `
3498+
type Props = {
3499+
firstname: string,
3500+
lastname: string,
3501+
}
3502+
class MyComponent extends React.Component<void, Props, void> {
3503+
render() {
3504+
return <div>Hello {this.props.firstname}</div>
3505+
}
3506+
}
3507+
`,
3508+
parser: 'babel-eslint',
3509+
errors: [{
3510+
message: '\'lastname\' PropType is defined but prop is never used'
3511+
}]
34723512
}
34733513

34743514
/* , {

0 commit comments

Comments
 (0)