javierbrea / eslint-plugin-boundaries

Eslint plugin checking architecture boundaries between elements
MIT License
473 stars 9 forks source link

Matching paths in external modules #297

Closed butzist closed 11 months ago

butzist commented 11 months ago

Thanks a lot for this plugin - it has been very useful so far. I have a question though related to the following usecase:

We are moving some elements to a shared library and would like to enforce the same rules, that we have inside the sources, e.g. from: "components", disallow: "repositories".

This is what I came up with so far - which is not working:

    "boundaries/external": [
      "error",
      {
        default: "allow",
        rules: [
          {
            from: ["components"],
            disallow: ["@group/package/repositories/*"],
          },
        ],
      },
    ],

Is there any way to specify the allowed paths in a module?

javierbrea commented 11 months ago

Hi @butzist , when the imported library is outside the project it is considered "external", so, you should use the external rule to allow or disallow imports. You can configure it also using micromatch patterns, or even allow/disallow importing specific variables when using destructured imports.

butzist commented 11 months ago

Thank you for your answer @javierbrea - I have tried micromatch patterns, but it seems like they are only applied to the package name and not to the esm path inside the package. An example:

I have the following piece of code:

image

The following rule unfortunately does not result in a eslint error:

    "boundaries/external": [
      "error",
      {
        default: "allow",
        rules: [
          {
            from: ["components"],
            disallow: ["@datahow/dhljs/services/**/*"],
          },
        ],
      },
    ],

whereas the following rule works, but seems to be only matching on the package name - not on the path:

    "boundaries/external": [
      "error",
      {
        default: "allow",
        rules: [
          {
            from: ["components"],
            disallow: ["@datahow/*"],
          },
        ],
      },
    ]

results in:

image

javierbrea commented 11 months ago

Hi @butzist , sorry, you're right, it only takes into account the module name. In order to avoid a breaking change, I'm adding a path option that may be used to match the module path using micromatch patterns. For example:

{
"boundaries/external": [
      "error",
      {
        default: "allow",
        rules: [
          {
            from: ["components"],
            disallow: [
                ["@datahow/dhljs", { path: "/services/*" }]
             ],
          },
        ],
      },
    ],
}
butzist commented 11 months ago

Awesome, thanks a lot @javierbrea! Will test this today.

javierbrea commented 11 months ago

Thanks to you for reporting the issue @butzist . Note that the rule documentation was also updated 😉 .