Skip to content

Commit f790779

Browse files
authored
fix(sort-objects): fix nested objects not impacted by styled components
1 parent 3b335a0 commit f790779

2 files changed

Lines changed: 68 additions & 20 deletions

File tree

rules/sort-objects.ts

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,14 @@ export default createEslintRule<Options, MESSAGE_ID>({
155155
return
156156
}
157157

158-
let isStyledCallExpression = (identifier: TSESTree.Expression): boolean =>
159-
identifier.type === 'Identifier' && identifier.name === 'styled'
160-
let isCssCallExpression = (identifier: TSESTree.Expression): boolean =>
161-
identifier.type === 'Identifier' && identifier.name === 'css'
162-
let isStyledComponents = (
163-
styledNode: TSESTree.Node | undefined,
164-
): boolean =>
165-
!!styledNode &&
166-
((styledNode.type === 'CallExpression' &&
167-
(isCssCallExpression(styledNode.callee) ||
168-
(styledNode.callee.type === 'MemberExpression' &&
169-
isStyledCallExpression(styledNode.callee.object)) ||
170-
(styledNode.callee.type === 'CallExpression' &&
171-
isStyledCallExpression(styledNode.callee.callee)))) ||
172-
(styledNode.type === 'JSXExpressionContainer' &&
173-
styledNode.parent.type === 'JSXAttribute' &&
174-
styledNode.parent.name.name === 'style'))
158+
let objectRoot =
159+
nodeObject.type === 'ObjectPattern' ? null : getRootObject(nodeObject)
175160
if (
161+
objectRoot &&
176162
!options.styledComponents &&
177-
(isStyledComponents(nodeObject.parent) ||
178-
(nodeObject.parent.type === 'ArrowFunctionExpression' &&
179-
isStyledComponents(nodeObject.parent.parent)))
163+
(isStyledComponents(objectRoot.parent) ||
164+
(objectRoot.parent.type === 'ArrowFunctionExpression' &&
165+
isStyledComponents(objectRoot.parent.parent)))
180166
) {
181167
return
182168
}
@@ -575,6 +561,19 @@ let getObjectParent = ({
575561
return null
576562
}
577563

564+
let getRootObject = (
565+
node: TSESTree.ObjectExpression,
566+
): TSESTree.ObjectExpression => {
567+
let objectRoot = node
568+
while (
569+
objectRoot.parent.type === 'Property' &&
570+
objectRoot.parent.parent.type === 'ObjectExpression'
571+
) {
572+
objectRoot = objectRoot.parent.parent
573+
}
574+
return objectRoot
575+
}
576+
578577
let getVariableParentName = ({
579578
onlyFirstParent,
580579
node,
@@ -624,3 +623,31 @@ let getCallExpressionParentName = ({
624623

625624
return callParent.callee.type === 'Identifier' ? callParent.callee.name : null
626625
}
626+
627+
let isStyledCallExpression = (identifier: TSESTree.Expression): boolean =>
628+
identifier.type === 'Identifier' && identifier.name === 'styled'
629+
630+
let isCssCallExpression = (identifier: TSESTree.Expression): boolean =>
631+
identifier.type === 'Identifier' && identifier.name === 'css'
632+
633+
let isStyledComponents = (styledNode: TSESTree.Node): boolean => {
634+
if (
635+
styledNode.type === 'JSXExpressionContainer' &&
636+
styledNode.parent.type === 'JSXAttribute' &&
637+
styledNode.parent.name.name === 'style'
638+
) {
639+
return true
640+
}
641+
642+
if (styledNode.type !== 'CallExpression') {
643+
return false
644+
}
645+
646+
return (
647+
isCssCallExpression(styledNode.callee) ||
648+
(styledNode.callee.type === 'MemberExpression' &&
649+
isStyledCallExpression(styledNode.callee.object)) ||
650+
(styledNode.callee.type === 'CallExpression' &&
651+
isStyledCallExpression(styledNode.callee.callee))
652+
)
653+
}

test/rules/sort-objects.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5507,6 +5507,27 @@ describe(ruleName, () => {
55075507
},
55085508
],
55095509
},
5510+
{
5511+
code: dedent`
5512+
const PropsBox = styled.div((props) => ({
5513+
nested1: {
5514+
nested2: {
5515+
[theme.breakpoints.down('mid2')]: {
5516+
right: '24px',
5517+
},
5518+
[theme.breakpoints.down('mid1')]: {
5519+
bottom: '-273px',
5520+
}
5521+
}
5522+
}
5523+
}))
5524+
`,
5525+
options: [
5526+
{
5527+
styledComponents: false,
5528+
},
5529+
],
5530+
},
55105531
],
55115532
invalid: [],
55125533
},

0 commit comments

Comments
 (0)