Skip to content

Commit

Permalink
Add support for ClassExpression in prop-types rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim Seminck committed Aug 27, 2017
1 parent e2f6460 commit 8a56b01
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ module.exports = {
* @returns {Boolean} True if the node is a class with generic prop types, false if not.
*/
function isSuperTypeParameterPropsDeclaration(node) {
if (node && node.type === 'ClassDeclaration') {
if (node && (node.type === 'ClassDeclaration' || node.type === 'ClassExpression')) {
if (node.superTypeParameters && node.superTypeParameters.params.length > 0) {
return true;
}
Expand Down Expand Up @@ -920,6 +920,12 @@ module.exports = {
}
},

ClassExpression: function(node) {
if (isSuperTypeParameterPropsDeclaration(node)) {
markPropTypesAsDeclared(node, resolveSuperParameterPropsType(node));
}
},

ClassProperty: function(node) {
if (isAnnotatedClassPropsDeclaration(node)) {
markPropTypesAsDeclared(node, resolveTypeAnnotation(node));
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,45 @@ ruleTester.run('prop-types', rule, {
`,
settings: {react: {flowVersion: '0.53'}},
parser: 'babel-eslint'
}, {
code: `
type Props = { foo: string }
function higherOrderComponent<Props>() {
return class extends React.Component<Props> {
render() {
return <div>{this.props.foo}</div>
}
}
}
`,
parser: 'babel-eslint'
}, {
code: `
function higherOrderComponent<P: { foo: string }>() {
return class extends React.Component<P> {
render() {
return <div>{this.props.foo}</div>
}
}
}
`,
parser: 'babel-eslint'
},
{
code: `
const withOverlayState = <P: {foo: string}>(WrappedComponent: ComponentType<P>): CpmponentType<P> => (
class extends React.Component<P> {
constructor(props) {
super(props);
this.state = {foo: props.foo}
}
render() {
return <div>Hello World</div>
}
}
)
`,
parser: 'babel-eslint'
},
// issue #1288
`function Foo() {
Expand Down

0 comments on commit 8a56b01

Please sign in to comment.