@starci/eslint-canon-be 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/data-access.mjs CHANGED
@@ -1,307 +1,306 @@
1
- /**
2
- * The rules that hold `data-access.md`.
3
- *
4
- * The first three read a constructor parameter or a decorator, all exact - there is no heuristic
5
- * here and no judgement to get wrong, which is why all three can be switched on without a burn-down
6
- * in a codebase that already follows them.
7
- *
8
- * The next two hold a narrower, exact slice of DATA-4 (Rule 5) and DATA-5 (Rule 6) - the slice of
9
- * each that a single file settles on its own, with no call graph and no knowledge of intent needed.
10
- *
11
- * `no-outer-manager-in-transaction` catches the one shape of DATA-4 a parser can see: a
12
- * `this.<field>.transaction(async (tx) => ...)` callback whose body reaches for `this.<field>`
13
- * again instead of `tx`. That field held the manager from BEFORE the transaction opened, so a call
14
- * through it runs and commits on its own - the exact "half of it was written" bug DATA-4 names. What
15
- * it does NOT see, and cannot: whether a HELPER defined in another file was handed `tx` by ITS
16
- * caller. That still needs the call graph and stays undecidable, same as the law says.
17
- *
18
- * `no-eager-relation` catches the one shape of DATA-5 a parser can see: `eager: true` written on a
19
- * relation decorator. What it does NOT see: whether a call site correctly asked for a relation it
20
- * needed. That still needs to know what the answer is for and stays undecidable, same as the law
21
- * says.
22
- *
23
- * Both remaining gaps are read by a person; a rule that guessed at either would fire on correct
24
- * code, which is how a correct rule gets disabled.
25
- */
26
-
27
- /** Forward-slash form of a filename, so Windows paths compare like every other path. */
28
- const normalizePath = (filename) => String(filename || "").replace(/\\/g, "/")
29
-
30
- /** A parameter property (`private readonly x: T`) wraps the parameter it declares. */
31
- const unwrapParam = (param) => (param.type === "TSParameterProperty" ? param.parameter : param)
32
-
33
- /** The type name a parameter is annotated with, or null. */
34
- const paramTypeName = (param) => {
35
- const annotation = param.typeAnnotation && param.typeAnnotation.typeAnnotation
36
- if (!annotation || annotation.type !== "TSTypeReference" || annotation.typeName.type !== "Identifier") return null
37
- return annotation.typeName.name
38
- }
39
-
40
- /** Decorator names on a parameter, including ones carried by the parameter property wrapping it. */
41
- const paramDecorators = (original) => {
42
- const carriers = [original]
43
- if (original.type === "TSParameterProperty") carriers.push(original.parameter)
44
- const names = []
45
- for (const carrier of carriers) {
46
- for (const decorator of carrier.decorators || []) {
47
- const expression = decorator.expression
48
- if (expression.type === "CallExpression" && expression.callee.type === "Identifier") {
49
- names.push(expression.callee.name)
50
- } else if (expression.type === "Identifier") {
51
- names.push(expression.name)
52
- }
53
- }
54
- }
55
- return names
56
- }
57
-
58
- /** Every parameter of a constructor definition. */
59
- const constructorParams = (node) =>
60
- node.kind === "constructor" && node.value && node.value.params ? node.value.params : []
61
-
62
- /** Repository types that bind a handle to one entity. */
63
- const REPOSITORY_TYPES = /^(?:Repository|TreeRepository|MongoRepository)$/
64
-
65
- /** The house decorator family that names a datasource. */
66
- const NAMES_DATASOURCE = /^Inject\w*EntityManager$/
67
-
68
- // -- DATA-1 ----------------------------------------------------------------------------------------
69
-
70
- /** An injected manager names the datasource it belongs to. */
71
- export const mustInjectEntityManager = {
72
- meta: {
73
- type: "problem",
74
- docs: { description: "An injected `EntityManager` names its datasource through a decorator." },
75
- schema: [],
76
- messages: {
77
- undecorated:
78
- "`EntityManager` is injected without an `@Inject*EntityManager()` decorator. The type alone does not say WHICH datasource this is, and this application has more than one - so this reads correctly while pointing at the wrong database. Name the connection at the injection site.",
79
- },
80
- },
81
- create(context) {
82
- return {
83
- MethodDefinition(node) {
84
- for (const original of constructorParams(node)) {
85
- const param = unwrapParam(original)
86
- if (paramTypeName(param) !== "EntityManager") continue
87
- if (paramDecorators(original).some((name) => NAMES_DATASOURCE.test(name))) continue
88
- context.report({ node: param, messageId: "undecorated" })
89
- }
90
- },
91
- }
92
- },
93
- }
94
-
95
- // -- DATA-2 ----------------------------------------------------------------------------------------
96
-
97
- /** Persistence never arrives as a repository. */
98
- export const noInjectedRepository = {
99
- meta: {
100
- type: "problem",
101
- docs: { description: "Persistence goes through `EntityManager`, never an injected repository." },
102
- schema: [],
103
- messages: {
104
- repo:
105
- "Inject `EntityManager` instead of a repository. A repository is bound to ONE entity, so it cannot carry a transaction into a second table - and a use case that grows a second write then has to be rewritten rather than extended.",
106
- },
107
- },
108
- create(context) {
109
- return {
110
- MethodDefinition(node) {
111
- for (const original of constructorParams(node)) {
112
- const param = unwrapParam(original)
113
- const byDecorator = paramDecorators(original).includes("InjectRepository")
114
- const byType = REPOSITORY_TYPES.test(paramTypeName(param) || "")
115
- if (byDecorator || byType) context.report({ node: param, messageId: "repo" })
116
- }
117
- },
118
- }
119
- },
120
- }
121
-
122
- // -- DATA-3 ----------------------------------------------------------------------------------------
123
-
124
- /** An entity names its table, so a class rename cannot become a dropped table. */
125
- export const requireEntityTableName = {
126
- meta: {
127
- type: "problem",
128
- docs: { description: "`@Entity()` names its table explicitly." },
129
- schema: [],
130
- messages: {
131
- inferred:
132
- "`@Entity()` here lets the ORM infer the table name from the class name, so renaming the class renames the table - which `synchronize` performs as a DROP and CREATE rather than a migration. A class rename is a refactor; a dropped table is an outage. Name the table.",
133
- },
134
- },
135
- create(context) {
136
- /** Both `@Entity("t")` and `@Entity({ name: "t" })` name the table. */
137
- const isTableName = (argument) =>
138
- (argument.type === "Literal" && typeof argument.value === "string") || argument.type === "TemplateLiteral"
139
- return {
140
- Decorator(node) {
141
- const expression = node.expression
142
- if (expression.type !== "CallExpression") return
143
- if (expression.callee.type !== "Identifier" || expression.callee.name !== "Entity") return
144
- // The options form is not a stylistic variant to discourage: it is the ONLY form that can
145
- // also carry a schema qualifier, so rejecting it would push an author to delete the schema
146
- // to satisfy the rule - a worse outcome than the inferred name this exists to prevent.
147
- const named = expression.arguments.some(
148
- (argument) =>
149
- isTableName(argument)
150
- || (argument.type === "ObjectExpression"
151
- && argument.properties.some(
152
- (property) =>
153
- property.type === "Property"
154
- && !property.computed
155
- && (property.key.name === "name" || property.key.value === "name")
156
- && isTableName(property.value),
157
- )),
158
- )
159
- if (!named) context.report({ node, messageId: "inferred" })
160
- },
161
- }
162
- },
163
- }
164
-
165
- // -- DATA-4 (Rule 5) --------------------------------------------------------------------------------
166
-
167
- /** Node types that rebind `this` - a `this.<field>` read past one of these belongs to a different call. */
168
- const REBINDS_THIS = /^(?:FunctionDeclaration|FunctionExpression|ClassDeclaration|ClassExpression)$/
169
-
170
- /** Walk every descendant of `node` that still runs under the SAME `this`, calling `visit` on each. */
171
- const walkPreservingThis = (node, visit) => {
172
- if (!node || typeof node.type !== "string") return
173
- visit(node)
174
- for (const key of Object.keys(node)) {
175
- if (key === "parent") continue
176
- const value = node[key]
177
- if (Array.isArray(value)) {
178
- for (const child of value) {
179
- if (child && typeof child.type === "string" && !REBINDS_THIS.test(child.type)) walkPreservingThis(child, visit)
180
- }
181
- } else if (value && typeof value.type === "string" && !REBINDS_THIS.test(value.type)) {
182
- walkPreservingThis(value, visit)
183
- }
184
- }
185
- }
186
-
187
- /** Everything inside a transaction receives the transactional manager, not the outer injected one. */
188
- export const noOuterManagerInTransaction = {
189
- meta: {
190
- type: "problem",
191
- docs: {
192
- description:
193
- "Inside `this.<field>.transaction(async (tx) => ...)`, every call uses `tx` - never `this.<field>` again (data-access.md DATA-4, Rule 5).",
194
- },
195
- schema: [],
196
- messages: {
197
- outerManager:
198
- "This is inside `this.{{field}}.transaction(...)`, but it reaches for `this.{{field}}` again instead of `{{param}}`, the manager the callback received. `this.{{field}}` is the manager from BEFORE the transaction opened - a call through it runs and commits on its own, which is how half a write survives a rollback. Use `{{param}}`.",
199
- },
200
- },
201
- create(context) {
202
- return {
203
- CallExpression(node) {
204
- const callee = node.callee
205
- if (callee.type !== "MemberExpression" || callee.computed) return
206
- if (callee.property.type !== "Identifier" || callee.property.name !== "transaction") return
207
- const object = callee.object
208
- if (object.type !== "MemberExpression" || object.computed) return
209
- if (object.object.type !== "ThisExpression") return
210
- if (object.property.type !== "Identifier") return
211
- const field = object.property.name
212
- // Only the arrow-function callback is covered. A plain `function (tx) {}` callback does not
213
- // inherit `this` from the enclosing method at all - TypeORM invokes it with no receiver, so
214
- // `this.<field>` inside one throws rather than silently escaping the transaction, and this
215
- // rule exists for the silent case.
216
- const callback = node.arguments[0]
217
- if (!callback || callback.type !== "ArrowFunctionExpression") return
218
- const param = callback.params[0]
219
- const paramName = param && param.type === "Identifier" ? param.name : "the transactional manager"
220
- walkPreservingThis(callback.body, (descendant) => {
221
- if (
222
- descendant.type === "MemberExpression"
223
- && !descendant.computed
224
- && descendant.object.type === "ThisExpression"
225
- && descendant.property.type === "Identifier"
226
- && descendant.property.name === field
227
- ) {
228
- context.report({ node: descendant, messageId: "outerManager", data: { field, param: paramName } })
229
- }
230
- })
231
- },
232
- }
233
- },
234
- }
235
-
236
- // -- DATA-5 (Rule 6) --------------------------------------------------------------------------------
237
-
238
- /** Relation decorators whose `eager` option can grant the relation to every caller. */
239
- const RELATION_DECORATORS = /^(?:ManyToOne|OneToOne|OneToMany|ManyToMany)$/
240
-
241
- /** A relation is asked for by the call site that needs it; the entity grants no relation eagerly. */
242
- export const noEagerRelation = {
243
- meta: {
244
- type: "problem",
245
- docs: {
246
- description: "A relation decorator carries no `eager: true` (data-access.md DATA-5, Rule 6).",
247
- },
248
- schema: [],
249
- messages: {
250
- eager:
251
- "`@{{decorator}}(..., { eager: true })` grants this relation to every caller whether it asked for it or not, so a query that wants one column now pays for the whole tree. State the relation in the `relations` the call site writes, and drop `eager` here.",
252
- },
253
- },
254
- create(context) {
255
- return {
256
- Decorator(node) {
257
- const expression = node.expression
258
- if (expression.type !== "CallExpression") return
259
- if (expression.callee.type !== "Identifier" || !RELATION_DECORATORS.test(expression.callee.name)) return
260
- for (const argument of expression.arguments) {
261
- if (argument.type !== "ObjectExpression") continue
262
- const eagerProperty = argument.properties.find(
263
- (property) =>
264
- property.type === "Property"
265
- && !property.computed
266
- && (property.key.name === "eager" || property.key.value === "eager"),
267
- )
268
- if (!eagerProperty) continue
269
- if (eagerProperty.value.type === "Literal" && eagerProperty.value.value === true) {
270
- context.report({ node, messageId: "eager", data: { decorator: expression.callee.name } })
271
- }
272
- }
273
- },
274
- }
275
- },
276
- }
277
-
278
- /** The rules this law contributes to the plugin. */
279
- export const rules = {
280
- "must-inject-entity-manager": mustInjectEntityManager,
281
- "no-injected-repository": noInjectedRepository,
282
- "require-entity-table-name": requireEntityTableName,
283
- "no-outer-manager-in-transaction": noOuterManagerInTransaction,
284
- "no-eager-relation": noEagerRelation,
285
- }
286
-
287
- /**
288
- * The level this law asks for, as the plugin's own opinion.
289
- *
290
- * All five measured against the reference repository (`src/**`) before shipping. The original three
291
- * measured at zero debt and ship at `error`. `no-outer-manager-in-transaction` also measured zero -
292
- * every `.transaction(async (tx) => ...)` callback in the reference repository already uses only the
293
- * parameter it was handed - and ships at `error`. `no-eager-relation` also measured zero - no entity
294
- * in the reference repository declares `eager: true` - and ships at `error` as well; a repository
295
- * adopting these into a tree that is not already clean measures first and lands anything above zero
296
- * at `warn` with the count beside it.
297
- */
298
- export const recommended = {
299
- "starci-be/must-inject-entity-manager": "error",
300
- "starci-be/no-injected-repository": "error",
301
- "starci-be/require-entity-table-name": "error",
302
- "starci-be/no-outer-manager-in-transaction": "error",
303
- "starci-be/no-eager-relation": "error",
304
- }
305
-
306
- /** Path helper shared with the other backend law modules. */
307
- export { normalizePath }
1
+ /**
2
+ * The rules that hold `data-access.md`.
3
+ *
4
+ * The first three read a constructor parameter or a decorator, all exact - there is no heuristic
5
+ * here and no judgement to get wrong, which is why all three can be switched on without a burn-down
6
+ * in a codebase that already follows them.
7
+ *
8
+ * The next two hold a narrower, exact slice of DATA-4 (Rule 5) and DATA-5 (Rule 6) - the slice of
9
+ * each that a single file settles on its own, with no call graph and no knowledge of intent needed.
10
+ *
11
+ * `no-outer-manager-in-transaction` catches the one shape of DATA-4 a parser can see: a
12
+ * `this.<field>.transaction(async (tx) => ...)` callback whose body reaches for `this.<field>`
13
+ * again instead of `tx`. That field held the manager from BEFORE the transaction opened, so a call
14
+ * through it runs and commits on its own - the exact "half of it was written" bug DATA-4 names. What
15
+ * it does NOT see, and cannot: whether a HELPER defined in another file was handed `tx` by ITS
16
+ * caller. That still needs the call graph and stays undecidable, same as the law says.
17
+ *
18
+ * `no-eager-relation` catches the one shape of DATA-5 a parser can see: `eager: true` written on a
19
+ * relation decorator. What it does NOT see: whether a call site correctly asked for a relation it
20
+ * needed. That still needs to know what the answer is for and stays undecidable, same as the law
21
+ * says.
22
+ *
23
+ * Both remaining gaps are read by a person; a rule that guessed at either would fire on correct
24
+ * code, which is how a correct rule gets disabled.
25
+ */
26
+
27
+ import { normalizePath } from "./lib/path.mjs"
28
+
29
+ /** A parameter property (`private readonly x: T`) wraps the parameter it declares. */
30
+ const unwrapParam = (param) => (param.type === "TSParameterProperty" ? param.parameter : param)
31
+
32
+ /** The type name a parameter is annotated with, or null. */
33
+ const paramTypeName = (param) => {
34
+ const annotation = param.typeAnnotation && param.typeAnnotation.typeAnnotation
35
+ if (!annotation || annotation.type !== "TSTypeReference" || annotation.typeName.type !== "Identifier") return null
36
+ return annotation.typeName.name
37
+ }
38
+
39
+ /** Decorator names on a parameter, including ones carried by the parameter property wrapping it. */
40
+ const paramDecorators = (original) => {
41
+ const carriers = [original]
42
+ if (original.type === "TSParameterProperty") carriers.push(original.parameter)
43
+ const names = []
44
+ for (const carrier of carriers) {
45
+ for (const decorator of carrier.decorators || []) {
46
+ const expression = decorator.expression
47
+ if (expression.type === "CallExpression" && expression.callee.type === "Identifier") {
48
+ names.push(expression.callee.name)
49
+ } else if (expression.type === "Identifier") {
50
+ names.push(expression.name)
51
+ }
52
+ }
53
+ }
54
+ return names
55
+ }
56
+
57
+ /** Every parameter of a constructor definition. */
58
+ const constructorParams = (node) =>
59
+ node.kind === "constructor" && node.value && node.value.params ? node.value.params : []
60
+
61
+ /** Repository types that bind a handle to one entity. */
62
+ const REPOSITORY_TYPES = /^(?:Repository|TreeRepository|MongoRepository)$/
63
+
64
+ /** The house decorator family that names a datasource. */
65
+ const NAMES_DATASOURCE = /^Inject\w*EntityManager$/
66
+
67
+ // -- DATA-1 ----------------------------------------------------------------------------------------
68
+
69
+ /** An injected manager names the datasource it belongs to. */
70
+ export const mustInjectEntityManager = {
71
+ meta: {
72
+ type: "problem",
73
+ docs: { description: "An injected `EntityManager` names its datasource through a decorator." },
74
+ schema: [],
75
+ messages: {
76
+ undecorated:
77
+ "`EntityManager` is injected without an `@Inject*EntityManager()` decorator. The type alone does not say WHICH datasource this is, and this application has more than one - so this reads correctly while pointing at the wrong database. Name the connection at the injection site.",
78
+ },
79
+ },
80
+ create(context) {
81
+ return {
82
+ MethodDefinition(node) {
83
+ for (const original of constructorParams(node)) {
84
+ const param = unwrapParam(original)
85
+ if (paramTypeName(param) !== "EntityManager") continue
86
+ if (paramDecorators(original).some((name) => NAMES_DATASOURCE.test(name))) continue
87
+ context.report({ node: param, messageId: "undecorated" })
88
+ }
89
+ },
90
+ }
91
+ },
92
+ }
93
+
94
+ // -- DATA-2 ----------------------------------------------------------------------------------------
95
+
96
+ /** Persistence never arrives as a repository. */
97
+ export const noInjectedRepository = {
98
+ meta: {
99
+ type: "problem",
100
+ docs: { description: "Persistence goes through `EntityManager`, never an injected repository." },
101
+ schema: [],
102
+ messages: {
103
+ repo:
104
+ "Inject `EntityManager` instead of a repository. A repository is bound to ONE entity, so it cannot carry a transaction into a second table - and a use case that grows a second write then has to be rewritten rather than extended.",
105
+ },
106
+ },
107
+ create(context) {
108
+ return {
109
+ MethodDefinition(node) {
110
+ for (const original of constructorParams(node)) {
111
+ const param = unwrapParam(original)
112
+ const byDecorator = paramDecorators(original).includes("InjectRepository")
113
+ const byType = REPOSITORY_TYPES.test(paramTypeName(param) || "")
114
+ if (byDecorator || byType) context.report({ node: param, messageId: "repo" })
115
+ }
116
+ },
117
+ }
118
+ },
119
+ }
120
+
121
+ // -- DATA-3 ----------------------------------------------------------------------------------------
122
+
123
+ /** An entity names its table, so a class rename cannot become a dropped table. */
124
+ export const requireEntityTableName = {
125
+ meta: {
126
+ type: "problem",
127
+ docs: { description: "`@Entity()` names its table explicitly." },
128
+ schema: [],
129
+ messages: {
130
+ inferred:
131
+ "`@Entity()` here lets the ORM infer the table name from the class name, so renaming the class renames the table - which `synchronize` performs as a DROP and CREATE rather than a migration. A class rename is a refactor; a dropped table is an outage. Name the table.",
132
+ },
133
+ },
134
+ create(context) {
135
+ /** Both `@Entity("t")` and `@Entity({ name: "t" })` name the table. */
136
+ const isTableName = (argument) =>
137
+ (argument.type === "Literal" && typeof argument.value === "string") || argument.type === "TemplateLiteral"
138
+ return {
139
+ Decorator(node) {
140
+ const expression = node.expression
141
+ if (expression.type !== "CallExpression") return
142
+ if (expression.callee.type !== "Identifier" || expression.callee.name !== "Entity") return
143
+ // The options form is not a stylistic variant to discourage: it is the ONLY form that can
144
+ // also carry a schema qualifier, so rejecting it would push an author to delete the schema
145
+ // to satisfy the rule - a worse outcome than the inferred name this exists to prevent.
146
+ const named = expression.arguments.some(
147
+ (argument) =>
148
+ isTableName(argument)
149
+ || (argument.type === "ObjectExpression"
150
+ && argument.properties.some(
151
+ (property) =>
152
+ property.type === "Property"
153
+ && !property.computed
154
+ && (property.key.name === "name" || property.key.value === "name")
155
+ && isTableName(property.value),
156
+ )),
157
+ )
158
+ if (!named) context.report({ node, messageId: "inferred" })
159
+ },
160
+ }
161
+ },
162
+ }
163
+
164
+ // -- DATA-4 (Rule 5) --------------------------------------------------------------------------------
165
+
166
+ /** Node types that rebind `this` - a `this.<field>` read past one of these belongs to a different call. */
167
+ const REBINDS_THIS = /^(?:FunctionDeclaration|FunctionExpression|ClassDeclaration|ClassExpression)$/
168
+
169
+ /** Walk every descendant of `node` that still runs under the SAME `this`, calling `visit` on each. */
170
+ const walkPreservingThis = (node, visit) => {
171
+ if (!node || typeof node.type !== "string") return
172
+ visit(node)
173
+ for (const key of Object.keys(node)) {
174
+ if (key === "parent") continue
175
+ const value = node[key]
176
+ if (Array.isArray(value)) {
177
+ for (const child of value) {
178
+ if (child && typeof child.type === "string" && !REBINDS_THIS.test(child.type)) walkPreservingThis(child, visit)
179
+ }
180
+ } else if (value && typeof value.type === "string" && !REBINDS_THIS.test(value.type)) {
181
+ walkPreservingThis(value, visit)
182
+ }
183
+ }
184
+ }
185
+
186
+ /** Everything inside a transaction receives the transactional manager, not the outer injected one. */
187
+ export const noOuterManagerInTransaction = {
188
+ meta: {
189
+ type: "problem",
190
+ docs: {
191
+ description:
192
+ "Inside `this.<field>.transaction(async (tx) => ...)`, every call uses `tx` - never `this.<field>` again (data-access.md DATA-4, Rule 5).",
193
+ },
194
+ schema: [],
195
+ messages: {
196
+ outerManager:
197
+ "This is inside `this.{{field}}.transaction(...)`, but it reaches for `this.{{field}}` again instead of `{{param}}`, the manager the callback received. `this.{{field}}` is the manager from BEFORE the transaction opened - a call through it runs and commits on its own, which is how half a write survives a rollback. Use `{{param}}`.",
198
+ },
199
+ },
200
+ create(context) {
201
+ return {
202
+ CallExpression(node) {
203
+ const callee = node.callee
204
+ if (callee.type !== "MemberExpression" || callee.computed) return
205
+ if (callee.property.type !== "Identifier" || callee.property.name !== "transaction") return
206
+ const object = callee.object
207
+ if (object.type !== "MemberExpression" || object.computed) return
208
+ if (object.object.type !== "ThisExpression") return
209
+ if (object.property.type !== "Identifier") return
210
+ const field = object.property.name
211
+ // Only the arrow-function callback is covered. A plain `function (tx) {}` callback does not
212
+ // inherit `this` from the enclosing method at all - TypeORM invokes it with no receiver, so
213
+ // `this.<field>` inside one throws rather than silently escaping the transaction, and this
214
+ // rule exists for the silent case.
215
+ const callback = node.arguments[0]
216
+ if (!callback || callback.type !== "ArrowFunctionExpression") return
217
+ const param = callback.params[0]
218
+ const paramName = param && param.type === "Identifier" ? param.name : "the transactional manager"
219
+ walkPreservingThis(callback.body, (descendant) => {
220
+ if (
221
+ descendant.type === "MemberExpression"
222
+ && !descendant.computed
223
+ && descendant.object.type === "ThisExpression"
224
+ && descendant.property.type === "Identifier"
225
+ && descendant.property.name === field
226
+ ) {
227
+ context.report({ node: descendant, messageId: "outerManager", data: { field, param: paramName } })
228
+ }
229
+ })
230
+ },
231
+ }
232
+ },
233
+ }
234
+
235
+ // -- DATA-5 (Rule 6) --------------------------------------------------------------------------------
236
+
237
+ /** Relation decorators whose `eager` option can grant the relation to every caller. */
238
+ const RELATION_DECORATORS = /^(?:ManyToOne|OneToOne|OneToMany|ManyToMany)$/
239
+
240
+ /** A relation is asked for by the call site that needs it; the entity grants no relation eagerly. */
241
+ export const noEagerRelation = {
242
+ meta: {
243
+ type: "problem",
244
+ docs: {
245
+ description: "A relation decorator carries no `eager: true` (data-access.md DATA-5, Rule 6).",
246
+ },
247
+ schema: [],
248
+ messages: {
249
+ eager:
250
+ "`@{{decorator}}(..., { eager: true })` grants this relation to every caller whether it asked for it or not, so a query that wants one column now pays for the whole tree. State the relation in the `relations` the call site writes, and drop `eager` here.",
251
+ },
252
+ },
253
+ create(context) {
254
+ return {
255
+ Decorator(node) {
256
+ const expression = node.expression
257
+ if (expression.type !== "CallExpression") return
258
+ if (expression.callee.type !== "Identifier" || !RELATION_DECORATORS.test(expression.callee.name)) return
259
+ for (const argument of expression.arguments) {
260
+ if (argument.type !== "ObjectExpression") continue
261
+ const eagerProperty = argument.properties.find(
262
+ (property) =>
263
+ property.type === "Property"
264
+ && !property.computed
265
+ && (property.key.name === "eager" || property.key.value === "eager"),
266
+ )
267
+ if (!eagerProperty) continue
268
+ if (eagerProperty.value.type === "Literal" && eagerProperty.value.value === true) {
269
+ context.report({ node, messageId: "eager", data: { decorator: expression.callee.name } })
270
+ }
271
+ }
272
+ },
273
+ }
274
+ },
275
+ }
276
+
277
+ /** The rules this law contributes to the plugin. */
278
+ export const rules = {
279
+ "must-inject-entity-manager": mustInjectEntityManager,
280
+ "no-injected-repository": noInjectedRepository,
281
+ "require-entity-table-name": requireEntityTableName,
282
+ "no-outer-manager-in-transaction": noOuterManagerInTransaction,
283
+ "no-eager-relation": noEagerRelation,
284
+ }
285
+
286
+ /**
287
+ * The level this law asks for, as the plugin's own opinion.
288
+ *
289
+ * All five measured against the reference repository (`src/**`) before shipping. The original three
290
+ * measured at zero debt and ship at `error`. `no-outer-manager-in-transaction` also measured zero -
291
+ * every `.transaction(async (tx) => ...)` callback in the reference repository already uses only the
292
+ * parameter it was handed - and ships at `error`. `no-eager-relation` also measured zero - no entity
293
+ * in the reference repository declares `eager: true` - and ships at `error` as well; a repository
294
+ * adopting these into a tree that is not already clean measures first and lands anything above zero
295
+ * at `warn` with the count beside it.
296
+ */
297
+ export const recommended = {
298
+ "starci-be/must-inject-entity-manager": "error",
299
+ "starci-be/no-injected-repository": "error",
300
+ "starci-be/require-entity-table-name": "error",
301
+ "starci-be/no-outer-manager-in-transaction": "error",
302
+ "starci-be/no-eager-relation": "error",
303
+ }
304
+
305
+ /** Path helper shared with the other backend law modules. */
306
+ export { normalizePath }