Skip to content

Commit

Permalink
fix: don't sort keys if right value depends on left
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 10, 2023
1 parent e435f91 commit 3e987ae
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 1 deletion.
45 changes: 44 additions & 1 deletion rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export enum Position {
'ignore' = 'ignore',
}

type SortingNodeWithPosition = SortingNode & { position: Position }
type SortingNodeWithPosition = SortingNode & {
position: Position
}

type Options = [
Partial<{
Expand Down Expand Up @@ -116,6 +118,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

let name: string
let position: Position = Position.ignore
let dependencies: string[] = []

if (prop.key.type === AST_NODE_TYPES.Identifier) {
;({ name } = prop.key)
Expand All @@ -132,8 +135,47 @@ export default createEslintRule<Options, MESSAGE_ID>({
position = Position.exception
}

if (prop.value.type === AST_NODE_TYPES.AssignmentPattern) {
let addDependencies = (
value: TSESTree.AssignmentPattern | TSESTree.BinaryExpression,
initialStart: boolean,
) => {
if (value.right.type === AST_NODE_TYPES.Identifier) {
dependencies.push(value.right.name)
}

if (
!initialStart &&
value.left.type === AST_NODE_TYPES.Identifier
) {
dependencies.push(value.left.name)
}

let handleBinaryExpression = (
expression: TSESTree.BinaryExpression,
) => {
if (expression.right.type === AST_NODE_TYPES.Identifier) {
dependencies.push(expression.right.name)
}

if (
expression.left.type === AST_NODE_TYPES.BinaryExpression
) {
addDependencies(expression.left, false)
}
}

if (value.right.type === AST_NODE_TYPES.BinaryExpression) {
handleBinaryExpression(value.right)
}
}

addDependencies(prop.value, true)
}

let value = {
size: rangeToDiff(prop.range),
dependencies,
node: prop,
position,
name,
Expand Down Expand Up @@ -184,6 +226,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
options['always-on-top'].indexOf(aNode.name) -
options['always-on-top'].indexOf(bNode.name),
),

sortNodes(getGroup(Position.ignore), options),
].flat()

Expand Down
277 changes: 277 additions & 0 deletions test/sort-objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,96 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}(${type}): does not sort keys if the right value depends on the left value`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [],
invalid: [
{
code: dedent`
let getPartiallyParasite = ({
parasite,
name = parasite,
school = 'West Heigh',
gender,
}) => {
// ...
}
`,
output: dedent`
let getPartiallyParasite = ({
gender,
parasite,
name = parasite,
school = 'West Heigh',
}) => {
// ...
}
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'school',
right: 'gender',
},
},
],
},
],
})
})

it(`${RULE_NAME}(${type}): works with complex dependencies`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [],
invalid: [
{
code: dedent`
let countPrisonSchoolGrade = ({
biology,
finalScore = biology + math + naturalScience,
math,
naturalScience,
}) => {
// ...
}
`,
output: dedent`
let countPrisonSchoolGrade = ({
biology,
math,
naturalScience,
finalScore = biology + math + naturalScience,
}) => {
// ...
}
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'finalScore',
right: 'math',
},
},
],
},
],
})
})
})

describe(`${RULE_NAME}: sorting by natural order`, () => {
Expand Down Expand Up @@ -1012,6 +1102,96 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}(${type}): does not sort keys if the right value depends on the left value`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [],
invalid: [
{
code: dedent`
let getPartiallyParasite = ({
parasite,
name = parasite,
school = 'West Heigh',
gender,
}) => {
// ...
}
`,
output: dedent`
let getPartiallyParasite = ({
gender,
parasite,
name = parasite,
school = 'West Heigh',
}) => {
// ...
}
`,
options: [
{
type: SortType.natural,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'school',
right: 'gender',
},
},
],
},
],
})
})

it(`${RULE_NAME}(${type}): works with complex dependencies`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [],
invalid: [
{
code: dedent`
let countPrisonSchoolGrade = ({
biology,
finalScore = biology + math + naturalScience,
math,
naturalScience,
}) => {
// ...
}
`,
output: dedent`
let countPrisonSchoolGrade = ({
biology,
math,
naturalScience,
finalScore = biology + math + naturalScience,
}) => {
// ...
}
`,
options: [
{
type: SortType.natural,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'finalScore',
right: 'math',
},
},
],
},
],
})
})
})

describe(`${RULE_NAME}: sorting by line length`, () => {
Expand Down Expand Up @@ -1514,6 +1694,103 @@ describe(RULE_NAME, () => {
],
})
})

it(`${RULE_NAME}(${type}): does not sort keys if the right value depends on the left value`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [],
invalid: [
{
code: dedent`
let getPartiallyParasite = ({
parasite,
name = parasite,
school = 'West Heigh',
gender,
}) => {
// ...
}
`,
output: dedent`
let getPartiallyParasite = ({
school = 'West Heigh',
parasite,
name = parasite,
gender,
}) => {
// ...
}
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'name',
right: 'school',
},
},
],
},
],
})
})

it(`${RULE_NAME}(${type}): works with complex dependencies`, () => {
ruleTester.run(RULE_NAME, rule, {
valid: [],
invalid: [
{
code: dedent`
let countPrisonSchoolGrade = ({
biology,
finalScore = biology + math + naturalScience,
math,
naturalScience,
}) => {
// ...
}
`,
output: dedent`
let countPrisonSchoolGrade = ({
naturalScience,
biology,
math,
finalScore = biology + math + naturalScience,
}) => {
// ...
}
`,
options: [
{
type: SortType['line-length'],
order: SortOrder.desc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'finalScore',
right: 'math',
},
},
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'math',
right: 'naturalScience',
},
},
],
},
],
})
})
})

describe(`${RULE_NAME}: misc`, () => {
Expand Down
1 change: 1 addition & 0 deletions typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum SortOrder {
}

export interface SortingNode {
dependencies?: string[]
node: TSESTree.Node
name: string
size: number
Expand Down
6 changes: 6 additions & 0 deletions utils/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export let compare = (
type: SortType
},
): boolean => {
if (b.dependencies?.includes(a.name)) {
return false
} else if (a.dependencies?.includes(b.name)) {
return true
}

let orderCoefficient = options.order === 'asc' ? 1 : -1
let sortingFunction: (a: SortingNode, b: SortingNode) => number

Expand Down

0 comments on commit 3e987ae

Please sign in to comment.