1 import {
2 cleanupFixtureWorkspaces,
3 createFixtureWorkspace,
4 createRuleTester,
5 } from "../../../test-utils"
6 import { rule } from "../rule"
7
8 afterAll(cleanupFixtureWorkspaces)
9
10 /**
11 * Writes a module on disk with a tsconfig declaring `paths` aliases, so the rule
12 * can resolve aliased imports (e.g. `@models`) the same way TypeScript does.
13 * Returns the absolute path of the model file under test.
14 */
15 const moduleWithAlias = (tsconfigPaths: Record<string, string[]>): string => {
16 const ws = createFixtureWorkspace("packages/modules/widget", [
17 {
18 rel: "tsconfig.json",
19 content: JSON.stringify({ compilerOptions: { paths: tsconfigPaths } }),
20 },
21 { rel: "src/models/widget.ts", content: "export default {}" },
22 { rel: "src/models/part.ts", content: "export default {}" },
23 ])
24 return ws.resolve("src/models/widget.ts")
25 }
26
27 // Alias `@models` resolving inside the module.
28 const aliasInModule = moduleWithAlias({ "@models": ["./src/models"] })
29 // Wildcard alias `@models/*` resolving inside the module.
30 const aliasSubpathInModule = moduleWithAlias({
31 "@models/*": ["./src/models/*"],
32 })
33 // Alias that points OUTSIDE the module — must still be flagged.
34 const aliasOutsideModule = moduleWithAlias({
35 "@external": ["../../other-module/src/models"],
36 })
37
38 const ruleTester = createRuleTester()
39
40 ruleTester.run("link-no-cross-module-relationship", rule, {
41 valid: [
42 // Same-module sibling import (relative).
43 {
44 code: `
45 import { model } from "@medusajs/framework/utils"
46 import PromotionRule from "./promotion-rule"
47 export default model.define("promotion_rule_value", {
48 promotion_rule: model.belongsTo(() => PromotionRule, { mappedBy: "values" }),
49 })
50 `,
51 },
52 // Parent-directory relative import (still same module).
53 {
54 code: `
55 import { model } from "@medusajs/framework/utils"
56 import Promotion from "../models/promotion"
57 export default model.define("campaign", {
58 promotions: model.hasMany(() => Promotion, {}),
59 })
60 `,
61 },
62 // Block body with single return + relative import.
63 {
64 code: `
65 import { model } from "@medusajs/framework/utils"
66 import Promotion from "./promotion"
67 export default model.define("campaign", {
68 promotions: model.hasMany(() => { return Promotion }, {}),
69 })
70 `,
71 },
72 // Aliased model import + relative.
73 {
74 code: `
75 import { model as m } from "@medusajs/framework/utils"
76 import Item from "./item"
77 export default m.define("order", {
78 items: m.hasMany(() => Item, {}),
79 })
80 `,
81 },
82 // Generic type arg + relative.
83 {
84 code: `
85 import { model } from "@medusajs/framework/utils"
86 import Order from "./order"
87 export default model.define("order_change", {
88 order: model.belongsTo<() => typeof Order>(() => Order, {}),
89 })
90 `,
91 },
92 // Non-framework `model` import — out of scope.
93 {
94 code: `
95 import { model } from "some-other-lib"
96 import Other from "@medusajs/other/models/other"
97 export default model.define("x", {
98 other: model.belongsTo(() => Other, {}),
99 })
100 `,
101 },
102 // Non-relationship method — ignored.
103 {
104 code: `
105 import { model } from "@medusajs/framework/utils"
106 export default model.define("x", {
107 name: model.text(),
108 })
109 `,
110 },
111 // Relative import that stays inside the module (sibling models/ dir).
112 {
113 filename: "/repo/src/modules/company/models/company.ts",
114 code: `
115 import { model } from "@medusajs/framework/utils"
116 import Office from "./office"
117 export default model.define("company", {
118 offices: model.hasMany(() => Office, {}),
119 })
120 `,
121 },
122 // Same-module sibling import with Windows path separators.
123 {
124 filename: "C:\\repo\\src\\modules\\company\\models\\company.ts",
125 code: `
126 import { model } from "@medusajs/framework/utils"
127 import Office from "./office"
128 export default model.define("company", {
129 offices: model.hasMany(() => Office, {}),
130 })
131 `,
132 },
133 // Relative import via parent dir, still inside the module.
134 {
135 filename: "/repo/src/modules/company/models/nested/info.ts",
136 code: `
137 import { model } from "@medusajs/framework/utils"
138 import Company from "../../company"
139 export default model.define("info", {
140 company: model.belongsTo(() => Company, {}),
141 })
142 `,
143 },
144 // Same-file declaration (not imported) — same module by definition.
145 {
146 code: `
147 import { model } from "@medusajs/framework/utils"
148 const Sibling = model.define("sibling", {})
149 const Parent = model.define("parent", {
150 sibling: model.belongsTo(() => Sibling, {}),
151 })
152 export default Parent
153 `,
154 },
155 // Path alias `@models` resolved via tsconfig — inside the same module.
156 {
157 filename: aliasInModule,
158 code: `
159 import { model } from "@medusajs/framework/utils"
160 import { Part } from "@models"
161 export default model.define("widget", {
162 parts: model.hasMany(() => Part, { mappedBy: "widget" }),
163 })
164 `,
165 },
166 // Wildcard alias `@models/*` resolved via tsconfig — inside the module.
167 {
168 filename: aliasSubpathInModule,
169 code: `
170 import { model } from "@medusajs/framework/utils"
171 import Part from "@models/part"
172 export default model.define("widget", {
173 parts: model.hasMany(() => Part, { mappedBy: "widget" }),
174 })
175 `,
176 },
177 ],
178 invalid: [
179 // belongsTo across modules.
180 {
181 code: `
182 import { model } from "@medusajs/framework/utils"
183 import Product from "@medusajs/medusa/product/models/product"
184 export default model.define("blog_post", {
185 product: model.belongsTo(() => Product, {}),
186 })
187 `,
188 errors: [
189 {
190 messageId: "crossModuleRelationship",
191 data: {
192 method: "belongsTo",
193 name: "Product",
194 source: "@medusajs/medusa/product/models/product",
195 },
196 },
197 ],
198 },
199 // hasMany across modules.
200 {
201 code: `
202 import { model } from "@medusajs/framework/utils"
203 import { Customer } from "@medusajs/customer"
204 export default model.define("loyalty", {
205 customers: model.hasMany(() => Customer, {}),
206 })
207 `,
208 errors: [{ messageId: "crossModuleRelationship" }],
209 },
210 // hasOne across modules.
211 {
212 code: `
213 import { model } from "@medusajs/framework/utils"
214 import Profile from "third-party-pkg/profile"
215 export default model.define("user", {
216 profile: model.hasOne(() => Profile, {}),
217 })
218 `,
219 errors: [{ messageId: "crossModuleRelationship" }],
220 },
221 // manyToMany across modules.
222 {
223 code: `
224 import { model } from "@medusajs/framework/utils"
225 import Tag from "@scope/tags"
226 export default model.define("post", {
227 tags: model.manyToMany(() => Tag, {}),
228 })
229 `,
230 errors: [{ messageId: "crossModuleRelationship" }],
231 },
232 // Block body with single return targeting an external import.
233 {
234 code: `
235 import { model } from "@medusajs/framework/utils"
236 import Other from "@medusajs/other"
237 export default model.define("x", {
238 other: model.belongsTo(() => { return Other }, {}),
239 })
240 `,
241 errors: [{ messageId: "crossModuleRelationship" }],
242 },
243 // Aliased model import.
244 {
245 code: `
246 import { model as m } from "@medusajs/framework/utils"
247 import Foreign from "@medusajs/other/models/foreign"
248 export default m.define("x", {
249 foreign: m.belongsTo(() => Foreign, {}),
250 })
251 `,
252 errors: [{ messageId: "crossModuleRelationship" }],
253 },
254 // Relative import escapes the current module via `../../<other-module>/`.
255 {
256 filename: "/repo/src/modules/company/models/country-company-info.ts",
257 code: `
258 import { model } from "@medusajs/framework/utils"
259 import Post from "../../blog/models/post"
260 export default model.define("country_company_info", {
261 post: model.hasOne(() => Post, {}),
262 })
263 `,
264 errors: [
265 {
266 messageId: "crossModuleRelationship",
267 data: {
268 method: "hasOne",
269 name: "Post",
270 source: "../../blog/models/post",
271 },
272 },
273 ],
274 },
275 // Relative import escapes via `../../../<other-module>/`.
276 {
277 filename: "/repo/src/modules/company/models/nested/info.ts",
278 code: `
279 import { model } from "@medusajs/framework/utils"
280 import Post from "../../../blog/models/post"
281 export default model.define("info", {
282 post: model.belongsTo(() => Post, {}),
283 })
284 `,
285 errors: [
286 {
287 messageId: "crossModuleRelationship",
288 data: {
289 method: "belongsTo",
290 name: "Post",
291 source: "../../../blog/models/post",
292 },
293 },
294 ],
295 },
296 // Unresolvable identifier (no binding in scope) — can't verify same-module.
297 {
298 code: `
299 import { model } from "@medusajs/framework/utils"
300 export default model.define("x", {
301 mystery: model.belongsTo(() => Mystery, {}),
302 })
303 `,
304 errors: [
305 { messageId: "unresolvableTarget", data: { method: "belongsTo" } },
306 ],
307 },
308 // Non-identifier return (member chain) — can't verify same-module.
309 {
310 code: `
311 import { model } from "@medusajs/framework/utils"
312 import Other from "@medusajs/other"
313 export default model.define("x", {
314 ref: model.belongsTo(() => Other.foo, {}),
315 })
316 `,
317 errors: [
318 { messageId: "unresolvableTarget", data: { method: "belongsTo" } },
319 ],
320 },
321 // Path alias resolves to a directory OUTSIDE the module — cross-module.
322 {
323 filename: aliasOutsideModule,
324 code: `
325 import { model } from "@medusajs/framework/utils"
326 import { Post } from "@external"
327 export default model.define("widget", {
328 post: model.belongsTo(() => Post, {}),
329 })
330 `,
331 errors: [
332 {
333 messageId: "crossModuleRelationship",
334 data: {
335 method: "belongsTo",
336 name: "Post",
337 source: "@external",
338 },
339 },
340 ],
341 },
342 // Multiple relationships, only one is cross-module.
343 {
344 code: `
345 import { model } from "@medusajs/framework/utils"
346 import Sibling from "./sibling"
347 import Foreign from "@medusajs/other/models/foreign"
348 export default model.define("x", {
349 sibling: model.belongsTo(() => Sibling, {}),
350 foreign: model.belongsTo(() => Foreign, {}),
351 })
352 `,
353 errors: [
354 {
355 messageId: "crossModuleRelationship",
356 data: {
357 method: "belongsTo",
358 name: "Foreign",
359 source: "@medusajs/other/models/foreign",
360 },
361 },
362 ],
363 },
364 ],
365 })