Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find target={'_blank'} expressions #2451

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/rules/jsx-no-target-blank.js
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
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