Skip to content

Commit f6986cb

Browse files
committed
no autofix if have comments
1 parent 11ccc85 commit f6986cb

File tree

2 files changed

+15
-84
lines changed

2 files changed

+15
-84
lines changed

lib/rules/sort-styles.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,29 @@ module.exports = (context) => {
4949
});
5050
}
5151

52-
function getActualRange(node) {
53-
const range = [].concat(node.range);
54-
range[0] = sourceCode
55-
.getCommentsBefore(node)
56-
.reduce((start, comment) => Math.min(start, comment.range[0]), range[0]);
57-
return range;
58-
}
59-
6052
function report(array, type, node, prev, current) {
6153
const currentName = getStylePropertyIdentifier(current);
6254
const prevName = getStylePropertyIdentifier(prev);
55+
const hasComments = array
56+
.map(prop => sourceCode.getComments(prop))
57+
.reduce(
58+
(hasComment, comment) =>
59+
hasComment || comment.leading.length > 0 || comment.trailing > 0,
60+
false
61+
);
62+
6363
context.report({
6464
node,
6565
message: `Expected ${type} to be in ${order}ending order. '${currentName}' should be before '${prevName}'.`,
6666
loc: current.key.loc,
67-
fix(fixer) {
67+
fix: hasComments ? undefined : (fixer) => {
6868
const sortedArray = sort(array);
6969
return array
7070
.map((item, i) => {
7171
if (item !== sortedArray[i]) {
72-
const actualRange = getActualRange(sortedArray[i]);
73-
return fixer.replaceTextRange(
74-
getActualRange(item),
75-
sourceCode.text.slice(actualRange[0], actualRange[1])
72+
return fixer.replaceText(
73+
item,
74+
sourceCode.getText(sortedArray[i])
7675
);
7776
}
7877
return null;

tests/lib/rules/sort-styles.js

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -398,90 +398,22 @@ const tests = {
398398
})
399399
`,
400400
output: `
401-
const styles = StyleSheet.create({
402-
a: {
403-
a: 1,
404-
b: 2,
405-
// comments 1
406-
c: 3,
407-
d: 4,
408-
},
409-
// comments 2
410-
b: {
411-
a: 1,
412-
b: 2,
413-
},
414-
c: {},
415-
d: {},
416-
// comments 3
417-
})
418-
`,
419-
errors: [
420-
{
421-
message:
422-
"Expected style properties to be in ascending order. 'c' should be before 'd'.",
423-
},
424-
{
425-
message:
426-
"Expected class names to be in ascending order. 'c' should be before 'd'.",
427-
},
428-
],
429-
},
430-
{
431-
code: `
432401
const styles = StyleSheet.create({
433402
a: {
434403
d: 4,
435-
// singleline 1
436-
// singleline 2
437-
// singleline 3
404+
// comments 1
438405
c: 3,
439406
a: 1,
440407
b: 2,
441408
},
442409
d: {},
443410
c: {},
444-
/*
445-
multiline 1
446-
*/
447-
/*
448-
multiline 2
449-
*/
450-
/*
451-
multiline 3
452-
*/
453-
b: {
454-
a: 1,
455-
b: 2,
456-
},
457-
})
458-
`,
459-
output: `
460-
const styles = StyleSheet.create({
461-
a: {
462-
a: 1,
463-
b: 2,
464-
// singleline 1
465-
// singleline 2
466-
// singleline 3
467-
c: 3,
468-
d: 4,
469-
},
470-
/*
471-
multiline 1
472-
*/
473-
/*
474-
multiline 2
475-
*/
476-
/*
477-
multiline 3
478-
*/
411+
// comments 2
479412
b: {
480413
a: 1,
481414
b: 2,
482415
},
483-
c: {},
484-
d: {},
416+
// comments 3
485417
})
486418
`,
487419
errors: [

0 commit comments

Comments
 (0)