Skip to content

Commit 952a768

Browse files
ljharbgaz77a
andcommitted
[Fix] jsx-no-target-blank: avoid crash on attr-only href
Fixes #3066 Co-authored-by: Jordan Harband <[email protected]> Co-authored-by: Gary Butler <[email protected]>
1 parent 1a5a970 commit 952a768

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
### Fixed
99
* [`jsx-no-useless-fragments`]: Handle insignificant whitespace correctly when `allowExpressions` is `true` ([#3061][] @benj-dobs)
1010
* [`prop-types`], `propTypes`: handle implicit `children` prop in react's generic types ([#3064][] @vedadeepta)
11-
* [`display-name`]: fix arrow function returning result of function call with JSX arguments being interpreted as component ([#3065][], @danielfinke)
11+
* [`display-name`]: fix arrow function returning result of function call with JSX arguments being interpreted as component ([#3065][] @danielfinke)
12+
* [`jsx-no-target-blank`]: avoid crash on attr-only href ([#3066][] @ljharb @gaz77a)
1213

14+
[#3066]: https://github.com/yannickcr/eslint-plugin-react/issue/3066
1315
[#3065]: https://github.com/yannickcr/eslint-plugin-react/pull/3065
1416
[#3064]: https://github.com/yannickcr/eslint-plugin-react/pull/3064
1517
[#3061]: https://github.com/yannickcr/eslint-plugin-react/pull/3061

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function attributeValuePossiblyBlank(attribute) {
4949

5050
function hasExternalLink(node, linkAttribute, warnOnSpreadAttributes, spreadAttributeIndex) {
5151
const linkIndex = findLastIndex(node.attributes, (attr) => attr.name && attr.name.name === linkAttribute);
52-
const foundExternalLink = linkIndex !== -1 && ((attr) => attr.value.type === 'Literal' && /^(?:\w+:|\/\/)/.test(attr.value.value))(
52+
const foundExternalLink = linkIndex !== -1 && ((attr) => attr.value && attr.value.type === 'Literal' && /^(?:\w+:|\/\/)/.test(attr.value.value))(
5353
node.attributes[linkIndex]);
5454
return foundExternalLink || (warnOnSpreadAttributes && linkIndex < spreadAttributeIndex);
5555
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ ruleTester.run('jsx-no-target-blank', rule, {
135135
{
136136
code: '<form action="http://example.com" target="_blank" rel="noopener noreferrer"></form>',
137137
options: [{forms: true, links: false}]
138+
},
139+
{
140+
code: '<a href target="_blank"/>'
138141
}
139142
],
140143
invalid: [

0 commit comments

Comments
 (0)