Skip to content

Commit

Permalink
[Fix] Don’t rely on Array#find existing pre node 4.
Browse files Browse the repository at this point in the history
Fixes #999.
  • Loading branch information
ljharb committed Dec 13, 2016
1 parent 466ab7f commit cc2be3b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -3,5 +3,10 @@ node_js:
- '6'
- '5'
- '4'
- 'iojs'
- '0.12'
- '0.10'
before_script:
- 'if [ "${TRAVIS_NODE_VERSION}" = "iojs" ] || [ "${TRAVIS_NODE_VERSION}" = "0.12" ] || [ "${TRAVIS_NODE_VERSION}" = "0.10" ]; then npm install eslint@2; fi'
after_success:
- npm run coveralls
4 changes: 3 additions & 1 deletion lib/rules/no-children-prop.js
Expand Up @@ -4,6 +4,8 @@
*/
'use strict';

var find = require('array.prototype.find');

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -53,7 +55,7 @@ module.exports = {
}

var props = node.arguments[1].properties;
var childrenProp = props.find(function(prop) {
var childrenProp = find(props, function(prop) {
return prop.key && prop.key.name === 'children';
});

Expand Down
9 changes: 5 additions & 4 deletions lib/rules/no-danger-with-children.js
Expand Up @@ -4,6 +4,7 @@
*/
'use strict';

var find = require('array.prototype.find');
var variableUtil = require('../util/variable');

// ------------------------------------------------------------------------------
Expand All @@ -20,7 +21,7 @@ module.exports = {
},
create: function(context) {
function findSpreadVariable(name) {
return variableUtil.variablesInScope(context).find(function (item) {
return find(variableUtil.variablesInScope(context), function (item) {
return item.name === name;
});
}
Expand All @@ -33,7 +34,7 @@ module.exports = {
if (!node.properties) {
return false;
}
return node.properties.find(function(prop) {
return find(node.properties, function(prop) {
if (prop.type === 'Property') {
return prop.key.name === propName;
} else if (prop.type === 'ExperimentalSpreadProperty') {
Expand All @@ -53,7 +54,7 @@ module.exports = {
*/
function findJsxProp(node, propName) {
var attributes = node.openingElement.attributes;
return attributes.find(function (attribute) {
return find(attributes, function (attribute) {
if (attribute.type === 'JSXSpreadAttribute') {
var variable = findSpreadVariable(attribute.argument.name);
if (variable && variable.defs.length && variable.defs[0].node.init) {
Expand Down Expand Up @@ -94,7 +95,7 @@ module.exports = {
var props = node.arguments[1];

if (props.type === 'Identifier') {
var variable = variableUtil.variablesInScope(context).find(function (item) {
var variable = find(variableUtil.variablesInScope(context), function (item) {
return item.name === props.name;
});
if (variable && variable.defs[0].node.init) {
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/require-default-props.js
Expand Up @@ -4,6 +4,7 @@
*/
'use strict';

var find = require('array.prototype.find');
var Components = require('../util/Components');
var variableUtil = require('../util/variable');
var annotations = require('../util/annotations');
Expand Down Expand Up @@ -57,7 +58,7 @@ module.exports = {
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
*/
function findVariableByName(name) {
var variable = variableUtil.variablesInScope(context).find(function(item) {
var variable = find(variableUtil.variablesInScope(context), function(item) {
return item.name === name;
});

Expand Down Expand Up @@ -188,7 +189,7 @@ module.exports = {
* from this ObjectExpression can't be resolved.
*/
function getDefaultPropsFromObjectExpression(objectExpression) {
var hasSpread = objectExpression.properties.find(function(property) {
var hasSpread = find(objectExpression.properties, function(property) {
return property.type === 'ExperimentalSpreadProperty';
});

Expand Down
5 changes: 3 additions & 2 deletions lib/rules/style-prop-object.js
Expand Up @@ -4,6 +4,7 @@
*/
'use strict';

var find = require('array.prototype.find');
var variableUtil = require('../util/variable');

// ------------------------------------------------------------------------------
Expand All @@ -25,7 +26,7 @@ module.exports = {
* @param {object} node A Identifier node
*/
function checkIdentifiers(node) {
var variable = variableUtil.variablesInScope(context).find(function (item) {
var variable = find(variableUtil.variablesInScope(context), function (item) {
return item.name === node.name;
});

Expand All @@ -47,7 +48,7 @@ module.exports = {
&& node.arguments.length > 1
) {
if (node.arguments[1].type === 'ObjectExpression') {
var style = node.arguments[1].properties.find(function(property) {
var style = find(node.arguments[1].properties, function(property) {
return property.key && property.key.name === 'style' && !property.computed;
});
if (style) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -24,12 +24,13 @@
"bugs": "https://github.com/yannickcr/eslint-plugin-react/issues",
"dependencies": {
"doctrine": "^1.2.2",
"jsx-ast-utils": "^1.3.4"
"jsx-ast-utils": "^1.3.4",
"array.prototype.find": "^2.0.1"
},
"devDependencies": {
"babel-eslint": "7.1.1",
"coveralls": "2.11.15",
"eslint": "3.11.1",
"eslint": "^2.0.0 || ^3.0.0",
"istanbul": "0.4.5",
"mocha": "3.2.0"
},
Expand Down

0 comments on commit cc2be3b

Please sign in to comment.