-
-
Notifications
You must be signed in to change notification settings - Fork 490
Add no-barrel-files rule
#3570
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
Merged
Merged
Add no-barrel-files rule
#3570
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
731eda7
Add `no-barrel-files` rule
sindresorhus ccc2261
`no-barrel-files`: Improve detection and coverage
sindresorhus b18b38e
`no-barrel-files`: Add AST coverage
sindresorhus 72467f4
`no-barrel-files`: Handle TypeScript default exports
sindresorhus f126fa3
`no-barrel-files`: Handle TypeScript instantiation expressions
sindresorhus 22aba38
`no-barrel-files`: Cover exports before imports
sindresorhus f2c6b8f
`no-barrel-files`: Document how to fix violations
sindresorhus 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # no-barrel-files | ||
|
|
||
| 📝 Disallow barrel files. | ||
|
|
||
| 🚫 This rule is _disabled_ in the following [configs](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config): ✅ `recommended`, ☑️ `unopinionated`. | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
| <!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> | ||
|
|
||
| A barrel file only imports and re-exports bindings, including type-only bindings, from other modules. Barrel files make it less clear where a dependency comes from and can cause consumers to load more modules than they need. | ||
|
|
||
| This rule treats any file whose top-level body consists only of imports and re-exports as a barrel, including a file that re-exports from only one module. It is intentionally syntax-only: it does not resolve modules or try to distinguish package entry points from internal barrel files. Disable it in intentional entry points. | ||
|
|
||
| At least one binding must be re-exported; files containing only empty exports are ignored. The rule recognizes ECMAScript module syntax, including TypeScript's type-only extensions. | ||
|
|
||
| Side-effect-only imports, including `import 'foo'` and `import {} from 'foo'`, are not part of a barrel file, so a file that combines one with re-exports is ignored. | ||
|
|
||
| Fix violations by removing the barrel file and importing directly from its source modules, for example, replace `import {foo} from './index.js'` with `import {foo} from './foo.js'`. Disable this rule for intentional package entry points. | ||
|
|
||
| ## Examples | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```js | ||
| export {foo} from './foo.js'; | ||
| ``` | ||
|
|
||
| ```js | ||
| import {foo} from './foo.js'; | ||
| export {foo}; | ||
| ``` | ||
|
|
||
| ```js | ||
| export * from './foo.js'; | ||
| export * from './bar.js'; | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```js | ||
| export const foo = 1; | ||
| ``` | ||
|
|
||
| ```js | ||
| import {foo} from './foo.js'; | ||
| export const bar = foo; | ||
| ``` | ||
|
|
||
| ```js | ||
| export {foo} from './foo.js'; | ||
| export const bar = 1; | ||
| ``` | ||
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
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,105 @@ | ||
| /** @import * as ESLint from 'eslint'; */ | ||
|
|
||
| import {unwrapTypeScriptExpression} from './utils/index.js'; | ||
|
|
||
| const MESSAGE_ID = 'no-barrel-files'; | ||
| const messages = { | ||
| [MESSAGE_ID]: 'Barrel files are not allowed.', | ||
| }; | ||
|
|
||
| const getImportedBindingNames = program => { | ||
| const importedBindingNames = new Set(); | ||
|
|
||
| for (const node of program.body) { | ||
| if (node.type !== 'ImportDeclaration') { | ||
| continue; | ||
| } | ||
|
|
||
| for (const specifier of node.specifiers) { | ||
| importedBindingNames.add(specifier.local.name); | ||
| } | ||
| } | ||
|
|
||
| return importedBindingNames; | ||
| }; | ||
|
|
||
| const isBarrelFile = program => { | ||
| const importedBindingNames = getImportedBindingNames(program); | ||
| let hasReExport = false; | ||
|
|
||
| for (const node of program.body) { | ||
| if (node.type === 'ImportDeclaration') { | ||
| if (node.specifiers.length === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| if (node.type === 'ExportAllDeclaration') { | ||
| hasReExport = true; | ||
| continue; | ||
| } | ||
|
|
||
| if (node.type === 'ExportNamedDeclaration') { | ||
| if ( | ||
| node.declaration | ||
| || (!node.source && node.specifiers.some(specifier => !importedBindingNames.has(specifier.local.name))) | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| hasReExport ||= node.specifiers.length > 0; | ||
| continue; | ||
| } | ||
|
|
||
| if (node.type === 'ExportDefaultDeclaration') { | ||
| let declaration = unwrapTypeScriptExpression(node.declaration); | ||
| while (declaration.type === 'TSInstantiationExpression') { | ||
| declaration = unwrapTypeScriptExpression(declaration.expression); | ||
| } | ||
|
|
||
| if (declaration.type === 'Identifier' && importedBindingNames.has(declaration.name)) { | ||
| hasReExport = true; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return hasReExport; | ||
| }; | ||
|
|
||
| /** @param {ESLint.Rule.RuleContext} context */ | ||
| const create = context => { | ||
| context.on('Program', node => { | ||
| if (!isBarrelFile(node)) { | ||
| return; | ||
| } | ||
|
|
||
| return { | ||
| node, | ||
| messageId: MESSAGE_ID, | ||
| }; | ||
| }); | ||
| }; | ||
|
|
||
| /** @type {ESLint.Rule.RuleModule} */ | ||
| const config = { | ||
| create, | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'Disallow barrel files.', | ||
| recommended: false, | ||
| }, | ||
| schema: [], | ||
| messages, | ||
| languages: [ | ||
| 'js/js', | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| export default config; |
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,90 @@ | ||
| import outdent from 'outdent'; | ||
| import {getTester, parsers} from './utils/test.js'; | ||
|
|
||
| const {test} = getTester(import.meta); | ||
|
|
||
| test.snapshot({ | ||
| valid: [ | ||
| 'import "foo";', | ||
| 'import {} from "foo";', | ||
| 'import {} from "foo"; export {bar} from "bar";', | ||
| 'import {foo} from "foo"; export const bar = foo;', | ||
| 'export {};', | ||
| 'export {} from "foo";', | ||
| 'const foo = 1; export {foo};', | ||
| 'export {foo} from "foo"; const bar = 1;', | ||
| 'export const foo = 1;', | ||
| 'export default foo;', | ||
| 'export default function foo() {}', | ||
| 'import "foo"; export {bar} from "bar";', | ||
| outdent` | ||
| import {foo} from './foo.js'; | ||
| const bar = foo; | ||
| export {bar}; | ||
| `, | ||
| ], | ||
| invalid: [ | ||
| 'export {foo} from "foo";', | ||
| 'export {foo as bar} from "foo";', | ||
| 'export {default} from "foo";', | ||
| 'export * from "foo";', | ||
| 'export * as namespace from "foo";', | ||
| 'export {}; export {foo} from "foo";', | ||
| 'import {foo} from "foo"; export {foo};', | ||
| 'import {foo as bar} from "foo"; export {bar};', | ||
| 'import foo from "foo"; export default foo;', | ||
| 'export default foo; import foo from "foo";', | ||
| 'import * as namespace from "foo"; export {namespace as default};', | ||
| 'export {foo}; import {foo} from "foo";', | ||
| outdent` | ||
| import {foo} from './foo.js'; | ||
| export {foo}; | ||
| export {bar} from './bar.js'; | ||
| `, | ||
| outdent` | ||
| // This is still a barrel file. | ||
| export {}; | ||
| export {foo} from './foo.js'; | ||
| export {bar} from './bar.js'; | ||
| `, | ||
| { | ||
| code: 'export type {Foo} from "foo";', | ||
| languageOptions: {parser: parsers.typescript}, | ||
| }, | ||
| { | ||
| code: 'export type * from "foo";', | ||
| languageOptions: {parser: parsers.typescript}, | ||
| }, | ||
| { | ||
| code: 'export type * as Namespace from "foo";', | ||
| languageOptions: {parser: parsers.typescript}, | ||
| }, | ||
| { | ||
| code: 'import foo from "foo"; export default foo as Foo;', | ||
| languageOptions: {parser: parsers.typescript}, | ||
| }, | ||
| { | ||
| code: 'import foo from "foo"; export default foo satisfies Foo;', | ||
| languageOptions: {parser: parsers.typescript}, | ||
| }, | ||
| { | ||
| code: 'import foo from "foo"; export default foo!;', | ||
| languageOptions: {parser: parsers.typescript}, | ||
| }, | ||
| { | ||
| code: 'import foo from "foo"; export default foo<string>;', | ||
| languageOptions: {parser: parsers.typescript}, | ||
| }, | ||
| { | ||
| code: 'import foo from "foo"; export default <Foo>foo;', | ||
| languageOptions: {parser: parsers.typescript}, | ||
| }, | ||
| { | ||
| code: outdent` | ||
| import type {Foo} from './foo.js'; | ||
| export type {Foo}; | ||
| `, | ||
| languageOptions: {parser: parsers.typescript}, | ||
| }, | ||
| ], | ||
| }); |
Oops, something went wrong.
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.
Would be good to tell how to fix this issue: don't create files that exist solely to import and re-exports; import directly from the source.