|
1
|
import type { Linter } from "eslint" |
|
2
|
import { ruleId } from "../constants" |
|
3
|
import { ignoresBlock, pluginBlock, tsParserBlock } from "./shared" |
|
4
|
|
|
5
|
/** |
|
6
|
* Modules preset — the rules that govern a Medusa **module** (or provider) |
|
7
|
* package: its service(s), its module definition, and its data models. |
|
8
|
* |
|
9
|
* Unlike `recommended`, whose module rules are scoped to a `modules/`-nested |
|
10
|
* path (matching how modules live inside a Medusa application), this preset |
|
11
|
* targets the layout of a standalone module/provider package — Medusa's own |
|
12
|
* under `packages/modules`, or one a user authors as its own package: |
|
13
|
* |
|
14
|
* - Services — applied across the whole tree. These rules only report on |
|
15
|
* classes extending `MedusaService`, so a broad scope is safe and needs no |
|
16
|
* per-folder configuration. |
|
17
|
* - Module definition (`module-name-snake-case`) — scoped to the entry file |
|
18
|
* `src/index.{ts,js}`, where `Module(...)` is called. |
|
19
|
* - Data models — scoped to model directories, where `model.define(...)` lives. |
|
20
|
* - `loader-must-be-exported-in-module-definition` inspects loader files (not |
|
21
|
* the entry file), so it stays in the broad block alongside the service |
|
22
|
* rules; it is self-guarding to loader paths. |
|
23
|
* |
|
24
|
* Each scoped block lists both a `src`-anchored pattern (for linting from a |
|
25
|
* module package root) and a recursive-glob-anchored pattern (for when the |
|
26
|
* package is nested, e.g. linted from a monorepo root). |
|
27
|
* |
|
28
|
* No rule in this preset needs type information, so the parser is configured |
|
29
|
* without `parserOptions.project` — the preset is zero-config. |
|
30
|
*/ |
|
31
|
export function buildModules(plugin: unknown): Linter.Config[] { |
|
32
|
return [ |
|
33
|
ignoresBlock, |
|
34
|
pluginBlock(plugin), |
|
35
|
tsParserBlock(), |
|
36
|
{ |
|
37
|
// Services. `loader-must-be-exported-in-module-definition` is self-guarding |
|
38
|
// to loader files, so it rides along here rather than on the entry file. |
|
39
|
files: ["**/*.{ts,tsx}"], |
|
40
|
rules: { |
|
41
|
[ruleId("medusa-context-on-context-param")]: "warn", |
|
42
|
[ruleId("service-constructor-must-call-super")]: "error", |
|
43
|
[ruleId("service-methods-must-be-async")]: "error", |
|
44
|
[ruleId("use-inject-manager-on-public-methods")]: "warn", |
|
45
|
[ruleId("loader-must-be-exported-in-module-definition")]: "warn", |
|
46
|
}, |
|
47
|
}, |
|
48
|
{ |
|
49
|
// Module definition — the `Module(...)` entry file. |
|
50
|
files: ["src/index.{ts,js}", "**/src/index.{ts,js}"], |
|
51
|
rules: { |
|
52
|
[ruleId("module-name-snake-case")]: "error", |
|
53
|
}, |
|
54
|
}, |
|
55
|
{ |
|
56
|
// Data models. |
|
57
|
files: ["src/models/**/*.{ts,js}", "**/models/**/*.{ts,js}"], |
|
58
|
rules: { |
|
59
|
[ruleId("data-model-table-name-snake-case")]: "warn", |
|
60
|
[ruleId("link-no-cross-module-relationship")]: "error", |
|
61
|
[ruleId("no-reserved-default-properties-in-model")]: "error", |
|
62
|
[ruleId("primary-key-required")]: "warn", |
|
63
|
}, |
|
64
|
}, |
|
65
|
] |
|
66
|
} |