@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/CHANGELOG.md +7 -0
- package/README.md +70 -69
- package/authorization.mjs +106 -106
- package/cdc.mjs +150 -150
- package/comments.mjs +388 -389
- package/cqrs.mjs +302 -303
- package/data-access.mjs +306 -307
- package/e2e-flow.mjs +222 -222
- package/event-delivery.mjs +93 -93
- package/exception-identity.mjs +200 -200
- package/exceptions.mjs +262 -265
- package/index.mjs +107 -107
- package/lib/path.mjs +2 -0
- package/module-layering.mjs +426 -432
- package/naming.mjs +219 -220
- package/observability.mjs +275 -275
- package/package.json +4 -2
- package/testing.mjs +500 -454
- package/transport.mjs +168 -168
- package/type-safety.mjs +510 -380
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (expression.type !== "
|
|
143
|
-
|
|
144
|
-
//
|
|
145
|
-
//
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
property.
|
|
154
|
-
&&
|
|
155
|
-
&& (property.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
)
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
if (callee.type !== "
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (object.type !== "
|
|
209
|
-
if (object.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
//
|
|
213
|
-
//
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
descendant.
|
|
223
|
-
&&
|
|
224
|
-
&& descendant.
|
|
225
|
-
&& descendant.property.
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (expression.type !== "
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
property.
|
|
265
|
-
&&
|
|
266
|
-
|
|
267
|
-
)
|
|
268
|
-
if (
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
"
|
|
281
|
-
"
|
|
282
|
-
"
|
|
283
|
-
"no-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
"starci-be/
|
|
300
|
-
"starci-be/
|
|
301
|
-
"starci-be/
|
|
302
|
-
"starci-be/no-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
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 }
|