Skip to content

Commit b7f388b

Browse files
V2dhaljharb
authored andcommitted
[Fix] jsx-no-target-blank: False negative when rel attribute is assigned using ConditionalExpression
1 parent 337e41a commit b7f388b

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2121
* [`display-name`], component detection: fix HOF returning null as Components ([#3347][] @jxm-math)
2222
* [`forbid-prop-types`]: Ignore objects that are not of type React.PropTypes ([#3326][] @TildaDares)
2323
* [`display-name`], component detection: fix false positive for HOF returning only nulls and literals ([#3305][] @golopot)
24+
* [`jsx-no-target-blank`]: False negative when rel attribute is assigned using ConditionalExpression ([#3332][] @V2dha)
2425

2526
### Changed
2627
* [Refactor] [`jsx-indent-props`]: improved readability of the checkNodesIndent function ([#3315][] @caroline223)
@@ -39,6 +40,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
3940
[#3339]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3339
4041
[#3338]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3338
4142
[#3335]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3335
43+
[#3332]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3332
4244
[#3331]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3331
4345
[#3328]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3328
4446
[#3327]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3327

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ function getStringFromValue(value) {
7474
if (value.expression.type === 'TemplateLiteral') {
7575
return value.expression.quasis[0].value.cooked;
7676
}
77-
return value.expression && value.expression.value;
77+
const expr = value.expression;
78+
return expr && (
79+
expr.type === 'ConditionalExpression'
80+
? [expr.consequent.value, expr.alternate.value]
81+
: expr.value
82+
);
7883
}
7984
}
8085
return null;
@@ -88,12 +93,15 @@ function hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttribu
8893

8994
const relAttribute = node.attributes[relIndex];
9095
const value = getStringFromValue(relAttribute.value);
91-
const tags = value && typeof value === 'string' && value.toLowerCase().split(' ');
92-
const noreferrer = tags && tags.indexOf('noreferrer') >= 0;
93-
if (noreferrer) {
94-
return true;
95-
}
96-
return allowReferrer && tags && tags.indexOf('noopener') >= 0;
96+
return [].concat(value).every((item) => {
97+
const tags = typeof item === 'string' ? item.toLowerCase().split(' ') : false;
98+
const noreferrer = tags && tags.indexOf('noreferrer') >= 0;
99+
if (noreferrer) {
100+
return true;
101+
}
102+
const noopener = tags && tags.indexOf('noopener') >= 0;
103+
return allowReferrer && noopener;
104+
});
97105
}
98106

99107
const messages = {

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

+49-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const parserOptions = {
2828

2929
const ruleTester = new RuleTester({ parserOptions });
3030
const defaultErrors = [{ messageId: 'noTargetBlankWithoutNoreferrer' }];
31+
const allowReferrerErrors = [{ messageId: 'noTargetBlankWithoutNoopener' }];
3132

3233
ruleTester.run('jsx-no-target-blank', rule, {
3334
valid: parsers.all([
@@ -141,6 +142,19 @@ ruleTester.run('jsx-no-target-blank', rule, {
141142
{
142143
code: '<a href target="_blank"/>',
143144
},
145+
{
146+
code: '<a href={href} target={isExternal ? "_blank" : undefined} rel="noopener noreferrer" />',
147+
},
148+
{
149+
code: '<a href={href} target={isExternal ? undefined : "_blank"} rel={isExternal ? "noreferrer" : "noopener noreferrer"} />',
150+
},
151+
{
152+
code: '<a href={href} target={isExternal ? undefined : "_blank"} rel={isExternal ? "noreferrer noopener" : "noreferrer"} />',
153+
},
154+
{
155+
code: '<a href={href} target="_blank" rel={isExternal ? "noreferrer" : "noopener"} />',
156+
options: [{ allowReferrer: true }],
157+
},
144158
]),
145159
invalid: parsers.all([
146160
{
@@ -251,13 +265,13 @@ ruleTester.run('jsx-no-target-blank', rule, {
251265
code: '<a href="https://example.com/20" target="_blank" rel></a>',
252266
output: '<a href="https://example.com/20" target="_blank" rel="noopener"></a>',
253267
options: [{ allowReferrer: true }],
254-
errors: [{ messageId: 'noTargetBlankWithoutNoopener' }],
268+
errors: allowReferrerErrors,
255269
},
256270
{
257271
code: '<a href="https://example.com/20" target="_blank"></a>',
258272
output: '<a href="https://example.com/20" target="_blank" rel="noopener"></a>',
259273
options: [{ allowReferrer: true }],
260-
errors: [{ messageId: 'noTargetBlankWithoutNoopener' }],
274+
errors: allowReferrerErrors,
261275
},
262276
{
263277
code: '<a target="_blank" href={ dynamicLink }></a>',
@@ -352,5 +366,38 @@ ruleTester.run('jsx-no-target-blank', rule, {
352366
options: [{ forms: true, links: false }],
353367
errors: defaultErrors,
354368
},
369+
{
370+
code: '<a href={href} target="_blank" rel={isExternal ? "undefined" : "undefined"} />',
371+
errors: defaultErrors,
372+
},
373+
{
374+
code: '<a href={href} target="_blank" rel={isExternal ? "noopener" : undefined} />',
375+
errors: defaultErrors,
376+
},
377+
{
378+
code: '<a href={href} target="_blank" rel={isExternal ? "undefined" : "noopener"} />',
379+
errors: defaultErrors,
380+
},
381+
{
382+
code: '<a href={href} target={isExternal ? "_blank" : undefined} rel={isExternal ? "noopener noreferrer" : undefined} />',
383+
errors: defaultErrors,
384+
},
385+
{
386+
code: '<a href={href} target={isExternal ? "_blank" : undefined} rel={isExternal ? undefined : "noopener noreferrer"} />',
387+
errors: defaultErrors,
388+
},
389+
{
390+
code: '<a href={href} target="_blank" rel={isExternal ? 3 : "noopener noreferrer"} />',
391+
errors: defaultErrors,
392+
},
393+
{
394+
code: '<a href={href} target="_blank" rel={isExternal ? "noopener noreferrer" : "3"} />',
395+
errors: defaultErrors,
396+
},
397+
{
398+
code: '<a href={href} target="_blank" rel={isExternal ? "noopener" : "2"} />',
399+
options: [{ allowReferrer: true }],
400+
errors: allowReferrerErrors,
401+
},
355402
]),
356403
});

0 commit comments

Comments
 (0)