From b37a504c63090dd1e10f3183c6ff93dc5408fad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Nowak?= Date: Tue, 9 Apr 2019 20:25:34 +0200 Subject: [PATCH] [Fix] `sort-prop-types`: Fix sorting props with numeric keys --- lib/rules/sort-prop-types.js | 12 +++++--- tests/lib/rules/sort-prop-types.js | 47 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/lib/rules/sort-prop-types.js b/lib/rules/sort-prop-types.js index 676785c061..71cff4a88b 100644 --- a/lib/rules/sort-prop-types.js +++ b/lib/rules/sort-prop-types.js @@ -85,6 +85,10 @@ module.exports = { return node.arguments && node.arguments[0] && node.arguments[0].properties; } + function toLowerCase(item) { + return String(item).toLowerCase(); + } + function sorter(a, b) { let aKey = getKey(a); let bKey = getKey(b); @@ -107,8 +111,8 @@ module.exports = { } if (ignoreCase) { - aKey = aKey.toLowerCase(); - bKey = bKey.toLowerCase(); + aKey = toLowerCase(aKey); + bKey = toLowerCase(bKey); } if (aKey < bKey) { @@ -188,8 +192,8 @@ module.exports = { const currentIsCallback = isCallbackPropName(currentPropName); if (ignoreCase) { - prevPropName = prevPropName.toLowerCase(); - currentPropName = currentPropName.toLowerCase(); + prevPropName = toLowerCase(prevPropName); + currentPropName = toLowerCase(currentPropName); } if (requiredFirst) { diff --git a/tests/lib/rules/sort-prop-types.js b/tests/lib/rules/sort-prop-types.js index 684444214f..4571bf3e57 100644 --- a/tests/lib/rules/sort-prop-types.js +++ b/tests/lib/rules/sort-prop-types.js @@ -410,6 +410,21 @@ ruleTester.run('sort-prop-types', rule, { options: [{ noSortAlphabetically: true }] + }, { + code: ` + class Component extends React.Component { + render() { + return
; + } + } + Component.propTypes = { + 0: PropTypes.any, + 1: PropTypes.any, + }; + `, + options: [{ + ignoreCase: true + }] }], invalid: [{ @@ -1565,5 +1580,37 @@ ruleTester.run('sort-prop-types', rule, { ' }', '});' ].join('\n') + }, { + code: ` + class Component extends React.Component { + render() { + return
; + } + } + Component.propTypes = { + 1: PropTypes.any, + 0: PropTypes.any, + }; + `, + options: [{ + ignoreCase: true + }], + errors: [{ + message: ERROR_MESSAGE, + line: 9, + column: 9, + type: 'Property' + }], + output: ` + class Component extends React.Component { + render() { + return
; + } + } + Component.propTypes = { + 0: PropTypes.any, + 1: PropTypes.any, + }; + ` }] });