Skip to content

Commit

Permalink
[Fix] jsx-no-target-blank: Improve fixer with option allowReferrer
Browse files Browse the repository at this point in the history
  • Loading branch information
apepper committed Jan 5, 2022
1 parent 151bb2b commit 180b468
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/rules/jsx-no-target-blank.js
Expand Up @@ -175,6 +175,7 @@ module.exports = {
|| (enforceDynamicLinks === 'always' && hasDynamicLink(node, linkAttribute));
if (hasDangerousLink && !hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttributeIndex)) {
const messageId = allowReferrer ? 'noTargetBlankWithoutNoopener' : 'noTargetBlankWithoutNoreferrer';
const relValue = allowReferrer ? 'noopener' : 'noreferrer';
report(context, messages[messageId], messageId, {
node,
fix(fixer) {
Expand All @@ -188,11 +189,11 @@ module.exports = {
}

if (!relAttribute) {
return fixer.insertTextAfter(nodeWithAttrs.attributes.slice(-1)[0], ' rel="noreferrer"');
return fixer.insertTextAfter(nodeWithAttrs.attributes.slice(-1)[0], ` rel="${relValue}"`);
}

if (!relAttribute.value) {
return fixer.insertTextAfter(relAttribute, '="noreferrer"');
return fixer.insertTextAfter(relAttribute, `="${relValue}"`);
}

if (relAttribute.value.type === 'Literal') {
Expand Down
8 changes: 7 additions & 1 deletion tests/lib/rules/jsx-no-target-blank.js
Expand Up @@ -249,7 +249,13 @@ ruleTester.run('jsx-no-target-blank', rule, {
},
{
code: '<a href="https://example.com/20" target="_blank"></a>',
output: '<a href="https://example.com/20" target="_blank" rel="noreferrer"></a>',
output: '<a href="https://example.com/20" target="_blank" rel="noopener"></a>',
options: [{ allowReferrer: true }],
errors: [{ messageId: 'noTargetBlankWithoutNoopener' }],
},
{
code: '<a href="https://example.com/20" target="_blank" rel></a>',
output: '<a href="https://example.com/20" target="_blank" rel="noopener"></a>',
options: [{ allowReferrer: true }],
errors: [{ messageId: 'noTargetBlankWithoutNoopener' }],
},
Expand Down

0 comments on commit 180b468

Please sign in to comment.