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

[New] jsx-no-target-blank: Improve fixer with option allowReferrer #3167

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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