-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add ESLint rule to disallow barrel/entrypoint imports #21296
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
Draft
NullVoxPopuli-ai-agent
wants to merge
4
commits into
emberjs:main
Choose a base branch
from
NullVoxPopuli-ai-agent:nvp/no-barrel-imports-lint-rule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+441
−0
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b3b5e33
Add custom ESLint rule to disallow barrel/entrypoint imports
NullVoxPopuli 55c5c80
Rewrite no-barrel-imports rule with auto-fix for all barrel imports
NullVoxPopuli 3a8ea31
Include correct import paths in error messages and handle export *
NullVoxPopuli 714c761
Only flag actual barrels, not standalone modules with index.ts
NullVoxPopuli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /** | ||
| * ESLint rule: no-barrel-imports | ||
| * | ||
| * Disallows importing from barrel/entrypoint files of internal packages. | ||
| * Internal source files should import directly from the specific lib/ file | ||
| * to enable proper tree-shaking by bundlers. | ||
| * | ||
| * For example: | ||
| * Bad: import { Renderer } from '@ember/-internals/glimmer'; | ||
| * Good: import { Renderer } from '@ember/-internals/glimmer/lib/renderer'; | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // Barrel packages that should not be imported directly from source files. | ||
| // Each entry maps a bare package specifier to a human-readable hint. | ||
| const BARREL_PACKAGES = new Map([ | ||
| [ | ||
| '@ember/-internals/glimmer', | ||
| "Import from '@ember/-internals/glimmer/lib/...' instead of the barrel '@ember/-internals/glimmer'.", | ||
| ], | ||
| [ | ||
| '@ember/-internals/environment', | ||
| "Import from '@ember/-internals/environment/lib/env' or '@ember/-internals/environment/lib/context' instead of the barrel '@ember/-internals/environment'.", | ||
| ], | ||
| ]); | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: | ||
| 'Disallow imports from barrel/entrypoint files; require direct file imports instead', | ||
| }, | ||
| messages: { | ||
| noBarrelImport: | ||
| "Do not import from the barrel '{{source}}'. {{hint}}", | ||
| }, | ||
| schema: [], // no options | ||
| }, | ||
|
|
||
| create(context) { | ||
| // Only apply to files inside packages/ that are NOT test or type-test files. | ||
| const filename = context.filename || context.getFilename(); | ||
|
|
||
| // Skip files outside packages/ | ||
| if (!filename.includes('/packages/')) { | ||
| return {}; | ||
| } | ||
|
|
||
| // Skip test and type-test files | ||
| if (/\/(tests|type-tests)\//.test(filename)) { | ||
| return {}; | ||
| } | ||
|
|
||
| function check(node) { | ||
| const source = node.source && node.source.value; | ||
| if (typeof source !== 'string') return; | ||
|
|
||
| const hint = BARREL_PACKAGES.get(source); | ||
| if (hint) { | ||
| context.report({ | ||
| node: node.source, | ||
| messageId: 'noBarrelImport', | ||
| data: { source, hint }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| ImportDeclaration: check, | ||
| // Also catch: export { foo } from '@ember/-internals/glimmer'; | ||
| ExportNamedDeclaration: check, | ||
| // Also catch: export * from '@ember/-internals/glimmer'; | ||
| ExportAllDeclaration: check, | ||
| }; | ||
| }, | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's just all of them. and an autofixer should read in the import from the barrel file, and get its real path. an error should still occur if the imported thing is a re-export
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it — will expand to all barrel imports with auto-fix that traces re-exports to their source files.