Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] no-parent-barrel-import: Forbid import from parent barrel file #2779

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jonioni
Copy link

@jonioni jonioni commented May 8, 2023

Resolves: #2318 (comment)

Forbid import from parent barrel file which often leads to runtime error cannot read ... of undefined.

Gien:

  • foo/
    • index.ts
    • bar/
      • index.ts
      • baz.ts
      • qux.ts
    • quux/
      • index.ts
      • ...
// foo/bar/baz.ts
import { T } from '../..';        // forbidden
import { T } from '..';           // forbidden
import { T } from '.';            // forbidden
import { T } from 'foo';          // forbidden
import { T } from 'foo/bar';      // forbidden

import { T } from './qux';        // permitted
import { T } from 'foo/bar/qux';  // permitted
import { U } from '../quux';      // permitted
import { U } from 'foo/quux';     // permitted

It resolves the missing cycle check from no-self-import, while being computationally cheap (over no-cycle)

Rationale

Though it seems that the issue should have been covered by existing rules, there actually isn't one that prevents this circular import / runtime error via parent barrel file (index.js) efficiently.

Related Issue

There is a related rule of a larger scope: #2408 but further adjustment might be needed. Also it seems to be overly restrictive on importing from non-parent folder (forbids import example2 from '../dir2'; from dir1/file.js) which might need more justification.

@jonioni jonioni marked this pull request as draft May 8, 2023 08:26
@jonioni jonioni force-pushed the feat/no-parent-barrel-import branch from 0620062 to 9a811b4 Compare May 8, 2023 10:04
@jonioni
Copy link
Author

jonioni commented May 8, 2023

Hi @ljharb, wondering if you could point out why the tests failed with context.report is not a function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant