From dbe2f72622a7e28daaae51ce31a8b558a4900144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Aaberg?= Date: Tue, 19 Dec 2017 08:32:09 +0100 Subject: [PATCH] jsx-no-target-blank Change config to support links and forms --- lib/rules/jsx-no-target-blank.js | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/rules/jsx-no-target-blank.js b/lib/rules/jsx-no-target-blank.js index 549607b66e..02ac12a87a 100644 --- a/lib/rules/jsx-no-target-blank.js +++ b/lib/rules/jsx-no-target-blank.js @@ -14,9 +14,12 @@ function isTargetBlank(attr) { attr.value.value.toLowerCase() === '_blank'; } -function hasExternalLink(element, preventInForms) { +function hasExternalLink(element, config) { return element.attributes.some(attr => attr.name && - (attr.name.name === 'href' || (preventInForms && attr.name.name === 'action')) && + ( + (config.links && attr.name.name === 'href') || + (config.forms && attr.name.name === 'action') + ) && attr.value.type === 'Literal' && /^(?:\w+:|\/\/)/.test(attr.value.value)); } @@ -41,8 +44,13 @@ module.exports = { schema: [{ type: 'object', properties: { - preventInForms: { - type: 'boolean' + links: { + type: 'boolean', + default: true + }, + forms: { + type: 'boolean', + default: false } }, additionalProperties: false @@ -52,18 +60,21 @@ module.exports = { create: function(context) { return { JSXAttribute: function(node) { - const preventInForms = context.options[0] ? context.options[0].preventInForms : false; + const config = Object.assign({ + links: true, + forms: false + }, context.options[0]); if ( - node.parent.name.name !== 'a' && - (preventInForms ? node.parent.name.name !== 'form' : true) + (config.links ? node.parent.name.name !== 'a' : true) && + (config.forms ? node.parent.name.name !== 'form' : true) ) { return; } if ( isTargetBlank(node) && - hasExternalLink(node.parent, preventInForms) && + hasExternalLink(node.parent, config) && !hasSecureRel(node.parent) ) { context.report(node, 'Using target="_blank" without rel="noopener noreferrer" ' +