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

Make it possible to designate a part of file for skipping reordering

Open rhnatiuk opened this issue 10 months ago • 3 comments

It would be nice to be able to mark a part of a file so that it would not be reordered. E.g., using comments:

...
// @tsco-ignore start
const A = 10;
const C = 20;
const B = A + B;
// @tsco-ignore end
...

Because now that snippet would turn into:

const A = 10;
const B = A + B;
const C = 20;

rightfully resulting in an error.

rhnatiuk avatar Feb 10 '25 10:02 rhnatiuk

TypeScript declaration dependency order has been a pain for this project for a while. In the solution you're proposing, I'd have to extract the text between // @tsco-ignore start and // @tsco-ignore end, parse the file using AST and reorder everything and print it all out. My problem here is where do I put the code after that (the whole file could be completely rearranged). Also, between the start/end comments there could be pretty much anything: classes, methods, or a combination of any of the members (or even just half a part of a class declaration). How would you deal with such situations?

aljazsim avatar Feb 11 '25 04:02 aljazsim

Good [as in "difficult" :)] question... Typically, I see this bad behavior in the variables/constants defined on a file level (i.e., not in a class). This is not that bad in a class, although fields' initialization would have to move to the constructor in this case, at least partially.

What if there would be two options? E.g.,

// will move the block as is to the beginning (top) of the file (after imports)
// @tsco-ignore start top
...
// @tsco-ignore end
// can be also @tsco-ignore-top start/end pair

and

// will move the block as is to the end (bottom) of the file (after imports)
// @tsco-ignore start bottom
...
// @tsco-ignore end
// can be also @tsco-ignore-bottom start/end pair

This is not ideal, but it would work for 99% of such cases (which are rare anyway).

rhnatiuk avatar Feb 11 '25 08:02 rhnatiuk

Ok, that would make sense. Let me give it a bit more thought!

aljazsim avatar Feb 11 '25 23:02 aljazsim

@aljazsim , you did something about organizing consts/lets/vars so that the thing does not interfere with the original scenario, it seems. All in all, this issue could be closed, perhaps.

rhnatiuk avatar Feb 26 '25 10:02 rhnatiuk