Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for createElement being called from React #1011

Merged
merged 3 commits into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,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' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this prevent import { createElement } from 'react'; from being detected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I found a way to check for this by using the ModuleScope of the Variable when importing createElement like that. However, it involves using Map, and it doesn't seem like I can get away with that with the project setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just temporarily add in a disable comment so I can feel good about pushing it up. I'll remove it after receiving feedback.

node[property].callee.property &&
node[property].callee.property.name === 'createElement'
;
Expand Down
24 changes: 15 additions & 9 deletions lib/util/variable.js
Original file line number Diff line number Diff line change
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have broken the build for Node < 4. We have a PR that removes support for Node < 4 (#1038) that will un-break this. @ljharb do you think we will merge this soon or should I quick fix this line? I'm in favor of merging your PR now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we absolutely must fix this first, since otherwise it's a semver-major change, and we need to publish master before a semver-major line.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I'll put up a PR shortly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries; I pushed up e332b08 to fix it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nevermind looks like you fixed it in e332b08

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
Original file line number Diff line number Diff line change
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";',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add cases for var { createElement } = React; and var createElement = React.createElement;.

Probably too difficult to cover var c = React.createElement; tho.

'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'
}]
}]
});