Skip to content

Commit

Permalink
fix: prevent order changes when adding new elements in line-length so…
Browse files Browse the repository at this point in the history
…rting
  • Loading branch information
azat-io committed Sep 6, 2023
1 parent 59c8f67 commit c0e2e60
Show file tree
Hide file tree
Showing 27 changed files with 169 additions and 88 deletions.
3 changes: 2 additions & 1 deletion rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -137,7 +138,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
) {
compareValue = true
} else {
compareValue = compare(left, right, options)
compareValue = isPositive(compare(left, right, options))
}

if (compareValue) {
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-astro-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -159,7 +160,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options))
(leftNum === rightNum &&
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedAstroAttributesOrder',
Expand Down
6 changes: 4 additions & 2 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import type { SortingNode } from '../typings'

import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { toSingleLine } from '../utils/to-single-line'
import { getNodeRange } from '../utils/get-node-range'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -172,7 +173,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
if (
left.name !== right.name &&
(leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options)))
(leftNum === rightNum &&
isPositive(compare(left, right, options))))
) {
context.report({
messageId: 'unexpectedClassesOrder',
Expand Down
3 changes: 2 additions & 1 deletion rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -90,7 +91,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
}))

pairwise(nodes, (left, right) => {
if (compare(left, right, options)) {
if (isPositive(compare(left, right, options))) {
context.report({
messageId: 'unexpectedEnumsOrder',
data: {
Expand Down
3 changes: 2 additions & 1 deletion rules/sort-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { SortingNode } from '../typings'

import { createEslintRule } from '../utils/create-eslint-rule'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -104,7 +105,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

for (let nodes of parts) {
pairwise(nodes, (left, right) => {
if (compare(left, right, options)) {
if (isPositive(compare(left, right, options))) {
context.report({
messageId: 'unexpectedExportsOrder',
data: {
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { getNodeRange } from '../utils/get-node-range'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -454,7 +455,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
) &&
!hasContentBetweenNodes(left, right) &&
(leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options)))
(leftNum === rightNum &&
isPositive(compare(left, right, options))))
) {
context.report({
messageId: 'unexpectedImportsOrder',
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -170,7 +171,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options))
(leftNum === rightNum &&
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedInterfacePropertiesOrder',
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -156,7 +157,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options))
(leftNum === rightNum &&
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedJSXPropsOrder',
Expand Down
3 changes: 2 additions & 1 deletion rules/sort-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -129,7 +130,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
})

pairwise(nodes, (left, right) => {
if (compare(left, right, options)) {
if (isPositive(compare(left, right, options))) {
context.report({
messageId: 'unexpectedMapElementsOrder',
data: {
Expand Down
3 changes: 2 additions & 1 deletion rules/sort-named-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { SortingNode } from '../typings'

import { createEslintRule } from '../utils/create-eslint-rule'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -84,7 +85,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
}))

pairwise(nodes, (left, right) => {
if (compare(left, right, options)) {
if (isPositive(compare(left, right, options))) {
context.report({
messageId: 'unexpectedNamedExportsOrder',
data: {
Expand Down
3 changes: 2 additions & 1 deletion rules/sort-named-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { SortingNode } from '../typings'

import { createEslintRule } from '../utils/create-eslint-rule'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -88,7 +89,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
}))

pairwise(nodes, (left, right) => {
if (compare(left, right, options)) {
if (isPositive(compare(left, right, options))) {
context.report({
messageId: 'unexpectedNamedImportsOrder',
data: {
Expand Down
3 changes: 2 additions & 1 deletion rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -149,7 +150,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options))
(leftNum === rightNum && isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedObjectTypesOrder',
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -273,7 +274,8 @@ export default createEslintRule<Options, MESSAGE_ID>({

if (
leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options))
(leftNum === rightNum &&
isPositive(compare(left, right, options)))
) {
let fix:
| ((fixer: TSESLint.RuleFixer) => TSESLint.RuleFix[])
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-svelte-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -166,7 +167,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options))
(leftNum === rightNum &&
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedSvelteAttributesOrder',
Expand Down
3 changes: 2 additions & 1 deletion rules/sort-union-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { sortNodes } from '../utils/sort-nodes'
import { makeFixes } from '../utils/make-fixes'
Expand Down Expand Up @@ -94,7 +95,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
}))

pairwise(nodes, (left, right) => {
let compareValue = compare(left, right, options)
let compareValue = isPositive(compare(left, right, options))

if (options['nullable-last']) {
if (left.group === 'nullable' && right.group === 'unknown') {
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-vue-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { SortingNode } from '../typings'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getGroupNumber } from '../utils/get-group-number'
import { rangeToDiff } from '../utils/range-to-diff'
import { isPositive } from '../utils/is-positive'
import { SortOrder, SortType } from '../typings'
import { useGroups } from '../utils/use-groups'
import { sortNodes } from '../utils/sort-nodes'
Expand Down Expand Up @@ -170,7 +171,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options))
(leftNum === rightNum &&
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedVueAttributesOrder',
Expand Down
10 changes: 5 additions & 5 deletions test/sort-array-includes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ describe(RULE_NAME, () => {
[
'Armored Titan',
'Attack Titan',
'Cart Titan',
...otherTitans,
'Cart Titan',
'Beast Titan',
'War Hammer Titan',
].includes(titan)
Expand All @@ -530,8 +530,8 @@ describe(RULE_NAME, () => {
[
'War Hammer Titan',
'Armored Titan',
...otherTitans,
'Attack Titan',
...otherTitans,
'Beast Titan',
'Cart Titan',
].includes(titan)
Expand All @@ -542,7 +542,7 @@ describe(RULE_NAME, () => {
messageId: 'unexpectedArrayIncludesOrder',
data: {
left: 'Cart Titan',
right: '...otherTitans',
right: 'Beast Titan',
},
},
{
Expand Down Expand Up @@ -582,8 +582,8 @@ describe(RULE_NAME, () => {
`,
output: dedent`
[
...lowerRanks,
...upperRanks,
...lowerRanks,
...demons,
].includes('Nezuko Kamado')
`,
Expand Down Expand Up @@ -619,7 +619,7 @@ describe(RULE_NAME, () => {
['Genos', 'Bang', 'King',, 'Saitama'].includes(hero)
`,
output: dedent`
['Saitama', 'Genos', 'King',, 'Bang'].includes(hero)
['Saitama', 'Genos', 'Bang',, 'King'].includes(hero)
`,
options: [options],
errors: [
Expand Down
12 changes: 6 additions & 6 deletions test/sort-classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ describe(RULE_NAME, () => {
x() {}
b() {}
get z() {}
set c() {}
get c() {}
set c() {}
}
`,
output: dedent`
Expand Down Expand Up @@ -995,8 +995,8 @@ describe(RULE_NAME, () => {
x() {}
b() {}
get z() {}
set c() {}
get c() {}
set c() {}
}
`,
output: dedent`
Expand Down Expand Up @@ -1161,13 +1161,13 @@ describe(RULE_NAME, () => {
this.stressLevel = value
}
increaseStressLevel(level) {
this.setStressLevel(this.stressLevel + (level ?? 10))
}
decreaseStressLevel(level) {
this.setStressLevel(this.stressLevel - (level ?? 10))
}
increaseStressLevel(level) {
this.setStressLevel(this.stressLevel + (level ?? 10))
}
}
`,
options: [
Expand Down
2 changes: 1 addition & 1 deletion test/sort-exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ describe(RULE_NAME, () => {
export { Daimon1, Daimon2 } from 'police'
`,
output: dedent`
export { Daimon1, Daimon2 } from 'police'
export { HiroshiOdokawa } from 'cab-park'
export { Daimon1, Daimon2 } from 'police'
export { Shibagaki, Baba } from 'radio'
export { Gouriki } from 'hospital'
`,
Expand Down
4 changes: 2 additions & 2 deletions test/sort-maps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,8 @@ describe(RULE_NAME, () => {
output: dedent`
new Map([
[3, 'three'],
[1, 'one'],
[2, 'two'],
[1, 'one'],
])
`,
options: [options],
Expand Down Expand Up @@ -754,8 +754,8 @@ describe(RULE_NAME, () => {
output: dedent`
let apps = new Map([
weatherApp,
musicApp,
booksApp,
musicApp,
mapsApp,
])
`,
Expand Down
Loading

0 comments on commit c0e2e60

Please sign in to comment.