Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

New rule: import-ordering #771

Closed
adidahiya opened this issue Nov 5, 2015 · 14 comments
Closed

New rule: import-ordering #771

adidahiya opened this issue Nov 5, 2015 · 14 comments

Comments

@adidahiya
Copy link
Contributor

adidahiya commented Nov 5, 2015

Update July 1, 2016

This rule was released as "ordered-imports" in TSLint v3.13.0.

Update Jun 14, 2016

Import ordering

Option: check-groups enforces formatting imports into groups (separated by single newlines) in a certain order:

  1. third-party imports (standard NPM dependencies)
  2. NPM-scoped imports
  3. relative path imports

Option: alphabetize enforces alphabetic ordering of from clause

  • if check-groups is enabled, then order is enforced within each group
  • if check-groups is not enabled, then enforce overall ordering
@panuhorsmalahti
Copy link
Contributor

👍.

@adidahiya adidahiya changed the title New rule: require node_modules imports (those without relative paths) to be listed first New rule: import-ordering Dec 7, 2015
@aeisenberg
Copy link

Some more suggestions here:

  1. Alphabetic ordering of from clause
  2. Remove unused import statements (maybe this should be a different rule, though)
  3. Extra newline between relative path imports and absolute path imports

@adidahiya
Copy link
Contributor Author

good ideas @aeisenberg. unused imports should be covered by no-unused-variable, but the error message should be improved: #550

@rjokelai
Copy link

👍

@danvk
Copy link
Contributor

danvk commented Jun 20, 2016

Here's a custom rule I put together that does something like this:
https://gist.github.com/danvk/8ddaddaf291769b9bcfa8d4504742150

  • The A, B, C in import {A, B, C} from 'foo' must be alphabetized.
  • Import sources within a group must be alphabetized.
  • A blank line creates a new "group" of imports.

@danvk
Copy link
Contributor

danvk commented Jun 21, 2016

FYI @adidahiya I've sent out #1325 which implements something along these lines.

@markwongsk
Copy link
Contributor

Where would ordering of the ImportClause (eg default imports, wildcard imports, es6-imports) lie in this proposal? It doesn't seem to fit into alphabetize (they are all categories in of themselves), but having to manually group all of them manually seems a bit too much of a waste of real estate.

@adidahiya
Copy link
Contributor Author

they are all categories in of themselves

We might differ in opinion on this -- I personally don't think default imports / namespace imports / etc. should be treated any differently from each other, so therefore you shouldn't worry about grouping them. There's not much that's semantically related about the import style, so I'd rather go by alphabetization based on module name.

Maybe an additional option called group-by-import-style would address your use case.

@markwongsk
Copy link
Contributor

That sounds fair. I think we can do with the general alph within groups.

@EugeneSnihovsky
Copy link

Hi guys. Is there a way to disable "Import sources within a group must be alphabetized" option in this rule? It will be helpful for:

  1. Angular 2 code style: imports from angular core should be before application imports. And they not always alphabetized with other imports.
  2. In unit tests last import is tested instance. And it not always alphabetized with other imports.

Thanks for your work and any help :)

@danvk
Copy link
Contributor

danvk commented Apr 17, 2017

@EugeneSnihovsky If I want to separate imports in that way, I usually put them into multiple groups separated by blank lines, just like you would with PEP8.

import * as fs from 'fs';
import * as os from 'os';

import * as bar from './bar';
import * as foo from './foo';

@eriktim
Copy link
Contributor

eriktim commented Jul 21, 2017

I've created a custom rule to check for such a default groups layout, i.e.

import * as fs from 'fs'; // libraries

import {foo} from '../foo'; // parent directories

import {bar} from './bar'; // current directory
import {baz} from './baz';

Edit: see pull request.

@tonystaark
Copy link

tonystaark commented Aug 13, 2020

For the error that is triggered by not following separated by single newlines, please throw error message specific to that instead of Import sources within a group must be alphabetized. , which is very misleading that makes developers bewildered by hours trying to debug.

@JoshuaKGoldberg
Copy link
Contributor

@tonystaark if you think that's bewildering, imagine learning that TSLint is deprecated: #4534. We highly recommend you stop using this code. 😄

@palantir palantir locked and limited conversation to collaborators Aug 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants