Skip to content

Commit

Permalink
Clean up variable utils and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jomasti committed Jan 2, 2017
1 parent 47405b4 commit 9b88474
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
5 changes: 2 additions & 3 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,9 @@ function componentRule(rule, context) {
var destructuredReactCreateElement = function () {
var variables = variableUtil.variablesInScope(context);
var variable = variableUtil.getVariable(variables, 'createElement');
if (variable !== null) {
if (variable) {
var map = variable.scope.set;
// eslint-disable-next-line no-undef
if (map instanceof Map && map.has('React')) {
if (map.has('React')) {
return true;
}
}
Expand Down
26 changes: 6 additions & 20 deletions lib/util/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,9 @@
* @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 false;
return variables.some(function (variable) {
return variable.name === name;
});
}

/**
Expand All @@ -30,16 +23,9 @@ function findVariable(variables, name) {
* @returns {Object} Variable if the variable was found, null if not.
*/
function getVariable(variables, name) {
var i;
var len;

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

return null;
return variables.find(function (variable) {
return variable.name === name;
});
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,5 +572,29 @@ ruleTester.run('display-name', rule, {
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 9b88474

Please sign in to comment.