Skip to content

Commit

Permalink
feat: add callback position option to sort-jsx-props rule
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jun 3, 2023
1 parent 416ffee commit 8c6189f
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/rules/sort-jsx-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ let Riko = () => (

- `boolean` (default: `false`) - only affects alphabetical and natural sorting. When `true` the rule ignores the case-sensitivity of the order.

### `callback`

- `enum` (default: `ignore`):
- `first` - enforce callback JSX props to be at the top of the list
- `ignore` - sort callback props in general order
- `last` - enforce callback JSX props to be at the end of the list

### `shorthand`

- `enum` (default: `ignore`):
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ let createConfigWithOptions = (options: {
'error',
{
shorthand: 'ignore',
callback: 'ignore',
},
],
[sortMapElementsName]: ['error'],
Expand Down
14 changes: 14 additions & 0 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Options = [
type: SortType
'ignore-case': boolean
shorthand: Position
callback: Position
}>,
]

Expand Down Expand Up @@ -66,6 +67,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
shorthand: {
enum: [Position.first, Position.last, Position.ignore],
},
callback: {
enum: [Position.first, Position.last, Position.ignore],
},
},
additionalProperties: false,
},
Expand All @@ -86,6 +90,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
let options = complete(context.options.at(0), {
type: SortType.alphabetical,
shorthand: Position.ignore,
callback: Position.ignore,
'ignore-case': false,
order: SortOrder.asc,
})
Expand All @@ -112,6 +117,15 @@ export default createEslintRule<Options, MESSAGE_ID>({
position = options.shorthand
}

if (
options.callback !== Position.ignore &&
attribute.name.type === AST_NODE_TYPES.JSXIdentifier &&
attribute.name.name.indexOf('on') === 0 &&
attribute.value !== null
) {
position = options.callback
}

let jsxNode = {
name:
attribute.name.type === AST_NODE_TYPES.JSXNamespacedName
Expand Down
Loading

0 comments on commit 8c6189f

Please sign in to comment.