Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertAKARobin committed Jan 9, 2025
1 parent 7ccba2c commit 2c210e9
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions src/rules/sort-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export default {
const sensitivity = caseSensitive ? "sensitive" : "insensitive";
const isValidOrder = comparators[direction][sortName][sensitivity];

const commentLineRanges = new Set();
const commentLines = new Set();
for (const comment of context.sourceCode.comments) {
commentLineRanges.add(
commentLines.add(
`${comment.loc.start.line}:${comment.loc.end.line}`,
);
}
Expand All @@ -108,41 +108,38 @@ export default {
for (const member of node.members) {
const thisName = member.name.value;

if (prevMember) {
const prevName = prevMember?.name.value;
const prevLine = prevMember?.loc.end.line;
const thisLine = member.loc.start.line;

const membersAreJoinedByComment =
commentLineRanges.has(`${prevLine}:${thisLine}`) ||
commentLineRanges.has(
`${prevLine + 1}:${thisLine}`,
) ||
commentLineRanges.has(
`${prevLine}:${thisLine - 1}`,
) ||
commentLineRanges.has(
`${prevLine + 1}:${thisLine - 1}`,
);

if (
(thisLine - prevLine < 2 ||
membersAreJoinedByComment) &&
isValidOrder(prevName, thisName) === false
) {
context.report({
node,
loc: member.name.loc,
messageId: "sortKeys",
data: {
thisName,
prevName,
direction,
sensitivity,
sortName,
},
});
}
if (!prevMember) {
prevMember = member;
continue;
}

const prevName = prevMember?.name.value;
const prevLine = prevMember?.loc.end.line;
const thisLine = member.loc.start.line;

const membersAreAdjacent =
thisLine - prevLine < 2 ||
commentLines.has(`${prevLine}:${thisLine}`) ||
commentLines.has(`${prevLine + 1}:${thisLine}`) ||
commentLines.has(`${prevLine}:${thisLine - 1}`) ||
commentLines.has(`${prevLine + 1}:${thisLine - 1}`);

if (
membersAreAdjacent &&
isValidOrder(prevName, thisName) === false
) {
context.report({
node,
loc: member.name.loc,
messageId: "sortKeys",
data: {
thisName,
prevName,
direction,
sensitivity,
sortName,
},
});
}

prevMember = member;
Expand Down

0 comments on commit 2c210e9

Please sign in to comment.