amatiasq / vsc-sort-imports

Sort ES6 imports automatically.
ISC License
60 stars 22 forks source link

Is there a way to get tslint-compatible alphabetical sorting? #12

Closed alextreppass closed 6 years ago

alextreppass commented 6 years ago

Hi, first off - thanks for fixing #5 - I'm trying it out now.

I have the following config in my tslint.json file:

"ordered-imports": [true, {
  "import-sources-order": "case-insensitive",
  "named-imports-order": "case-insensitive"
}],

(ordered-import rule docs here: https://palantir.github.io/tslint/rules/ordered-imports/)

No configuration in my package.json - it's a shared project and I can't commit IDE-specific settings.

I can't seem to get import-sort to sort files in a manner that tslint is happy with. Am I missing something simple?

In the meantime, I've found "tslint.autoFixOnSave": true from the vscode-tslint seems to do the job automatically.

cliffkoh commented 6 years ago

Reading that tslint rule, "sort-imports.default-sort-style": "module-scoped" seems like it would be compatible.

It would be great if you could provide an example of imports sorted by this scheme, that still fails the tslint rule...

alextreppass commented 6 years ago

Sure, sorry - should have included one in the OP.

ts-lint sorted (via tslint --fix):

import { Qux } from 'foo_bar/qux';
import { Qux } from 'foo_bar_baz/qux';

sort-imports sorted (produces ts lint errors):

import { Qux } from 'foo_bar_baz/qux';
import { Qux } from 'foo_bar/qux';

It doesn't seem to be handling the / path segments alphabetically, maybe it's doing the entire string?

cliffkoh commented 6 years ago

Did you change your vscode user settings to add in "sort-imports.default-sort-style": "module-scoped"?

If this is not taking effect, there might be an entry in package.json or .importsortrc somewhere...

cliffkoh commented 6 years ago

Nevermind, I was able to repro it. Taking a look

cliffkoh commented 6 years ago

So this happens because all the module-based sorts are sorting "naturally" (by "en" locale sorting rules).

"foo_bar".localeCompare("foo/bar", "en") === -1

which is why we see what we see above.

The TSLint rule is probably using unicode based sorting, in which case foo/bar comes before foo_bar.

I can make a fix in the upstream dependency, but we would most likely need an update of this extension for the updated upstream dependency to be picked up and packaged (and published).

alextreppass commented 6 years ago

Ah yep, that makes sense. Thanks for digging into it 👍

No rush here - vscode-tslint with autoFixOnSave works well enough in the meantime.

cliffkoh commented 6 years ago

@amatiasq Updated import-sort-style-module-scoped to 1.0.3 that sorts by unicode, that should fix the issue here. Will need to update the dependency and repackage/publish.

amatiasq commented 6 years ago

@alextreppass Good catch! TSLint support is a huge win.

@cliffkoh Thanks! That was a quick reaction. I'll publish another minor in a few hours. Maybe I can add you as a publisher so you too can release a version, what do you think?

cliffkoh commented 6 years ago

@amatiasq Sure. :)

amatiasq commented 6 years ago

A new version has been released with the fix, thanks for your patience.

@cliffkoh please contact me at amatiasq@gmail.com, I need your Microsoft account email to add you as a publisher.

gfpacheco commented 4 years ago

I'm sorry, but it's still not sorting alphabetically for me, I tried module-compact as well but it sorts my imports like this:

import React from 'react';
import styled from '@emotion/styled';

Instead of what tslint expects:

import styled from '@emotion/styled';
import React from 'react';