renke / import-sort

Sort ES2015 (aka ES6) imports. Both JavaScript and TypeScript are supported.
ISC License
466 stars 69 forks source link

Comments are moved in files with CRLF line endings #83

Open pfaffle opened 5 years ago

pfaffle commented 5 years ago

My TypeScript source files have some eslint ignore lines in them next to imports at the top of the file; but when I run import-sort on them, it moves them after all of the imports so they no longer apply to the correct lines and cause my eslint rules to give me errors.

before:

import * as React from 'react'
// eslint-disable-next-line no-restricted-imports
import Flexbox from 'flexbox-react'
import { connect } from 'react-redux'
import {
  Redirect,
  Route,
  RouteComponentProps,
  Switch,
  withRouter
} from 'react-router'

after:

import Flexbox from 'flexbox-react'
import * as React from 'react'
import { connect } from 'react-redux'
import {
  Redirect,
  Route,
  RouteComponentProps,
  Switch,
  withRouter
} from 'react-router'

// eslint-disable-next-line no-restricted-imports

The files have CRLF line endings because I'm on Windows. If I save them with LF line endings and then run import-sort, it has the expected result:

// eslint-disable-next-line no-restricted-imports
import Flexbox from 'flexbox-react'
import * as React from 'react'
import { connect } from 'react-redux'
import {
  Redirect,
  Route,
  RouteComponentProps,
  Switch,
  withRouter
} from 'react-router'
amatiasq commented 5 years ago

We are having the same issue on the VS Code extension https://github.com/amatiasq/vsc-sort-imports/issues/57, changing the line endings to LF works fine.

Can it be related to the detect-newline dependency? https://github.com/renke/import-sort/blob/master/packages/import-sort/src/index.ts#L70

jquense commented 5 years ago

node already exposes os.EOL maybe we should use that instead?

amatiasq commented 5 years ago

@jquense I think there is no guarantee the system EOL is the file's EOL. On cross-OS teams there is usually a convention for all to use the same EOL style whatever system each one uses.