-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Prevent usage of `button` elements without an explicit `type` attribute (react/jsx-no-typeless-button) | ||
|
||
The default value of `type` attribute for `button` HTML element is `"submit"` which is often not the desired behavior and may lead to unexpected page reloads. | ||
This rules enforces an explicit `type` attribute for all the `button` elements. | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered errors: | ||
|
||
```jsx | ||
var Hello = <button>Hello</button> | ||
``` | ||
|
||
The following patterns are **not** considered errors: | ||
|
||
```jsx | ||
var Hello = <span>Hello</span> | ||
var Hello = <button type="button">Hello</button> | ||
var Hello = <button type="submit">Hello</button> | ||
var Hello = <button type="reset">Hello</button> | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you use only `"submit"` buttons, you can disable this rule |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @fileoverview Forbid "button" element without an explicit "type" attribute | ||
* @author Filipp Riabchun | ||
*/ | ||
'use strict'; | ||
|
||
const hasProp = require('jsx-ast-utils/hasProp'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Forbid "button" element without an explicit "type" attribute', | ||
category: 'Possible Errors', | ||
recommended: false | ||
}, | ||
schema: [] | ||
}, | ||
|
||
create: function(context) { | ||
return { | ||
JSXElement: function(node) { | ||
if (node.openingElement.name.name !== 'button') { | ||
return; | ||
} | ||
|
||
if (hasProp(node.openingElement.attributes, 'type')) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: node, | ||
message: 'Missing an explicit "type" attribute for button' | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
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,41 @@ | ||
/** | ||
* @fileoverview Forbid "button" element without an explicit "type" attribute | ||
* @author Filipp Riabchun | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/jsx-no-typeless-button'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const parserOptions = { | ||
ecmaVersion: 8, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
jsx: true | ||
} | ||
}; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({parserOptions}); | ||
ruleTester.run('jsx-no-typeless-button', rule, { | ||
valid: [ | ||
{code: '<span/>'}, | ||
{code: '<button type="button"/>'}, | ||
{code: '<button type="submit"/>'}, | ||
{code: '<button type="reset"/>'} | ||
], | ||
invalid: [{ | ||
code: '<button/>', | ||
errors: [{ | ||
message: 'Missing an explicit "type" attribute for button' | ||
}] | ||
}] | ||
}); |