Skip to content

Commit

Permalink
Merge pull request #1011 from jomasti/fix-996
Browse files Browse the repository at this point in the history
Check for createElement being called from React
  • Loading branch information
lencioni committed Jan 29, 2017
2 parents 3949b50 + 9b88474 commit 4df3bab
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
14 changes: 14 additions & 0 deletions lib/util/Components.js
Expand Up @@ -256,9 +256,23 @@ function componentRule(rule, context) {
node[property] &&
node[property].type === 'JSXElement'
;
var destructuredReactCreateElement = function () {
var variables = variableUtil.variablesInScope(context);
var variable = variableUtil.getVariable(variables, 'createElement');
if (variable) {
var map = variable.scope.set;
if (map.has('React')) {
return true;
}
}
return false;
};
var returnsReactCreateElement =
destructuredReactCreateElement() ||
node[property] &&
node[property].callee &&
node[property].callee.object &&
node[property].callee.object.name === 'React' &&
node[property].callee.property &&
node[property].callee.property.name === 'createElement'
;
Expand Down
24 changes: 15 additions & 9 deletions lib/util/variable.js
Expand Up @@ -11,16 +11,21 @@
* @returns {Boolean} True if the variable was found, false if not.
*/
function findVariable(variables, name) {
var i;
var len;

for (i = 0, len = variables.length; i < len; i++) {
if (variables[i].name === name) {
return true;
}
}
return variables.some(function (variable) {
return variable.name === name;
});
}

return false;
/**
* Find and return a particular variable in a list
* @param {Array} variables The variables list.
* @param {Array} name The name of the variable to search.
* @returns {Object} Variable if the variable was found, null if not.
*/
function getVariable(variables, name) {
return variables.find(function (variable) {
return variable.name === name;
});
}

/**
Expand Down Expand Up @@ -52,5 +57,6 @@ function variablesInScope(context) {

module.exports = {
findVariable: findVariable,
getVariable: getVariable,
variablesInScope: variablesInScope
};
48 changes: 48 additions & 0 deletions tests/lib/rules/display-name.js
Expand Up @@ -378,6 +378,19 @@ ruleTester.run('display-name', rule, {
')'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'module.exports = {',
' createElement: tagName => document.createElement(tagName)',
'};'
].join('\n'),
parser: 'babel-eslint'
}, {
code: [
'const { createElement } = document;',
'createElement("a");'
].join('\n'),
parser: 'babel-eslint'
}],

invalid: [{
Expand Down Expand Up @@ -548,5 +561,40 @@ ruleTester.run('display-name', rule, {
errors: [{
message: 'Component definition is missing display name'
}]
}, {
code: [
'import React, { createElement } from "react";',
'export default (props) => {',
' return createElement("div", {}, "hello");',
'};'
].join('\n'),
parser: 'babel-eslint',
errors: [{
message: 'Component definition is missing display name'
}]
}, {
code: [
'import React from "react";',
'const { createElement } = React;',
'export default (props) => {',
' return createElement("div", {}, "hello");',
'};'
].join('\n'),
parser: 'babel-eslint',
errors: [{
message: 'Component definition is missing display name'
}]
}, {
code: [
'import React from "react";',
'const createElement = React.createElement;',
'export default (props) => {',
' return createElement("div", {}, "hello");',
'};'
].join('\n'),
parser: 'babel-eslint',
errors: [{
message: 'Component definition is missing display name'
}]
}]
});

0 comments on commit 4df3bab

Please sign in to comment.