question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In order to avoid unnecessary git changes, we sort our import statements using the default sort feature of Atom or VSCode. We do this by selecting all import lines and running the sort action.

This works okayish but breaks complelty when we break multiple imports into multiple lines, e.g:

import {
  Foo,
  Bar,
  Baz
} from 'foobarbaz';

Since this will (obviously) be sorted to:

  Bar,
  Baz
  Foo,
} from 'foobarbaz';
import {

Are there any plans to support sorting of import blocks using prettier?

The idea would be to detect blocks that consists of import lines like:

import Bar from 'foo';
import Foo from 'foo';
import foo from 'foo';
import bar from 'foo';
import { 
  Foo,
  Bar
} from 'foo';
import * from 'foo';
import { Bar } from 'foo';
import {
  foo,
  bar
} from 'foo';
import { Foo } from 'foo';
import type Foo from 'foo';
import type Bar from 'foo';
import { bar } from 'foo';
import { foo } from 'foo';

and turn it to something like:

import { bar } from 'foo';
import { Bar } from 'foo';
import { foo } from 'foo';
import { Foo } from 'foo';
import { 
  foo,
  bar
} from 'foo';
import {
  Foo,
  Bar
} from 'foo';
import * from 'foo';
import Bar from 'foo';
import bar from 'foo';
import foo from 'foo';
import Foo from 'foo';
import type Bar from 'foo';
import type Foo from 'foo';

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
lydellcommented, Feb 22, 2017

Unfortunately, importing modules can have side effects so changing the order of imports is not safe. So I guess this one has to be closed for the same reasons as #323 and #766. You could use the sort-imports ESLint rule, though.

2reactions
suchipicommented, Feb 14, 2018

FYI/for future readers, eslint-plugin-import’s import/order rule can enforce an import order based on module location (but not alphabetization). However, the rule is not fixable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sort-imports - ESLint - Pluggable JavaScript Linter
This rule checks all import declarations and verifies that all imports are first sorted by the used member syntax and then alphabetically by...
Read more >
lydell/eslint-plugin-simple-import-sort - GitHub
Easy autofixable import sorting. ✅️ Runs via eslint --fix – no new tooling; ✅️ Also sorts exports where possible; ✅️ Handles ...
Read more >
Sorting your imports with ESLint - DEV Community ‍ ‍
The "react" import should always come first; Package imports should come next, sorted by alphabetical order; The named imports should be sorted ......
Read more >
eslint-plugin-simple-import-sort - npm
Easy autofixable import sorting. ✅️ Runs via eslint --fix – no new tooling; ✅️ Also sorts exports where possible; ✅️ Handles ...
Read more >
How to Sort Imports in React Project - Level Up Coding
Sort imports with Eslint ... Eslint has many plugin for sort imports, like eslint-plugin-simple-import-sort, eslint-plugin-import, sort-imports. I prefer to use ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found