Skip to content

Commit

Permalink
fix: fix linting of nodes that are neighboring to ignored nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Sep 26, 2024
1 parent 7a0a96c commit a11841a
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 6 deletions.
7 changes: 1 addition & 6 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,19 +680,14 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)
// Ignore nodes belonging to `unknown` group when that group is not referenced in the
// `groups` option.
let isLeftOrRightIgnored =
leftNum === options.groups.length ||
rightNum === options.groups.length

let indexOfLeft = sortedNodes.indexOf(left)
let indexOfRight = sortedNodes.indexOf(right)
let firstUnorderedNodeDependentOnRight =
getFirstUnorderedNodeDependentOn(right, nodes)
if (
firstUnorderedNodeDependentOnRight ||
(!isLeftOrRightIgnored && indexOfLeft > indexOfRight)
indexOfLeft > indexOfRight
) {
let messageId: MESSAGE_ID
if (firstUnorderedNodeDependentOnRight) {
Expand Down
160 changes: 160 additions & 0 deletions test/sort-classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3923,6 +3923,77 @@ describe(ruleName, () => {
rightGroup: 'private-method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'z',
leftGroup: 'private-method',
right: 'method3',
rightGroup: 'unknown',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'method3',
leftGroup: 'unknown',
right: 'y',
rightGroup: 'private-method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'method1',
leftGroup: 'unknown',
right: 'x',
rightGroup: 'private-method',
},
},
],
},
{
code: dedent`
class Class {
b
someMethod() {
}
a
}
`,
output: dedent`
class Class {
a
someMethod() {
}
b
}
`,
options: [
{
...options,
groups: ['property'],
},
],
errors: [
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'b',
leftGroup: 'property',
right: 'someMethod',
rightGroup: 'unknown',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'someMethod',
leftGroup: 'unknown',
right: 'a',
rightGroup: 'property',
},
},
],
},
],
Expand Down Expand Up @@ -5135,6 +5206,77 @@ describe(ruleName, () => {
rightGroup: 'private-method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'z',
leftGroup: 'private-method',
right: 'method3',
rightGroup: 'unknown',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'method3',
leftGroup: 'unknown',
right: 'y',
rightGroup: 'private-method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'method1',
leftGroup: 'unknown',
right: 'x',
rightGroup: 'private-method',
},
},
],
},
{
code: dedent`
class Class {
b
someMethod() {
}
a
}
`,
output: dedent`
class Class {
a
someMethod() {
}
b
}
`,
options: [
{
...options,
groups: ['property'],
},
],
errors: [
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'b',
leftGroup: 'property',
right: 'someMethod',
rightGroup: 'unknown',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'someMethod',
leftGroup: 'unknown',
right: 'a',
rightGroup: 'property',
},
},
],
},
],
Expand Down Expand Up @@ -5970,6 +6112,24 @@ describe(ruleName, () => {
rightGroup: 'private-method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'method3',
leftGroup: 'unknown',
right: 'y',
rightGroup: 'private-method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'method1',
leftGroup: 'unknown',
right: 'x',
rightGroup: 'private-method',
},
},
],
},
],
Expand Down

0 comments on commit a11841a

Please sign in to comment.