Skip to content

Commit f94d851

Browse files
[Fix] jsx-sort-props: only use localeCompare when case is ignored
Fixes jsx-eslint#2381. Fixes jsx-eslint#2530. Co-authored-by: tanmoyopenroot <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
1 parent c481a26 commit f94d851

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

lib/rules/jsx-sort-props.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ function contextCompare(a, b, options) {
7171
if (options.ignoreCase) {
7272
aProp = aProp.toLowerCase();
7373
bProp = bProp.toLowerCase();
74+
return aProp.localeCompare(bProp);
7475
}
75-
return aProp.localeCompare(bProp);
76+
if (aProp === bProp) {
77+
return 0;
78+
}
79+
return aProp < bProp ? -1 : 1;
7680
}
7781

7882
/**
@@ -342,7 +346,14 @@ module.exports = {
342346
}
343347
}
344348

345-
if (!noSortAlphabetically && previousPropName.localeCompare(currentPropName) > 0) {
349+
if (
350+
!noSortAlphabetically
351+
&& (
352+
ignoreCase
353+
? previousPropName.localeCompare(currentPropName) > 0
354+
: previousPropName > currentPropName
355+
)
356+
) {
346357
context.report({
347358
node: decl.name,
348359
message: 'Props should be sorted alphabetically',

tests/lib/rules/jsx-sort-props.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,18 @@ ruleTester.run('jsx-sort-props', rule, {
109109
{code: '<App a="c" b="b" c="a" />;'},
110110
{code: '<App {...this.props} a="c" b="b" c="a" />;'},
111111
{code: '<App c="a" {...this.props} a="c" b="b" />;'},
112-
{code: '<App a A />;'},
113-
{code: '<App aa aB />;'},
112+
{code: '<App A a />;'},
113+
{code: '<App aB aa/>;'},
114114
{code: '<App aA aB />;'},
115-
{code: '<App aaa aB />;'},
116-
{code: '<App a aa aB />;'},
115+
{code: '<App aB aaa />;'},
116+
{code: '<App a aB aa />;'},
117+
{code: '<App Number="2" name="John" />;'},
117118
// Ignoring case
118119
{code: '<App a A />;', options: ignoreCaseArgs},
120+
{code: '<App aa aB />;', options: ignoreCaseArgs},
119121
{code: '<App a B c />;', options: ignoreCaseArgs},
120122
{code: '<App A b C />;', options: ignoreCaseArgs},
123+
{code: '<App name="John" Number="2" />;', options: ignoreCaseArgs},
121124
// Sorting callbacks below all other props
122125
{code: '<App a z onBar onFoo />;', options: callbacksLastArgs},
123126
{code: '<App z onBar onFoo />;', options: ignoreCaseAndCallbackLastArgs},
@@ -175,9 +178,14 @@ ruleTester.run('jsx-sort-props', rule, {
175178
output: '<App a aB />;'
176179
},
177180
{
178-
code: '<App A a />;',
181+
code: '<App fistName="John" tel={5555555} name="John Smith" lastName="Smith" Number="2" />;',
182+
errors: [expectedError, expectedError, expectedError],
183+
output: '<App Number="2" fistName="John" lastName="Smith" name="John Smith" tel={5555555} />;'
184+
},
185+
{
186+
code: '<App aa aB />;',
179187
errors: [expectedError],
180-
output: '<App a A />;'
188+
output: '<App aB aa />;'
181189
},
182190
{
183191
code: '<App aB aA />;',
@@ -191,8 +199,8 @@ ruleTester.run('jsx-sort-props', rule, {
191199
},
192200
{
193201
code: '<App aaB aaa aA a />;',
194-
errors: [expectedError, expectedError, expectedError],
195-
output: '<App a aA aaa aaB />;'
202+
errors: [expectedError, expectedError],
203+
output: '<App a aA aaB aaa />;'
196204
},
197205
{
198206
code: '<App {...this.props} b a />;',
@@ -204,6 +212,12 @@ ruleTester.run('jsx-sort-props', rule, {
204212
errors: [expectedError],
205213
output: '<App c {...this.props} a b />;'
206214
},
215+
{
216+
code: '<App fistName="John" tel={5555555} name="John Smith" lastName="Smith" Number="2" />;',
217+
options: ignoreCaseArgs,
218+
errors: [expectedError, expectedError, expectedError],
219+
output: '<App fistName="John" lastName="Smith" name="John Smith" Number="2" tel={5555555} />;'
220+
},
207221
{
208222
code: '<App B a />;',
209223
options: ignoreCaseArgs,

0 commit comments

Comments
 (0)