Skip to content

Commit

Permalink
add check for input type button
Browse files Browse the repository at this point in the history
  • Loading branch information
ataker committed Nov 1, 2023
1 parent 5f1143c commit e7685ce
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/eslint-plugin/lib/rules/prefer-button-component.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const jsxAstUtils = require('jsx-ast-utils');

const { elementType } = jsxAstUtils;
const { elementType, getProp, getLiteralPropValue } = jsxAstUtils;

const MESSAGE =
'The <va-button> Web Component should be used to instead of the button HTML element.';
Expand All @@ -14,13 +14,18 @@ module.exports = {
create(context) {
return {
JSXElement(node) {
// Exit early if we aren't on a button
if (elementType(node.openingElement) !== 'button') return;
const anchorNode = node.openingElement;
const typeProp = getProp(anchorNode.attributes, 'type');

context.report({
node,
message: MESSAGE,
})
// Only display if we are on a button or input with type button
if (elementType(anchorNode) === 'button' ||
(elementType(anchorNode) === 'input' && getLiteralPropValue(typeProp) === 'button')){

context.report({
node,
message: MESSAGE,
})
}
},
};
},
Expand Down
26 changes: 26 additions & 0 deletions packages/eslint-plugin/tests/lib/rules/prefer-button-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,31 @@ ruleTester.run('prefer-button-component', rule, {
},
],
},
{
code: `
const button = () => (
<input type="button">Click me</input>
)
`,
errors: [
{
message:
'The <va-button> Web Component should be used to instead of the button HTML element.'
},
],
},
{
code: `
const button = () => (
<input type="button" value="Click me" />
)
`,
errors: [
{
message:
'The <va-button> Web Component should be used to instead of the button HTML element.'
},
],
},
],
});

0 comments on commit e7685ce

Please sign in to comment.