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

Sort-comp rule doesn't enforce getDerivedStateFromProps placement #1793

Closed
lynxtaa opened this issue May 14, 2018 · 9 comments
Closed

Sort-comp rule doesn't enforce getDerivedStateFromProps placement #1793

lynxtaa opened this issue May 14, 2018 · 9 comments

Comments

@lynxtaa
Copy link
Contributor

lynxtaa commented May 14, 2018

With "react/sort-comp": "warn" provided code shouldn't be considered valid:

static getDerivedStateFromProps(nextProps, prevState) {}

state = {}

Tested on version 7.8.2

@ljharb
Copy link
Member

ljharb commented May 14, 2018

What is your full eslint config?

@lynxtaa
Copy link
Contributor Author

lynxtaa commented May 14, 2018

@ljharb
Copy link
Member

ljharb commented May 14, 2018

@lynxtaa you should be specifying a version pragma in "settings" (see the read me), but I don't think that's it. Could you also share the entire component code in question?

@lynxtaa
Copy link
Contributor Author

lynxtaa commented May 14, 2018

Still the same issue with specified pragma.

Example component Test.jsx:

import React from 'react'

class Test extends React.Component {
	static getDerivedStateFromProps() {
		return null
	}

	constructor(props) {
		super(props)
		this.state = { test: 'test' }
	}

	render() {
		return this.state.test
	}
}

export default Test

React 16.3.2.

@ljharb
Copy link
Member

ljharb commented May 14, 2018

ok, what warning do you get with that component?

@lynxtaa
Copy link
Contributor Author

lynxtaa commented May 14, 2018

No warning. But i should get "react/sort-comp" warning according to this https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/sort-comp.md

getDerivedStateFromProps should be declared after constructor.

@ljharb
Copy link
Member

ljharb commented May 14, 2018

aha, this seems to be a bug in the sort-comp implementation then - since the lifecycle methods are typically instance methods, but getDerivedStateFromProps is a static method.

cc @joe-denea / #1771 which has tests for the "valid" case for this method, but not for the "invalid" case.

@dejoean
Copy link
Contributor

dejoean commented May 15, 2018

Sorry about that, I should definitely have tested that.

@ljharb
Copy link
Member

ljharb commented May 15, 2018

Fixed in #1795.

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

No branches or pull requests

3 participants