Skip to content

Commit fdacc74

Browse files
committed
Allow forbidding particular attribute type. Support createElement
1 parent 224d949 commit fdacc74

File tree

3 files changed

+176
-15
lines changed

3 files changed

+176
-15
lines changed

docs/rules/button-has-type.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
# Prevent usage of `button` elements without an explicit `type` attribute (react/button-has-type)
22

33
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.
4-
This rules enforces an explicit `type` attribute for all the `button` elements.
4+
This rules enforces an explicit `type` attribute for all the `button` elements and checks that its value is valid per spec (i.e., is one of `"button"`, `"submit"`, and `"reset"`).
55

66
## Rule Details
77

88
The following patterns are considered errors:
99

1010
```jsx
1111
var Hello = <button>Hello</button>
12+
var Hello = <button type="foo">Hello</button>
13+
14+
var Hello = React.createElement('button', {}, 'Hello')
15+
var Hello = React.createElement('button', {type: 'foo'}, 'Hello')
1216
```
1317

1418
The following patterns are **not** considered errors:
1519

1620
```jsx
1721
var Hello = <span>Hello</span>
22+
var Hello = <span type="foo">Hello</span>
1823
var Hello = <button type="button">Hello</button>
1924
var Hello = <button type="submit">Hello</button>
2025
var Hello = <button type="reset">Hello</button>
26+
27+
var Hello = React.createElement('span', {}, 'Hello')
28+
var Hello = React.createElement('span', {type: 'foo'}, 'Hello')
29+
var Hello = React.createElement('button', {type: 'button'}, 'Hello')
30+
var Hello = React.createElement('button', {type: 'submit'}, 'Hello')
31+
var Hello = React.createElement('button', {type: 'reset'}, 'Hello')
32+
```
33+
34+
## Rule Options
35+
36+
```js
37+
...
38+
"react/default-props-match-prop-types": [<enabled>, {
39+
"button": <boolean>,
40+
"submit": <boolean>,
41+
"reset": <boolean>
42+
}]
43+
...
44+
```
45+
46+
You can forbid particular type attribute values by passing `false` as corresponding option (by default all of them are `true`).
47+
48+
The following patterns are considered errors when using `"react/default-props-match-prop-types": ["error", {reset: false}]`:
49+
50+
```jsx
51+
var Hello = <button type="reset">Hello</button>
52+
53+
var Hello = React.createElement('button', {type: 'reset'}, 'Hello')
2154
```
2255

2356
## When Not To Use It

lib/rules/button-has-type.js

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@
44
*/
55
'use strict';
66

7-
const hasProp = require('jsx-ast-utils/hasProp');
7+
const getProp = require('jsx-ast-utils/getProp');
8+
const getLiteralPropValue = require('jsx-ast-utils/getLiteralPropValue');
9+
10+
// ------------------------------------------------------------------------------
11+
// Helpers
12+
// ------------------------------------------------------------------------------
13+
14+
function isCreateElement(node) {
15+
return node.callee
16+
&& node.callee.type === 'MemberExpression'
17+
&& node.callee.property.name === 'createElement'
18+
&& node.arguments.length > 0;
19+
}
820

921
// ------------------------------------------------------------------------------
1022
// Rule Definition
@@ -17,24 +29,92 @@ module.exports = {
1729
category: 'Possible Errors',
1830
recommended: false
1931
},
20-
schema: []
32+
schema: [{
33+
type: 'object',
34+
properties: {
35+
button: {
36+
default: true,
37+
type: 'boolean'
38+
},
39+
submit: {
40+
default: true,
41+
type: 'boolean'
42+
},
43+
reset: {
44+
default: true,
45+
type: 'boolean'
46+
}
47+
},
48+
additionalProperties: false
49+
}]
2150
},
2251

2352
create: function(context) {
53+
const configuration = Object.assign({
54+
button: true,
55+
submit: true,
56+
reset: true
57+
}, context.options[0]);
58+
59+
function reportMissing(node) {
60+
context.report({
61+
node: node,
62+
message: 'Missing an explicit type attribute for button'
63+
});
64+
}
65+
66+
function checkValue(node, value) {
67+
if (!(value in configuration)) {
68+
context.report({
69+
node: node,
70+
message: `"${value}" is an invalid value for button type attribute`
71+
});
72+
} else if (!configuration[value]) {
73+
context.report({
74+
node: node,
75+
message: `"${value}" is a forbidden value for button type attribute`
76+
});
77+
}
78+
}
79+
2480
return {
2581
JSXElement: function(node) {
2682
if (node.openingElement.name.name !== 'button') {
2783
return;
2884
}
2985

30-
if (hasProp(node.openingElement.attributes, 'type')) {
86+
const typeProp = getProp(node.openingElement.attributes, 'type');
87+
88+
if (!typeProp) {
89+
reportMissing(node);
90+
return;
91+
}
92+
93+
checkValue(node, getLiteralPropValue(typeProp));
94+
},
95+
CallExpression: function(node) {
96+
if (!isCreateElement(node)) {
3197
return;
3298
}
3399

34-
context.report({
35-
node: node,
36-
message: 'Missing an explicit "type" attribute for button'
37-
});
100+
if (node.arguments[0].type !== 'Literal' || node.arguments[0].value !== 'button') {
101+
return;
102+
}
103+
104+
if (!node.arguments[1] || node.arguments[1].type !== 'ObjectExpression') {
105+
reportMissing(node);
106+
return;
107+
}
108+
109+
const props = node.arguments[1].properties;
110+
const typeProp = props.find(prop => prop.key && prop.key.name === 'type');
111+
112+
if (!typeProp || typeProp.value.type !== 'Literal') {
113+
reportMissing(node);
114+
return;
115+
}
116+
117+
checkValue(node, typeProp.value.value);
38118
}
39119
};
40120
}

tests/lib/rules/button-has-type.js

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,62 @@ const ruleTester = new RuleTester({parserOptions});
2828
ruleTester.run('button-has-type', rule, {
2929
valid: [
3030
{code: '<span/>'},
31+
{code: '<span type="foo"/>'},
3132
{code: '<button type="button"/>'},
3233
{code: '<button type="submit"/>'},
33-
{code: '<button type="reset"/>'}
34+
{code: '<button type="reset"/>'},
35+
{
36+
code: '<button type="button"/>',
37+
options: [{reset: false}]
38+
},
39+
{code: 'React.createElement("span")'},
40+
{code: 'React.createElement("span", {type: "foo"})'},
41+
{code: 'React.createElement("button", {type: "button"})'},
42+
{code: 'React.createElement("button", {type: "submit"})'},
43+
{code: 'React.createElement("button", {type: "reset"})'},
44+
{
45+
code: 'React.createElement("button", {type: "button"})',
46+
options: [{reset: false}]
47+
}
3448
],
35-
invalid: [{
36-
code: '<button/>',
37-
errors: [{
38-
message: 'Missing an explicit "type" attribute for button'
39-
}]
40-
}]
49+
invalid: [
50+
{
51+
code: '<button/>',
52+
errors: [{
53+
message: 'Missing an explicit type attribute for button'
54+
}]
55+
},
56+
{
57+
code: '<button type="foo"/>',
58+
errors: [{
59+
message: '"foo" is an invalid value for button type attribute'
60+
}]
61+
},
62+
{
63+
code: '<button type="reset"/>',
64+
options: [{reset: false}],
65+
errors: [{
66+
message: '"reset" is a forbidden value for button type attribute'
67+
}]
68+
},
69+
{
70+
code: 'React.createElement("button")',
71+
errors: [{
72+
message: 'Missing an explicit type attribute for button'
73+
}]
74+
},
75+
{
76+
code: 'React.createElement("button", {type: "foo"})',
77+
errors: [{
78+
message: '"foo" is an invalid value for button type attribute'
79+
}]
80+
},
81+
{
82+
code: 'React.createElement("button", {type: "reset"})',
83+
options: [{reset: false}],
84+
errors: [{
85+
message: '"reset" is a forbidden value for button type attribute'
86+
}]
87+
}
88+
]
4189
});

0 commit comments

Comments
 (0)