buzinas / tslint-eslint-rules

Improve your TSLint with the missing ESLint rules
MIT License
712 stars 105 forks source link

Sort-Imports fails when 'none' is first sort option #363

Open worldlee78 opened 5 years ago

worldlee78 commented 5 years ago

tslint and typescript versions

$ tslint --version
5.11.0
$ tsc --version
Version 3.2.1

tslint.json configuration

{
  extends: [
    'tslint-react',
    'tslint-eslint-rules',
    'tslint-config-prettier'
  ],
  defaultSeverity: 'error',
  rulesDirectory: 'tslint-microsoft-contrib',
  rules: {
    'sort-imports': [true, { 'member-syntax-sort-order': ['none', 'all', 'single', 'multiple', 'alias'], 'ignore-case': true }],
  }
};

Typescript code being linted:

import './index.css';
import * as React from 'react';
import App from './App';
import ReactDOM from 'react-dom';

ReactDOM.render(<App />, document.getElementById('root'));

Actual Behavior: ERROR: src/index.tsx[1, 1]: All imports of type "None" must occur before all imports of type "All"

Expected Behavior: Should not have any errors

worldlee78 commented 5 years ago

If you look at the code, inside the _processMemberSyntaxSortOrder method, the sortOptions does not add the 'none' option to the order array because the if statement of (memberSyntaxTypeMap['none']) evaluates to 0, which is false, and thus is skips adding the option. Changing this to explicitly check for undefined instead of using the "evaluates to false" shortcut should resolve this issue.

It was causing sort-imports to incorrectly state that none must occur before all imports of type all in the above member-syntax-sort-order: ['none', 'all', 'single', 'multiple', 'alias'] even though all none imports were above all imports.