eslint-plugin-boundaries icon indicating copy to clipboard operation
eslint-plugin-boundaries copied to clipboard

Allow imports from a given nested folder

Open Igorkowalski94 opened this issue 10 months ago • 3 comments

Hello,

Is it possible to write such a rule?

  • Feature cannot import files from other features unless they are in the features/shared folder.

I tried this configuration but unfortunately it doesn't work.

 "settings": {
        "boundaries/elements": [
            {
                "type": "features",
                "pattern": "features/*",
                "mode": "folder"
            },
            {
                "type": "featuresShared",
                "pattern": "features/shared/*",
                "mode": "folder"
            }
        ],
        "import/resolver": {
            "typescript": {
                "alwaysTryTypes": true
            }
        }
    },
    "rules": {
        "boundaries/element-types": [
            2,
            {
                "default": "disallow",
                "rules": [
                    {
                        "from": "features",
                        "allow": [
                            "featuresShared"
                        ]
                    }
                ]
            }
        ],
    }

.
├── ...
└── 📂 src
    └── 📂 features
        ├── 📂 shared
        ├── 📂 Feature1
        └── 📂 Feature2
 

Igorkowalski94 avatar Apr 11 '24 10:04 Igorkowalski94

Hi @Igorkowalski94 , could you please provide more details? You can enable the debug mode in order to see which element type the plugin is assigning to each file by using the ESLINT_PLUGIN_BOUNDARIES_DEBUG environment variable.

ESLINT_PLUGIN_BOUNDARIES_DEBUG=1 npm run lint

javierbrea avatar Apr 12 '24 06:04 javierbrea

I have the following structure:

.
├── ...
└── 📂 src
    ├── ...
    └── 📂 features
        ├── ...
        ├── 📂 Feature1
        │   ├── 📄 Feature1.tsx
        │   ├── 📄 feature1.types.ts   
        │   ├── 📂 helpers             // Helpers only for Feature1 folder
        │   └── 📂 components          // Components only for Feature1 folder
        │        ├── ...
        │        ├── 📂 Child1         // Same structure as Child2
        │        └── 📂 Child2
        │            ├── 📄 Child2.tsx
        │            ├── 📄 child2.types.ts  
        │            ├── 📂 helpers           // Helpers only for Child2 folder     
        │            └── 📂 components       // Components only for Child2 folder
        │ 
        └── 📂 Feature2 // Same structure as Feature1

I would like to achieve the following rules:

  • The Feature1 can import Feature2.tsx and feature2.types.tsx but cannot import Feature2 components and helpers.
  • The Feature2 can import Feature1.tsx and feature1.types.tsx but cannot import Feature1 components and helpers.
  • Feature1.tsx can import Child1.tsx, child1.types.ts and Child2.tsx, child2.types.ts but cannot import their components and helpers.
  • Child1 folder can import Child2.tsx and child2.types.ts but cannot import Child2 components and helpers.
  • Child2 folder can import Child1.tsx and child1.types.ts but cannot import Child1 components and helpers.
  • The entire components and helpers folder and Feature1.tsx can import feature1.types.ts

I tried many configurations but couldn't achieve these rules. Is it possible?

Igorkowalski94 avatar Apr 12 '24 17:04 Igorkowalski94

Yes, it is possible, but you should try to debug your configuration in order to know if your elements are being categorized properly. I suspect that maybe all of them are being categorized as features because they match first the features pattern, but I'm not sure. Could you please provide logs?

javierbrea avatar Apr 16 '24 05:04 javierbrea

@Igorkowalski94 were you able to resolve this? I am having a similar issue.

rishitank avatar Sep 02 '24 11:09 rishitank

@rishitank Yes, I created my own solution eslint-plugin-project-structure. I'm closing the issue.

Igorkowalski94 avatar Sep 02 '24 11:09 Igorkowalski94