Skip to content

Commit 07f7e51

Browse files
author
Ken Earley
committed
jsx-no-target-blank should fail on dynamic link
1 parent fb7411d commit 07f7e51

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/rules/jsx-no-target-blank.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ function hasExternalLink(element) {
2323
/^(?:\w+:|\/\/)/.test(attr.value.value));
2424
}
2525

26+
function hasDynamicLink(element) {
27+
return element.attributes.some(attr => attr.name &&
28+
attr.name.name === 'href' &&
29+
attr.value.type === 'JSXExpressionContainer');
30+
}
31+
32+
2633
function hasSecureRel(element) {
2734
return element.attributes.find(attr => {
2835
if (attr.type === 'JSXAttribute' && attr.name.name === 'rel') {
@@ -53,7 +60,7 @@ module.exports = {
5360

5461
if (
5562
isTargetBlank(node) &&
56-
hasExternalLink(node.parent) &&
63+
(hasExternalLink(node.parent) || hasDynamicLink(node.parent)) &&
5764
!hasSecureRel(node.parent)
5865
) {
5966
context.report(node, 'Using target="_blank" without rel="noopener noreferrer" ' +

tests/lib/rules/jsx-no-target-blank.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const parserOptions = {
2525
// ------------------------------------------------------------------------------
2626

2727
const ruleTester = new RuleTester({parserOptions});
28+
2829
ruleTester.run('jsx-no-target-blank', rule, {
2930
valid: [
3031
{code: '<a href="foobar"></a>'},
@@ -94,5 +95,11 @@ ruleTester.run('jsx-no-target-blank', rule, {
9495
message: 'Using target="_blank" without rel="noopener noreferrer" is a security risk:' +
9596
' see https://mathiasbynens.github.io/rel-noopener'
9697
}]
98+
}, {
99+
code: '<a target="_blank" href={ dynamicLink }></a>',
100+
errors: [{
101+
message: 'Using target="_blank" without rel="noopener noreferrer" is a security risk:' +
102+
' see https://mathiasbynens.github.io/rel-noopener'
103+
}]
97104
}]
98105
});

0 commit comments

Comments
 (0)