1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("service-constructor-must-call-super", rule, {
7 valid: [
8 // Constructor with super(...arguments).
9 {
10 code: `
11 import { MedusaService } from "@medusajs/framework/utils"
12 class FooService extends MedusaService({}) {
13 constructor() {
14 super(...arguments)
15 }
16 }
17 `,
18 },
19 // Constructor with a bare super() call also satisfies the rule.
20 {
21 code: `
22 import { MedusaService } from "@medusajs/framework/utils"
23 class FooService extends MedusaService({}) {
24 constructor(container) {
25 super(container)
26 }
27 }
28 `,
29 },
30 // No constructor defined — nothing to flag.
31 {
32 code: `
33 import { MedusaService } from "@medusajs/framework/utils"
34 class FooService extends MedusaService({}) {
35 async create() {}
36 }
37 `,
38 },
39 // Class doesn't extend MedusaService — out of scope (even with *Service name).
40 {
41 code: `
42 class FooService {
43 constructor() {}
44 }
45 `,
46 },
47 // MedusaService imported from a non-framework source is ignored.
48 {
49 code: `
50 import { MedusaService } from "some-other-lib"
51 class FooService extends MedusaService({}) {
52 constructor() {}
53 }
54 `,
55 },
56 // Class extends something other than MedusaService.
57 {
58 code: `
59 import { MedusaService } from "@medusajs/framework/utils"
60 class FooService extends SomethingElse {
61 constructor() {}
62 }
63 `,
64 },
65 // Honors aliased MedusaService import.
66 {
67 code: `
68 import { MedusaService as MS } from "@medusajs/framework/utils"
69 class FooService extends MS({}) {
70 constructor() {
71 super(...arguments)
72 }
73 }
74 `,
75 },
76 // super() call interleaved with other statements is still fine.
77 {
78 code: `
79 import { MedusaService } from "@medusajs/framework/utils"
80 class FooService extends MedusaService({}) {
81 constructor(container) {
82 this.foo = "bar"
83 super(container)
84 }
85 }
86 `,
87 },
88 ],
89 invalid: [
90 // Empty constructor body.
91 {
92 code: `
93 import { MedusaService } from "@medusajs/framework/utils"
94 class FooService extends MedusaService({}) {
95 constructor() {}
96 }
97 `,
98 output: `
99 import { MedusaService } from "@medusajs/framework/utils"
100 class FooService extends MedusaService({}) {
101 constructor() { super(...arguments) }
102 }
103 `,
104 errors: [{ messageId: "missingSuperCall" }],
105 },
106 // Multi-line empty body — fixer must insert with newline + indentation,
107 // not jam everything on the opening-brace line.
108 {
109 code: `
110 import { MedusaService } from "@medusajs/framework/utils"
111 class FooService extends MedusaService({}) {
112 constructor() {
113
114 }
115 }
116 `,
117 output: `
118 import { MedusaService } from "@medusajs/framework/utils"
119 class FooService extends MedusaService({}) {
120 constructor() {
121 super(...arguments)
122 }
123 }
124 `,
125 errors: [{ messageId: "missingSuperCall" }],
126 },
127 // Constructor body with statements but no super call.
128 {
129 code: `
130 import { MedusaService } from "@medusajs/framework/utils"
131 class FooService extends MedusaService({}) {
132 constructor(container) {
133 this.container = container
134 }
135 }
136 `,
137 output: `
138 import { MedusaService } from "@medusajs/framework/utils"
139 class FooService extends MedusaService({}) {
140 constructor(container) {
141 super(...arguments)
142 this.container = container
143 }
144 }
145 `,
146 errors: [{ messageId: "missingSuperCall" }],
147 },
148 // Aliased MedusaService import.
149 {
150 code: `
151 import { MedusaService as MS } from "@medusajs/framework/utils"
152 class FooService extends MS({}) {
153 constructor() {}
154 }
155 `,
156 output: `
157 import { MedusaService as MS } from "@medusajs/framework/utils"
158 class FooService extends MS({}) {
159 constructor() { super(...arguments) }
160 }
161 `,
162 errors: [{ messageId: "missingSuperCall" }],
163 },
164 // Class expression.
165 {
166 code: `
167 import { MedusaService } from "@medusajs/framework/utils"
168 const FooService = class extends MedusaService({}) {
169 constructor() {}
170 }
171 `,
172 output: `
173 import { MedusaService } from "@medusajs/framework/utils"
174 const FooService = class extends MedusaService({}) {
175 constructor() { super(...arguments) }
176 }
177 `,
178 errors: [{ messageId: "missingSuperCall" }],
179 },
180 // Constructor calls something that looks like super but isn't (e.g. a method named super-something).
181 {
182 code: `
183 import { MedusaService } from "@medusajs/framework/utils"
184 class FooService extends MedusaService({}) {
185 constructor() {
186 this.init()
187 }
188 }
189 `,
190 output: `
191 import { MedusaService } from "@medusajs/framework/utils"
192 class FooService extends MedusaService({}) {
193 constructor() {
194 super(...arguments)
195 this.init()
196 }
197 }
198 `,
199 errors: [{ messageId: "missingSuperCall" }],
200 },
201 ],
202 })