Skip to content

Add JSXExpressionContainer as the href value type for target=_blank #1737

Closed
@ksdmitrieva

Description

@ksdmitrieva

Right now the jsx-no-target-blank rule only fires if the href is a literal string. What about the cases when the value of the href comes from a JSX expression? For example:

<a target='_blank' href={this.props.url}></a>

This might be even more dangerous if the URL property is provided by the user or the third-party.

Would it make sense to add a function hasDynamicLink() like this one:
function hasDynamicLink(element) {
return element.attributes.some(attr => attr.name &&
attr.name.name === 'href' &&
attr.value.type === 'JSXExpressionContainer');
}

And then include it in the condition for the rule:

if (
isTargetBlank(node) &&
(hasExternalLink(node.parent) || hasDynamicLink(node.parent)) &&
!hasSecureRel(node.parent)
)
I tested it out locally. Will be happy to help with a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions