conarti / eslint-plugin-feature-sliced

Feature sliced design eslint plugin
30 stars 1 forks source link

Public api rule not working as expected #11

Open jesperume opened 3 months ago

jesperume commented 3 months ago

Thanks for a great plugin!

However the rule "@conarti/feature-sliced/public-api" is not working as expected.

In the FSD public api docs the very first example says the following should not be allowed. But when I try this path "features/auth-form/components/view/form" is does not show an eslint error.

Bad: moving or renaming this component inside the feature will lead to the need to refactor imports in all places where the component is used.

  • import { Form } from "features/auth-form/ui/form" Good: the interface of the feature does not display its internal structure, external "users" of the feature will not suffer from moving or renaming the component inside the feature

  • import { AuthForm } from "features/auth-form"

I've tried to clone the repo and manually add the following test. It failes with: "Should have 1 error but had 0. Can you have a look?

File: src\rules\public-api\index.test.ts

invalid: [
    {
      code: "import { addCommentFormActions, addCommentFormReducer } from 'features/auth-form/components/view/form'",
      errors: [
        makeErrorWithSuggestion(
          ...
        ),
      ],
    },

What I'm looking for is to configure the rule so that only "import from features/auth-form" is allowed and everything under features/auth-form/** is disallowed.

conarti commented 3 months ago

Hi. Thank you for feedback.

It doesn't work because path has unknown segments like 'components'/'view'/'form' and plugin think that it is just a group folders.

But yea, it is the problem, so plugin needs one more setting to understand which segments your project has and define group folders correctly and throw errors if it is the segment.

I will try to find more time to fix this in next months. But if you want to contribute or want to make it faster - you are welcome.

jesperume commented 3 months ago

That makes sense. One idea would be to have a configuration to only validate by the level of depth. That would be the most strict variant that would never expose anything outside the index file. For me that would make most sense. For that to work you will need to be able to configure if you are using group folders or not per layer.

Verifying by looking for segments could be problematic, I have even seen some examples in FSD web site that does not use segment folders at all: Example 1, Example 2

I have found an workaround using no-restricted-imports rule that works in conjunction with the recommended rules in eslint-plugin-feature-sliced. For info I'm not using group folders. So it seems to work, but it would be better if it could be configured in the eslint-plugin-feature-sliced plugin.

"no-restricted-imports": [
          "error",
          {
            "patterns": [
              { "message": "Prefer public imports", "group": ["@/app/*/**"] },
              { "message": "Prefer public imports", "group": ["@/pages/*/**"] },
              { "message": "Prefer public imports", "group": ["@/widgets/*/**"] },
              { "message": "Prefer public imports", "group": ["@/features/*/**"] },
              { "message": "Prefer public imports", "group": ["@/entities/*/**"] },
              { "message": "Prefer public imports", "group": ["@/shared/*/**"] },
           ]
        }
]

I won't be able to contribute this time but I will gladly test it out if you have time to improve the code. I appreciate your work.