Skip to content

Commit

Permalink
fix: handle more complex expressions in sort-objects
Browse files Browse the repository at this point in the history
  • Loading branch information
stovmascript authored Aug 15, 2023
1 parent ec2e2f5 commit a7d966c
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 12 deletions.
63 changes: 51 additions & 12 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

if (prop.value.type === AST_NODE_TYPES.AssignmentPattern) {
let addDependencies = (
value: TSESTree.AssignmentPattern | TSESTree.BinaryExpression,
value: TSESTree.AssignmentPattern,
initialStart: boolean,
) => {
if (value.right.type === AST_NODE_TYPES.Identifier) {
Expand All @@ -172,22 +172,61 @@ export default createEslintRule<Options, MESSAGE_ID>({
dependencies.push(value.left.name)
}

let handleBinaryExpression = (
expression: TSESTree.BinaryExpression,
let handleComplexExpression = (
expression:
| TSESTree.ArrowFunctionExpression
| TSESTree.ConditionalExpression
| TSESTree.LogicalExpression
| TSESTree.BinaryExpression
| TSESTree.CallExpression,
) => {
if (expression.right.type === AST_NODE_TYPES.Identifier) {
dependencies.push(expression.right.name)
}
let nodes = []

switch (expression.type) {
case AST_NODE_TYPES.ArrowFunctionExpression:
nodes.push(expression.body)
break

case AST_NODE_TYPES.ConditionalExpression:
nodes.push(expression.consequent, expression.alternate)
break

case AST_NODE_TYPES.LogicalExpression:
case AST_NODE_TYPES.BinaryExpression:
nodes.push(expression.left, expression.right)
break

if (
expression.left.type === AST_NODE_TYPES.BinaryExpression
) {
addDependencies(expression.left, false)
case AST_NODE_TYPES.CallExpression:
nodes.push(...expression.arguments)
break

default:
}

nodes.forEach(nestedNode => {
if (nestedNode.type === AST_NODE_TYPES.Identifier) {
dependencies.push(nestedNode.name)
}

if (
nestedNode.type === AST_NODE_TYPES.BinaryExpression ||
nestedNode.type === AST_NODE_TYPES.ConditionalExpression
) {
handleComplexExpression(nestedNode)
}
})
}

if (value.right.type === AST_NODE_TYPES.BinaryExpression) {
handleBinaryExpression(value.right)
switch (value.right.type) {
case AST_NODE_TYPES.ArrowFunctionExpression:
case AST_NODE_TYPES.ConditionalExpression:
case AST_NODE_TYPES.LogicalExpression:
case AST_NODE_TYPES.BinaryExpression:
case AST_NODE_TYPES.CallExpression:
handleComplexExpression(value.right)
break

default:
}
}

Expand Down
185 changes: 185 additions & 0 deletions test/sort-objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,191 @@ describe(RULE_NAME, () => {
},
],
},
{
code: dedent`
let countPrisonSchoolGrade = ({
biology,
finalScore = () => biology + math,
math,
naturalScience,
}) => {
// ...
}
`,
output: dedent`
let countPrisonSchoolGrade = ({
biology,
math,
finalScore = () => biology + math,
naturalScience,
}) => {
// ...
}
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'finalScore',
right: 'math',
},
},
],
},
{
code: dedent`
let countPrisonSchoolGrade = ({
biology,
finalScore = 1 === 1 ? 1 === 1 ? math : biology : biology,
math,
naturalScience,
}) => {
// ...
}
`,
output: dedent`
let countPrisonSchoolGrade = ({
biology,
math,
finalScore = 1 === 1 ? 1 === 1 ? math : biology : biology,
naturalScience,
}) => {
// ...
}
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'finalScore',
right: 'math',
},
},
],
},
{
code: dedent`
let countPrisonSchoolGrade = ({
biology,
finalScore = ['a', 'b', 'c'].includes(naturalScience, math, biology),
math,
naturalScience,
}) => {
// ...
}
`,
output: dedent`
let countPrisonSchoolGrade = ({
biology,
math,
naturalScience,
finalScore = ['a', 'b', 'c'].includes(naturalScience, math, biology),
}) => {
// ...
}
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'finalScore',
right: 'math',
},
},
],
},
{
code: dedent`
let countPrisonSchoolGrade = ({
biology,
finalScore = math || biology,
math,
naturalScience,
}) => {
// ...
}
`,
output: dedent`
let countPrisonSchoolGrade = ({
biology,
math,
finalScore = math || biology,
naturalScience,
}) => {
// ...
}
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'finalScore',
right: 'math',
},
},
],
},
{
code: dedent`
let countPrisonSchoolGrade = ({
biology,
finalScore = 1 === 1 ? math : biology,
math,
naturalScience,
}) => {
// ...
}
`,
output: dedent`
let countPrisonSchoolGrade = ({
biology,
math,
finalScore = 1 === 1 ? math : biology,
naturalScience,
}) => {
// ...
}
`,
options: [
{
type: SortType.alphabetical,
order: SortOrder.asc,
},
],
errors: [
{
messageId: 'unexpectedObjectsOrder',
data: {
left: 'finalScore',
right: 'math',
},
},
],
},
],
})
})
Expand Down

0 comments on commit a7d966c

Please sign in to comment.