Skip to content

Commit

Permalink
feat: add ability to disable sorting objects in jsx styles
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Sep 30, 2024
1 parent 9434334 commit 45f7661
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/content/rules/sort-objects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ In this example, the `partitionByNewLine` option will cause the rule to treat ea

<sub>default: `true`</sub>

Determines whether this rule should be applied to styled-components like libraries.
Determines whether this rule should be applied to styled-components like libraries or `style` JSX attribute.

- `true` — Apply the rule to styled-components.
- `false` — Disable the rule for styled-components.
Expand Down
15 changes: 9 additions & 6 deletions rules/sort-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,15 @@ export default createEslintRule<Options, MESSAGE_ID>({
styledNode: TSESTree.Node | undefined,
): boolean =>
styledNode !== undefined &&
styledNode.type === 'CallExpression' &&
(isCssCallExpression(styledNode.callee) ||
(styledNode.callee.type === 'MemberExpression' &&
isStyledCallExpression(styledNode.callee.object)) ||
(styledNode.callee.type === 'CallExpression' &&
isStyledCallExpression(styledNode.callee.callee)))
((styledNode.type === 'CallExpression' &&
(isCssCallExpression(styledNode.callee) ||
(styledNode.callee.type === 'MemberExpression' &&
isStyledCallExpression(styledNode.callee.object)) ||
(styledNode.callee.type === 'CallExpression' &&
isStyledCallExpression(styledNode.callee.callee)))) ||
(styledNode.type === 'JSXExpressionContainer' &&
styledNode.parent.type === 'JSXAttribute' &&
styledNode.parent.name.name === 'style'))
if (
!options.styledComponents &&
(isStyledComponents(node.parent) ||
Expand Down
39 changes: 39 additions & 0 deletions test/sort-objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3287,6 +3287,16 @@ describe(ruleName, () => {
})

describe(`${ruleName}: misc`, () => {
let ruleTesterJSX = new RuleTester({
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
})

ruleTester.run(
`${ruleName}: sets alphabetical asc sorting as default`,
rule,
Expand Down Expand Up @@ -3787,5 +3797,34 @@ describe(ruleName, () => {
],
})
})

ruleTesterJSX.run(
'allows to disable sorting object is style prop in jsx',
rule,
{
valid: [
{
code: dedent`
let Element = () => (
<div
style={{
display: 'block',
margin: 0,
padding: 20,
background: 'orange',
}}
/>
)
`,
options: [
{
styledComponents: false,
},
],
},
],
invalid: [],
},
)
})
})

0 comments on commit 45f7661

Please sign in to comment.