-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add button eslint rule for va-button and va-table (#984)
* Add button eslint rule * add check for input type button * add table tests * typo fix
- Loading branch information
Showing
7 changed files
with
260 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/eslint-plugin/lib/rules/prefer-button-component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const jsxAstUtils = require('jsx-ast-utils'); | ||
|
||
const { elementType, getProp, getLiteralPropValue } = jsxAstUtils; | ||
|
||
const MESSAGE = | ||
'The <va-button> Web Component should be used instead of the button HTML element.'; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
fixable: 'code', | ||
}, | ||
|
||
create(context) { | ||
return { | ||
JSXElement(node) { | ||
const anchorNode = node.openingElement; | ||
const typeProp = getProp(anchorNode.attributes, 'type'); | ||
|
||
// 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, | ||
}) | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
30 changes: 30 additions & 0 deletions
30
packages/eslint-plugin/lib/rules/prefer-table-component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const jsxAstUtils = require('jsx-ast-utils'); | ||
|
||
const { elementType } = jsxAstUtils; | ||
|
||
const MESSAGE = | ||
'The <va-table> Web Component should be used to instead of the table HTML element.'; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
fixable: 'code', | ||
}, | ||
|
||
create(context) { | ||
return { | ||
JSXElement(node) { | ||
const anchorNode = node.openingElement; | ||
|
||
// Only display if we are on a table element | ||
if (elementType(anchorNode) === 'table'){ | ||
|
||
context.report({ | ||
node, | ||
message: MESSAGE, | ||
}) | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
packages/eslint-plugin/tests/lib/rules/prefer-button-component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
'use strict'; | ||
|
||
const rule = require('../../../lib/rules/prefer-button-component'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
// sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}; | ||
|
||
const ruleTester = new RuleTester({ parserOptions }); | ||
|
||
ruleTester.run('prefer-button-component', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
const button = () => (<va-button | ||
id="cancel" | ||
onClick={handlers.onCancel} | ||
text="Cancel" | ||
/>) | ||
`, | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
const button = () => ( | ||
<button | ||
className="foo" | ||
id="bar" | ||
onClick={() => window.print()} | ||
> | ||
Print this page | ||
</button> | ||
) | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'The <va-button> Web Component should be used instead of the button HTML element.' | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const button = () => (<button | ||
onClick={() => window.print()} | ||
> | ||
{variable} | ||
</button>) | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'The <va-button> Web Component should be used instead of the button HTML element.' | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const button = () => (<button | ||
onClick={() => window.print()} | ||
> | ||
Some plain text next to a {variable} | ||
</button>) | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'The <va-button> Web Component should be used instead of the button HTML element.' | ||
}, | ||
] | ||
}, | ||
{ | ||
code: ` | ||
const button = () => (<button | ||
onClick={() => window.print()} | ||
> | ||
<span>Button Text</span> | ||
</button>) | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'The <va-button> Web Component should be used instead of the button HTML element.' | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const button = () => ( | ||
<input type="button">Click me</input> | ||
) | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'The <va-button> Web Component should be used 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 instead of the button HTML element.' | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
73 changes: 73 additions & 0 deletions
73
packages/eslint-plugin/tests/lib/rules/prefer-table-component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const rule = require('../../../lib/rules/prefer-table-component'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
// sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}; | ||
|
||
const ruleTester = new RuleTester({ parserOptions }); | ||
|
||
ruleTester.run('prefer-table-component', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
const table = () => (<va-table | ||
id="cancel" | ||
onClick={handlers.onCancel} | ||
text="Cancel" | ||
/>) | ||
`, | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
const table = () => ( | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Date</th> | ||
<th>Name</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>Nov 9</td> | ||
<td>Kelly</td> | ||
</tr> | ||
<tr> | ||
<td>Dec 19</td> | ||
<td>Franklin</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
) | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'The <va-table> Web Component should be used to instead of the table HTML element.' | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const table = () => ( | ||
<table></table> | ||
) | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'The <va-table> Web Component should be used to instead of the table HTML element.' | ||
}, | ||
], | ||
} | ||
], | ||
}); |