Skip to content

Commit 0d72eb2

Browse files
committed
Address comments
1 parent aef553d commit 0d72eb2

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

lib/rules/jsx-sort-props.js

+23-30
Original file line numberDiff line numberDiff line change
@@ -40,56 +40,49 @@ function isReservedPropName(name, list) {
4040
return list.indexOf(name) >= 0;
4141
}
4242

43-
4443
function alphabeticalCompare(a, b, ignoreCase) {
4544
if (ignoreCase) {
4645
a = a.toLowerCase();
4746
b = b.toLowerCase();
4847
}
49-
50-
if (a < b) {
51-
return -1;
52-
} else if (a > b) {
53-
return 1;
54-
}
55-
56-
return 0;
48+
return a.localeCompare(b);
5749
}
5850

5951
function generateFixerFunction(node, context) {
6052
var sourceCode = context.getSourceCode();
61-
var sortedAttributes = node.attributes.slice(0);
53+
var attributes = node.attributes.slice(0);
6254
var configuration = context.options[0] || {};
6355
var ignoreCase = configuration.ignoreCase || false;
6456

6557
// Sort props according to the context. Only supports ignoreCase.
66-
sortedAttributes.sort(function(a, b) {
67-
// Put spread attributes at the end.
68-
if (a.type === 'JSXSpreadAttribute' && b.type !== 'JSXSpreadAttribute') {
69-
return 1;
70-
} else if (a.type !== 'JSXSpreadAttribute' && b.type === 'JSXSpreadAttribute') {
71-
return -1;
72-
} else if (a.type === 'JSXSpreadAttribute' && b.type === 'JSXSpreadAttribute') {
73-
return alphabeticalCompare(
74-
sourceCode.getText(a),
75-
sourceCode.getText(b),
76-
ignoreCase
77-
);
78-
}
79-
80-
var propNameA = propName(a);
81-
var propNameB = propName(b);
58+
// Since we cannot safely move JSXSpreadAttributes (due to potential variable overrides),
59+
// we only consider the sortable attributes.
60+
const sortableAttributes = attributes.filter(function(attr) {
61+
return attr.type !== 'JSXSpreadAttribute';
62+
});
8263

83-
return alphabeticalCompare(propNameA, propNameB, ignoreCase);
64+
sortableAttributes.sort(function(a, b) {
65+
return alphabeticalCompare(propName(a), propName(b), ignoreCase);
8466
});
8567

8668
return function(fixer) {
8769
// Replace each unsorted attribute with the sorted one.
88-
return node.attributes.map(function(attr, index) {
89-
var sortedAttr = sortedAttributes[index];
70+
const fixers = [];
71+
let skipIndex = 0; // skip over spread attributes
72+
73+
node.attributes.forEach(function(attr, index) {
74+
if (attr.type === 'JSXSpreadAttribute') {
75+
skipIndex++;
76+
return;
77+
}
78+
79+
var sortedAttr = sortableAttributes[index - skipIndex];
9080
var sortedAttrText = sourceCode.getText(sortedAttr);
91-
return fixer.replaceTextRange([attr.start, attr.end], sortedAttrText);
81+
fixers.push(
82+
fixer.replaceTextRange([attr.start, attr.end], sortedAttrText)
83+
);
9284
});
85+
return fixers;
9386
};
9487
}
9588

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,17 @@ ruleTester.run('jsx-sort-props', rule, {
161161
{
162162
code: '<App {...this.props} b a />;',
163163
errors: [expectedError],
164-
output: '<App a b {...this.props} />;'
164+
output: '<App {...this.props} a b />;'
165165
},
166166
{
167167
code: '<App c {...this.props} b a />;',
168168
errors: [expectedError],
169-
output: '<App a b c {...this.props} />;'
169+
output: '<App a {...this.props} b c />;'
170170
},
171171
{
172172
code: '<App a A />;',
173173
errors: [expectedError],
174-
output: '<App A a />;'
174+
output: '<App a A />;'
175175
},
176176
{
177177
code: '<App B a />;',
@@ -192,12 +192,12 @@ ruleTester.run('jsx-sort-props', rule, {
192192
},
193193
{
194194
code: '<App {...this.props} c="a" a="c" b="b" />;',
195-
output: '<App a="c" b="b" c="a" {...this.props} />;',
195+
output: '<App {...this.props} a="c" b="b" c="a" />;',
196196
errors: 2
197197
},
198198
{
199199
code: '<App d="d" b="b" {...this.props} c="a" a="c" />;',
200-
output: '<App a="c" b="b" c="a" d="d" {...this.props} />;',
200+
output: '<App a="c" b="b" {...this.props} c="a" d="d" />;',
201201
errors: 2
202202
},
203203
{
@@ -221,9 +221,9 @@ ruleTester.run('jsx-sort-props', rule, {
221221
'b={false}',
222222
'onHandle={function(){}}',
223223
'r',
224+
'{...this.props}',
224225
'z',
225-
'{...otherProps}',
226-
'{...this.props}>',
226+
'{...otherProps}>',
227227
' {test}',
228228
'</App>'
229229
].join('\n'),

0 commit comments

Comments
 (0)