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

jsx-indent rule autofixer #830

Merged
merged 3 commits into from
Sep 28, 2016
Merged

Conversation

eelyafi
Copy link
Contributor

@eelyafi eelyafi commented Sep 14, 2016

Due to limitations of the RuleFixer class there is no way to safely fix these two cases:

  1. When a file uses spaces but options requires tab and vice-versa.
  2. If a parent node didn't pass the rule, autofixer won't fix folded child nodes. Second run will actually fix the next child node.

Even with these limitations the autofixer found to be very useful and was already used on Pinterest codebase.

@ljharb
Copy link
Member

ljharb commented Sep 14, 2016

Can you explain more about why you can't autofix to tabs?

@eelyafi
Copy link
Contributor Author

eelyafi commented Sep 14, 2016

Let's say we have "\t" and we want to replace "\t" with 4 spaces. In order to do so we have to run fixer.replace(). Unfortunately fixer.replace() won't let you increase number of characters.
Basically, if there are two spaces it won't replace them with more than two characters. On the other hand fixer won't let you run more than one command. Because of that there is no way to replace spaces with empty string first and then add needed amount of spacing.

@ljharb
Copy link
Member

ljharb commented Sep 14, 2016

@eelyafi does that mean it's impossible to have a fixer for indent in eslint core?

@eelyafi
Copy link
Contributor Author

eelyafi commented Sep 14, 2016

@ljharb, after additional debugging I found that replaceTextRange will let you replace text. The actual reason why it won't replace one type of spacing with another is in getNodeIndent function.
The regExp in this function will look for spaces or tabs depending on the options passed in. Thus gotten variable will be always zero if file uses spaces and tabs are passed as an option.

@eelyafi
Copy link
Contributor Author

eelyafi commented Sep 15, 2016

Ping :)

@ljharb
Copy link
Member

ljharb commented Sep 15, 2016

I'm very hesitant to merge an autofixer that doesn't fix all forms of the rule. If we can't support tabs, we shouldn't support spaces, and vice versa.

@eelyafi
Copy link
Contributor Author

eelyafi commented Sep 15, 2016

In reality many existing autofixers are far away from perfection. This autofixer will cover most of the cases and won't break code.

@@ -69,6 +69,30 @@ module.exports = {
}
}

var indentChar = indentType === 'space' ? ' ' : '\u0009';
Copy link
Member

Choose a reason for hiding this comment

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

Can this just be '\t' instead of the character code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

@@ -39,7 +39,7 @@ module.exports = {
category: 'Stylistic Issues',
recommended: false
},

fixable: 'whitespace',
Copy link
Member

Choose a reason for hiding this comment

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

can we add a comment here detailing what cases are not covered? Ideally we'd also add skipped/commented-out test cases for that future work.

@eelyafi
Copy link
Contributor Author

eelyafi commented Sep 16, 2016

Addressed the comments

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

LGTM pending a rebase

@eelyafi
Copy link
Contributor Author

eelyafi commented Sep 19, 2016

Rebased

@eelyafi
Copy link
Contributor Author

eelyafi commented Sep 26, 2016

@ljharb is it good to merge ?

@ljharb
Copy link
Member

ljharb commented Sep 26, 2016

@eelyafi I'm waiting for one of the other contributors to review it.

@lencioni
Copy link
Collaborator

It would be good to note the limitations of the autofixer in the documentation, and also open an issue to improve it. Otherwise LGTM.

@eelyafi
Copy link
Contributor Author

eelyafi commented Sep 26, 2016

@lencioni I updated docs and created an issue #865

@eelyafi
Copy link
Contributor Author

eelyafi commented Sep 28, 2016

@ljharb @lencioni ping

@ljharb ljharb merged commit cc39fb8 into jsx-eslint:master Sep 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants