Skip to content

Commit e6d3850

Browse files
apepperljharb
authored andcommitted
[New] jsx-no-target-blank: Improve fixer with option allowReferrer
1 parent 5f49f51 commit e6d3850

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
77

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

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

1920
[#3174]: https://github.com/yannickcr/eslint-plugin-react/pull/3174
21+
[#3167]: https://github.com/yannickcr/eslint-plugin-react/pull/3167
2022
[#3163]: https://github.com/yannickcr/eslint-plugin-react/pull/3163
2123
[#3160]: https://github.com/yannickcr/eslint-plugin-react/pull/3160
2224
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133

lib/rules/jsx-no-target-blank.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ module.exports = {
175175
|| (enforceDynamicLinks === 'always' && hasDynamicLink(node, linkAttribute));
176176
if (hasDangerousLink && !hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttributeIndex)) {
177177
const messageId = allowReferrer ? 'noTargetBlankWithoutNoopener' : 'noTargetBlankWithoutNoreferrer';
178+
const relValue = allowReferrer ? 'noopener' : 'noreferrer';
178179
report(context, messages[messageId], messageId, {
179180
node,
180181
fix(fixer) {
@@ -188,11 +189,11 @@ module.exports = {
188189
}
189190

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

194195
if (!relAttribute.value) {
195-
return fixer.insertTextAfter(relAttribute, '="noreferrer"');
196+
return fixer.insertTextAfter(relAttribute, `="${relValue}"`);
196197
}
197198

198199
if (relAttribute.value.type === 'Literal') {

tests/lib/rules/jsx-no-target-blank.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,15 @@ ruleTester.run('jsx-no-target-blank', rule, {
247247
output: '<a target={"_blank"} href="//example.com/19" rel="noreferrer"></a>',
248248
errors: defaultErrors,
249249
},
250+
{
251+
code: '<a href="https://example.com/20" target="_blank" rel></a>',
252+
output: '<a href="https://example.com/20" target="_blank" rel="noopener"></a>',
253+
options: [{ allowReferrer: true }],
254+
errors: [{ messageId: 'noTargetBlankWithoutNoopener' }],
255+
},
250256
{
251257
code: '<a href="https://example.com/20" target="_blank"></a>',
252-
output: '<a href="https://example.com/20" target="_blank" rel="noreferrer"></a>',
258+
output: '<a href="https://example.com/20" target="_blank" rel="noopener"></a>',
253259
options: [{ allowReferrer: true }],
254260
errors: [{ messageId: 'noTargetBlankWithoutNoopener' }],
255261
},

0 commit comments

Comments
 (0)