Skip to content

Commit 6a60a06

Browse files
committed
Add autofixer
1 parent 09d0574 commit 6a60a06

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/rules/__tests__/prefer-action-list-item-onselect.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,34 @@ ruleTester.run('prefer-action-list-item-onselect', rule, {
1717
{code: `<ActionList.Item onSelect={() => console.log(1)} />`},
1818
{code: `<ActionList.Item onSelect={() => console.log(1)} onClick={() => console.log(1)} />`},
1919
{code: `<Other onClick={() => console.log(1)} />`},
20+
{code: `<Button onClick={onSelect} />`},
21+
{code: `<Button onSelect={onClick} />`},
2022
{code: `<ActionList.Item onClick={() => console.log(1)} onKeyDown={() => console.log(1)} />`},
2123
{code: `<ActionList.Item onClick={() => console.log(1)} onKeyUp={() => console.log(1)} />`},
24+
// For now, we are not handling spread attributes as this is much less common
25+
{code: `<ActionList.Item {...onClick} />`},
2226
],
2327
invalid: [
2428
{
2529
code: `<ActionList.Item onClick={() => console.log(1)} />`,
2630
errors: [{messageId: 'prefer-action-list-item-onselect'}],
31+
output: `<ActionList.Item onSelect={() => console.log(1)} />`,
2732
},
2833
{
2934
code: `<ActionList.Item aria-label="Edit item 1" onClick={() => console.log(1)} />`,
3035
errors: [{messageId: 'prefer-action-list-item-onselect'}],
36+
output: `<ActionList.Item aria-label="Edit item 1" onSelect={() => console.log(1)} />`,
37+
},
38+
{
39+
code: `<ActionList.Item aria-label="Edit item 1" onClick={onClick} />`,
40+
errors: [{messageId: 'prefer-action-list-item-onselect'}],
41+
output: `<ActionList.Item aria-label="Edit item 1" onSelect={onClick} />`,
42+
},
43+
// This is invalid, but we can fix it anyway
44+
{
45+
code: `<ActionList.Item aria-label="Edit item 1" onClick />`,
46+
errors: [{messageId: 'prefer-action-list-item-onselect'}],
47+
output: `<ActionList.Item aria-label="Edit item 1" onSelect />`,
3148
},
3249
],
3350
})

src/rules/prefer-action-list-item-onselect.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module.exports = {
2121
create(context) {
2222
return {
2323
JSXElement(node) {
24+
// Only check components that have the name `ActionList.Item`. We don't check if this comes from Primer
25+
// because the chance of conflict here is very low
2426
const isActionListItem =
2527
node.openingElement.name.type === 'JSXMemberExpression' &&
2628
node.openingElement.name.object.type === 'JSXIdentifier' &&
@@ -51,6 +53,16 @@ module.exports = {
5153
context.report({
5254
node: onClickAttribute,
5355
messageId: 'prefer-action-list-item-onselect',
56+
fix: fixer => {
57+
// Replace `onClick` with `onSelect`
58+
if (onClickAttribute.type === 'JSXAttribute') {
59+
return fixer.replaceTextRange(
60+
[onClickAttribute.name.range[0], onClickAttribute.name.range[1]],
61+
'onSelect',
62+
)
63+
}
64+
return null
65+
},
5466
})
5567
},
5668
}

0 commit comments

Comments
 (0)