diff --git a/lib/rules/button-has-type.js b/lib/rules/button-has-type.js index 64de17bbed..724623c39b 100644 --- a/lib/rules/button-has-type.js +++ b/lib/rules/button-has-type.js @@ -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; } @@ -97,7 +101,7 @@ module.exports = { checkValue(node, getLiteralPropValue(typeProp)); }, CallExpression: function(node) { - if (!isCreateElement(node)) { + if (!isCreateElement(node, context)) { return; } diff --git a/tests/lib/rules/button-has-type.js b/tests/lib/rules/button-has-type.js index b4a4a85d26..4bd02a164d 100644 --- a/tests/lib/rules/button-has-type.js +++ b/tests/lib/rules/button-has-type.js @@ -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: [ @@ -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' + } + } } ] });