Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-monkeys-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/eslint-plugin": patch
---

Add rule for disallowing direct barrel imports
7 changes: 3 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FlatCompat } from "@eslint/eslintrc"
import eslint from "@eslint/js"
import * as tsResolver from "eslint-import-resolver-typescript"
import importPlugin from "eslint-plugin-import-x"
Expand All @@ -7,16 +6,15 @@ import sortDestructureKeys from "eslint-plugin-sort-destructure-keys"
import * as Path from "node:path"
import * as Url from "node:url"
import tseslint from "typescript-eslint"
import eslintPluginPrettier from 'eslint-plugin-prettier/recommended'

const __filename = Url.fileURLToPath(import.meta.url)
const __dirname = Path.dirname(__filename)

const compat = new FlatCompat({
baseDirectory: __dirname,
})

export default tseslint.config(
{
files: ["src/**/*.ts", "test/**/*.ts"],
ignores: ["**/dist", "**/build", "**/docs", "**/*.md"],
},
eslint.configs.recommended,
Expand Down Expand Up @@ -111,4 +109,5 @@ export default tseslint.config(
"@typescript-eslint/unified-signatures": "off",
},
},
eslintPluginPrettier
)
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
"prettier": "^3.3.2",
"typescript": "^5.7.3",
"typescript-eslint": "^8.21.0",
"vitest": "^3.0.4"
"vitest": "^3.0.4",
"eslint-plugin-prettier":"^5.2.6",
"eslint-config-prettier":"^10.1.2"
},
"imports": {
"#dist/*": {
Expand Down
56 changes: 56 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { dprint } from "@effect/eslint-plugin/rules/dprint"
import { noImportFromBarrelPackage } from "@effect/eslint-plugin/rules/no-import-from-barrel-package"

export const meta = {
name: "@effect/eslint-plugin",
}

export const rules = {
dprint,
noImportFromBarrelPackage,
}

// NOTE: unfortunately plugins needs a self-reference inside configs,
Expand Down
88 changes: 88 additions & 0 deletions src/rules/no-import-from-barrel-package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { createRule } from "@effect/eslint-plugin/utils/eslint"
import { AST_NODE_TYPES } from "@typescript-eslint/utils"
import type { RuleFixer } from "@typescript-eslint/utils/ts-eslint"

export type Options = {
packageNames: Array<string>
}
export type MessageIds = "replaceImport"

export const noImportFromBarrelPackage = createRule<[Options], MessageIds>({
name: "no-import-from-barrel-package",
meta: {
type: "suggestion",
docs: {
description:
"Disallow importing from barrel packages, and encourages importing the specific module instead.",
},
fixable: "code",
messages: {
replaceImport: `Use import * as {{localName}} from "{{packageName}}/{{moduleName}}" instead`,
},
schema: [
{
type: "object",
properties: {
packageNames: {
type: "array",
description: "List of packages to check for barrel imports",
items: [
{
type: "string",
},
],
},
},
additionalProperties: false,
},
],
},
defaultOptions: [{ packageNames: [] }],
create: (context, options) => {
return {
ImportDeclaration: node => {
// destruct options
const [{ packageNames }] = options
// first we check if the import is from one of the configured modules
const packageName = node.source.value
if (packageNames.indexOf(packageName) > -1) {
for (const specifier of node.specifiers) {
// check only imports with style import {A, B} from "foo"
if (specifier.type === AST_NODE_TYPES.ImportSpecifier) {
// we are fine with type imports
if (specifier.importKind === "type") continue
const moduleName =
specifier.imported.type === AST_NODE_TYPES.Identifier
? specifier.imported.name
: specifier.imported.value
const localName = specifier.local.name
// fix only with a single specifier
const fixable =
node.specifiers.length === 1
? {
fix: (fixer: RuleFixer) =>
fixer.replaceTextRange(
node.range,
`import * as ${localName} from "${packageName}/${moduleName}"`,
),
}
: {}
// report the error
context.report({
loc: specifier.loc,
node: specifier,
messageId: "replaceImport",
data: {
packageName,
moduleName,
localName,
},
...fixable,
})
}
}
}
},
}
},
})
45 changes: 45 additions & 0 deletions test/no-import-from-barrel-package.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { Options } from "@effect/eslint-plugin/rules/no-import-from-barrel-package"
import { noImportFromBarrelPackage as rule } from "@effect/eslint-plugin/rules/no-import-from-barrel-package"
import { ruleTester } from "@effect/eslint-plugin/test/utils/index"

const options: [Options] = [{ packageNames: ["effect"] }]

ruleTester.run("dprint", rule, {
valid: [
{
code: `import * as T from "effect/Effect"`,
options,
},
{
code: `import { Effect as Eff} from "effect/Effect"
`,
options,
},
{
code: `import Effect from "effect/Effect"`,
options,
},
{
code: `import {type Effect } from "effect";`,
options,
},
{
code: `import {test} from "lodash";`,
options,
},
],
invalid: [
{
code: `import { Effect } from "effect"`,
options,
errors: [{ line: 1, messageId: "replaceImport" }],
output: `import * as Effect from "effect/Effect"`,
},
{
code: `import { Effect as Eff } from "effect"`,
options,
errors: [{ line: 1, messageId: "replaceImport" }],
output: `import * as Eff from "effect/Effect"`,
},
],
})