Typescript: Organize imports should remove blank lines within imports

Created on 27 Mar 2018  ·  7Comments  ·  Source: microsoft/TypeScript

TypeScript Version: 2.9.0-dev.20190327


Search Terms:

  • Organize imports

Code

  1. For the js:
import { x } from 'x';


import { y } from 'y';

console.log(x, y)
  1. Run organize imports.

Expected behavior:
Blank lines within the import are removed

import { x } from 'x';
import { y } from 'y';

console.log(x, y)

Actual behavior:
Blanklines preserved but shifted to end of imports:

import { x } from 'x';
import { y } from 'y';



console.log(x, y)
Awaiting More Feedback Organize Imports Suggestion

Most helpful comment

I agree that the current behavior of moving blank lines to the end of the imports is not helpful.
But removing them in general is also not always desired. I like to group my imports using blank lines.
For example one block of imports could be related to DB stuff and another one to features from math libraries.

Maybe the removal of blank lines could be optional via a config flag?

All 7 comments

Just to makes dure, I'm New gere, it's approved for community to help on this onde? Evento without the label :confused:

Sorry, my auto complete feature fault, I'm new here, and we can help on this one? Even without the label?

Out of interest: How should this behave if there's something else between the imports? I usually structure my code in test suites in two blocks: 1. framework-related imports, 2. component related imports, e.g.:

import { assert, expect, should } from "chai";
// ... more framework-related imports
// enable the should interface with sinon
should();

// ^^^ --- I want the space here --- vvv

import * as nodeUrl from "url";
// ... more component-related imports

@AlCalzone I do this too, and in my regular files too, I think it's cool to stay :+1:

I agree that the current behavior of moving blank lines to the end of the imports is not helpful.
But removing them in general is also not always desired. I like to group my imports using blank lines.
For example one block of imports could be related to DB stuff and another one to features from math libraries.

Maybe the removal of blank lines could be optional via a config flag?

I also group my imports with space between them. (libraries/frameworks vs my own code)
Keeping the groups would be helpful, but moving the blank lines to the bottom is never useful. So I think a reasonable behavior is a flag to preserve space. If true, imports are pruned as usual, but sorting is only done within contiguous blocks and any comments / empty lines are kept as-is.

If preserveSpace is false, then it behaves like it does now, but removing empty lines rather than collecting them at the bottom.

+1 to have a flag to allow keeping spaces/groups.

Was this page helpful?
0 / 5 - 0 ratings