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

Docs/examples suggestion for forbidding feature folder dependencies #440

Closed
jzaefferer opened this issue Feb 9, 2021 · 1 comment · Fixed by #444
Closed

Docs/examples suggestion for forbidding feature folder dependencies #440

jzaefferer opened this issue Feb 9, 2021 · 1 comment · Fixed by #444
Labels
enhancement this will make dependency-cruiser sweeter

Comments

@jzaefferer
Copy link

Summary

Thank you for this great tool! It took me a while to build two specific rules, and since I had to look through issues to find an answer, I figured my solution could be useful for others. Specifically I wanted rules that a src/feature folder can only depend on itself or on modules outside of src/features (but not one feature to another). And except two specific modules, no module is allowed to depend on any src/feature folder.

Context

The context is a React app with a src/App.tsx that does a lot of imports from folders in src/features/ folders. The App is allowed to import these features, but nothing else should like (src/components). And each feature should be standalone, and only depend on shared components (e.g. src/components).

I eventually managed to put these rules together:

forbidden: {
    {
      name: 'not-into-features',
      comment:
        'Code outside of features should not depend on features, except App and Shell',
      severity: 'error',
      from: { pathNot: ['^src/features', '^src/App', '^src/Shell'] },
      to: { path: '^src/features' },
    },
    {
      name: 'features-not-to-features',
      comment:
        'One feature should not depend on another feature (in a separate folder)',
      severity: 'error',
      from: { path: '(^src/features/)([^/]+)/' },
      to: { path: '^$1', pathNot: '$1$2' },
    },
}

Example violations:

error not-into-features: src/hooks/useSearchBar.ts → src/features/LibraryPage/buildUrl.ts
error features-not-to-features: src/features/user-profile/components/UserProfile.jsx → src/features/ChangeAvatarModal/ChangeAvatarModalContent.jsx

The first rule was relatively easy, but the 2nd rule was rather difficult to write, and I only managed after reading #330 (comment)

Maybe these could be useful examples for others, e.g. on https://github.com/sverweij/dependency-cruiser/blob/develop/doc/rules-tutorial.md where its going to be easier to find than in issues.

@sverweij sverweij added the enhancement this will make dependency-cruiser sweeter label Feb 10, 2021
@sverweij
Copy link
Owner

Hi @jzaefferer

I've been toying with expanding the documentation in this respect (looking for inspiration to http-proxy-middleware's 'recipes'), but was doubting it'd actually fill a need - as as an author it's one of my blind spots. This is why your feedback is very valuable. Thank you!

I'll expand the documentation with a bunch of examples/ recipes, starting with yours!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement this will make dependency-cruiser sweeter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants