1 import medusa from "@medusajs/eslint-plugin"
2
3 const astScopedRules = [
4 "link-create-keys-modules-enum",
5 "link-uses-linkable-properties",
6 "medusa-context-on-context-param",
7 "module-name-snake-case",
8 "no-async-workflow-constructor",
9 "no-conditional-expressions-in-workflow",
10 "no-console-log-in-workflow",
11 "no-deprecated-remote-query-config",
12 "no-direct-variable-mutation-in-workflow",
13 "no-duplicate-step-id-in-workflow",
14 "no-if-in-workflow-constructor",
15 "no-loops-in-workflow",
16 "no-mikroorm-direct-import",
17 "no-nested-when-in-workflow",
18 "no-new-date-in-workflow",
19 "no-spread-in-workflow",
20 "no-throw-in-transform",
21 "no-throw-in-workflow-constructor",
22 "no-try-catch-in-workflow",
23 "no-wildcard-with-specific-fields",
24 "prefer-container-registration-keys",
25 "prefer-link-over-remote-link",
26 "prefer-modules-enum",
27 "prices-in-major-units",
28 "read-only-link-requires-field",
29 "service-constructor-must-call-super",
30 "service-methods-must-be-async",
31 "step-must-return-step-response",
32 "use-inject-manager-on-public-methods",
33 "when-block-must-have-name",
34 "workflow-must-return-workflow-response",
35 ]
36
37 const pathScopedRules = [
38 {
39 // `@medusajs/framework/*` is server-side only. Storefront and admin code
40 // are browser bundles and import from the standalone `@medusajs/types` /
41 // `@medusajs/js-sdk` packages instead, so this rule has to stay off both —
42 // the plugin's own `recommended` preset likewise excludes `src/admin/**`.
43 paths: ["**/*.{md,mdx}/**/*.{js,jsx,ts,tsx}"],
44 ignores: [
45 "**/storefront-development/**",
46 "**/nextjs-starter/**",
47 "**/storefront/page.mdx/**",
48 "**/*.{md,mdx}/**/src/admin/**",
49 "**/*.{md,mdx}/**/src/app/**",
50 "**/*.{md,mdx}/**/src/lib/**",
51 "**/*.{md,mdx}/**/src/types/**",
52 "**/*.{md,mdx}/**/src/modules/**/components/**",
53 "**/*.{md,mdx}/**/src/modules/**/templates/**",
54 ],
55 rules: ["import-from-framework-not-internal"],
56 },
57 {
58 paths: ["**/*.{md,mdx}/**/src/**/*.{ts,js}"],
59 ignores: ["**/*.{md,mdx}/**/src/admin/**"],
60 rules: ["zod-import-source"],
61 },
62 {
63 // Server-side code only. The docs also carry Next.js storefront examples
64 // under `src/lib/**` and `src/modules/*/components/**`, where a plain
65 // `Error` is correct and `MedusaError` is not available.
66 paths: [
67 "**/*.{md,mdx}/**/src/api/**/*.{ts,tsx}",
68 "**/*.{md,mdx}/**/src/workflows/**/*.{ts,js}",
69 "**/*.{md,mdx}/**/src/subscribers/**/*.{ts,js}",
70 "**/*.{md,mdx}/**/src/jobs/**/*.{ts,js}",
71 "**/*.{md,mdx}/**/src/links/**/*.{ts,js}",
72 "**/*.{md,mdx}/**/src/modules/**/service.{ts,js}",
73 "**/*.{md,mdx}/**/src/modules/**/loaders/**/*.{ts,js}",
74 ],
75 rules: ["use-medusa-error-not-generic-error"],
76 },
77 {
78 paths: ["**/*.{md,mdx}/**/src/api/**/*.{ts,tsx}"],
79 rules: [
80 "authenticate-flag-name-and-type",
81 "no-service-mutations-in-api-route",
82 "route-handler-exports-uppercase",
83 "route-params-must-be-defined",
84 "use-validated-body-or-query",
85 ],
86 },
87 {
88 paths: [
89 "**/*.{md,mdx}/**/src/api/**/*.{ts,tsx}",
90 "**/*.{md,mdx}/**/src/admin/routes/**/*.{ts,tsx}",
91 ],
92 rules: ["route-dynamic-folder-syntax"],
93 },
94 {
95 paths: [
96 "**/*.{md,mdx}/**/middleware.{ts,js}",
97 "**/*.{md,mdx}/**/middlewares.{ts,js}",
98 ],
99 rules: ["middleware-must-call-next", "no-trailing-slash-in-route-matcher"],
100 },
101 {
102 // Data models. A page explaining one property type shows a `model.define`
103 // with just that property, so the model-shape rules only make sense where
104 // the block claims to be a real model file.
105 paths: ["**/*.{md,mdx}/**/src/modules/**/models/**/*.{ts,js}"],
106 rules: [
107 "data-model-table-name-snake-case",
108 "link-no-cross-module-relationship",
109 "no-reserved-default-properties-in-model",
110 "primary-key-required",
111 ],
112 },
113 {
114 paths: ["**/*.{md,mdx}/**/src/links/**/*.{ts,tsx,js,mjs,cjs}"],
115 rules: ["link-uses-linkable-properties", "read-only-link-requires-field"],
116 },
117 {
118 paths: ["**/*.{md,mdx}/**/src/subscribers/*.{ts,js}"],
119 rules: [
120 "prefer-workflow-event-over-module-event",
121 "subscriber-default-export-must-be-async",
122 ],
123 },
124 {
125 paths: ["**/*.{md,mdx}/**/src/jobs/*.{ts,js}"],
126 rules: [
127 "scheduled-job-default-export-async",
128 "scheduled-job-schedule-valid-cron",
129 ],
130 },
131 {
132 paths: ["**/*.{md,mdx}/**/src/admin/**/*.{ts,tsx,js,jsx}"],
133 rules: ["admin-env-vars-import-meta", "admin-no-medusa-utils-import"],
134 },
135 {
136 paths: [
137 "**/*.{md,mdx}/**/src/admin/widgets/**/*.{tsx,jsx}",
138 "**/*.{md,mdx}/**/src/admin/routes/**/page.{tsx,jsx}",
139 ],
140 rules: [
141 "admin-component-must-be-arrow-function",
142 "widget-zone-must-be-string-literal",
143 ],
144 },
145 {
146 paths: ["**/*.{md,mdx}/**/src/admin/routes/**/*.{tsx,jsx}"],
147 rules: ["ui-route-file-name-page-tsx"],
148 },
149 {
150 paths: ["**/*.{md,mdx}/**/src/admin/routes/**/page.{tsx,jsx}"],
151 rules: [
152 "no-config-on-dynamic-ui-route",
153 "ui-route-config-via-define-route-config",
154 ],
155 },
156 ]
157
158 const withoutFixer = (rule) => ({
159 ...rule,
160 meta: { ...rule.meta, fixable: undefined, hasSuggestions: false },
161 create(context) {
162 const proxy = Object.create(context, {
163 report: {
164 value: (descriptor) => {
165 const { fix, suggest, ...rest } = descriptor
166 context.report(rest)
167 },
168 },
169 })
170 return rule.create(proxy)
171 },
172 })
173
174 const usedRules = new Set([
175 ...astScopedRules,
176 ...pathScopedRules.flatMap(({ rules }) => rules),
177 ])
178
179 const plugin = {
180 ...medusa,
181 rules: Object.fromEntries(
182 [...usedRules].map((name) => [name, withoutFixer(medusa.rules[name])])
183 ),
184 }
185
186 const asWarnings = (rules) =>
187 Object.fromEntries(rules.map((name) => [`@medusajs/${name}`, "error"]))
188
189 export const medusaContentConfigs = [
190 {
191 files: ["**/*.{md,mdx}/**/*.{js,jsx,ts,tsx}"],
192 plugins: { "@medusajs": plugin },
193 rules: asWarnings(astScopedRules),
194 },
195 ...pathScopedRules.map(({ paths, ignores, rules }) => ({
196 files: paths,
197 ...(ignores ? { ignores } : {}),
198 rules: asWarnings(rules),
199 })),
200 ]
201
202 export default medusaContentConfigs