diff --git a/rules/sort-objects.ts b/rules/sort-objects.ts index deaa5a816..6aa45f4ee 100644 --- a/rules/sort-objects.ts +++ b/rules/sort-objects.ts @@ -158,7 +158,7 @@ export default createEslintRule({ 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) { @@ -172,22 +172,61 @@ export default createEslintRule({ 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: } } diff --git a/test/sort-objects.test.ts b/test/sort-objects.test.ts index fd4526647..2deadbe57 100644 --- a/test/sort-objects.test.ts +++ b/test/sort-objects.test.ts @@ -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', + }, + }, + ], + }, ], }) })