Skip to content

Commit

Permalink
[Fix] jsx-no-target-blank: avoid crash on attr-only href
Browse files Browse the repository at this point in the history
Fixes #3066

Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Gary Butler <gary.butler@myob.com>
  • Loading branch information
ljharb and gaz77a committed Sep 2, 2021
1 parent 1a5a970 commit 952a768
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -8,8 +8,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
* [`jsx-no-useless-fragments`]: Handle insignificant whitespace correctly when `allowExpressions` is `true` ([#3061][] @benj-dobs)
* [`prop-types`], `propTypes`: handle implicit `children` prop in react's generic types ([#3064][] @vedadeepta)
* [`display-name`]: fix arrow function returning result of function call with JSX arguments being interpreted as component ([#3065][], @danielfinke)
* [`display-name`]: fix arrow function returning result of function call with JSX arguments being interpreted as component ([#3065][] @danielfinke)
* [`jsx-no-target-blank`]: avoid crash on attr-only href ([#3066][] @ljharb @gaz77a)

[#3066]: https://github.com/yannickcr/eslint-plugin-react/issue/3066
[#3065]: https://github.com/yannickcr/eslint-plugin-react/pull/3065
[#3064]: https://github.com/yannickcr/eslint-plugin-react/pull/3064
[#3061]: https://github.com/yannickcr/eslint-plugin-react/pull/3061
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-target-blank.js
Expand Up @@ -49,7 +49,7 @@ function attributeValuePossiblyBlank(attribute) {

function hasExternalLink(node, linkAttribute, warnOnSpreadAttributes, spreadAttributeIndex) {
const linkIndex = findLastIndex(node.attributes, (attr) => attr.name && attr.name.name === linkAttribute);
const foundExternalLink = linkIndex !== -1 && ((attr) => attr.value.type === 'Literal' && /^(?:\w+:|\/\/)/.test(attr.value.value))(
const foundExternalLink = linkIndex !== -1 && ((attr) => attr.value && attr.value.type === 'Literal' && /^(?:\w+:|\/\/)/.test(attr.value.value))(
node.attributes[linkIndex]);
return foundExternalLink || (warnOnSpreadAttributes && linkIndex < spreadAttributeIndex);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/lib/rules/jsx-no-target-blank.js
Expand Up @@ -135,6 +135,9 @@ ruleTester.run('jsx-no-target-blank', rule, {
{
code: '<form action="http://example.com" target="_blank" rel="noopener noreferrer"></form>',
options: [{forms: true, links: false}]
},
{
code: '<a href target="_blank"/>'
}
],
invalid: [
Expand Down

0 comments on commit 952a768

Please sign in to comment.