Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support aliases from tsconfig.json #29

Open
FFdhorkin opened this issue Aug 17, 2023 · 2 comments
Open

Support aliases from tsconfig.json #29

FFdhorkin opened this issue Aug 17, 2023 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@FFdhorkin
Copy link

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:

"paths": {
    "@/*": [
        "./reactCommon/*",
        "./reactCommon/shared/*"
    ],
    "@/tictactoe/*": [
        "./TicTacToe/react/*"
    ]
}

I'd like to be able to do something like this:

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

or alternatively

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

Describe alternatives you've considered

{
    "groupRules": [
        ...,
        {},
        "^[@]/((?!tictactoe).)*$",
        "^[@]/tictactoe",

or

{
    "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?"

@daidodo
Copy link
Owner

daidodo commented Aug 26, 2023

Thanks for the feedback!

Could you give some examples for me to understand your requirements better?

@FFdhorkin
Copy link
Author

FFdhorkin commented Sep 7, 2023

Sure. In a nutshell, because of the aliases in my tsconfig, the @/ prefix can actually resolve to very different paths. I want common/shared imports to be higher than subproject-specific imports.

Suppose I have the following:

Path in .ts file Filesystem path
@/a ./reactCommon/a
@/z ./reactCommon/shared/z
@/f ./arbitrary/f
@/tictactoe/b ./tictactoe/react/b

I would like it to sort & group the following:

import {...} from '@/f';
import {...} from '@/z';
import {...} from '@/a'; 
import {...} from '@/tictactoe/b';
import {v4} from 'uuid';

As the following:

import {v4} from 'uuid';

import {...} from '@/a';
import {...} from '@/z';

import {...} from '@/f';
import {...} from '@/tictactoe/b';

without explicitly mentioning tictactoe or f (or anything other than reactCommon) in my config, because a rule for ./reactCommon (through @/* alias) is higher than the generic rule for @/*

As things are right now, it will try to put @/tictactoe between @/a and @/z, unless I make explicit rules for various sub-paths, which could become a pain to maintain.

The only other alternative I've come up with is using #/* as a separate alias for non-shared paths. While that'd address the sorting/grouping without a config maintanence hassle, I think it would be confusing to have both @ and # refer to the local repository.

@daidodo daidodo added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants