Skip to content

Commit 27309f1

Browse files
committed
[Fix] sort-prop-types jsx-eslint#3470
1 parent 462750e commit 27309f1

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

lib/util/propTypesSort.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,26 @@ function fixPropTypesSort(fixer, context, declarations, ignoreCase, requiredFirs
125125
const node = allNodes[i];
126126
let commentAfter = [];
127127
let commentBefore = [];
128+
let newStart = 0;
129+
let newEnd = 0;
128130
try {
129131
commentBefore = sourceCode.getCommentsBefore(node);
130132
commentAfter = sourceCode.getCommentsAfter(node);
131133
} catch (e) { /**/ };
132-
if (commentAfter.length === 0 && commentBefore.length === 0) {
133-
commentnodeMap.set(node, { start: node.range[0], end: node.range[1], hasComment: false });
134-
} else {
135-
const firstCommentBefore = commentBefore[0];
136-
if (commentBefore.length === 1) {
137-
commentnodeMap.set(node, { start: firstCommentBefore.range[0], end: node.range[1], hasComment: true });
138-
}
139-
const firstCommentAfter = commentAfter[0];
140-
if (commentAfter.length === 1) {
141-
commentnodeMap.set(node, { start: node.range[0], end: firstCommentAfter.range[1], hasComment: true });
142-
}
143-
if (commentBefore.length === 1 && commentAfter.length === 1) {
144-
commentnodeMap.set(node, { start: firstCommentBefore.range[0], end: firstCommentAfter.range[1], hasComment: true });
145-
}
134+
if (commentAfter.length === 0 || commentBefore.length === 0) {
135+
newStart = node.range[0]
136+
newEnd = node.range[1]
137+
}
138+
const firstCommentBefore = commentBefore[0];
139+
if (commentBefore.length >= 1) {
140+
newStart = firstCommentBefore.range[0]
141+
}
142+
const lastCommentAfter = commentAfter[commentAfter.length - 1];
143+
if (commentAfter.length >= 1) {
144+
newEnd = lastCommentAfter.range[1]
146145
}
147-
};
146+
commentnodeMap.set(node, { start: newStart, end: newEnd, hasComment: true });
147+
};
148148
const nodeGroups = allNodes.reduce((acc, curr) => {
149149
if (curr.type === 'ExperimentalSpreadProperty' || curr.type === 'SpreadElement') {
150150
acc.push([]);

tests/lib/rules/sort-prop-types.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -2098,6 +2098,56 @@ ruleTester.run('sort-prop-types', rule, {
20982098
type: 'Property',
20992099
},
21002100
],
2101-
} : []
2101+
} : [],
2102+
semver.satisfies(eslintPkg.version, '> 3') ? {
2103+
code: `
2104+
var First = createReactClass({
2105+
propTypes: {
2106+
/* z */
2107+
/* z */
2108+
z: PropTypes.string /* z */,
2109+
/* a */
2110+
a: PropTypes.any /* a */
2111+
/* a */
2112+
/* a */,
2113+
b: PropTypes.any
2114+
},
2115+
render: function() {
2116+
return <div />;
2117+
}
2118+
});
2119+
`,
2120+
output: `
2121+
var First = createReactClass({
2122+
propTypes: {
2123+
/* a */
2124+
a: PropTypes.any /* a */
2125+
/* a */
2126+
/* a */,
2127+
b: PropTypes.any,
2128+
/* z */
2129+
/* z */
2130+
z: PropTypes.string /* z */
2131+
},
2132+
render: function() {
2133+
return <div />;
2134+
}
2135+
});
2136+
`,
2137+
errors: [
2138+
{
2139+
messageId: 'propsNotSorted',
2140+
line: 8,
2141+
column: 13,
2142+
type: 'Property',
2143+
},
2144+
{
2145+
messageId: 'propsNotSorted',
2146+
line: 11,
2147+
column: 13,
2148+
type: 'Property',
2149+
},
2150+
],
2151+
} : [],
21022152
)),
21032153
});

0 commit comments

Comments
 (0)