javierbrea / eslint-plugin-boundaries

Eslint plugin checking architecture boundaries between elements
MIT License
474 stars 8 forks source link

[entry-point] Cannot default disallow entry points other than index.js within one element type #197

Closed dantman closed 2 years ago

dantman commented 2 years ago

Is your issue related to a problem? Please describe. I want to disallow using anything but index.js as an entry point to one type of element (feature in my case, but others may do this for modules). But I do not want to do this for all element types, so the overall default is 'allow'.

Describe the issue When the entry-point default is allow there is no way to declare "disallow everything except index.js".

dantman commented 2 years ago

This appears to be possible by creating two separate rules with the same target, the first one with the disallow: '*' and the second with the allow: 'index.*'.

javierbrea commented 2 years ago

Your solution is right @dantman,

As you mentioned, defining both allow and disallow in the same rule is not possible. From the docs: "If one rule contains both allow and disallow properties, the disallow one has priority. It will not try to match the allow one if disallow matches. The result for that rule will be disallow in that case."

So, if you have "allow" as default value, and you want to allow only certain pattern only for one element type, you'll first have to disallow everything for that element type, and then allow only the specific pattern in separated rules.

Just in case it is useful also for other users, here is an example:

{
  rules: {
    "boundaries/entry-point": [2, {
        // allow all entry-points by default
        default: "allow",
        rules: [
          {
            // Override the default value for the element type
            target: ["modules"],
            // Disallow everything
            disallow: "*"
          },
          {
            // Add another rule for the same type, allowing only some entry point
            target: ["modules"],
            // only allow index.js
            allow: "index.js"
          }
        ]
      }
    ]
  }
}