daidodo / daidodo/format-imports

Support aliases from tsconfig.json

Open
#29 2 comments 0 reactions 0 assignees View on GitHub
enhancement good first issue help wanted
Dominant language
TypeScript
Stars
51
Forks
4
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**
I'm using some import aliases in my `tsconfig.json`. My project is broken up into a "shared" alias, and subprojects. It's not exactly a monorepo since there's only one package.json, but functionally, this is intended to be a project that includes some sample games using a library.

Currently, to manually ensure the "shared" aliases are sorted higher, you have to either manually exclude or manually include folders, which is somewhat more brittle (see below).

While this approach works, I'd rather set up rules based on the **real** import path, not the aliased paths

**Describe the solution you'd like**
With this in my `tsconfig.json`:
```json
"paths": {
"@/*": [
"./reactCommon/*",
"./reactCommon/shared/*"
],
"@/tictactoe/*": [
"./TicTacToe/react/*"
]
}
```
I'd like to be able to do something like this:

```json
{
"groupRules": [
...,
{},
{
"useTsConfigAliases": true,
"regex": "./reactCommon"
},
"^[@]"
```
or alternatively

```json
{
"groupRules": [
...,
{},
{
"regex": "^[@]",
"sortRules": {
"paths": [{"aliased": "./reactCommon"}, "_", "aA"]
}
}
```

**Describe alternatives you've considered**
```json
{
"groupRules": [
...,
{},
"^[@]/((?!tictactoe).)*$",
"^[@]/tictactoe",
```
or
```json
{
"groupRules": [
...,
{},
"^[@]/(app|components|public|shared)"
"^[@]/tictactoe",
```

I also could differentiate the aliases (e.g. `@/` and `#/` or something), but I'd prefer not to do that.

**Additional context**
My goal is to sort anything that's in the `reactCommon` folder higher than anything that's specific to anything specific to an individual game, without having to manually specify either the names of the games or the names of the folders under `reactCommon`

As things are now, without those explicit folder mentions, when it sees `@/util` and `@/tictactoe`, it ends up sorting the imports alphabetically rather than grouping them by "is this shared sample code or sample code specific to one game?"

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the requested paths and groupRules behavior alongside the example tsconfig.json paths. Determine how aliased imports could map to their real paths and how that mapping should interact with sortRules. Done means imports using aliases can be grouped by resolved locations, such as placing reactCommon imports before game-specific imports, without manually naming each folder.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.