Skip to content

Commit

Permalink
[minor] jsx-no-target-blank: warn on target={'_blank'} expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
timkraut authored and ljharb committed Oct 7, 2019
1 parent 1436799 commit 03bdefc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ function isTargetBlank(attr) {
return attr.name &&
attr.name.name === 'target' &&
attr.value &&
attr.value.type === 'Literal' &&
attr.value.value.toLowerCase() === '_blank';
((
attr.value.type === 'Literal' &&
attr.value.value.toLowerCase() === '_blank'
) || (
attr.value.type === 'JSXExpressionContainer' &&
attr.value.expression &&
attr.value.expression.value &&
attr.value.expression.value.toLowerCase() === '_blank'
));
}

function hasExternalLink(element, linkAttribute) {
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,20 @@ ruleTester.run('jsx-no-target-blank', rule, {
{code: '<a target={targetValue} rel="noopener noreferrer"></a>'},
{code: '<a target={targetValue} href="relative/path"></a>'},
{code: '<a target={targetValue} href="/absolute/path"></a>'},
{code: '<a target={\'targetValue\'} href="/absolute/path"></a>'},
{code: '<a target={"targetValue"} href="/absolute/path"></a>'},
{
code: '<a target="_blank" href={ dynamicLink }></a>',
options: [{enforceDynamicLinks: 'never'}]
},
{
code: '<a target={"_blank"} href={ dynamicLink }></a>',
options: [{enforceDynamicLinks: 'never'}]
},
{
code: '<a target={\'_blank\'} href={ dynamicLink }></a>',
options: [{enforceDynamicLinks: 'never'}]
},
{
code: '<Link target="_blank" href={ dynamicLink }></Link>',
options: [{enforceDynamicLinks: 'never'}],
Expand Down Expand Up @@ -90,6 +100,12 @@ ruleTester.run('jsx-no-target-blank', rule, {
}, {
code: '<a target="_blank" href={ dynamicLink }></a>',
errors: defaultErrors
}, {
code: '<a target={\'_blank\'} href="//example.com"></a>',
errors: defaultErrors
}, {
code: '<a target={"_blank"} href="//example.com"></a>',
errors: defaultErrors
}, {
code: '<a target="_blank" href={ dynamicLink }></a>',
options: [{enforceDynamicLinks: 'always'}],
Expand Down

0 comments on commit 03bdefc

Please sign in to comment.