Skip to content

Commit

Permalink
Implements explicit React Component declaration detection
Browse files Browse the repository at this point in the history
  • Loading branch information
gausie committed Mar 24, 2016
1 parent 5607e76 commit 51fde3c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 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 @@ -143,6 +144,10 @@ function componentRule(rule, context) {
* @returns {Boolean} True if the node is a React ES5 component, false if not
*/
isES5Component: function(node) {
if (utils.isExplicitComponent(node)) {
return true;
}

if (!node.parent) {
return false;
}
Expand All @@ -156,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
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",
"eslint": "2.4.0",
"istanbul": "0.4.2",
"mocha": "2.4.5"
Expand Down

0 comments on commit 51fde3c

Please sign in to comment.