Skip to content

Commit 8a56b01

Browse files
author
Joachim Seminck
committed
Add support for ClassExpression in prop-types rule
1 parent e2f6460 commit 8a56b01

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/rules/prop-types.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ module.exports = {
173173
* @returns {Boolean} True if the node is a class with generic prop types, false if not.
174174
*/
175175
function isSuperTypeParameterPropsDeclaration(node) {
176-
if (node && node.type === 'ClassDeclaration') {
176+
if (node && (node.type === 'ClassDeclaration' || node.type === 'ClassExpression')) {
177177
if (node.superTypeParameters && node.superTypeParameters.params.length > 0) {
178178
return true;
179179
}
@@ -920,6 +920,12 @@ module.exports = {
920920
}
921921
},
922922

923+
ClassExpression: function(node) {
924+
if (isSuperTypeParameterPropsDeclaration(node)) {
925+
markPropTypesAsDeclared(node, resolveSuperParameterPropsType(node));
926+
}
927+
},
928+
923929
ClassProperty: function(node) {
924930
if (isAnnotatedClassPropsDeclaration(node)) {
925931
markPropTypesAsDeclared(node, resolveTypeAnnotation(node));

tests/lib/rules/prop-types.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,45 @@ ruleTester.run('prop-types', rule, {
16691669
`,
16701670
settings: {react: {flowVersion: '0.53'}},
16711671
parser: 'babel-eslint'
1672+
}, {
1673+
code: `
1674+
type Props = { foo: string }
1675+
function higherOrderComponent<Props>() {
1676+
return class extends React.Component<Props> {
1677+
render() {
1678+
return <div>{this.props.foo}</div>
1679+
}
1680+
}
1681+
}
1682+
`,
1683+
parser: 'babel-eslint'
1684+
}, {
1685+
code: `
1686+
function higherOrderComponent<P: { foo: string }>() {
1687+
return class extends React.Component<P> {
1688+
render() {
1689+
return <div>{this.props.foo}</div>
1690+
}
1691+
}
1692+
}
1693+
`,
1694+
parser: 'babel-eslint'
1695+
},
1696+
{
1697+
code: `
1698+
const withOverlayState = <P: {foo: string}>(WrappedComponent: ComponentType<P>): CpmponentType<P> => (
1699+
class extends React.Component<P> {
1700+
constructor(props) {
1701+
super(props);
1702+
this.state = {foo: props.foo}
1703+
}
1704+
render() {
1705+
return <div>Hello World</div>
1706+
}
1707+
}
1708+
)
1709+
`,
1710+
parser: 'babel-eslint'
16721711
},
16731712
// issue #1288
16741713
`function Foo() {

0 commit comments

Comments
 (0)