eslint-config icon indicating copy to clipboard operation
eslint-config copied to clipboard

LINT: (Public API) Add `semantic-imports` rule

Open azinit opened this issue 3 years ago • 2 comments

Glossary

  • "External module" - entity/foo <-> entity/bar, entity/foo <-> shared/ui/button
  • "Internal module" - entity/foo <-> entity/foo/ui, entity/foo/ui/smth <-> entitiy/foo/lib

Description

  • [ ] Restrict relative imports for external modules (enforce for internalmodules)
  • [ ] Restrict absolute imports for internalmodules (enforce for external modules)

Example

// Fail 👎 
// @path features/baz/model/index.ts
import { ... } from "../../bar" // Sibling feature-slice
import { ... } from "@/features/baz/model/smth" // Invalid access to internal resource as external module
// Pass 👍 
// @path features/baz/model/index.ts
import { ... } from "../lib" // Import of internal module as internal resource - no illegal
import { ... } from "../entities/foo" // Lower layer slice - no illegal
import { ... } from "@entities/foo" // Import as external module
import { ... } from "shared/ui/button" // Import as external module

Reference

https://github.com/feature-sliced/documentation/discussions/52#discussioncomment-477443

azinit avatar Dec 04 '21 16:12 azinit

I built something a bit similar for my company's custom eslint-plugin, mine is a typescript specific plugin but you can probably adapt the code to build this check. https://github.com/tablecheck/tablecheck-react-system/blob/refactor-to-vite-and-typescript/packages/eslint-plugin/src/shortestImport.ts

SimeonC avatar May 31 '23 02:05 SimeonC

@SimeonC Thanks! ❤️

azinit avatar May 31 '23 06:05 azinit