Skip to content

autofix for 'sort-styles' rule #222

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

Merged

Conversation

tanhauhau
Copy link
Contributor

  • added autofix for 'sort-styles' rule.

@coveralls
Copy link

coveralls commented Mar 13, 2019

Coverage Status

Coverage increased (+2.4%) to 77.509% when pulling f6986cb on tanhauhau:tanhauhau/autofix-sort-styles into 89c3a11 on Intellicode:master.

@maxis1718
Copy link

According to the ESlint docs, we need to specify the fixable property.

Important: Without the fixable property, ESLint does not apply fixes even if the rule implements fix functions. Omit the fixable property if the rule is not fixable.

@tanhauhau tanhauhau force-pushed the tanhauhau/autofix-sort-styles branch from 12bf72a to b5f116b Compare March 13, 2019 14:48
Copy link
Contributor

@draperunner draperunner left a comment

Choose a reason for hiding this comment

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

Great job! This is very helpful. I have a couple of questions, though.

I'm curious about how comments would be treated. For instance, consider the following code:

const styles = StyleSheet.create({
  container: {
    margin: 20,
    // Never EVER edit the next line!!!
    alignItems: 'center',
  }
})

If we end up flipping the margin and alignItems lines, but keeping the comment between them, the meaning of the comment would change, and that's not good. I suggest just ignoring cases like these, leaving it to the user to manually fix these cases.

Another thing is overlapping styles when mixing shorthand properties with their full-length ones. Example:

const styles = StyleSheet.create({
  container: {
    marginLeft: 20,
    margin: 40,
  }
})

If we sort the styles here, we would change the appearance of the container thing. I guess these cases should also be ignored. I guess some hard coding is needed to detect these cases?

node.expression.type === 'ArrayExpression'
return (
node &&
Boolean(
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is just a Prettier/ESLint formatting fix, but how about moving the Boolean call up a level, like so:

return Boolean(
  node &&
  node.type === 'JSXExpressionContainer' &&
  node.expression &&
  node.expression.type === 'ArrayExpression'
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i've reverted the formatting change, we probably should do this in another PR?

node.name &&
node.name.name &&
node.name.name.toLowerCase().includes('style')
node.name &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't these lines indented too far?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reverted formatting changes

@tanhauhau
Copy link
Contributor Author

tanhauhau commented Mar 14, 2019

I can add some test case for this..

@tanhauhau tanhauhau force-pushed the tanhauhau/autofix-sort-styles branch from b5f116b to 2006336 Compare March 14, 2019 18:13
@tanhauhau
Copy link
Contributor Author

tanhauhau commented Mar 15, 2019

@draperunner these test cases should suffice for maintaining the comments?

@tanhauhau
Copy link
Contributor Author

added test case for shorthands too

@draperunner
Copy link
Contributor

The test cases for shorthand properties look good. Great job!

Regarding comments, I think it's better to just not autofix a chunk if it contains comments. It's impossible to know what the comment means. Consider the case

const styles = StyleSheet.create({
  container: {
    d: 4,
    // Never EVER edit the next THREE lines!!!
    c: 3,
    a: 1,
    b: 2,
  }
})

If we just don't autofix this, but still report it as an error, the user will have to fix it manually. When we are not sure what the correct way to fix things is, it's better to ignore it than potentially break something.

Copy link
Contributor

@draperunner draperunner left a comment

Choose a reason for hiding this comment

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

Great work!

@Intellicode Intellicode merged commit 1e6a73d into Intellicode:master Mar 26, 2019
@Intellicode
Copy link
Owner

Very nice! Sorry, I have been away for a bit, but I'll try to get out a release soonish!

@lukebars
Copy link

Any updates on this?

@tanhauhau tanhauhau deleted the tanhauhau/autofix-sort-styles branch December 10, 2019 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants