-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Draft
43081j
wants to merge
5
commits into
un-ts:master
Choose a base branch
from
43081j:barrels-of-joy
base: master
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.
+1,049
−0
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,27 @@ | ||
# import-x/avoid-barrel-files | ||
|
||
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,87 @@ | ||
# import-x/avoid-importing-barrel-files | ||
|
||
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,25 @@ | ||
# import-x/avoid-namespace-import | ||
|
||
This rule forbids the use of namespace imports as they can lead to unused imports and prevent treeshaking. | ||
|
||
## 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,12 @@ | ||
# import-x/avoid-re-export-all | ||
|
||
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 | ||
} | ||
} | ||
} | ||
|
||
if ( | ||
exports > declarations && | ||
exports > amountOfExportsToConsiderModuleAsBarrel | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'avoidBarrel', | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
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.
Uh oh!
There was an error while loading. Please reload this page.