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

Suggestion: Add Rule to Disallow Imports Between Certain Packages

Open fabb opened this issue 5 years ago • 3 comments

Our repo is structured into yarn workspaces like this:

  • applications/app1
  • applications/app2
  • common/c1
  • common/c2

The desired rule is that applications might import from packages common/c1 and common/c2, but never from another application. In other words, in app1 there must never be an import from app2.

Currently we use depencency-cruiser for such advanced checks. Sounds like a good use case for this plugin though.

fabb avatar Sep 15 '20 08:09 fabb

Was looking for the exact same feature :) Is this somehow possible using this package or any other lint plugin?

chin2km avatar Apr 19 '21 20:04 chin2km

assume your application package has an @application prefix then, using standard eslint rule

"no-restricted-imports": ["error",
      {
        name: '@applications/*',
        message: 'Please don't import from another application. Extract common parts into separate package.'
      }
    ]

And you either can put this into your root .eslintrc.js or create a dedicated in a sub-package folder.

timofei-iatsenko avatar Sep 06 '21 14:09 timofei-iatsenko

what about cases of importing a backend package from a frontend package?

"no-restricted-imports": ["error",
      {
        restrictImportOf: '@backend/*',
        restrictOnlyFrom:  '@frontend/*',
        message: 'Please don't import packages from backend to frontend code.'
      },
      {
        restrictImportOf: '@frontend/*',
        restrictOnlyFrom:  '@backend/*',
        message: 'Please don't import packages from frontend to backend code.'
      }
    ]

AlonMiz avatar Nov 28 '21 18:11 AlonMiz