Skip to content

Commit

Permalink
fix(sort-objects): fix function name pattern usage with variable assi…
Browse files Browse the repository at this point in the history
…gnment
  • Loading branch information
hugop95 authored Jan 3, 2025
1 parent 4045595 commit 8d15b98
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
29 changes: 23 additions & 6 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
return
}

let objectParent = getObjectParent({ node: nodeObject })

let objectParent = getObjectParent({
onlyFirstParent: true,
node: nodeObject,
})
let matchedContextOptions = getMatchingContextOptions({
nodeNames: nodeObject.properties
.map(property => getNodeName({ sourceCode, property }))
Expand Down Expand Up @@ -178,10 +180,14 @@ export default createEslintRule<Options, MESSAGE_ID>({
return
}

let objectParentForIgnorePattern = getObjectParent({
onlyFirstParent: false,
node: nodeObject,
})
if (
objectParent?.name &&
objectParentForIgnorePattern?.name &&
options.ignorePattern.some(pattern =>
matches(objectParent.name, pattern),
matches(objectParentForIgnorePattern.name, pattern),
)
) {
return
Expand Down Expand Up @@ -629,21 +635,26 @@ let getNodeName = ({
}

let getObjectParent = ({
onlyFirstParent,
node,
}: {
node: TSESTree.ObjectExpression | TSESTree.ObjectPattern
onlyFirstParent: boolean
}): {
type: 'VariableDeclarator' | 'CallExpression'
name: string
} | null => {
let variableParentName = getVariableParentName({ node })
let variableParentName = getVariableParentName({ onlyFirstParent, node })
if (variableParentName) {
return {
type: 'VariableDeclarator',
name: variableParentName,
}
}
let callParentName = getCallExpressionParentName({ node })
let callParentName = getCallExpressionParentName({
onlyFirstParent,
node,
})
if (callParentName) {
return {
type: 'CallExpression',
Expand All @@ -654,15 +665,18 @@ let getObjectParent = ({
}

let getVariableParentName = ({
onlyFirstParent,
node,
}: {
node: TSESTree.ObjectExpression | TSESTree.ObjectPattern
onlyFirstParent: boolean
}): string | null => {
let variableParent = getFirstNodeParentWithType({
allowedTypes: [
TSESTree.AST_NODE_TYPES.VariableDeclarator,
TSESTree.AST_NODE_TYPES.Property,
],
onlyFirstParent,
node,
})
if (!variableParent) {
Expand All @@ -682,12 +696,15 @@ let getVariableParentName = ({
}

let getCallExpressionParentName = ({
onlyFirstParent,
node,
}: {
node: TSESTree.ObjectExpression | TSESTree.ObjectPattern
onlyFirstParent: boolean
}): string | null => {
let callParent = getFirstNodeParentWithType({
allowedTypes: [TSESTree.AST_NODE_TYPES.CallExpression],
onlyFirstParent,
node,
})
if (!callParent) {
Expand Down
7 changes: 7 additions & 0 deletions rules/sort-objects/get-first-node-parent-with-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ type NodeOfType<Type> = { type: Type } & TSESTree.Node
export let getFirstNodeParentWithType = <
NodeType extends TSESTree.AST_NODE_TYPES,
>({
onlyFirstParent,
allowedTypes,
node,
}: {
allowedTypes: NodeType[]
onlyFirstParent: boolean
node: TSESTree.Node
}): NodeOfType<NodeType> | null => {
let { parent } = node
if (onlyFirstParent) {
return parent && (allowedTypes as string[]).includes(parent.type)
? (parent as NodeOfType<NodeType>)
: null
}
while (parent) {
if ((allowedTypes as string[]).includes(parent.type)) {
return parent as NodeOfType<NodeType>
Expand Down
30 changes: 30 additions & 0 deletions test/rules/sort-objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,24 @@ describe(ruleName, () => {
},
messageId: 'unexpectedObjectsGroupOrder',
},
{
data: {
rightGroup: 'g',
leftGroup: 'b',
right: 'g',
left: 'b',
},
messageId: 'unexpectedObjectsGroupOrder',
},
{
data: {
rightGroup: 'r',
leftGroup: 'g',
right: 'r',
left: 'g',
},
messageId: 'unexpectedObjectsGroupOrder',
},
],
options: [
{
Expand Down Expand Up @@ -2311,6 +2329,12 @@ describe(ruleName, () => {
g: string,
b: string
})
let a = someFunction(true, {
r: string,
g: string,
b: string
})
`,
code: dedent`
let obj = {
Expand All @@ -2324,6 +2348,12 @@ describe(ruleName, () => {
g: string,
r: string
})
let a = someFunction(true, {
b: string,
g: string,
r: string
})
`,
},
],
Expand Down

0 comments on commit 8d15b98

Please sign in to comment.