Skip to content

Commit edd1353

Browse files
committed
fix comments position
1 parent fcf8e1c commit edd1353

File tree

2 files changed

+95
-7
lines changed

2 files changed

+95
-7
lines changed

lib/rules/sort-styles.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ module.exports = (context) => {
4646
});
4747
}
4848

49+
function getActualRange(node) {
50+
const range = [].concat(node.range);
51+
range[0] = sourceCode
52+
.getCommentsBefore(node)
53+
.reduce((start, comment) => Math.min(start, comment.range[0]), range[0]);
54+
return range;
55+
}
56+
4957
function report(array, type, node, prev, current) {
5058
const currentName = getStylePropertyIdentifier(current);
5159
const prevName = getStylePropertyIdentifier(prev);
@@ -58,7 +66,11 @@ module.exports = (context) => {
5866
return array
5967
.map((item, i) => {
6068
if (item !== sortedArray[i]) {
61-
return fixer.replaceText(item, sourceCode.getText(sortedArray[i]));
69+
const actualRange = getActualRange(sortedArray[i]);
70+
return fixer.replaceTextRange(
71+
getActualRange(item),
72+
sourceCode.text.slice(actualRange[0], actualRange[1])
73+
);
6274
}
6375
return null;
6476
})

tests/lib/rules/sort-styles.js

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,41 +329,117 @@ const tests = {
329329
const styles = StyleSheet.create({
330330
a: {
331331
d: 4,
332-
// comments
332+
// comments 1
333333
c: 3,
334334
a: 1,
335335
b: 2,
336336
},
337337
d: {},
338338
c: {},
339-
// comments
339+
// comments 2
340340
b: {
341341
a: 1,
342342
b: 2,
343343
},
344+
// comments 3
344345
})
345346
`,
346347
output: `
347348
const styles = StyleSheet.create({
348349
a: {
349350
a: 1,
350351
b: 2,
351-
// comments
352+
// comments 1
352353
c: 3,
353354
d: 4,
354355
},
355-
// comments
356+
// comments 2
356357
b: {
357358
a: 1,
358359
b: 2,
359360
},
360361
c: {},
361362
d: {},
363+
// comments 3
362364
})
363365
`,
364366
errors: [
365-
{ message: "Expected style properties to be in ascending order. 'c' should be before 'd'." },
366-
{ message: "Expected class names to be in ascending order. 'c' should be before 'd'." },
367+
{
368+
message:
369+
"Expected style properties to be in ascending order. 'c' should be before 'd'.",
370+
},
371+
{
372+
message:
373+
"Expected class names to be in ascending order. 'c' should be before 'd'.",
374+
},
375+
],
376+
},
377+
{
378+
code: `
379+
const styles = StyleSheet.create({
380+
a: {
381+
d: 4,
382+
// singleline 1
383+
// singleline 2
384+
// singleline 3
385+
c: 3,
386+
a: 1,
387+
b: 2,
388+
},
389+
d: {},
390+
c: {},
391+
/*
392+
multiline 1
393+
*/
394+
/*
395+
multiline 2
396+
*/
397+
/*
398+
multiline 3
399+
*/
400+
b: {
401+
a: 1,
402+
b: 2,
403+
},
404+
})
405+
`,
406+
output: `
407+
const styles = StyleSheet.create({
408+
a: {
409+
a: 1,
410+
b: 2,
411+
// singleline 1
412+
// singleline 2
413+
// singleline 3
414+
c: 3,
415+
d: 4,
416+
},
417+
/*
418+
multiline 1
419+
*/
420+
/*
421+
multiline 2
422+
*/
423+
/*
424+
multiline 3
425+
*/
426+
b: {
427+
a: 1,
428+
b: 2,
429+
},
430+
c: {},
431+
d: {},
432+
})
433+
`,
434+
errors: [
435+
{
436+
message:
437+
"Expected style properties to be in ascending order. 'c' should be before 'd'.",
438+
},
439+
{
440+
message:
441+
"Expected class names to be in ascending order. 'c' should be before 'd'.",
442+
},
367443
],
368444
},
369445
],

0 commit comments

Comments
 (0)