diff --git a/rules/sort-classes.ts b/rules/sort-classes.ts index e08bd0798..3be6d45b5 100644 --- a/rules/sort-classes.ts +++ b/rules/sort-classes.ts @@ -680,11 +680,6 @@ export default createEslintRule({ 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) @@ -692,7 +687,7 @@ export default createEslintRule({ getFirstUnorderedNodeDependentOn(right, nodes) if ( firstUnorderedNodeDependentOnRight || - (!isLeftOrRightIgnored && indexOfLeft > indexOfRight) + indexOfLeft > indexOfRight ) { let messageId: MESSAGE_ID if (firstUnorderedNodeDependentOnRight) { diff --git a/test/sort-classes.test.ts b/test/sort-classes.test.ts index d3fd2a2a3..cf60f5c02 100644 --- a/test/sort-classes.test.ts +++ b/test/sort-classes.test.ts @@ -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', + }, + }, ], }, ], @@ -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', + }, + }, ], }, ], @@ -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', + }, + }, ], }, ],