Skip to content

Commit

Permalink
Handle when object members are joined by a comment
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertAKARobin committed Jan 9, 2025
1 parent 049194b commit 67968b5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
36 changes: 29 additions & 7 deletions src/rules/sort-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,16 @@ export default {
const sensitivity = caseSensitive ? "sensitive" : "insensitive";
const isValidOrder = comparators[direction][sortName][sensitivity];

const commentLineRanges = new Set();
for (const comment of context.sourceCode.comments) {
commentLineRanges.add(
`${comment.loc.start.line}:${comment.loc.end.line}`,
);
}

return {
Object(node) {
let prevMember;
let prevName;

if (node.members.length < minKeys) {
return;
Expand All @@ -102,11 +108,28 @@ export default {
for (const member of node.members) {
const thisName = member.name.value;

if (
prevMember &&
member.loc.start.line - prevMember.loc.end.line < 2
) {
if (isValidOrder(prevName, thisName) === false) {
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,
Expand All @@ -123,7 +146,6 @@ export default {
}

prevMember = member;
prevName = thisName;
}
},
};
Expand Down
49 changes: 24 additions & 25 deletions tests/rules/sort-keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1753,30 +1753,29 @@ ruleTester.run("sort-keys", rule, {
},
],
},
// TODO: This reports in eslint/sort-keys but does not here. Would need to check whether each member is preceded by a comment, which would require mapping members to specific tokens in the AST tree, and there isn't much support for that in the JSON language service right now
// {
// code: `
// {
// "b": 1
// // comment before comma
// , "a": 2
// }
// `,
// language: "json/jsonc",
// options: ["asc", { allowLineSeparatedGroups: true }],
// languageOptions: { ecmaVersion: 6 },
// errors: [
// {
// messageId: "sortKeys",
// data: {
// sortName: "alphanumeric",
// sensitivity: "sensitive",
// direction: "ascending",
// thisName: "a",
// prevName: "b",
// },
// },
// ],
// },
{
code: `
{
"b": 1
// comment before comma
, "a": 2
}
`,
language: "json/jsonc",
options: ["asc", { allowLineSeparatedGroups: true }],
languageOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "sortKeys",
data: {
sortName: "alphanumeric",
sensitivity: "sensitive",
direction: "ascending",
thisName: "a",
prevName: "b",
},
},
],
},
],
});

0 comments on commit 67968b5

Please sign in to comment.