From cc2be3b460e4b49007876ea9e43d644ee077ba99 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 13 Dec 2016 00:31:00 -0800 Subject: [PATCH] =?UTF-8?q?[Fix]=20Don=E2=80=99t=20rely=20on=20`Array#find?= =?UTF-8?q?`=20existing=20pre=20node=204.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #999. --- .travis.yml | 5 +++++ lib/rules/no-children-prop.js | 4 +++- lib/rules/no-danger-with-children.js | 9 +++++---- lib/rules/require-default-props.js | 5 +++-- lib/rules/style-prop-object.js | 5 +++-- package.json | 5 +++-- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8febbc53bf..41a9ef23e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/lib/rules/no-children-prop.js b/lib/rules/no-children-prop.js index 54540db784..ebf8304912 100644 --- a/lib/rules/no-children-prop.js +++ b/lib/rules/no-children-prop.js @@ -4,6 +4,8 @@ */ 'use strict'; +var find = require('array.prototype.find'); + // ------------------------------------------------------------------------------ // Helpers // ------------------------------------------------------------------------------ @@ -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'; }); diff --git a/lib/rules/no-danger-with-children.js b/lib/rules/no-danger-with-children.js index d0920dd58b..671bdf40ea 100644 --- a/lib/rules/no-danger-with-children.js +++ b/lib/rules/no-danger-with-children.js @@ -4,6 +4,7 @@ */ 'use strict'; +var find = require('array.prototype.find'); var variableUtil = require('../util/variable'); // ------------------------------------------------------------------------------ @@ -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; }); } @@ -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') { @@ -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) { @@ -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) { diff --git a/lib/rules/require-default-props.js b/lib/rules/require-default-props.js index 784733a9e4..550ac4b8a8 100644 --- a/lib/rules/require-default-props.js +++ b/lib/rules/require-default-props.js @@ -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'); @@ -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; }); @@ -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'; }); diff --git a/lib/rules/style-prop-object.js b/lib/rules/style-prop-object.js index 8f3b3a470e..17718b8576 100644 --- a/lib/rules/style-prop-object.js +++ b/lib/rules/style-prop-object.js @@ -4,6 +4,7 @@ */ 'use strict'; +var find = require('array.prototype.find'); var variableUtil = require('../util/variable'); // ------------------------------------------------------------------------------ @@ -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; }); @@ -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) { diff --git a/package.json b/package.json index 69ad945c6a..3264fcf9c1 100644 --- a/package.json +++ b/package.json @@ -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" },