Skip to content

Feature Suggestion: Folder encapsulation determined by index/exports.js #2565

@carpben

Description

@carpben

The problem

A popular pattern is to include an index.js in the folder, that re-exports entities from the folder. The developer declares which entities he would like to expose out of that folder. But in practice this intention isn't enforced, and index.js doesn't create encapsulation. Objects that are exported from another file in the folder could be imported in the entire project. Objects that are reexported from index.js can now be imported from both the index file, and the original file.

On the other hand plugin's no-restricted-imports rule can promote folder encapsulation, But requires special configuration. index.js for exports is such a common pattern, that configuring a boundary for each file is simply not practical.

Proposal

  • User can opt in to a index.js|exports.js folder encapsulation feature.
  • Existence of files by that name will create an encapsulated folder.

Examples of proper and improper use of this rule

Imagine a project with the following file structure:

src domains posts index.js postsUtils components PostBanner PostsList pages Some Page 

And the following index.js

// src/domains/posts/index.js  export PostBanner from "./components/PostBanner export PostsList from "./components/PostsList

Example of proper use of this rule

// src/pages/SomePage import PostBanner from "../domains/Posts" 
// src/domains/components/postsList import PostBanner from "./PostBanner" 

Example of improper use of this rule

// src/pages/SomePage import PostBanner from "../domains/Posts/components/PostBanner" 
// src/pages/SomePage import PostBanner from "../domains/Posts/postsUtils" 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions