Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse explicit declaration that class extends React.Component #513

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -156,12 +157,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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devDependencies": {
"babel-eslint": "6.0.0-beta.6",
"coveralls": "2.11.8",
"doctrine": "^1.2.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would need to be a full dependency since it's not related to actually developing the module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops! Quite right.
On 25 Mar 2016 01:46, "Jordan Harband" notifications@github.com wrote:

In package.json
#513 (comment)
:

@@ -25,6 +25,7 @@
"devDependencies": {
"babel-eslint": "6.0.0-beta.6",
"coveralls": "2.11.8",

  • "doctrine": "^1.2.0",

this would need to be a full dependency since it's not related to
actually developing the module.


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/yannickcr/eslint-plugin-react/pull/513/files/f3e634c59e8928ab170a9bfa9d056cf0f8778064#r57411882

"eslint": "2.4.0",
"istanbul": "0.4.2",
"mocha": "2.4.5"
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 @@ -1233,6 +1233,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