uhyo / eslint-plugin-import-access

https://www.npmjs.com/package/eslint-plugin-import-access
MIT License
355 stars 9 forks source link

Proposal: selfReferencingLoophole #12

Closed m-shaka closed 1 year ago

m-shaka commented 1 year ago

As this concerns the API design of this plugin, I will start from this proposal. As a new feature, I would like to add selfReferencingLoophole via the exports field in the package.json.

Let me explain in details with a specific use case. In the component library I am currently developing, I am writing Storybook stories only for the public API. In the package.json, I have specified the exports as follows, and I use them in Storybook like import { SomeComponent } from 'my-library/components'. (This is called as self-referencing in the Node.js document)

{
  "name": "my-library",
  "exports": {
    "./components": {
      "type": "./src/components/index.ts",
      "default": "./src/components/index.ts",
    },
    "./styles": {
      "type": "./src/styles/index.ts",
      "default": "./src/styles/index.ts",
    },
}

Currently, I have to manually add the @public comment tag to all the members exported from src/components/index.ts. This is because I have set the defaultImportability to 'package', and I place story files in a 'stories' directory which is a sibling directory of 'src'.

However, since src/components/index.ts is intended as the public API, the task of adding comment tags feels excessively cumbersome. It would be greatly appreciated if, as an option, we could have self-referencing automatically treated as public.

uhyo commented 1 year ago

Thanks for the suggestion!

I understand your use case and am in favor of the proposed change. Maybe selfReferencingLookhole could have true as the default value.

m-shaka commented 1 year ago

Thank you! Please let me know if there's anything I can help with.

I have a feeling that testing is a bit difficult because it depends on package.json of this plugin itself 🤔 You can add the exports field to package.json, but it may have an impact on the users

uhyo commented 1 year ago

Released in v2.1.0 🎁

I changed the name to treatSelfReferenceAs which takes internal and external. The default value is external where self referencing is treated as if external import so you can import without restrictions.