Skip to content

Commit f80e744

Browse files
authored
Merge pull request jsx-eslint#1851 from alexzherdev/1847-button-has-type-pragma
Account for pragma in button-has-type
2 parents dc59667 + 6f7cf87 commit f80e744

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/rules/button-has-type.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
const getProp = require('jsx-ast-utils/getProp');
88
const getLiteralPropValue = require('jsx-ast-utils/getLiteralPropValue');
99
const docsUrl = require('../util/docsUrl');
10+
const pragmaUtil = require('../util/pragma');
1011

1112
// ------------------------------------------------------------------------------
1213
// Helpers
1314
// ------------------------------------------------------------------------------
1415

15-
function isCreateElement(node) {
16+
function isCreateElement(node, context) {
17+
const pragma = pragmaUtil.getFromContext(context);
1618
return node.callee
1719
&& node.callee.type === 'MemberExpression'
1820
&& node.callee.property.name === 'createElement'
21+
&& node.callee.object
22+
&& node.callee.object.name === pragma
1923
&& node.arguments.length > 0;
2024
}
2125

@@ -97,7 +101,7 @@ module.exports = {
97101
checkValue(node, getLiteralPropValue(typeProp));
98102
},
99103
CallExpression: function(node) {
100-
if (!isCreateElement(node)) {
104+
if (!isCreateElement(node, context)) {
101105
return;
102106
}
103107

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

+22
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ ruleTester.run('button-has-type', rule, {
4343
{
4444
code: 'React.createElement("button", {type: "button"})',
4545
options: [{reset: false}]
46+
},
47+
{
48+
code: 'document.createElement("button")'
49+
},
50+
{
51+
code: 'Foo.createElement("span")',
52+
settings: {
53+
react: {
54+
pragma: 'Foo'
55+
}
56+
}
4657
}
4758
],
4859
invalid: [
@@ -83,6 +94,17 @@ ruleTester.run('button-has-type', rule, {
8394
errors: [{
8495
message: '"reset" is a forbidden value for button type attribute'
8596
}]
97+
},
98+
{
99+
code: 'Foo.createElement("button")',
100+
errors: [{
101+
message: 'Missing an explicit type attribute for button'
102+
}],
103+
settings: {
104+
react: {
105+
pragma: 'Foo'
106+
}
107+
}
86108
}
87109
]
88110
});

0 commit comments

Comments
 (0)