azat-io / eslint-plugin-perfectionist

🦄 ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
https://eslint-plugin-perfectionist.azat.io
MIT License
1.62k stars 28 forks source link

Feature: Add checking ignore patterns for JSX properties name in sort-object #96

Open hstemplewski opened 5 months ago

hstemplewski commented 5 months ago

What rule do you want to change?

JSX properties are missed when checking ignore rule pattern

Describe the problem

When we are using React as a framework and added perfectionist which sorts object keys, the pattern for ignore sorting does not check the JSX prop names when the object is passed directly to the component.

Code example

<Table
  //This one shouldn't be sorted
  columns={{
    firstCol: "COL 1",
    secondCol: "COL 2",
    thirdCol: "COL 3",
    fourhtCol: "COL 4"
  }}
/>

the rule is applied like:

"perfectionist/sort-objects": [
      "error",
      {
        type: "natural",
        order: "asc",
        "ignore-pattern": ["[Cc]olumn*"],
        groups: ["id", "unknown"],
        "custom-groups": {
          id: "id",
        },
      },
    ],

Additional comments

I think this one needs to be changes from

 let variableIdentifier =
        node.parent.type === 'VariableDeclarator' &&
        node.parent.id.type === 'Identifier'
          ? node.parent.id.name
          : null

to (or sth simmillar)

 let variableIdentifier =
        (node.parent.type === 'VariableDeclarator' || node.parent.type === 'JSXAttribute') &&
        node.parent.id.type === 'Identifier'
          ? node.parent.id.name
          : null

Validations

azat-io commented 5 months ago

Thanks, that's a really good point, I'll think about how this can be fixed.