Skip to content

Commit

Permalink
[New] jsx-no-target-blank: Improve fixer with option allowReferrer
Browse files Browse the repository at this point in the history
  • Loading branch information
apepper authored and ljharb committed Jan 5, 2022
1 parent 5f49f51 commit e6d3850
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

### Added
* add [`hook-use-state`] rule to enforce symmetric useState hook variable names ([#2921][] @duncanbeevers)
* [`jsx-no-target-blank`]: Improve fixer with option `allowReferrer` ([#3167][] @apepper)

### Fixed
* [`prop-types`], `propTypes`: add support for exported type inference ([#3163][] @vedadeepta)
Expand All @@ -17,6 +18,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [Docs] HTTP => HTTPS ([#3133][] @Schweinepriester)

[#3174]: https://github.com/yannickcr/eslint-plugin-react/pull/3174
[#3167]: https://github.com/yannickcr/eslint-plugin-react/pull/3167
[#3163]: https://github.com/yannickcr/eslint-plugin-react/pull/3163
[#3160]: https://github.com/yannickcr/eslint-plugin-react/pull/3160
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133
Expand Down
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 @@ -247,9 +247,15 @@ ruleTester.run('jsx-no-target-blank', rule, {
output: '<a target={"_blank"} href="//example.com/19" rel="noreferrer"></a>',
errors: defaultErrors,
},
{
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' }],
},
{
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' }],
},
Expand Down

0 comments on commit e6d3850

Please sign in to comment.