digitalbazaar / eslint-config-digitalbazaar

BSD 3-Clause "New" or "Revised" License
2 stars 1 forks source link

Add sort imports #60

Closed aljones15 closed 2 years ago

aljones15 commented 2 years ago

new rule:

https://eslint.org/docs/rules/sort-imports

  1. Sorts by imported method name over module path
  2. I turned on ignoreCase: true this is because Capitalized Class names would then trump other imports.

Example 1:

// this shows that member name is less important than module path 
import {a} from 'z.js'
import {b} from 'y.hs'

Example 2:

// this shows that a goes before Z even if Z is capitalized
import a from 'b.js';
import {ZcapClient} from '@digitalbazaar/ezcap'; 

sort-imports sorts first by import type and then secondly by alphabetized member name. We're using this sort order:

  1. all - import * as jsonld from 'jsonld';
  2. multiple - import {a, b} from './foo.js';
  3. single - import a from 'z.js';
  4. {single} - import {bar} from 'foo.js';
  5. none - import 'bedrock-test';

There for our imports would look like this:

// all alphabetized by member name
import * as allOfIt from 'old-lib'
import * as oneBigImport from 'big-import';
// multiple with alphabetized member names
import {a, b, c} from 'multi-es6';
// single import
import {oneThing} from './src/local.js';
import zed from 'zed';
// none
import 'no-members';
dlongley commented 2 years ago

none should be last instead of first.

dlongley commented 2 years ago

We should use the rule (if we're not already) that allows groupings to be individually sorted as well.

aljones15 commented 2 years ago

We should use the rule (if we're not already) that allows groupings to be individually sorted as well.

do you mean this one? https://eslint.org/docs/rules/sort-imports#ignoremembersort

This means:

import {a,b,c} from 'b.js'
import {z, d, a} from 'z.js'

is ok. the --fix option will automatically alphabetize members if that is a concern.

or do you mean: https://eslint.org/docs/rules/sort-imports#allowseparatedgroups

dlongley commented 2 years ago

@aljones15,

do you mean this one? https://eslint.org/docs/rules/sort-imports#ignoremembersort

No, this one: https://eslint.org/docs/rules/sort-imports#allowseparatedgroups

davidlehn commented 2 years ago

Before merging this, we need to possibly address https://github.com/digitalbazaar/eslint-config-digitalbazaar/pull/59. I'm thinking of just reverting that change and this would need a rebase.