Skip to content

Commit

Permalink
Commas do not count as group-separating lines
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertAKARobin committed Jan 17, 2025
1 parent 05f069d commit 7cfda22
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 30 deletions.
29 changes: 24 additions & 5 deletions src/rules/sort-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,36 @@ export default {
}
}

const commaLinesByPosition = {};
for (const token of context.sourceCode.ast.tokens) {
if (token.type === `Comma`) {
commaLinesByPosition[token.range[0]] = token.loc.start.line; // Assume commas are always a single character
}
}

// Note that there can be comments *inside* members, e.g. `{"foo: /* comment *\/ "bar"}`, but these are ignored when calculating line-separated groups
function isLineSeparated(prevMember, member) {
const prevLine = prevMember.loc.end.line;
const thisLine = member.loc.start.line;
let prevMemberEndLine = prevMember.loc.end.line;

const prevMemberEndPosition = prevMember.range[1];
const memberStartPosition = member.range[0];
let position = prevMemberEndPosition + 1;
while (position < memberStartPosition) {
if (position in commaLinesByPosition) {
prevMemberEndLine = commaLinesByPosition[position];
break;
}

position += 1;
}

if (thisLine - prevLine < 2) {
const thisStartLine = member.loc.start.line;
if (thisStartLine - prevMemberEndLine < 2) {
return false;
}

let lineNum = prevLine + 1;
while (lineNum < thisLine) {
let lineNum = prevMemberEndLine + 1;
while (lineNum < thisStartLine) {
if (!commentLineNums.has(lineNum)) {
return true;
}
Expand Down
77 changes: 52 additions & 25 deletions tests/rules/sort-keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,31 +440,6 @@ ruleTester.run("sort-keys", rule, {
`,
options: ["asc", { allowLineSeparatedGroups: true }],
},
{
code: `
{
"b": 1
,"a": 2
}
`,
options: ["asc", { allowLineSeparatedGroups: true }],
languageOptions: { ecmaVersion: 6 },
},
{
code: `
{
"b": 1
// comment before comma
,
"a": 2
}
`,
language: "json/jsonc",
options: ["asc", { allowLineSeparatedGroups: true }],
languageOptions: { ecmaVersion: 6 },
},
{
code: `
{
Expand Down Expand Up @@ -499,6 +474,32 @@ ruleTester.run("sort-keys", rule, {
language: `json/json5`,
options: ["desc", { allowLineSeparatedGroups: true }],
},

// Commas are not considered separating lines
{
code: `
{
"b": 1
,
"a": 2
}
`,
options: ["asc", { allowLineSeparatedGroups: true }],
},
{
code: `
{
"a": 1
,
"b": 2
}
`,
options: ["asc", { allowLineSeparatedGroups: false }],
},
],
invalid: [
// default (asc)
Expand Down Expand Up @@ -1914,6 +1915,32 @@ ruleTester.run("sort-keys", rule, {
},
],
},
{
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: `
[
Expand Down

0 comments on commit 7cfda22

Please sign in to comment.