Skip to content

Commit

Permalink
Add support for explicit declaration that class extends React.Compone…
Browse files Browse the repository at this point in the history
…nt (fixes #68)
  • Loading branch information
gausie authored and yannickcr committed Jun 5, 2016
1 parent 2a04fd4 commit 8cbbe4d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict';

var util = require('util');
var doctrine = require('doctrine');
var variableUtil = require('./variable');
var pragmaUtil = require('./pragma');

Expand Down Expand Up @@ -160,12 +161,41 @@ function componentRule(rule, context) {
* @returns {Boolean} True if the node is a React ES6 component, false if not
*/
isES6Component: function(node) {
if (utils.isExplicitComponent(node)) {
return true;
}

if (!node.superClass) {
return false;
}
return new RegExp('^(' + pragma + '\\.)?Component$').test(sourceCode.getText(node.superClass));
},

/**
* Check if the node is explicitly declared as a descendant of a React Component
*
* @param {ASTNode} node The AST node being checked (can be a ReturnStatement or an ArrowFunctionExpression).
* @returns {Boolean} True if the node is explicitly declared as a descendant of a React Component, false if not
*/
isExplicitComponent: function(node) {
var comment = sourceCode.getJSDocComment(node);

if (comment === null) {
return false;
}

var commentAst = doctrine.parse(comment.value, {
unwrap: true,
tags: ['extends', 'augments']
});

var relevantTags = commentAst.tags.filter(function(tag) {
return tag.name === 'React.Component';
});

return relevantTags.length > 0;
},

/**
* Check if the node is returning JSX
*
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
},
"homepage": "https://github.com/yannickcr/eslint-plugin-react",
"bugs": "https://github.com/yannickcr/eslint-plugin-react/issues",
"dependencies": {
"doctrine": "^1.2.0"
},
"devDependencies": {
"babel-eslint": "6.0.4",
"coveralls": "2.11.9",
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,22 @@ ruleTester.run('prop-types', rule, {
column: 35,
type: 'Identifier'
}]
}, {
code: [
'/** @extends React.Component */',
'class Hello extends ChildComponent {',
' render() {',
' return <div>Hello {this.props.name}</div>;',
' }',
'}'
].join('\n'),
parserOptions: parserOptions,
errors: [{
message: '\'name\' is missing in props validation',
line: 4,
column: 35,
type: 'Identifier'
}]
}, {
code: [
'class Hello extends React.Component {',
Expand Down

0 comments on commit 8cbbe4d

Please sign in to comment.