Skip to content

Commit

Permalink
jsx-no-target-blank Change config to support links and forms
Browse files Browse the repository at this point in the history
  • Loading branch information
jaaberg committed Dec 19, 2017
1 parent b77a762 commit dbe2f72
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/rules/jsx-no-target-blank.js
Expand Up @@ -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));
}
Expand All @@ -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
Expand All @@ -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" ' +
Expand Down

0 comments on commit dbe2f72

Please sign in to comment.