ts-importer icon indicating copy to clipboard operation
ts-importer copied to clipboard

Insert import statement ordered by filename

Open codeandcats opened this issue 7 years ago • 4 comments

Currently ts-importer adds new import statements to the end of the import list.

It would be great if it instead inserted new import statements alphabetically amongst all the import statements.

e.g. Instead of

import { yada } from './a';
import { etc } from './c';
import { nested } from './c/a';
import { andSoOn } from './d';
import { blah } from './b'; // <-- Just added

It would do this:

import { yada } from './a';
import { blah } from './b'; // <-- Just added
import { etc } from './c';
import { nested } from './c/a';
import { andSoOn } from './d';

This way one wouldn't have to use the fixer in ts-lint to reorder after each added import.

codeandcats avatar Oct 17 '17 01:10 codeandcats

This is a bit harder to implement, because there may be empty lines or commented lines between the statements ...

pmneo avatar Oct 18 '17 08:10 pmneo

I was thinking, if you’re not already using it, the TypeScript compiler API might be useful. You could parse the code on demand, then navigate the AST to find the correct location. Would still be a bit of work but maybe easier than trying to implement your own parser.

codeandcats avatar Oct 18 '17 08:10 codeandcats

Nope because of performance issues

pmneo avatar Oct 18 '17 08:10 pmneo

Really? You wouldn't need to parse the entire project, just the one file, and only on demand, i.e. when actually needing to do the import insert. I'm unfamiliar with the VS Code plugin architecture but could you do the parse asynchronously and then once you work out where to perform the insert, check if anything before that part of the file has changed in the meantime, and if hasn't changed then perform the insert otherwise parse again asynchronously and check again. Does that make sense?

codeandcats avatar Oct 18 '17 09:10 codeandcats