ts-importer
ts-importer copied to clipboard
Insert import statement ordered by filename
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.
This is a bit harder to implement, because there may be empty lines or commented lines between the statements ...
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.
Nope because of performance issues
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?