Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle spread props in jsx-no-target-blank rule #679

Merged

Conversation

randycoulman
Copy link
Contributor

When running the jsx-no-target-blank rule against code like the following:

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

the following error would be raised:

TypeError: Cannot read property 'name' of undefined

The root cause is that the node representing the spread props does not have a name property.

Note that if the desired rel attribute appears before the spread props, the error does not occur.

@@ -27,7 +27,8 @@ ruleTester.run('jsx-no-target-blank', rule, {
valid: [
{code: '<a href="foobar"></a>', parserOptions: parserOptions},
{code: '<a randomTag></a>', parserOptions: parserOptions},
{code: '<a href="foobar" target="_blank" rel="noopener noreferrer"></a>', parserOptions: parserOptions}
{code: '<a href="foobar" target="_blank" rel="noopener noreferrer"></a>', parserOptions: parserOptions},
{code: '<a target="_blank" {...spreadProps} rel="noopener noreferrer"></a>', parserOptions: parserOptions}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the ordering of "rel" and the spread props seems to matter, should we also have a test for one where "rel" appears first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only matters because we break out of the for loop as soon as we find the rel attribute we're looking for. I could add another test case, but it would pass on the old code, so doesn't illustrate the problem. I didn't think it worth it at the time, but if you think it is, I'm happy to add it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's important to add, to prevent a regression - if there's already order-dependent bugs, there could easily be more added later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thought. Added in 3db97e4.

Since the bug being fixed here depends on the relative order of the
spread props and the `rel` attribute, add a second test case with the
opposite order to protect against regressions.
@yannickcr yannickcr merged commit c7a6c67 into jsx-eslint:master Jul 24, 2016
@yannickcr
Copy link
Member

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants