Skip to content

Commit e54118b

Browse files
edemaineljharb
authored andcommitted
[Fix] jsx-no-target-blank: Allow rel="noreferrer" when allowReferrer is true
1 parent 734dc53 commit e54118b

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1717
* [`no-typos`]: avoid a crash on bindingless `prop-types` import; add warning ([#2899][] @ljharb)
1818
* [`jsx-curly-brace-presence`]: ignore containers with comments ([#2900][] @golopot)
1919
* [`destructuring-assignment`]: fix a false positive for local prop named `context` in SFC ([#2929][] @SyMind)
20+
* [`jsx-no-target-blank`]: Allow rel="noreferrer" when `allowReferrer` is true ([#2925][] @edemaine)
2021

2122
### Changed
2223
* [Docs] [`jsx-no-constructed-context-values`][]: fix invalid example syntax ([#2910][] @kud)
2324
* [readme] Replace lists of rules with tables in readme ([#2908][] @motato1)
2425
* [Docs] added missing curly braces ([#2923][] @Muditxofficial)
2526

2627
[#2929]: https://github.com/yannickcr/eslint-plugin-react/pull/2929
28+
[#2925]: https://github.com/yannickcr/eslint-plugin-react/pull/2925
2729
[#2923]: https://github.com/yannickcr/eslint-plugin-react/pull/2923
2830
[#2910]: https://github.com/yannickcr/eslint-plugin-react/pull/2910
2931
[#2908]: https://github.com/yannickcr/eslint-plugin-react/pull/2908

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ function hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttribu
8888
const relAttribute = node.attributes[relIndex];
8989
const value = getStringFromValue(relAttribute.value);
9090
const tags = value && typeof value === 'string' && value.toLowerCase().split(' ');
91-
return tags && (allowReferrer ? tags.indexOf('noopener') >= 0 : tags.indexOf('noreferrer') >= 0);
91+
const noreferrer = tags && tags.indexOf('noreferrer') >= 0;
92+
if (noreferrer) {
93+
return true;
94+
}
95+
return allowReferrer && tags && tags.indexOf('noopener') >= 0;
9296
}
9397

9498
module.exports = {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ ruleTester.run('jsx-no-target-blank', rule, {
103103
code: '<a href="foobar" target="_blank" rel="noopener"></a>',
104104
options: [{allowReferrer: true}]
105105
},
106+
{
107+
code: '<a href="foobar" target="_blank" rel="noreferrer"></a>',
108+
options: [{allowReferrer: true}]
109+
},
106110
{
107111
code: '<a target={3} />'
108112
}
@@ -212,6 +216,12 @@ ruleTester.run('jsx-no-target-blank', rule, {
212216
output: '<a target={"_blank"} href="//example.com/19" rel="noreferrer"></a>',
213217
errors: defaultErrors
214218
},
219+
{
220+
code: '<a href="http://example.com/20" target="_blank"></a>',
221+
output: '<a href="http://example.com/20" target="_blank" rel="noreferrer"></a>',
222+
options: [{allowReferrer: true}],
223+
errors: defaultErrors
224+
},
215225
{
216226
code: '<a target="_blank" href={ dynamicLink }></a>',
217227
output: '<a target="_blank" href={ dynamicLink } rel="noreferrer"></a>',

0 commit comments

Comments
 (0)