Skip to content

Commit 47405b4

Browse files
committed
Add check for destructured createElement
Due to the previous change of checking for `createElement` being called on React, this left out the use case of a destructured import of `createElement`.
1 parent c6264d0 commit 47405b4

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

lib/util/Components.js

+13
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,20 @@ function componentRule(rule, context) {
255255
node[property] &&
256256
node[property].type === 'JSXElement'
257257
;
258+
var destructuredReactCreateElement = function () {
259+
var variables = variableUtil.variablesInScope(context);
260+
var variable = variableUtil.getVariable(variables, 'createElement');
261+
if (variable !== null) {
262+
var map = variable.scope.set;
263+
// eslint-disable-next-line no-undef
264+
if (map instanceof Map && map.has('React')) {
265+
return true;
266+
}
267+
}
268+
return false;
269+
};
258270
var returnsReactCreateElement =
271+
destructuredReactCreateElement() ||
259272
node[property] &&
260273
node[property].callee &&
261274
node[property].callee.object &&

lib/util/variable.js

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,25 @@ function findVariable(variables, name) {
2323
return false;
2424
}
2525

26+
/**
27+
* Find and return a particular variable in a list
28+
* @param {Array} variables The variables list.
29+
* @param {Array} name The name of the variable to search.
30+
* @returns {Object} Variable if the variable was found, null if not.
31+
*/
32+
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;
43+
}
44+
2645
/**
2746
* List all variable in a given scope
2847
*
@@ -52,5 +71,6 @@ function variablesInScope(context) {
5271

5372
module.exports = {
5473
findVariable: findVariable,
74+
getVariable: getVariable,
5575
variablesInScope: variablesInScope
5676
};

tests/lib/rules/display-name.js

+17
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ ruleTester.run('display-name', rule, {
385385
'};'
386386
].join('\n'),
387387
parser: 'babel-eslint'
388+
}, {
389+
code: [
390+
'const { createElement } = document;',
391+
'createElement("a");'
392+
].join('\n'),
393+
parser: 'babel-eslint'
388394
}],
389395

390396
invalid: [{
@@ -555,5 +561,16 @@ ruleTester.run('display-name', rule, {
555561
errors: [{
556562
message: 'Component definition is missing display name'
557563
}]
564+
}, {
565+
code: [
566+
'import React, { createElement } from "react";',
567+
'export default (props) => {',
568+
' return createElement("div", {}, "hello");',
569+
'};'
570+
].join('\n'),
571+
parser: 'babel-eslint',
572+
errors: [{
573+
message: 'Component definition is missing display name'
574+
}]
558575
}]
559576
});

0 commit comments

Comments
 (0)