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

[Fix] - Fix sorting props with numeric keys #2230

Merged
merged 1 commit into from
Apr 9, 2019
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
12 changes: 8 additions & 4 deletions lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -107,8 +111,8 @@ module.exports = {
}

if (ignoreCase) {
aKey = aKey.toLowerCase();
bKey = bKey.toLowerCase();
aKey = toLowerCase(aKey);
bKey = toLowerCase(bKey);
}

if (aKey < bKey) {
Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,21 @@ ruleTester.run('sort-prop-types', rule, {
options: [{
noSortAlphabetically: true
}]
}, {
code: `
class Component extends React.Component {
render() {
return <div />;
}
}
Component.propTypes = {
0: PropTypes.any,
1: PropTypes.any,
};
`,
options: [{
ignoreCase: true
}]
}],

invalid: [{
Expand Down Expand Up @@ -1565,5 +1580,37 @@ ruleTester.run('sort-prop-types', rule, {
' }',
'});'
].join('\n')
}, {
code: `
class Component extends React.Component {
render() {
return <div />;
}
}
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 <div />;
}
}
Component.propTypes = {
0: PropTypes.any,
1: PropTypes.any,
};
`
}]
});