Skip to content

Commit 9b88474

Browse files
committed
Clean up variable utils and add more tests
1 parent 47405b4 commit 9b88474

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

lib/util/Components.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,9 @@ function componentRule(rule, context) {
258258
var destructuredReactCreateElement = function () {
259259
var variables = variableUtil.variablesInScope(context);
260260
var variable = variableUtil.getVariable(variables, 'createElement');
261-
if (variable !== null) {
261+
if (variable) {
262262
var map = variable.scope.set;
263-
// eslint-disable-next-line no-undef
264-
if (map instanceof Map && map.has('React')) {
263+
if (map.has('React')) {
265264
return true;
266265
}
267266
}

lib/util/variable.js

+6-20
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,9 @@
1111
* @returns {Boolean} True if the variable was found, false if not.
1212
*/
1313
function findVariable(variables, name) {
14-
var i;
15-
var len;
16-
17-
for (i = 0, len = variables.length; i < len; i++) {
18-
if (variables[i].name === name) {
19-
return true;
20-
}
21-
}
22-
23-
return false;
14+
return variables.some(function (variable) {
15+
return variable.name === name;
16+
});
2417
}
2518

2619
/**
@@ -30,16 +23,9 @@ function findVariable(variables, name) {
3023
* @returns {Object} Variable if the variable was found, null if not.
3124
*/
3225
function getVariable(variables, name) {
33-
var i;
34-
var len;
35-
36-
for (i = 0, len = variables.length; i < len; i++) {
37-
if (variables[i].name === name) {
38-
return variables[i];
39-
}
40-
}
41-
42-
return null;
26+
return variables.find(function (variable) {
27+
return variable.name === name;
28+
});
4329
}
4430

4531
/**

tests/lib/rules/display-name.js

+24
Original file line numberDiff line numberDiff line change
@@ -572,5 +572,29 @@ ruleTester.run('display-name', rule, {
572572
errors: [{
573573
message: 'Component definition is missing display name'
574574
}]
575+
}, {
576+
code: [
577+
'import React from "react";',
578+
'const { createElement } = React;',
579+
'export default (props) => {',
580+
' return createElement("div", {}, "hello");',
581+
'};'
582+
].join('\n'),
583+
parser: 'babel-eslint',
584+
errors: [{
585+
message: 'Component definition is missing display name'
586+
}]
587+
}, {
588+
code: [
589+
'import React from "react";',
590+
'const createElement = React.createElement;',
591+
'export default (props) => {',
592+
' return createElement("div", {}, "hello");',
593+
'};'
594+
].join('\n'),
595+
parser: 'babel-eslint',
596+
errors: [{
597+
message: 'Component definition is missing display name'
598+
}]
575599
}]
576600
});

0 commit comments

Comments
 (0)