-
-
Notifications
You must be signed in to change notification settings - Fork 52
feat: port eslint-plugin-barrel-files #304
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
222877c
feat: port eslint-plugin-barrel-files
43081j 9bad699
chore: pr fixes
43081j fa2d7ee
chore: remove debug flag
43081j 8322621
chore: leftover debug flags
43081j eeb133d
docs: update example to flat config
43081j 8b98c33
chore: re-use default options
43081j aafc70c
Merge branch 'master' into barrels-of-joy
43081j d6425c1
chore: upgrade barrel utils
43081j ce9d6dd
docs: update docs for barrel rules
43081j 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
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,29 @@ | ||
# import-x/avoid-barrel-files | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
This rule disallows _authoring_ barrel files in your project. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
export { foo } from 'foo' | ||
export { bar } from 'bar' | ||
export { baz } from 'baz' | ||
``` | ||
|
||
## Options | ||
|
||
This rule has the following options, with these defaults: | ||
|
||
```js | ||
"import-x/avoid-barrel-files": ["error", { | ||
"amountOfExportsToConsiderModuleAsBarrel": 5 | ||
}] | ||
``` | ||
|
||
### `amountOfExportsToConsiderModuleAsBarrel` | ||
|
||
This option sets the number of exports that will be considered a barrel file. Anything over will trigger the rule. |
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,89 @@ | ||
# import-x/avoid-importing-barrel-files | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
This rule aims to avoid importing barrel files that lead to loading large module graphs. This rule is different from the `avoid-barrel-files` rule, which lints against _authoring_ barrel files. This rule lints against _importing_ barrel files. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
// If `foo` is a barrel file leading to a module graph of more than 20 modules | ||
export { foo } from 'foo' | ||
``` | ||
|
||
## Options | ||
|
||
This rule has the following options, with these defaults: | ||
|
||
```js | ||
"import-x/avoid-importing-barrel-files": ["error", { | ||
allowList: [], | ||
maxModuleGraphSizeAllowed: 20, | ||
amountOfExportsToConsiderModuleAsBarrel: 3, | ||
exportConditions: ["node", "import"], | ||
mainFields: ["module", "browser", "main"], | ||
extensions: [".js", ".ts", ".tsx", ".jsx", ".json", ".node"], | ||
}] | ||
``` | ||
|
||
### `allowList` | ||
|
||
List of modules from which to allow barrel files being imported. | ||
|
||
### `maxModuleGraphSizeAllowed` | ||
|
||
Maximum allowed module graph size. If an imported module results in loading more than this many modules, it will be considered a barrel file. | ||
|
||
### `amountOfExportsToConsiderModuleAsBarrel` | ||
|
||
Amount of exports to consider a module as a barrel file. | ||
|
||
### `exportConditions` | ||
|
||
Export conditions to use when resolving modules. | ||
|
||
### `mainFields` | ||
|
||
Main fields to use when resolving modules. | ||
|
||
### `extensions` | ||
|
||
Extensions to use when resolving modules. | ||
|
||
### `tsconfig` | ||
|
||
TSConfig options to pass to the underlying resolver when resolving modules. | ||
|
||
This can be useful when resolving project references in TypeScript. | ||
|
||
### `alias` | ||
|
||
The rule can accept an `alias` option whose value can be an object that matches Webpack's [resolve.alias](https://webpack.js.org/configuration/resolve/) configuration. | ||
|
||
```js | ||
// eslint.config.js | ||
import path from 'node:path' | ||
import { defineConfig } from 'eslint/config' | ||
import importPlugin from 'eslint-plugin-import-x' | ||
|
||
export default defineConfig([ | ||
{ | ||
plugins: { | ||
'import-x': importPlugin, | ||
}, | ||
rules: { | ||
'import-x/avoid-importing-barrel-files': [ | ||
'error', | ||
{ | ||
alias: { | ||
// "@/foo/bar.js" => "./src/foo/bar.js" | ||
'@': [path.resolve('.', 'src')], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
]) | ||
``` |
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,27 @@ | ||||||
# import-x/avoid-namespace-import | ||||||
|
||||||
<!-- end auto-generated rule header --> | ||||||
|
||||||
This rule forbids the use of namespace imports as they can lead to unused imports and prevent treeshaking. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typographical suggestion: Consider using “tree shaking” (with a space) instead of “treeshaking” to match the common terminology.
Suggested change
|
||||||
|
||||||
## Rule Details | ||||||
|
||||||
Examples of **incorrect** code for this rule: | ||||||
|
||||||
```js | ||||||
import * as foo from 'foo' | ||||||
``` | ||||||
|
||||||
## Options | ||||||
|
||||||
This rule has the following options, with these defaults: | ||||||
|
||||||
```js | ||||||
"import-x/avoid-namespace-import": ["error", { | ||||||
allowList: [] | ||||||
}] | ||||||
``` | ||||||
|
||||||
### `allowList` | ||||||
|
||||||
A list of namespace imports that are allowed. |
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,14 @@ | ||
# import-x/avoid-re-export-all | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
This rule forbids exporting `*` from a module as it can lead to unused imports and prevent treeshaking. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
export * from 'foo' | ||
export * as foo from 'foo' | ||
``` |
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,100 @@ | ||
import { createRule } from '../utils/index.js' | ||
|
||
export interface Options { | ||
amountOfExportsToConsiderModuleAsBarrel: number | ||
} | ||
|
||
export type MessageId = 'avoidBarrel' | ||
|
||
const defaultOptions: Options = { | ||
amountOfExportsToConsiderModuleAsBarrel: 3, | ||
} | ||
|
||
export default createRule<[Options?], MessageId>({ | ||
name: 'avoid-barrel-files', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Forbid authoring of barrel files.', | ||
category: 'Performance', | ||
recommended: true, | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
amountOfExportsToConsiderModuleAsBarrel: { | ||
type: 'number', | ||
description: | ||
'Minimum amount of exports to consider module as barrelfile', | ||
default: 3, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
avoidBarrel: | ||
'Avoid barrel files, they slow down performance, and cause large module graphs with modules that go unused.', | ||
}, | ||
}, | ||
defaultOptions: [defaultOptions], | ||
create(context) { | ||
const options = context.options[0] || defaultOptions | ||
const amountOfExportsToConsiderModuleAsBarrel = | ||
options.amountOfExportsToConsiderModuleAsBarrel | ||
|
||
return { | ||
Program(node) { | ||
let declarations = 0 | ||
let exports = 0 | ||
|
||
for (const n of node.body) { | ||
if (n.type === 'VariableDeclaration') { | ||
declarations += n.declarations.length | ||
} | ||
|
||
if ( | ||
n.type === 'FunctionDeclaration' || | ||
n.type === 'ClassDeclaration' || | ||
n.type === 'TSTypeAliasDeclaration' || | ||
n.type === 'TSInterfaceDeclaration' | ||
) { | ||
declarations += 1 | ||
} | ||
|
||
if (n.type === 'ExportNamedDeclaration') { | ||
exports += n.specifiers.length | ||
} | ||
|
||
if (n.type === 'ExportAllDeclaration' && n?.exportKind !== 'type') { | ||
exports += 1 | ||
} | ||
|
||
if (n.type === 'ExportDefaultDeclaration') { | ||
if ( | ||
n.declaration.type === 'FunctionDeclaration' || | ||
n.declaration.type === 'CallExpression' | ||
) { | ||
declarations += 1 | ||
} else if (n.declaration.type === 'ObjectExpression') { | ||
exports += n.declaration.properties.length | ||
} else { | ||
exports += 1 | ||
} | ||
} | ||
43081j marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if ( | ||
exports > declarations && | ||
exports > amountOfExportsToConsiderModuleAsBarrel | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'avoidBarrel', | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
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.
The default value here is set to 5, but the rule implementation uses 3. Please synchronize the docs with the code.