Skip to content

Commit

Permalink
resolved alternate, added test cases and helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
V2dha committed Jul 18, 2022
1 parent d998b85 commit 6eba766
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
31 changes: 15 additions & 16 deletions lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ function hasDynamicLink(node, linkAttribute) {
}
}

function attributeValuePossiblyRel(value) {
if (
typeof value === 'string'
&& (
(value && value.toLowerCase() === 'noreferrer')
|| (value && value.toLowerCase() === 'noopener noreferrer')
|| (value && value.toLowerCase() === 'noreferrer noopener')
)
) {
return true;
}
}

function getStringFromValue(value) {
if (value) {
if (value.type === 'Literal') {
Expand All @@ -76,24 +89,10 @@ function getStringFromValue(value) {
}
const expr = value.expression;
if (expr.type === 'ConditionalExpression') {
if (
typeof expr.consequent.value === 'string'
&& (
(expr.consequent.value && expr.consequent.value.toLowerCase() === 'noreferrer')
|| (expr.consequent.value && expr.consequent.value.toLowerCase() === 'noopener noreferrer')
|| (expr.consequent.value && expr.consequent.value.toLowerCase() === 'noreferrer noopener')
)
) {
if (attributeValuePossiblyRel(expr.consequent.value)) {
return expr.consequent.value;
}
if (
typeof expr.alternate.value === 'string'
&& (
(expr.alternate.value && expr.alternate.value.toLowerCase() === 'noreferrer')
|| (expr.alternate.value && expr.alternate.value.toLowerCase() === 'noopener noreferrer')
|| (expr.consequent.value && expr.consequent.value.toLowerCase() === 'noreferrer noopener')
)
) {
if (attributeValuePossiblyRel(expr.alternate.value)) {
return expr.alternate.value;
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ ruleTester.run('jsx-no-target-blank', rule, {
{
code: '<a href={href} target="_blank" rel={isExternal ? "noopener" : "noopener noreferrer"} />',
},
{
code: '<a href={href} target="_blank" rel={isExternal ? 3 : "noopener noreferrer"} />',
},
{
code: '<a href={href} target="_blank" rel={isExternal ? "noopener noreferrer" : "3"} />',
},
]),
invalid: parsers.all([
{
Expand Down

0 comments on commit 6eba766

Please sign in to comment.