SoominHan / import-sorter

MIT License
87 stars 28 forks source link

underscore sort order in tslint 6.x changed #60

Closed pburkindine closed 4 years ago

pburkindine commented 4 years ago

The underscore now sorts to the bottom in tslint 6.x; different settings of ordered-imports:ordered-imports do not affect this

Right now we are getting by, by using TS Import Sorter to sort and group (such a great extension!), then the hotfix provided by tslint to fix the alphabetization of entries with underscores. Would be great if import-sorter could handle this!

daidodo commented 4 years ago

Thanks for using JS/TS Import Sorter!

My best guess is your problem has something to do with import-sources-order: https://palantir.github.io/tslint/rules/ordered-imports/

Possible values for "import-sources-order" are: "case-insensitive': Correct order is "Bar", "baz", "Foo". (This is the default.) "lowercase-first": Correct order is "baz", "Bar", "Foo". "lowercase-last": Correct order is "Bar", "Foo", "baz". "any": Allow any order.

The order for different options are:

  1. case-insensitive-legacy: _ < A-Z and a-z (not documented)
  2. case-insensitive: A-Z and a-z < _
  3. lowercase-first: a-z < _ < A-Z
  4. lowercase-last: A-Z < _ < a-z

The behaviour can be explained by TSLint import path normalisation and path comparison source code.

I believe case-insensitive-legacy is what TypeScript Import Sorter and JS/TS Import Sorter adopt.

Hope that helps!

pburkindine commented 4 years ago

@daidodo I appreciate the feedback, but I'm not sure' it's the best solution. case-insensitive-legacy is an undocumented option, and deprecated behavior. The extensions should not require developers to know the option exists or to tie their codebase to legacy rules. What about adding a setting to control which sort order is used?

pburkindine commented 4 years ago

Update: We did try case-insensitive-legacy as a stopgap; for whatever reason this setting is ignored by tslint and the new order (_ last) is still required:

    "ordered-imports": [
      true,
      {
        "grouped-imports": true,
        "groups": ["^[A-Za-z].*$", "^@.*$", "^\\."],
        "import-sources-order": "case-insensitive-legacy"
      }
    ],
daidodo commented 4 years ago

Hi @pburkindine, this issue has been resolved in JS/TS Import Sorter from v1.2.3.

Now you can customise sorting rules for import paths in the config:

{
  "sortRules": { "paths": ["Aa", "_"] }
}

Some useful examples:

More details can be found in the wiki.

Tell me if you have any questions! Thanks!

pburkindine commented 4 years ago

@daidodo I'm a little confused, is JS/TS Import Sorter not a different extension anymore? I thought this one (https://marketplace.visualstudio.com/items?itemName=mike-co.import-sorter) was distinct from https://marketplace.visualstudio.com/items?itemName=dozerg.tsimportsorter ?

daidodo commented 4 years ago

You’re right they are different extensions. (I created TS/JS Import Sorter because this extension no longer updates.) I was just trying to finish our discussion after I implemented the new feature. I’d appreciate it if you could try it out and give me feedbacks! Thanks!

pburkindine commented 4 years ago

@daidodo Thanks! we were able to switch our project over to your extension and configure the sorting rules to behave the way TSLint expects.