Skip to content

Commit

Permalink
[Fix] sort-prop-types #3470
Browse files Browse the repository at this point in the history
  • Loading branch information
ROSSROSALES committed Nov 26, 2022
1 parent cfef97f commit 1ee5db8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 21 deletions.
36 changes: 16 additions & 20 deletions lib/util/propTypesSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,26 @@ function fixPropTypesSort(fixer, context, declarations, ignoreCase, requiredFirs
const node = allNodes[i];
let commentAfter = [];
let commentBefore = [];
let newStart = 0;
let newEnd = 0;
try {
commentBefore = sourceCode.getCommentsBefore(node);
commentAfter = sourceCode.getCommentsAfter(node);
} catch (e) { /**/ }
if (commentAfter.length === 0 && commentBefore.length === 0) {
commentnodeMap.set(node, { start: node.range[0], end: node.range[1], hasComment: false });
} else {
const firstCommentBefore = commentBefore[0];
if (commentBefore.length === 1) {
commentnodeMap.set(node, { start: firstCommentBefore.range[0], end: node.range[1], hasComment: true });
}
const firstCommentAfter = commentAfter[0];
if (commentAfter.length === 1) {
commentnodeMap.set(node, { start: node.range[0], end: firstCommentAfter.range[1], hasComment: true });
}
if (commentBefore.length === 1 && commentAfter.length === 1) {
commentnodeMap.set(node, {
start: firstCommentBefore.range[0],
end: firstCommentAfter.range[1],
hasComment: true,
});
}
} catch (e) { /**/ };
if (commentAfter.length === 0 || commentBefore.length === 0) {
newStart = node.range[0]
newEnd = node.range[1]
}
}
const firstCommentBefore = commentBefore[0];
if (commentBefore.length >= 1) {
newStart = firstCommentBefore.range[0]
}
const lastCommentAfter = commentAfter[commentAfter.length - 1];
if (commentAfter.length >= 1) {
newEnd = lastCommentAfter.range[1]
}
commentnodeMap.set(node, { start: newStart, end: newEnd, hasComment: true });
};
const nodeGroups = allNodes.reduce((acc, curr) => {
if (curr.type === 'ExperimentalSpreadProperty' || curr.type === 'SpreadElement') {
acc.push([]);
Expand Down
52 changes: 51 additions & 1 deletion tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,56 @@ ruleTester.run('sort-prop-types', rule, {
type: 'Property',
},
],
} : []
} : [],
semver.satisfies(eslintPkg.version, '> 3') ? {
code: `
var First = createReactClass({
propTypes: {
/* z */
/* z */
z: PropTypes.string /* z */,
/* a */
a: PropTypes.any /* a */
/* a */
/* a */,
b: PropTypes.any
},
render: function() {
return <div />;
}
});
`,
output: `
var First = createReactClass({
propTypes: {
/* a */
a: PropTypes.any /* a */
/* a */
/* a */,
b: PropTypes.any,
/* z */
/* z */
z: PropTypes.string /* z */
},
render: function() {
return <div />;
}
});
`,
errors: [
{
messageId: 'propsNotSorted',
line: 8,
column: 13,
type: 'Property',
},
{
messageId: 'propsNotSorted',
line: 11,
column: 13,
type: 'Property',
},
],
} : [],
)),
});

0 comments on commit 1ee5db8

Please sign in to comment.