Skip to content

Commit

Permalink
Merge pull request #1851 from alexzherdev/1847-button-has-type-pragma
Browse files Browse the repository at this point in the history
Account for pragma in button-has-type
  • Loading branch information
ljharb committed Jun 26, 2018
2 parents dc59667 + 6f7cf87 commit f80e744
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/rules/button-has-type.js
Expand Up @@ -7,15 +7,19 @@
const getProp = require('jsx-ast-utils/getProp');
const getLiteralPropValue = require('jsx-ast-utils/getLiteralPropValue');
const docsUrl = require('../util/docsUrl');
const pragmaUtil = require('../util/pragma');

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------

function isCreateElement(node) {
function isCreateElement(node, context) {
const pragma = pragmaUtil.getFromContext(context);
return node.callee
&& node.callee.type === 'MemberExpression'
&& node.callee.property.name === 'createElement'
&& node.callee.object
&& node.callee.object.name === pragma
&& node.arguments.length > 0;
}

Expand Down Expand Up @@ -97,7 +101,7 @@ module.exports = {
checkValue(node, getLiteralPropValue(typeProp));
},
CallExpression: function(node) {
if (!isCreateElement(node)) {
if (!isCreateElement(node, context)) {
return;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/button-has-type.js
Expand Up @@ -43,6 +43,17 @@ ruleTester.run('button-has-type', rule, {
{
code: 'React.createElement("button", {type: "button"})',
options: [{reset: false}]
},
{
code: 'document.createElement("button")'
},
{
code: 'Foo.createElement("span")',
settings: {
react: {
pragma: 'Foo'
}
}
}
],
invalid: [
Expand Down Expand Up @@ -83,6 +94,17 @@ ruleTester.run('button-has-type', rule, {
errors: [{
message: '"reset" is a forbidden value for button type attribute'
}]
},
{
code: 'Foo.createElement("button")',
errors: [{
message: 'Missing an explicit type attribute for button'
}],
settings: {
react: {
pragma: 'Foo'
}
}
}
]
});

0 comments on commit f80e744

Please sign in to comment.