Skip to content

Commit 4f2d1eb

Browse files
Nokel81ljharb
authored andcommitted
[New] jsx-no-target-blank: add fixer
1 parent 4406aba commit 4f2d1eb

File tree

3 files changed

+241
-110
lines changed

3 files changed

+241
-110
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## Unreleased
77

8+
### Added
9+
* [`jsx-no-target-blank`]: add fixer ([#2862][] @Nokel81)
10+
811
### Fixed
912
* [`jsx-no-constructed-context-values`]: avoid a crash with `as X` TS code ([#2894][] @ljharb)
1013
* [`jsx-no-constructed-context-values`]: avoid a crash with boolean shorthand ([#2895][] @ljharb)
@@ -15,6 +18,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1518
[#2895]: https://github.com/yannickcr/eslint-plugin-react/issues/2895
1619
[#2894]: https://github.com/yannickcr/eslint-plugin-react/issues/2894
1720
[#2893]: https://github.com/yannickcr/eslint-plugin-react/pull/2893
21+
[#2862]: https://github.com/yannickcr/eslint-plugin-react/pull/2862
1822

1923
## [7.22.0] - 2020.12.29
2024

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

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@ const linkComponentsUtil = require('../util/linkComponents');
1212
// Rule Definition
1313
// ------------------------------------------------------------------------------
1414

15-
function lastIndexMatching(arr, condition) {
16-
return arr.map(condition).lastIndexOf(true);
15+
function findLastIndex(arr, condition) {
16+
for (let i = arr.length - 1; i >= 0; i -= 1) {
17+
if (condition(arr[i])) {
18+
return i;
19+
}
20+
}
21+
22+
return -1;
1723
}
1824

1925
function attributeValuePossiblyBlank(attribute) {
20-
if (!attribute.value) {
26+
if (!attribute || !attribute.value) {
2127
return false;
2228
}
2329
const value = attribute.value;
24-
if (value.type === 'Literal' && typeof value.value === 'string' && value.value.toLowerCase() === '_blank') {
25-
return true;
30+
if (value.type === 'Literal') {
31+
return typeof value.value === 'string' && value.value.toLowerCase() === '_blank';
2632
}
2733
if (value.type === 'JSXExpressionContainer') {
2834
const expr = value.expression;
29-
if (expr.type === 'Literal' && typeof expr.value === 'string' && expr.value.toLowerCase() === '_blank') {
30-
return true;
35+
if (expr.type === 'Literal') {
36+
return typeof expr.value === 'string' && expr.value.toLowerCase() === '_blank';
3137
}
3238
if (expr.type === 'ConditionalExpression') {
3339
if (expr.alternate.type === 'Literal' && expr.alternate.value && expr.alternate.value.toLowerCase() === '_blank') {
@@ -41,21 +47,15 @@ function attributeValuePossiblyBlank(attribute) {
4147
return false;
4248
}
4349

44-
function hasTargetBlank(node, warnOnSpreadAttributes, spreadAttributeIndex) {
45-
const targetIndex = lastIndexMatching(node.attributes, (attr) => attr.name && attr.name.name === 'target');
46-
const foundTargetBlank = targetIndex !== -1 && attributeValuePossiblyBlank(node.attributes[targetIndex]);
47-
return foundTargetBlank || (warnOnSpreadAttributes && targetIndex < spreadAttributeIndex);
48-
}
49-
5050
function hasExternalLink(node, linkAttribute, warnOnSpreadAttributes, spreadAttributeIndex) {
51-
const linkIndex = lastIndexMatching(node.attributes, (attr) => attr.name && attr.name.name === linkAttribute);
51+
const linkIndex = findLastIndex(node.attributes, (attr) => attr.name && attr.name.name === linkAttribute);
5252
const foundExternalLink = linkIndex !== -1 && ((attr) => attr.value.type === 'Literal' && /^(?:\w+:|\/\/)/.test(attr.value.value))(
5353
node.attributes[linkIndex]);
5454
return foundExternalLink || (warnOnSpreadAttributes && linkIndex < spreadAttributeIndex);
5555
}
5656

5757
function hasDynamicLink(node, linkAttribute) {
58-
const dynamicLinkIndex = lastIndexMatching(node.attributes, (attr) => attr.name
58+
const dynamicLinkIndex = findLastIndex(node.attributes, (attr) => attr.name
5959
&& attr.name.name === linkAttribute
6060
&& attr.value
6161
&& attr.value.type === 'JSXExpressionContainer');
@@ -64,29 +64,36 @@ function hasDynamicLink(node, linkAttribute) {
6464
}
6565
}
6666

67-
function hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttributeIndex) {
68-
const relIndex = lastIndexMatching(node.attributes, (attr) => (attr.type === 'JSXAttribute' && attr.name.name === 'rel'));
67+
function getStringFromValue(value) {
68+
if (value) {
69+
if (value.type === 'Literal') {
70+
return value.value;
71+
}
72+
if (value.type === 'JSXExpressionContainer') {
73+
if (value.expression.type === 'TemplateLiteral') {
74+
return value.expression.quasis[0].value.cooked;
75+
}
76+
return value.expression && value.expression.value;
77+
}
78+
}
79+
return null;
80+
}
6981

82+
function hasSecureRel(node, allowReferrer, warnOnSpreadAttributes, spreadAttributeIndex) {
83+
const relIndex = findLastIndex(node.attributes, (attr) => (attr.type === 'JSXAttribute' && attr.name.name === 'rel'));
7084
if (relIndex === -1 || (warnOnSpreadAttributes && relIndex < spreadAttributeIndex)) {
7185
return false;
7286
}
7387

7488
const relAttribute = node.attributes[relIndex];
75-
const value = relAttribute.value
76-
&& ((
77-
relAttribute.value.type === 'Literal'
78-
&& relAttribute.value.value
79-
) || (
80-
relAttribute.value.type === 'JSXExpressionContainer'
81-
&& relAttribute.value.expression
82-
&& relAttribute.value.expression.value
83-
));
89+
const value = getStringFromValue(relAttribute.value);
8490
const tags = value && typeof value === 'string' && value.toLowerCase().split(' ');
8591
return tags && (allowReferrer ? tags.indexOf('noopener') >= 0 : tags.indexOf('noreferrer') >= 0);
8692
}
8793

8894
module.exports = {
8995
meta: {
96+
fixable: 'code',
9097
docs: {
9198
description: 'Forbid `target="_blank"` attribute without `rel="noreferrer"`',
9299
category: 'Best Practices',
@@ -123,9 +130,17 @@ module.exports = {
123130
return;
124131
}
125132

126-
const spreadAttributeIndex = lastIndexMatching(node.attributes, (attr) => (attr.type === 'JSXSpreadAttribute'));
127-
if (!hasTargetBlank(node, warnOnSpreadAttributes, spreadAttributeIndex)) {
128-
return;
133+
const targetIndex = findLastIndex(node.attributes, (attr) => attr.name && attr.name.name === 'target');
134+
const spreadAttributeIndex = findLastIndex(node.attributes, (attr) => (attr.type === 'JSXSpreadAttribute'));
135+
136+
if (!attributeValuePossiblyBlank(node.attributes[targetIndex])) {
137+
const hasSpread = spreadAttributeIndex >= 0;
138+
139+
if (warnOnSpreadAttributes && hasSpread) {
140+
// continue to check below
141+
} else if ((hasSpread && targetIndex < spreadAttributeIndex) || !hasSpread) {
142+
return;
143+
}
129144
}
130145

131146
const linkAttribute = components.get(node.name.name);
@@ -135,7 +150,48 @@ module.exports = {
135150
context.report({
136151
node,
137152
message: 'Using target="_blank" without rel="noreferrer" '
138-
+ 'is a security risk: see https://html.spec.whatwg.org/multipage/links.html#link-type-noopener'
153+
+ 'is a security risk: see https://html.spec.whatwg.org/multipage/links.html#link-type-noopener',
154+
fix(fixer) {
155+
// eslint 5 uses `node.attributes`; eslint 6+ uses `node.parent.attributes`
156+
const nodeWithAttrs = node.parent.attributes ? node.parent : node;
157+
// eslint 5 does not provide a `name` property on JSXSpreadElements
158+
const relAttribute = nodeWithAttrs.attributes.find((attr) => attr.name && attr.name.name === 'rel');
159+
160+
if (targetIndex < spreadAttributeIndex || (spreadAttributeIndex >= 0 && !relAttribute)) {
161+
return null;
162+
}
163+
164+
if (!relAttribute) {
165+
return fixer.insertTextAfter(nodeWithAttrs.attributes.slice(-1)[0], ' rel="noreferrer"');
166+
}
167+
168+
if (!relAttribute.value) {
169+
return fixer.insertTextAfter(relAttribute, '="noreferrer"');
170+
}
171+
172+
if (relAttribute.value.type === 'Literal') {
173+
const parts = relAttribute.value.value
174+
.split('noreferrer')
175+
.filter(Boolean);
176+
return fixer.replaceText(relAttribute.value, `"${parts.concat('noreferrer').join(' ')}"`);
177+
}
178+
179+
if (relAttribute.value.type === 'JSXExpressionContainer') {
180+
if (relAttribute.value.expression.type === 'Literal') {
181+
if (typeof relAttribute.value.expression.value === 'string') {
182+
const parts = relAttribute.value.expression.value
183+
.split('noreferrer')
184+
.filter(Boolean);
185+
return fixer.replaceText(relAttribute.value.expression, `"${parts.concat('noreferrer').join(' ')}"`);
186+
}
187+
188+
// for undefined, boolean, number, symbol, bigint, and null
189+
return fixer.replaceText(relAttribute.value, '"noreferrer"');
190+
}
191+
}
192+
193+
return null;
194+
}
139195
});
140196
}
141197
}

0 commit comments

Comments
 (0)