1 import { createRuleTester } from "../../../test-utils"
2 import { rule } from "../rule"
3
4 const ruleTester = createRuleTester()
5
6 ruleTester.run("data-model-table-name-snake-case", rule, {
7 valid: [
8 // snake_case name
9 {
10 code: `
11 import { model } from "@medusajs/framework/utils"
12 export const Brand = model.define("brand", { id: model.id().primaryKey() })
13 `,
14 },
15 // Multi-word snake_case.
16 {
17 code: `
18 import { model } from "@medusajs/framework/utils"
19 export const Brand = model.define("product_media", { id: model.id().primaryKey() })
20 `,
21 },
22 // Digits allowed (not at start).
23 {
24 code: `
25 import { model } from "@medusajs/framework/utils"
26 export const M = model.define("brand_v2", { id: model.id().primaryKey() })
27 `,
28 },
29 // Identifier referencing a const snake_case string — resolved and accepted.
30 {
31 code: `
32 import { model } from "@medusajs/framework/utils"
33 const NAME = "brand"
34 export const Brand = model.define(NAME, { id: model.id().primaryKey() })
35 `,
36 },
37 // Identifier whose init is non-literal — cannot resolve, skip.
38 {
39 code: `
40 import { model } from "@medusajs/framework/utils"
41 const NAME = getName()
42 export const Brand = model.define(NAME, {})
43 `,
44 },
45 // \`model\` imported from somewhere else — should not flag.
46 {
47 code: `
48 import { model } from "some-other-lib"
49 export const Brand = model.define("BadName", {})
50 `,
51 },
52 // Aliased import — unaliased identifier should not match.
53 {
54 code: `
55 import { model as m } from "@medusajs/framework/utils"
56 export const Brand = m.define("brand", {})
57 `,
58 },
59 // Method call on `model` for something other than `define` should not flag.
60 {
61 code: `
62 import { model } from "@medusajs/framework/utils"
63 const col = model.text()
64 `,
65 },
66 ],
67 invalid: [
68 // Dash-separated — autofix to snake_case.
69 {
70 code: `
71 import { model } from "@medusajs/framework/utils"
72 export const Brand = model.define("my-brand", {})
73 `,
74 errors: [{ messageId: "invalidTableName" }],
75 output: `
76 import { model } from "@medusajs/framework/utils"
77 export const Brand = model.define("my_brand", {})
78 `,
79 },
80 // camelCase — autofix.
81 {
82 code: `
83 import { model } from "@medusajs/framework/utils"
84 export const ProductMedia = model.define("productMedia", {})
85 `,
86 errors: [{ messageId: "invalidTableName" }],
87 output: `
88 import { model } from "@medusajs/framework/utils"
89 export const ProductMedia = model.define("product_media", {})
90 `,
91 },
92 // PascalCase — autofix.
93 {
94 code: `
95 import { model } from "@medusajs/framework/utils"
96 export const Brand = model.define("BrandModel", {})
97 `,
98 errors: [{ messageId: "invalidTableName" }],
99 output: `
100 import { model } from "@medusajs/framework/utils"
101 export const Brand = model.define("brand_model", {})
102 `,
103 },
104 // Consecutive uppercase — handles XML-style.
105 {
106 code: `
107 import { model } from "@medusajs/framework/utils"
108 export const X = model.define("myXMLParser", {})
109 `,
110 errors: [{ messageId: "invalidTableName" }],
111 output: `
112 import { model } from "@medusajs/framework/utils"
113 export const X = model.define("my_xml_parser", {})
114 `,
115 },
116 // Uppercase — autofix.
117 {
118 code: `
119 import { model } from "@medusajs/framework/utils"
120 export const Brand = model.define("BRAND", {})
121 `,
122 errors: [{ messageId: "invalidTableName" }],
123 output: `
124 import { model } from "@medusajs/framework/utils"
125 export const Brand = model.define("brand", {})
126 `,
127 },
128 // Aliased model import binding.
129 {
130 code: `
131 import { model as m } from "@medusajs/framework/utils"
132 export const Brand = m.define("my-brand", {})
133 `,
134 errors: [{ messageId: "invalidTableName" }],
135 output: `
136 import { model as m } from "@medusajs/framework/utils"
137 export const Brand = m.define("my_brand", {})
138 `,
139 },
140 // Identifier referencing a const string with an invalid value — flagged on
141 // the identifier (no autofix; only literals are rewritten).
142 {
143 code: `
144 import { model } from "@medusajs/framework/utils"
145 const NAME = "my-brand"
146 export const Brand = model.define(NAME, {})
147 `,
148 errors: [{ messageId: "invalidTableName" }],
149 },
150 // Multiple model.define calls — each flagged.
151 {
152 code: `
153 import { model } from "@medusajs/framework/utils"
154 model.define("bad-one", {})
155 model.define("bad.two", {})
156 `,
157 errors: [
158 { messageId: "invalidTableName" },
159 { messageId: "invalidTableName" },
160 ],
161 output: `
162 import { model } from "@medusajs/framework/utils"
163 model.define("bad_one", {})
164 model.define("bad_two", {})
165 `,
166 },
167 // Single quotes preserved by autofix.
168 {
169 code: `
170 import { model } from "@medusajs/framework/utils"
171 model.define('my-brand', {})
172 `,
173 errors: [{ messageId: "invalidTableName" }],
174 output: `
175 import { model } from "@medusajs/framework/utils"
176 model.define('my_brand', {})
177 `,
178 },
179 ],
180 })