import-sort icon indicating copy to clipboard operation
import-sort copied to clipboard

Comments are moved in files with CRLF line endings

Open pfaffle opened this issue 6 years ago • 3 comments

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'

pfaffle avatar Mar 19 '19 21:03 pfaffle

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

amatiasq avatar Sep 05 '19 10:09 amatiasq

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

jquense avatar Sep 05 '19 13:09 jquense

@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.

amatiasq avatar Sep 05 '19 13:09 amatiasq