Bug 462980 - [eslint] Provide rule for mixed tabs and spaces
Summary: [eslint] Provide rule for mixed tabs and spaces
Status: RESOLVED FIXED
Alias: None
Product: Orion (Archived)
Classification: ECD
Component: JS Tools (show other bugs)
Version: 8.0   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 11.0   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-03-24 11:51 EDT by Michael Rennie CLA
Modified: 2015-11-30 12:34 EST (History)
4 users (show)

See Also:


Attachments
Proposed patch including a regression test (11.68 KB, patch)
2015-11-23 14:20 EST, Olivier Thomann CLA
no flags Details | Diff
Proposed patch including a regression test (11.99 KB, patch)
2015-11-23 14:45 EST, Olivier Thomann CLA
no flags Details | Diff
Proposed patch including a regression test (11.39 KB, patch)
2015-11-23 15:39 EST, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Rennie CLA 2015-03-24 11:51:18 EDT
It can be pretty annoying to load up files and have the spacing whitespace chars all mixed up. We should provide the following two rules from eslint:

http://eslint.org/docs/rules/no-irregular-whitespace.html

http://eslint.org/docs/rules/no-mixed-spaces-and-tabs.html
Comment 1 Mark Macdonald CLA 2015-03-24 13:43:27 EDT
IMO 'smart-tabs' is the best setting for the no-mixed-spaces-and-tabs rule. ESLint chose a more aggressive setting by default, not sure why.

[1] http://www.emacswiki.org/emacs/SmartTabs
Comment 2 Olivier Thomann CLA 2015-11-19 12:47:34 EST
I will use this bug report to focus on the rule for no-mixed-spaces-and-tabs. I opened bug report 482616 to take care of the second case for no-irregular-whitespaces.
Comment 3 Olivier Thomann CLA 2015-11-19 12:59:52 EST
My biggest issue with this rule is that it has an option called smart-tabs.
Smart-tabs basically let you have spaces after tabs in order to align field names or other things. Check this link for further details:
http://www.emacswiki.org/emacs/SmartTabs

I think smart-tabs is what all developers want. So we have two ways to handle it:
1) consider implicitly smart-tabs to be true
2) add an extra preference in the preference page to handle smart-tabs with a "on/off" option.
I would add the no-mixed-spaces-and-tabs option in the Settings>Javascript validation page under the code style section.
I would go with (1).

Any comment on that?

If we provide such an option, it should be disabled by default as it requires to check each line of the whole source file.
Also it would be good to provide a way to fix existing files. Something like Control + I in the Eclipse Java editor.
Comment 4 Michael Rennie CLA 2015-11-19 13:41:38 EST
(In reply to Olivier Thomann from comment #3)
> My biggest issue with this rule is that it has an option called smart-tabs.
> Smart-tabs basically let you have spaces after tabs in order to align field
> names or other things. Check this link for further details:
> http://www.emacswiki.org/emacs/SmartTabs
> 
> I think smart-tabs is what all developers want. So we have two ways to
> handle it:
> 1) consider implicitly smart-tabs to be true
> 2) add an extra preference in the preference page to handle smart-tabs with
> a "on/off" option.
> I would add the no-mixed-spaces-and-tabs option in the Settings>Javascript
> validation page under the code style section.
> I would go with (1).
> 
> Any comment on that?
> 

For now, I think we have to go with (1), until we figure out a story for related preferences.

> If we provide such an option, it should be disabled by default as it
> requires to check each line of the whole source file.
> Also it would be good to provide a way to fix existing files. Something like
> Control + I in the Eclipse Java editor.

See bug 475852, we are currently working on the 'fix all' story.
Comment 5 Michael Rennie CLA 2015-11-19 17:51:10 EST
As part of the fix for bug 468188 I extracted out the default rule values from validator.js and put them in rules.json (this was so I could share them in the worker code without loading more scripts + dependents). So when you add the new rule, you will have to update the rules.json file (the same way you would have had to update validator.js).
Comment 6 Olivier Thomann CLA 2015-11-20 14:16:48 EST
I am working on making the filtering for mixed spaces and tabs within comments. Do we agree that these mixed spaces and tabs should not be reported when located inside comments?
I made the change for the rules.json file. It works fine.
Comment 7 Michael Rennie CLA 2015-11-20 16:06:33 EST
(In reply to Olivier Thomann from comment #6)
> I am working on making the filtering for mixed spaces and tabs within
> comments. Do we agree that these mixed spaces and tabs should not be
> reported when located inside comments?
> I made the change for the rules.json file. It works fine.

Yes, I agree comments should be ignored.

We should probably also ignore String / RegEx literals.
Comment 8 Olivier Thomann CLA 2015-11-23 10:40:25 EST
(In reply to Michael Rennie from comment #7)
> Yes, I agree comments should be ignored.
> We should probably also ignore String / RegEx literals.
I thought about that, but since my regex to match wrong lines starts with the line start (^) I don't see how I could match a line that contains a mixed tabs and spaces inside a string literal or a regex. Is this possible?
Comment 9 Curtis Windatt CLA 2015-11-23 10:46:49 EST
(In reply to Olivier Thomann from comment #8)
> (In reply to Michael Rennie from comment #7)
> > Yes, I agree comments should be ignored.
> > We should probably also ignore String / RegEx literals.
> I thought about that, but since my regex to match wrong lines starts with
> the line start (^) I don't see how I could match a line that contains a
> mixed tabs and spaces inside a string literal or a regex. Is this possible?

We should only care about mixed tab/space at the start of the line.  Trailing whitespace is a separate issue and I would assume any unusual whitespace inside a line is for some specific aligning.
Comment 10 Olivier Thomann CLA 2015-11-23 14:20:52 EST
Created attachment 258227 [details]
Proposed patch including a regression test

This patch doesn't implement the smart-tabs feature. It only checks that spaces and tabs cannot be mixed at the beginning of a line. I am working on implementing the smart-tabs feature. With it enabled, we should not complain when tabs are followed by spaces.
Comment 11 Olivier Thomann CLA 2015-11-23 14:45:09 EST
Created attachment 258228 [details]
Proposed patch including a regression test

This patch implements the smart-tabs feature. The only thing I don't like is that I don't detect "spaces" that are not required for indentations. Not sure if we should actually check for that for performance reasons. Michael, let me know if you have any comments on that patch. I tried to add comments where needed.
Comment 12 Olivier Thomann CLA 2015-11-23 15:39:43 EST
Created attachment 258230 [details]
Proposed patch including a regression test

Remove the sorting of comments are the collection of comments is already sorted.