vs-code-typescript-class-organizer icon indicating copy to clipboard operation
vs-code-typescript-class-organizer copied to clipboard

Major formatting issue with Typescript imports (reopen)

Open rhnatiuk opened this issue 10 months ago • 2 comments

It seems that the problem persists in the latest version (2.0.8?) (i.e., https://github.com/aljazsim/vs-code-typescript-code-organizer/issues/93 did not fix the problem).

Following imports:

import type { A } from "@c/api";
import { B } from "@c/api";

are still wrongly "formatted" as:

import { A, B } from "@c/api";

[type is removed for A] despite in tsco.json all import formatting is disabled:

	"imports": {
		"removeUnusedImports": false,
		"sortImportsBySource": false,
		"sortImportsByName": false,
		"groupImportsBySource": false,
		"separateImportGroups": false,
		"quote": "double"
	},

The expected fix would be to either format type {xxx} imports properly (preferred), or disable formatting for imports if config says so.

rhnatiuk avatar Feb 10 '25 10:02 rhnatiuk

I can confirm this behaviour I have done a quick test of 2.0.8 and with the following in the file

/*******************************************************************************
 * Copyright (c) 2025. My Corporation.                                                                    
 ******************************************************************************/
import { TokenEnum } from "./TokenEnum.ts";
import type { IType2 } from "./IType2.ts";
import type { IType1 } from "./IType1.ts";
import { convertToType1 } from "./IType2.ts";
import { createWorker } from "./helpers/WorkerCreator";

the result of organizing it is

import { IType1 } from "./IType1.ts";
import { IType2, convertToType1 } from "./IType2.ts";
/*******************************************************************************
 * Copyright (c) 2025. My Corporation.                                                                    
 ******************************************************************************/
import { TokenEnum } from "./TokenEnum.ts";
import { createWorker } from "./helpers/WorkerCreator";

As you can see it still drops the type. It also seems to be moving the header comment but that may be a separate issue.

acousins72 avatar Feb 10 '25 15:02 acousins72

@aljazsim , any hope to see this problem fixed?

rhnatiuk avatar Feb 14 '25 08:02 rhnatiuk

@aljazsim ?

rhnatiuk avatar Feb 21 '25 12:02 rhnatiuk

I've fixed the issue with type only import, please update to version 2.0.9. With the file header what happens is the comment is considered to be the leading comment of the first import by AST, hence it moves down to keep with the first import. It's a separate issue from type only import. I'm not sure how to properly fix the issue with the comment though. Always treat the first comment of an import as the file header? But what if it's an actual comment and should belong with the first import? Or maybe just treat /** ... */ comments as file headers? Is there a standard how file headers should be formatted?

aljazsim avatar Feb 23 '25 10:02 aljazsim

Okay, it seems to work now (at least the imports don't miss type anymore). Thanks!

rhnatiuk avatar Feb 25 '25 10:02 rhnatiuk

Cool types are working for me now too, Thanks

As for the comment, I kinda assumed thats how they were being handled and I don't think that you will be able to find a way to detect it being a header , to many people/companies have their own standards on it. The only thing that I can see is having an option to treat the first comment as a header. I'm working on a project with a lot files that I'm organizing as I do code changes so let me know if you need any help testing.

For now it is not breaking the code and I can look out for it (or it will be caught in code review) but I'm hoping that you can fix it so I don't have to. :)

Thanks for all your work on this tool

acousins72 avatar Feb 25 '25 21:02 acousins72

Thanks Andy! I was thinking if there's a comment on the first element (could be an Import or a Class/Interface/Type), and it is a multiline comment separated by an empty line, that could be considered a file header and should be left on top

aljazsim avatar Feb 25 '25 23:02 aljazsim

That would be good for most cases but I'm dealing with a code base that was implemented by many developers that all thought they knew best and as such there is no consistency with any formatting let alone things like the header having an empty line after it. That being said implementing that rule will take care of a majority of my cases and the few it doesn't I should be able to deal with and be compliant from then on.

acousins72 avatar Feb 26 '25 15:02 acousins72

Awesome, I'll see what can be done!

aljazsim avatar Feb 26 '25 23:02 aljazsim

I've added support for handling file header docs, please update to version 2.0.10 and give it a try. Header should be at least 3 lines long and can be a comment like:

/*
 * This is a header.
 */

or a comment like:

//
// This is a header.
//

aljazsim avatar Mar 02 '25 12:03 aljazsim