Skip to content

Commit

Permalink
[Fix] jsx-no-target-blank: allow rel to be an expression
Browse files Browse the repository at this point in the history
Previously this would be an error, since the rel wasn't read:

    <a target={"_blank"} href={"//"} rel={"noopener noreferrer"}>

Now this works, so you can enable this rule together with
`jsx-curly-brace-presence`.

Fixes #2508.
  • Loading branch information
odinho authored and ljharb committed Jan 15, 2020
1 parent fc6e406 commit e69b113
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@ function hasDynamicLink(element, linkAttribute) {
function hasSecureRel(element, allowReferrer) {
return element.attributes.find((attr) => {
if (attr.type === 'JSXAttribute' && attr.name.name === 'rel') {
const tags = attr.value && attr.value.type === 'Literal' && attr.value.value.toLowerCase().split(' ');
return tags && (tags.indexOf('noopener') >= 0 && (allowReferrer || tags.indexOf('noreferrer') >= 0));
const value = attr.value &&
((
attr.value.type === 'Literal' &&
attr.value.value
) || (
attr.value.type === 'JSXExpressionContainer' &&
attr.value.expression &&
attr.value.expression.value
));
const tags = value && value.toLowerCase && value.toLowerCase().split(' ');
return tags && tags.indexOf('noopener') >= 0 && (allowReferrer || tags.indexOf('noreferrer') >= 0);
}
return false;
});
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,23 @@ ruleTester.run('jsx-no-target-blank', rule, {
{code: '<a randomTag></a>'},
{code: '<a target />'},
{code: '<a href="foobar" target="_blank" rel="noopener noreferrer"></a>'},
{code: '<a href="foobar" target="_blank" rel={"noopener noreferrer"}></a>'},
{code: '<a href={"foobar"} target={"_blank"} rel={"noopener noreferrer"}></a>'},
{code: '<a href={\'foobar\'} target={\'_blank\'} rel={\'noopener noreferrer\'}></a>'},
{code: '<a href={`foobar`} target={`_blank`} rel={`noopener noreferrer`}></a>'},
{code: '<a target="_blank" {...spreadProps} rel="noopener noreferrer"></a>'},
{code: '<a {...spreadProps} target="_blank" rel="noopener noreferrer" href="http://example.com">s</a>'},
{code: '<a target="_blank" rel="noopener noreferrer" {...spreadProps}></a>'},
{code: '<p target="_blank"></p>'},
{code: '<a href="foobar" target="_BLANK" rel="NOOPENER noreferrer"></a>'},
{code: '<a target="_blank" rel={relValue}></a>'},
{code: '<a target={targetValue} rel="noopener noreferrer"></a>'},
{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={null} href="//example.com"></a>'},
{
code: '<a target="_blank" href={ dynamicLink }></a>',
options: [{enforceDynamicLinks: 'never'}]
Expand Down Expand Up @@ -98,6 +104,12 @@ ruleTester.run('jsx-no-target-blank', rule, {
}, {
code: '<a target="_blank" href="//example.com" rel={null}></a>',
errors: defaultErrors
}, {
code: '<a target="_blank" href="//example.com" rel={"noopenernoreferrer"}></a>',
errors: defaultErrors
}, {
code: '<a target={"_blank"} href={"//example.com"} rel={"noopenernoreferrer"}></a>',
errors: defaultErrors
}, {
code: '<a target="_blank" href="//example.com" rel></a>',
errors: defaultErrors
Expand Down

0 comments on commit e69b113

Please sign in to comment.