Skip to content

Commit f9c34d3

Browse files
Dustin Saverydanez
authored andcommitted
Adding @extends React.Component support to isReactComponentClass (#269)
* [component-verification] Adding additional method to verify component is a React Component * [test] adding unit test * [tiny] Initializing variable to satisfy TravisCI * [fixes] incorporating feedback * [fix] fixing a few more nits * [refactor] cleaning up some checks
1 parent 446b29e commit f9c34d3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/utils/__tests__/isReactComponentClass-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ describe('isReactComponentClass', () => {
5252
});
5353
});
5454

55+
describe('JSDoc @extends React.Component', () => {
56+
it('accepts class declarations declaring `@extends React.Component` in JSDoc', () => {
57+
var def = parse(`
58+
var React = require('react');
59+
/**
60+
* @class Foo
61+
* @extends React.Component
62+
*/
63+
class Foo extends Bar {}
64+
`).get('body', 1);
65+
66+
expect(isReactComponentClass(def)).toBe(true);
67+
});
68+
});
69+
5570
describe('React.Component inheritance', () => {
5671
it('accepts class declarations extending React.Component', () => {
5772
var def = parse(`

src/utils/isReactComponentClass.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ export default function isReactComponentClass(
4444
return true;
4545
}
4646

47+
// check for @extends React.Component in docblock
48+
if (path.parentPath && path.parentPath.value) {
49+
var classDeclaration = Array.isArray(path.parentPath.value)
50+
? path.parentPath.value.find(function(declaration) { return declaration.type === 'ClassDeclaration' })
51+
: path.parentPath.value;
52+
53+
if (classDeclaration &&
54+
classDeclaration.leadingComments &&
55+
classDeclaration.leadingComments.some(function (comment) {
56+
return /@extends\s+React\.Component/.test(comment.value);
57+
})) {
58+
return true;
59+
}
60+
}
61+
4762
// extends ReactComponent?
4863
if (!node.superClass) {
4964
return false;

0 commit comments

Comments
 (0)